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
|
package main
import (
"encoding/json"
"testing"
)
var cases = []struct {
in interface{}
want token
}{
{make(map[string]interface{}), token{"{}", typEmptyObject}},
{make([]interface{}, 0), token{"[]", typEmptyArray}},
{json.Number("1.2"), token{"1.2", typNumber}},
{"foo", token{`"foo"`, typString}},
{"<3", token{`"<3"`, typString}},
{"&", token{`"&"`, typString}},
{"\b", token{`"\b"`, typString}},
{"\f", token{`"\f"`, typString}},
{"\n", token{`"\n"`, typString}},
{"\r", token{`"\r"`, typString}},
{"\t", token{`"\t"`, typString}},
{"wat \u001e", token{`"wat \u001E"`, typString}},
{"Hello, 世界", token{`"Hello, 世界"`, typString}},
{true, token{"true", typTrue}},
{false, token{"false", typFalse}},
{nil, token{"null", typNull}},
{struct{}{}, token{"", typError}},
}
func TestValueTokenFromInterface(t *testing.T) {
for _, c := range cases {
have := valueTokenFromInterface(c.in)
if have != c.want {
t.Logf("input: %#v", have)
t.Logf("have: %#v", have)
t.Logf("want: %#v", c.want)
t.Errorf("have != want")
}
}
}
func BenchmarkValueTokenFromInterface(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, c := range cases {
_ = valueTokenFromInterface(c.in)
}
}
}
|