string_test.go 2.6 KB

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