-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dictionary_test.go
125 lines (100 loc) · 2.75 KB
/
dictionary_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
package httpsfv
import (
"reflect"
"strings"
"testing"
)
func TestDictionnary(t *testing.T) {
t.Parallel()
dict := NewDictionary()
add := []struct {
in string
expected Member
valid bool
}{
{"f_o1o3-", NewItem(10.0), true},
{"deleteme", NewItem(""), true},
{"*f0.o*", NewItem(""), true},
{"t", NewItem(true), true},
{"f", NewItem(false), true},
{"b", NewItem([]byte{0, 1}), true},
{"0foo", NewItem(""), false},
{"mAj", NewItem(""), false},
{"_foo", NewItem(""), false},
{"foo", NewItem(Token("é")), false},
}
var b strings.Builder
for _, d := range add {
vDict := NewDictionary()
vDict.Add(d.in, d.expected)
b.Reset()
if valid := vDict.marshalSFV(&b) == nil; valid != d.valid {
t.Errorf("(%v, %v).isValid() = %v; %v expected", d.in, d.expected, valid, d.valid)
}
if d.valid {
dict.Add(d.in, d.expected)
}
}
i := NewItem(123.0)
dict.Add("f_o1o3-", i)
newValue, _ := dict.Get("f_o1o3-")
if newValue != i {
t.Errorf(`Add("f_o1o3-") must overwrite the existing value`)
}
if !dict.Del("deleteme") {
t.Errorf(`Del("deleteme") must return true`)
}
if dict.Del("deleteme") {
t.Errorf(`the second call to Del("deleteme") must return false`)
}
if v, ok := dict.Get("*f0.o*"); v.(Item).Value != "" || !ok {
t.Errorf(`Get("*f0.o*") = %v, %v; "", true expected`, v, ok)
}
if v, ok := dict.Get("notexist"); v != nil || ok {
t.Errorf(`Get("notexist") = %v, %v; nil, false expected`, v, ok)
}
if k := dict.Names(); len(k) != 5 {
t.Errorf(`Names() = %v; {"f_o1o3-", "*f0.o*"} expected`, k)
}
m, _ := dict.Get("f_o1o3-")
i = m.(Item)
i.Params.Add("foo", 9.5)
b.Reset()
_ = dict.marshalSFV(&b)
if b.String() != `f_o1o3-=123.0;foo=9.5, *f0.o*="", t, f=?0, b=:AAE=:` {
t.Errorf(`Dictionnary.marshalSFV(): invalid serialization: %v`, b.String())
}
}
func TestUnmarshalDictionary(t *testing.T) {
t.Parallel()
d1 := NewDictionary()
d1.Add("a", NewItem(false))
d1.Add("b", NewItem(true))
c := NewItem(true)
c.Params.Add("foo", Token("bar"))
d1.Add("c", c)
data := []struct {
in []string
expected *Dictionary
valid bool
}{
{[]string{"a=?0, b, c; foo=bar"}, d1, false},
{[]string{"a="}, nil, false},
{[]string{"a=?0, b", "c; foo=bar"}, d1, false},
{[]string{""}, NewDictionary(), false},
{[]string{"é"}, nil, true},
{[]string{`foo="é"`}, nil, true},
{[]string{`foo;é`}, nil, true},
{[]string{`f="foo" é`}, nil, true},
{[]string{`f="foo",`}, nil, true},
}
for _, d := range data {
l, err := UnmarshalDictionary(d.in)
if d.valid && err == nil {
t.Errorf("UnmarshalDictionary(%s): error expected", d.in)
}
if !d.valid && !reflect.DeepEqual(d.expected, l) {
t.Errorf("UnmarshalDictionary(%s) = %v, %v; %v, <nil> expected", d.in, l, err, d.expected)
}
}
}