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