|
1 | 1 | package fastjson
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "encoding/json" |
4 | 6 | "fmt"
|
5 | 7 | "math"
|
6 | 8 | "strings"
|
@@ -68,6 +70,40 @@ func TestParseRawNumber(t *testing.T) {
|
68 | 70 | })
|
69 | 71 | }
|
70 | 72 |
|
| 73 | +func TestEscapeString(t *testing.T) { |
| 74 | + t.Run("controlchars", func(t *testing.T) { |
| 75 | + var r rune |
| 76 | + for r = 0; r <= 0x20; r++ { |
| 77 | + testEscapeString(t, string(r)) |
| 78 | + testEscapeString(t, string(r)+" hello") |
| 79 | + testEscapeString(t, "hello "+string(r)) |
| 80 | + testEscapeString(t, "hello "+string(r)+" world") |
| 81 | + } |
| 82 | + }) |
| 83 | + t.Run("misc", func(t *testing.T) { |
| 84 | + testEscapeString(t, `hello � world`) |
| 85 | + testEscapeString(t, "hello \u0014 world") |
| 86 | + testEscapeString(t, "newline\ntab\ndoublequote\"reversesolidus\\solidus/formfeed\fcarriagereturn\r") |
| 87 | + }) |
| 88 | + t.Run("unicode", func(t *testing.T) { |
| 89 | + testEscapeString(t, `你好,世界`) |
| 90 | + testEscapeString(t, "こんにちは、世界") |
| 91 | + testEscapeString(t, "ສະບາຍດີ, ໂລກ") |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +func testEscapeString(t *testing.T, s string) { |
| 96 | + t.Helper() |
| 97 | + |
| 98 | + // Check that fastjson encoding is identical to standard library encoding |
| 99 | + expected, _ := json.Marshal(s) |
| 100 | + a := Arena{} |
| 101 | + actual := a.NewString(s).MarshalTo(nil) |
| 102 | + if !bytes.Equal(actual, expected) { |
| 103 | + t.Fatalf("expected='%s', actual='%s'", expected, actual) |
| 104 | + } |
| 105 | +} |
| 106 | + |
71 | 107 | func TestUnescapeStringBestEffort(t *testing.T) {
|
72 | 108 | t.Run("success", func(t *testing.T) {
|
73 | 109 | testUnescapeStringBestEffort(t, ``, ``)
|
@@ -349,7 +385,7 @@ func TestValueGetTyped(t *testing.T) {
|
349 | 385 | }
|
350 | 386 | un64 = v.GetUint64("bar")
|
351 | 387 | if n != 0 {
|
352 |
| - t.Fatalf("unexpected non-zero value; got %d", n64) |
| 388 | + t.Fatalf("unexpected non-zero value; got %d", un64) |
353 | 389 | }
|
354 | 390 | f := v.GetFloat64("foo")
|
355 | 391 | if f != 123.0 {
|
|
0 commit comments