-
Notifications
You must be signed in to change notification settings - Fork 4
/
invalid_test.go
79 lines (73 loc) · 1.8 KB
/
invalid_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
package jx
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestInvalidFloat(t *testing.T) {
inputs := []string{
`1.e1`, // dot without following digit
`1.`, // dot can not be the last char
``, // empty number
`01`, // extra leading zero
`-`, // negative without digit
`--`, // double negative
`--2`, // double negative
}
for _, input := range inputs {
t.Run(input, func(t *testing.T) {
should := require.New(t)
iter := DecodeStr(input + ",")
should.Error(iter.Skip())
iter = DecodeStr(input + ",")
_, err := iter.Float64()
should.Error(err)
iter = DecodeStr(input + ",")
_, err = iter.Float32()
should.Error(err)
})
}
}
func TestValidPositive(t *testing.T) {
for _, tt := range []struct {
Name string
Value string
}{
// https://github.com/json-iterator/go/issues/520
{Name: "Number", Value: "1"},
{Name: "BlankObj", Value: "{}"},
{Name: "BlankArr", Value: "[]"},
{Name: "Example", Value: `{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}`},
} {
t.Run(tt.Name, func(t *testing.T) {
require.True(t, Valid([]byte(tt.Value)), "should be valid")
})
}
}
func TestValidNegative(t *testing.T) {
for _, tt := range []struct {
Name string
Value string
}{
{Name: "Blank", Value: ""},
{Name: "InvalidCharacters", Value: "foo"},
{Name: "NotClosed", Value: "{"},
{Name: "NotClosed2", Value: "{{}"},
{Name: "NotOpened", Value: "{}}"},
{Name: "NotOpenedArr", Value: "{[}]"},
{Name: "NotOpenedArr2", Value: "{}]"},
} {
t.Run(tt.Name, func(t *testing.T) {
require.False(t, Valid([]byte(tt.Value)), "should be invalid")
})
}
}