common_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package parse // import "github.com/tdewolff/parse"
  2. import (
  3. "encoding/base64"
  4. "mime"
  5. "testing"
  6. "github.com/tdewolff/test"
  7. )
  8. func TestParseNumber(t *testing.T) {
  9. var numberTests = []struct {
  10. number string
  11. expected int
  12. }{
  13. {"5", 1},
  14. {"0.51", 4},
  15. {"0.5e-99", 7},
  16. {"0.5e-", 3},
  17. {"+50.0", 5},
  18. {".0", 2},
  19. {"0.", 1},
  20. {"", 0},
  21. {"+", 0},
  22. {".", 0},
  23. {"a", 0},
  24. }
  25. for _, tt := range numberTests {
  26. t.Run(tt.number, func(t *testing.T) {
  27. n := Number([]byte(tt.number))
  28. test.T(t, n, tt.expected)
  29. })
  30. }
  31. }
  32. func TestParseDimension(t *testing.T) {
  33. var dimensionTests = []struct {
  34. dimension string
  35. expectedNum int
  36. expectedUnit int
  37. }{
  38. {"5px", 1, 2},
  39. {"5px ", 1, 2},
  40. {"5%", 1, 1},
  41. {"5em", 1, 2},
  42. {"px", 0, 0},
  43. {"1", 1, 0},
  44. {"1~", 1, 0},
  45. }
  46. for _, tt := range dimensionTests {
  47. t.Run(tt.dimension, func(t *testing.T) {
  48. num, unit := Dimension([]byte(tt.dimension))
  49. test.T(t, num, tt.expectedNum, "number")
  50. test.T(t, unit, tt.expectedUnit, "unit")
  51. })
  52. }
  53. }
  54. func TestMediatype(t *testing.T) {
  55. var mediatypeTests = []struct {
  56. mediatype string
  57. expectedMimetype string
  58. expectedParams map[string]string
  59. }{
  60. {"text/plain", "text/plain", nil},
  61. {"text/plain;charset=US-ASCII", "text/plain", map[string]string{"charset": "US-ASCII"}},
  62. {" text/plain ; charset = US-ASCII ", "text/plain", map[string]string{"charset": "US-ASCII"}},
  63. {" text/plain a", "text/plain", nil},
  64. {"text/plain;base64", "text/plain", map[string]string{"base64": ""}},
  65. {"text/plain;inline=;base64", "text/plain", map[string]string{"inline": "", "base64": ""}},
  66. }
  67. for _, tt := range mediatypeTests {
  68. t.Run(tt.mediatype, func(t *testing.T) {
  69. mimetype, _ := Mediatype([]byte(tt.mediatype))
  70. test.String(t, string(mimetype), tt.expectedMimetype, "mimetype")
  71. //test.T(t, params, tt.expectedParams, "parameters") // TODO
  72. })
  73. }
  74. }
  75. func TestParseDataURI(t *testing.T) {
  76. var dataURITests = []struct {
  77. dataURI string
  78. expectedMimetype string
  79. expectedData string
  80. expectedErr error
  81. }{
  82. {"www.domain.com", "", "", ErrBadDataURI},
  83. {"data:,", "text/plain", "", nil},
  84. {"data:text/xml,", "text/xml", "", nil},
  85. {"data:,text", "text/plain", "text", nil},
  86. {"data:;base64,dGV4dA==", "text/plain", "text", nil},
  87. {"data:image/svg+xml,", "image/svg+xml", "", nil},
  88. {"data:;base64,()", "", "", base64.CorruptInputError(0)},
  89. }
  90. for _, tt := range dataURITests {
  91. t.Run(tt.dataURI, func(t *testing.T) {
  92. mimetype, data, err := DataURI([]byte(tt.dataURI))
  93. test.T(t, err, tt.expectedErr)
  94. test.String(t, string(mimetype), tt.expectedMimetype, "mimetype")
  95. test.String(t, string(data), tt.expectedData, "data")
  96. })
  97. }
  98. }
  99. func TestParseQuoteEntity(t *testing.T) {
  100. var quoteEntityTests = []struct {
  101. quoteEntity string
  102. expectedQuote byte
  103. expectedN int
  104. }{
  105. {""", '"', 5},
  106. {"'", '\'', 6},
  107. {""", '"', 8},
  108. {"'", '\'', 6},
  109. {""", '"', 6},
  110. {"'", '\'', 6},
  111. {">", 0x00, 0},
  112. {"&", 0x00, 0},
  113. }
  114. for _, tt := range quoteEntityTests {
  115. t.Run(tt.quoteEntity, func(t *testing.T) {
  116. quote, n := QuoteEntity([]byte(tt.quoteEntity))
  117. test.T(t, quote, tt.expectedQuote, "quote")
  118. test.T(t, n, tt.expectedN, "quote length")
  119. })
  120. }
  121. }
  122. ////////////////////////////////////////////////////////////////
  123. func BenchmarkParseMediatypeStd(b *testing.B) {
  124. mediatype := "text/plain"
  125. for i := 0; i < b.N; i++ {
  126. mime.ParseMediaType(mediatype)
  127. }
  128. }
  129. func BenchmarkParseMediatypeParamStd(b *testing.B) {
  130. mediatype := "text/plain;inline=1"
  131. for i := 0; i < b.N; i++ {
  132. mime.ParseMediaType(mediatype)
  133. }
  134. }
  135. func BenchmarkParseMediatypeParamsStd(b *testing.B) {
  136. mediatype := "text/plain;charset=US-ASCII;language=US-EN;compression=gzip;base64"
  137. for i := 0; i < b.N; i++ {
  138. mime.ParseMediaType(mediatype)
  139. }
  140. }
  141. func BenchmarkParseMediatypeParse(b *testing.B) {
  142. mediatype := []byte("text/plain")
  143. for i := 0; i < b.N; i++ {
  144. Mediatype(mediatype)
  145. }
  146. }
  147. func BenchmarkParseMediatypeParamParse(b *testing.B) {
  148. mediatype := []byte("text/plain;inline=1")
  149. for i := 0; i < b.N; i++ {
  150. Mediatype(mediatype)
  151. }
  152. }
  153. func BenchmarkParseMediatypeParamsParse(b *testing.B) {
  154. mediatype := []byte("text/plain;charset=US-ASCII;language=US-EN;compression=gzip;base64")
  155. for i := 0; i < b.N; i++ {
  156. Mediatype(mediatype)
  157. }
  158. }