github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/jsoni/value_tests/string_test.go (about) 1 package test 2 3 import ( 4 "context" 5 "encoding/json" 6 "testing" 7 "unicode/utf8" 8 9 "github.com/bingoohuang/gg/pkg/jsoni" 10 ) 11 12 func init() { 13 marshalCases = append(marshalCases, 14 `>`, 15 `"数字山谷"`, 16 "he\u2029\u2028he", 17 ) 18 for i := 0; i < utf8.RuneSelf; i++ { 19 marshalCases = append(marshalCases, string([]byte{byte(i)})) 20 } 21 } 22 23 func Test_read_string(t *testing.T) { 24 badInputs := []string{ 25 ``, 26 `"`, 27 `"\"`, 28 `"\\\"`, 29 "\"\n\"", 30 `"\U0001f64f"`, 31 `"\uD83D\u00"`, 32 } 33 for i := 0; i < 32; i++ { 34 // control characters are invalid 35 badInputs = append(badInputs, string([]byte{'"', byte(i), '"'})) 36 } 37 38 ctx := context.Background() 39 for _, input := range badInputs { 40 testReadString(t, input, "", true, "json.Unmarshal", json.Unmarshal) 41 testReadString(t, input, "", true, "jsoni.Unmarshal", jsoni.Unmarshal) 42 testReadString(t, input, "", true, "jsoni.ConfigCompatibleWithStandardLibrary.Unmarshal", 43 func(data []byte, v interface{}) error { 44 return jsoni.ConfigCompatibleWithStandardLibrary.Unmarshal(ctx, data, v) 45 }, 46 ) 47 } 48 49 goodInputs := []struct { 50 input string 51 expectValue string 52 }{ 53 {`""`, ""}, 54 {`"a"`, "a"}, 55 {`null`, ""}, 56 {`"Iñtërnâtiônàlizætiøn,💝🐹🌇⛔"`, "Iñtërnâtiônàlizætiøn,💝🐹🌇⛔"}, 57 {`"\uD83D"`, string([]byte{239, 191, 189})}, 58 {`"\uD83D\\"`, string([]byte{239, 191, 189, '\\'})}, 59 {`"\uD83D\ub000"`, string([]byte{239, 191, 189, 235, 128, 128})}, 60 {`"\uD83D\ude04"`, "😄"}, 61 {`"\uDEADBEEF"`, string([]byte{239, 191, 189, 66, 69, 69, 70})}, 62 {`"hel\"lo"`, `hel"lo`}, 63 {`"hel\\\/lo"`, `hel\/lo`}, 64 {`"hel\\blo"`, `hel\blo`}, 65 {`"hel\\\blo"`, "hel\\\blo"}, 66 {`"hel\\nlo"`, `hel\nlo`}, 67 {`"hel\\\nlo"`, "hel\\\nlo"}, 68 {`"hel\\tlo"`, `hel\tlo`}, 69 {`"hel\\flo"`, `hel\flo`}, 70 {`"hel\\\flo"`, "hel\\\flo"}, 71 {`"hel\\\rlo"`, "hel\\\rlo"}, 72 {`"hel\\\tlo"`, "hel\\\tlo"}, 73 {`"\u4e2d\u6587"`, "中文"}, 74 {`"\ud83d\udc4a"`, "\xf0\x9f\x91\x8a"}, 75 } 76 77 for _, tc := range goodInputs { 78 testReadString(t, tc.input, tc.expectValue, false, "json.Unmarshal", json.Unmarshal) 79 testReadString(t, tc.input, tc.expectValue, false, "jsoni.Unmarshal", jsoni.Unmarshal) 80 testReadString(t, tc.input, tc.expectValue, false, "jsoni.ConfigCompatibleWithStandardLibrary.Unmarshal", 81 func(data []byte, v interface{}) error { 82 return jsoni.ConfigCompatibleWithStandardLibrary.Unmarshal(ctx, data, v) 83 }) 84 } 85 } 86 87 func testReadString(t *testing.T, input string, expectValue string, expectError bool, marshalerName string, marshaler func([]byte, interface{}) error) { 88 var value string 89 err := marshaler([]byte(input), &value) 90 if expectError != (err != nil) { 91 t.Errorf("%q: %s: expected error %v, got %v", input, marshalerName, expectError, err) 92 return 93 } 94 if value != expectValue { 95 t.Errorf("%q: %s: expected %q, got %q", input, marshalerName, expectValue, value) 96 return 97 } 98 }