gitee.com/quant1x/pkg@v0.2.8/fastjson/validate_test.go (about) 1 package fastjson 2 3 import ( 4 "encoding/json" 5 "strings" 6 "testing" 7 ) 8 9 func TestValidateSimple(t *testing.T) { 10 if err := Validate(`123`); err != nil { 11 t.Fatalf("cannot validate number: %s", err) 12 } 13 if err := Validate(`"foobar"`); err != nil { 14 t.Fatalf("cannot validate string: %s", err) 15 } 16 if err := Validate(`null`); err != nil { 17 t.Fatalf("cannot validate null: %s", err) 18 } 19 if err := Validate(`true`); err != nil { 20 t.Fatalf("cannot validate true: %s", err) 21 } 22 if err := Validate(`false`); err != nil { 23 t.Fatalf("cannot validate false: %s", err) 24 } 25 if err := Validate(`foobar`); err == nil { 26 t.Fatalf("validation unexpectedly passed") 27 } 28 if err := Validate(`XDF`); err == nil { 29 t.Fatalf("validation unexpectedly passed") 30 } 31 32 if err := ValidateBytes([]byte(`{"foo":["bar", 123]}`)); err != nil { 33 t.Fatalf("cannot validate valid JSON: %s", err) 34 } 35 if err := ValidateBytes([]byte(`{"foo": bar`)); err == nil { 36 t.Fatalf("validation unexpectedly passed") 37 } 38 } 39 40 func TestValidateNumberZeroLen(t *testing.T) { 41 tail, err := validateNumber("") 42 if err == nil { 43 t.Fatalf("expecting non-nil error") 44 } 45 if tail != "" { 46 t.Fatalf("unexpected non-empty tail: %q", tail) 47 } 48 } 49 50 func TestValidate(t *testing.T) { 51 var tests = []string{ 52 "", 53 " ", 54 " z", 55 " 1 1", 56 " 1 {}", 57 " 1 []", 58 " 1 true", 59 " 1 null", 60 " 1 \"n\"", 61 62 // string 63 `"foo"`, 64 "\"\xe2\x80\xa8\xe2\x80\xa9\"", // line-sep and paragraph-sep 65 ` "\uaaaa" `, 66 `"\uz"`, 67 ` "\`, 68 ` "\z`, 69 " \"f\x00o\"", // control char 70 "\"foo\nbar\"", // control char 71 `"foo\qw"`, // unknown escape sequence 72 ` "foo`, 73 ` "\uazaa" `, 74 `"\"\\\/\b\f\n\r\t"`, 75 76 // number 77 "1", 78 " 0 ", 79 " 0e1 ", 80 " 0e+0 ", 81 " -0e+0 ", 82 "-0", 83 "1e6", 84 "1e+6", 85 "-1e+6", 86 "-0e+6", 87 " -103e+1 ", 88 "-0.01e+006", 89 "-z", 90 "-", 91 "1e", 92 "1e+", 93 " 03e+1 ", 94 " 1e.1 ", 95 " 00 ", 96 "1.e3", 97 "01e+6", 98 "-0.01e+0.6", 99 "123.", 100 "123.345", 101 "001 ", 102 "001", 103 104 // object 105 "{}", 106 `{"foo": 3}`, 107 "{\"f\x00oo\": 3}", 108 `{"foo\WW": 4}`, // unknown escape sequence 109 `{"foo": 3 "bar"}`, 110 ` {} `, 111 strings.Repeat(`{"f":`, 1000) + "{}" + strings.Repeat("}", 1000), 112 `{"foo": [{"":3, "4": "3"}, 4, {}], "t_wo": 1}`, 113 ` {"foo": 2,"fudge}`, 114 `{{"foo": }}`, 115 `{{"foo": [{"":3, 4: "3"}, 4, "5": {4}]}, "t_wo": 1}`, 116 "{", 117 `{"foo"`, 118 `{"foo",f}`, 119 `{"foo",`, 120 `{"foo"f`, 121 "{}}", 122 `{"foo": 234`, 123 `{"foo\"bar": 123}`, 124 "{\n\t\"foo\" \n\b\f: \t123}", 125 126 // array 127 `[]`, 128 `[ 1, {}]`, 129 strings.Repeat("[", 1000) + strings.Repeat("]", 1000), 130 `[1, 2, 3, 4, {}]`, 131 `[`, 132 `[1,`, 133 `[1a`, 134 `[]]`, 135 `[1 `, 136 137 // boolean 138 "true", 139 " true ", 140 "tree", 141 "false", 142 " true f", 143 "fals", 144 "falsee", 145 146 // null 147 "null ", 148 " null ", 149 " nulll ", 150 "no", 151 } 152 for i, test := range tests { 153 in := []byte(test) 154 got := ValidateBytes(in) == nil 155 exp := json.Valid(in) 156 157 if got != exp { 158 t.Errorf("#%d: %q got valid? %v, exp? %v", i, in, got, exp) 159 } 160 } 161 }