-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattributes_test.go
112 lines (103 loc) · 2.73 KB
/
attributes_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
package latex
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestMeasure(t *testing.T) {
tt := []struct {
name string
input string
value float32
unit string
}{
{name: "px", input: "131.02px", value: 131.02, unit: "px"},
{name: "em", input: ".025em", value: .025, unit: "em"},
{name: "negative float", input: "-.025em", value: -.025, unit: "em"},
{name: "negative int", input: "-25em", value: -25, unit: "em"},
{name: "%", input: "25%", value: 25, unit: "%"},
{name: "\\textwidth", input: "0.25\\textwidth", value: 0.25, unit: "\\textwidth"},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
v, u, err := Measure(tc.input)
if err != nil {
t.Fatal(err)
}
if v != tc.value {
t.Errorf("Value does not match: want %v, got %v", tc.value, v)
}
if u != tc.unit {
t.Errorf("Unit does not match: want %v, got %v", tc.unit, u)
}
})
}
}
func TestKeyValue(t *testing.T) {
tt := []struct {
name string
input string
output map[string]string
}{
{
name: "one arg",
input: "key=value",
output: map[string]string{"key": "value"},
},
{
name: "few arg",
input: "scale=1.2, angle=45",
output: map[string]string{"scale": "1.2", "angle": "45"},
},
{
name: "lower case",
input: "SCALE=1.2, angle=45",
output: map[string]string{"scale": "1.2", "angle": "45"},
},
{
name: "with spaces",
input: "scale=1.2, angle= 45",
output: map[string]string{"scale": "1.2", "angle": "45"},
},
{
name: "escaped values",
input: "escaped=\"scale=1.2, \\\"angle\\\"= 45\", another=44",
output: map[string]string{"escaped": "scale=1.2, \"angle\"= 45", "another": "44"},
},
{
name: "single-quote escaped values",
input: "escaped='scale=1.2, \\'angle\\'= 45', another=44",
output: map[string]string{"escaped": "scale=1.2, 'angle'= 45", "another": "44"},
},
{
name: "escaped value followed by spaces",
input: "a=\"1\" , b=30",
output: map[string]string{"a": "1", "b": "30"},
},
{
name: "values surrounded by spaces",
input: "a = 1 , b = 3",
output: map[string]string{"a": "1", "b": "3"},
},
{
name: "cyrillic values",
input: "type=note, title=\"Привіт 👋\"",
output: map[string]string{"type": "note", "title": "Привіт 👋"},
},
{
name: "ignore invalid parts",
input: "type=note @ 2, fo, [email protected]",
output: map[string]string{"type": "note", "from": "[email protected]"},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
v, err := KeyValue(tc.input)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(v, tc.output) {
t.Errorf("Value does not match:\n%s\n", cmp.Diff(tc.output, v))
}
})
}
}