Debian Codegraph @

converter_test.go

raw
1 package schema
2
3 import (
4 "fmt"
5 "reflect"
6 "testing"
7 )
8
9 func TestConverters(t *testing.T) {
10 tests := []struct {
11 name string
12 v reflect.Value
13 want interface{}
14 valid bool
15 }{
16 {"boolTrue", convertBool("true"), true, true},
17 {"boolOn", convertBool("on"), true, true},
18 {"boolInvalid", convertBool("x"), nil, false},
19 {"float32", convertFloat32("1.5"), float32(1.5), true},
20 {"float32Invalid", convertFloat32("x"), nil, false},
21 {"float64", convertFloat64("2.5"), 2.5, true},
22 {"float64Invalid", convertFloat64("x"), nil, false},
23 {"int", convertInt("10"), int(10), true},
24 {"intInvalid", convertInt("x"), nil, false},
25 {"uint", convertUint("5"), uint(5), true},
26 {"uintInvalid", convertUint("-1"), nil, false},
27 {"string", convertString("abc"), "abc", true},
28 }
29 for _, tt := range tests {
30 if tt.valid {
31 if !tt.v.IsValid() {
32 t.Errorf("%s: expected valid value", tt.name)
33 continue
34 }
35 if got := tt.v.Interface(); got != tt.want {
36 t.Errorf("%s: expected %v, got %v", tt.name, tt.want, got)
37 }
38 } else if tt.v.IsValid() {
39 t.Errorf("%s: expected invalid value", tt.name)
40 }
41 }
42 }
43
44 func TestBuiltinConverters(t *testing.T) {
45 kinds := []reflect.Kind{
46 reflect.Bool, reflect.Float32, reflect.Float64,
47 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
48 reflect.String,
49 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
50 }
51 for _, k := range kinds {
52 if builtinConverters[k] == nil {
53 t.Errorf("missing converter for %v", k)
54 }
55 }
56 }
57
58 func BenchmarkConvertBool(b *testing.B) {
59 for b.Loop() {
60 convertBool("true")
61 }
62 }
63
64 func BenchmarkConvertInt(b *testing.B) {
65 for b.Loop() {
66 convertInt("42")
67 }
68 }
69
70 // setBuiltinKind must handle exactly the kinds builtinConverters covers, and
71 // with the same parse semantics; the two dispatch tables must not drift.
72 func TestSetBuiltinKindMatchesConverters(t *testing.T) {
73 samples := map[reflect.Kind][]string{
74 reflect.Bool: {"true", "on", "0", "bogus"},
75 reflect.Float32: {"1.5", "x"},
76 reflect.Float64: {"2.25", "x"},
77 reflect.Int: {"42", "-7", "x", "9999999999999999999999"},
78 reflect.Int8: {"127", "128", "x"},
79 reflect.Int16: {"32767", "32768"},
80 reflect.Int32: {"1", "2147483648"},
81 reflect.Int64: {"12345678901", "x"},
82 reflect.String: {"anything", ""},
83 reflect.Uint: {"42", "-1", "x"},
84 reflect.Uint8: {"255", "256"},
85 reflect.Uint16: {"65535", "65536"},
86 reflect.Uint32: {"1", "4294967296"},
87 reflect.Uint64: {"12345678901", "-1"},
88 }
89 for k, conv := range builtinConverters {
90 vals, ok := samples[k]
91 if !ok {
92 t.Fatalf("no samples for kind %v; extend this test alongside builtinConverters", k)
93 }
94 for _, s := range vals {
95 target := reflect.New(typeForKind(k)).Elem()
96 handled, setOK := setBuiltinKind(target, k, s)
97 if !handled {
98 t.Fatalf("setBuiltinKind does not handle kind %v present in builtinConverters", k)
99 }
100 convResult := conv(s)
101 if setOK != convResult.IsValid() {
102 t.Errorf("kind %v value %q: setBuiltinKind ok=%v but converter valid=%v", k, s, setOK, convResult.IsValid())
103 }
104 if setOK && convResult.IsValid() {
105 if got, want := fmt.Sprint(target.Interface()), fmt.Sprint(convResult.Interface()); got != want {
106 t.Errorf("kind %v value %q: setBuiltinKind stored %v, converter produced %v", k, s, got, want)
107 }
108 }
109 }
110 }
111 for k := reflect.Invalid; k <= reflect.UnsafePointer; k++ {
112 if builtinConverters[k] == nil {
113 v := reflect.New(reflect.TypeOf(struct{}{})).Elem()
114 if handled, _ := setBuiltinKind(v, k, "x"); handled {
115 t.Errorf("setBuiltinKind handles kind %v that builtinConverters does not", k)
116 }
117 }
118 }
119 }
120
121 func typeForKind(k reflect.Kind) reflect.Type {
122 switch k {
123 case reflect.Bool:
124 return reflect.TypeOf(false)
125 case reflect.Float32:
126 return reflect.TypeOf(float32(0))
127 case reflect.Float64:
128 return reflect.TypeOf(float64(0))
129 case reflect.Int:
130 return reflect.TypeOf(int(0))
131 case reflect.Int8:
132 return reflect.TypeOf(int8(0))
133 case reflect.Int16:
134 return reflect.TypeOf(int16(0))
135 case reflect.Int32:
136 return reflect.TypeOf(int32(0))
137 case reflect.Int64:
138 return reflect.TypeOf(int64(0))
139 case reflect.String:
140 return reflect.TypeOf("")
141 case reflect.Uint:
142 return reflect.TypeOf(uint(0))
143 case reflect.Uint8:
144 return reflect.TypeOf(uint8(0))
145 case reflect.Uint16:
146 return reflect.TypeOf(uint16(0))
147 case reflect.Uint32:
148 return reflect.TypeOf(uint32(0))
149 case reflect.Uint64:
150 return reflect.TypeOf(uint64(0))
151 }
152 return reflect.TypeOf(struct{}{})
153 }
154
155 // decodeBuiltinSlice's sizing contract relies on no non-string builtin kind
156 // parsing a comma-containing value as a whole; pin that invariant.
157 func TestNoBuiltinKindParsesCommas(t *testing.T) {
158 inputs := []string{"1,2", "true,false", "on,off", "1.5,2", ",", "1,"}
159 for k, conv := range builtinConverters {
160 if k == reflect.String {
161 continue
162 }
163 for _, s := range inputs {
164 if conv(s).IsValid() {
165 t.Errorf("kind %v unexpectedly parses %q; decodeBuiltinSlice sizing depends on this failing", k, s)
166 }
167 }
168 }
169 }
170
171 func TestGetBuiltinConverterOutOfRangeKind(t *testing.T) {
172 if getBuiltinConverter(reflect.Kind(200)) != nil {
173 t.Error("expected nil converter for out-of-range kind")
174 }
175 if getBuiltinConverter(reflect.Map) != nil {
176 t.Error("expected nil converter for unsupported kind")
177 }
178 }