Debian Codegraph @

scanner_test.go

raw symbols SCIP
1 // Copyright 2016 Richard Gibson. All rights reserved.
2 // Copyright 2010 The Go Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 package canonicaljson
7
8 import (
9 "encoding/json"
10 "testing"
11 )
12
13 // Tests of simple examples.
14
15 type example struct {
16 compact string
17 indent string
18 }
19
20 var examples = []example{
21 {`1`, `1`},
22 {`{}`, `{}`},
23 {`[]`, `[]`},
24 {`{"":2}`, "{\n\t\"\": 2\n}"},
25 {`[3]`, "[\n\t3\n]"},
26 {`[1,2,3]`, "[\n\t1,\n\t2,\n\t3\n]"},
27 {`{"x":1}`, "{\n\t\"x\": 1\n}"},
28 {ex1, ex1i},
29 }
30
31 var ex1 = `[true,false,null,"x",1,1.5E0,0,-500]`
32
33 var ex1i = `[
34 true,
35 false,
36 null,
37 "x",
38 1,
39 1.5E0,
40 0,
41 -500
42 ]`
43
44 func TestMarshal(t *testing.T) {
45 var obj interface{}
46 for _, tt := range examples {
47 if err := json.Unmarshal([]byte(tt.compact), &obj); err != nil {
48 t.Errorf("json.Unmarshal(%#q): %v", tt.compact, err)
49 } else {
50 s, err := Marshal(obj)
51 if err != nil {
52 t.Errorf("Marshal(%#q): %v", tt.compact, err)
53 } else if string(s) != tt.compact {
54 t.Errorf("Marshal(%#q) = %#q, want original", tt.compact, string(s))
55 }
56 }
57
58 if err := json.Unmarshal([]byte(tt.indent), &obj); err != nil {
59 t.Errorf("json.Unmarshal(%#q): %v", tt.indent, err)
60 } else {
61 s, err := Marshal(obj)
62 if err != nil {
63 t.Errorf("Marshal(%#q): %v", tt.indent, err)
64 } else if string(s) != tt.compact {
65 t.Errorf("Marshal(%#q) = %#q, want %#q", tt.indent, string(s), tt.compact)
66 }
67 }
68 }
69 }
70
71 func TestMarshalSeparators(t *testing.T) {
72 // U+2028 and U+2029 should be unescaped inside strings.
73 // They should not appear outside strings.
74 tests := []struct {
75 in, compact string
76 }{
77 {"{\"\u2028\": 1}", "{\"\u2028\":1}"},
78 {"{\"\u2029\" :2}", "{\"\u2029\":2}"},
79 }
80 for _, tt := range tests {
81 var obj interface{}
82 if err := json.Unmarshal([]byte(tt.in), &obj); err != nil {
83 t.Errorf("json.Unmarshal(%q): %v", tt.in, err)
84 } else {
85 s, err := Marshal(obj)
86 if err != nil {
87 t.Errorf("Marshal(%#q): %v", tt.in, err)
88 } else if string(s) != tt.compact {
89 t.Errorf("Marshal(%q) = %q, want %q", tt.in, s, tt.compact)
90 }
91 }
92 }
93 }
94
95 func TestMarshalIndent(t *testing.T) {
96 var obj interface{}
97 for _, tt := range examples {
98 if err := json.Unmarshal([]byte(tt.indent), &obj); err != nil {
99 t.Errorf("json.Unmarshal(%#q): %v", tt.indent, err)
100 } else {
101 s, err := MarshalIndent(obj, "", "\t")
102 if err != nil {
103 t.Errorf("MarshalIndent(%#q): %v", tt.indent, err)
104 } else if string(s) != tt.indent {
105 t.Errorf("MarshalIndent(%#q) = %#q, want original", tt.indent, s)
106 }
107 }
108
109 if err := json.Unmarshal([]byte(tt.compact), &obj); err != nil {
110 t.Errorf("json.Unmarshal(%#q): %v", tt.compact, err)
111 } else {
112 s, err := MarshalIndent(obj, "", "\t")
113 if err != nil {
114 t.Errorf("MarshalIndent(%#q): %v", tt.compact, err)
115 } else if string(s) != tt.indent {
116 t.Errorf("MarshalIndent(%#q) = %#q, want %#q", tt.compact, s, tt.indent)
117 }
118 }
119 }
120 }