| 1 |
/* Copyright 2016-2017 Vector Creations Ltd |
| 2 |
* |
| 3 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
* you may not use this file except in compliance with the License. |
| 5 |
* You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software |
| 10 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
* See the License for the specific language governing permissions and |
| 13 |
* limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
package canonicaljson |
| 17 |
|
| 18 |
import ( |
| 19 |
"testing" |
| 20 |
) |
| 21 |
|
| 22 |
func testSortJSON(t *testing.T, input, want string) { |
| 23 |
got := SortJSON([]byte(input), nil) |
| 24 |
|
| 25 |
// Squash out the whitespace before comparing the JSON in case SortJSON had inserted whitespace. |
| 26 |
if string(CompactJSON(got, nil)) != want { |
| 27 |
t.Errorf("SortJSON(%q): want %q got %q", input, want, got) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestSortJSON(t *testing.T) { |
| 32 |
testSortJSON(t, `[{"b":"two","a":1}]`, `[{"a":1,"b":"two"}]`) |
| 33 |
testSortJSON(t, `{"B":{"4":4,"3":3},"A":{"1":1,"2":2}}`, |
| 34 |
`{"A":{"1":1,"2":2},"B":{"3":3,"4":4}}`) |
| 35 |
testSortJSON(t, `[true,false,null]`, `[true,false,null]`) |
| 36 |
testSortJSON(t, `[9007199254740991]`, `[9007199254740991]`) |
| 37 |
testSortJSON(t, "\t\n[9007199254740991]", `[9007199254740991]`) |
| 38 |
} |
| 39 |
|
| 40 |
func testCompactJSON(t *testing.T, input, want string) { |
| 41 |
got := string(CompactJSON([]byte(input), nil)) |
| 42 |
if got != want { |
| 43 |
t.Errorf("CompactJSON(%q): want %q got %q", input, want, got) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func TestCompactJSON(t *testing.T) { |
| 48 |
testCompactJSON(t, "{ }", "{}") |
| 49 |
|
| 50 |
input := `["\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007"]` |
| 51 |
want := input |
| 52 |
testCompactJSON(t, input, want) |
| 53 |
|
| 54 |
input = `["\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F"]` |
| 55 |
want = `["\b\t\n\u000B\f\r\u000E\u000F"]` |
| 56 |
testCompactJSON(t, input, want) |
| 57 |
|
| 58 |
input = `["\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017"]` |
| 59 |
want = input |
| 60 |
testCompactJSON(t, input, want) |
| 61 |
|
| 62 |
input = `["\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F"]` |
| 63 |
want = input |
| 64 |
testCompactJSON(t, input, want) |
| 65 |
|
| 66 |
testCompactJSON(t, `["\u0061\u005C\u0042\u0022"]`, `["a\\B\""]`) |
| 67 |
testCompactJSON(t, `["\u0120"]`, "[\"\u0120\"]") |
| 68 |
testCompactJSON(t, `["\u0FFF"]`, "[\"\u0FFF\"]") |
| 69 |
testCompactJSON(t, `["\u1820"]`, "[\"\u1820\"]") |
| 70 |
testCompactJSON(t, `["\uFFFF"]`, "[\"\uFFFF\"]") |
| 71 |
testCompactJSON(t, `["\uD842\uDC20"]`, "[\"\U00020820\"]") |
| 72 |
testCompactJSON(t, `["\uDBFF\uDFFF"]`, "[\"\U0010FFFF\"]") |
| 73 |
|
| 74 |
testCompactJSON(t, `["\"\\\/"]`, `["\"\\/"]`) |
| 75 |
} |
| 76 |
|
| 77 |
func testReadHex(t *testing.T, input string, want uint32) { |
| 78 |
got := readHexDigits([]byte(input)) |
| 79 |
if want != got { |
| 80 |
t.Errorf("readHexDigits(%q): want 0x%x got 0x%x", input, want, got) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func TestReadHex(t *testing.T) { |
| 85 |
testReadHex(t, "0123", 0x0123) |
| 86 |
testReadHex(t, "4567", 0x4567) |
| 87 |
testReadHex(t, "89AB", 0x89AB) |
| 88 |
testReadHex(t, "CDEF", 0xCDEF) |
| 89 |
testReadHex(t, "89ab", 0x89AB) |
| 90 |
testReadHex(t, "cdef", 0xCDEF) |
| 91 |
} |