Debian Codegraph @

pkg/internal/third_party/go-json-experiment/json/jsontext/value_test.go

raw
1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package jsontext
6
7 import (
8 "io"
9 "strings"
10 "testing"
11
12 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/internal/jsontest"
13 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/internal/jsonwire"
14 )
15
16 type valueTestdataEntry struct {
17 name jsontest.CaseName
18 in string
19 wantValid bool
20 wantCompacted string
21 wantCompactErr error // implies wantCompacted is in
22 wantIndented string // wantCompacted if empty; uses "\t" for indent prefix and " " for indent
23 wantIndentErr error // implies wantCompacted is in
24 wantCanonicalized string // wantCompacted if empty
25 wantCanonicalizeErr error // implies wantCompacted is in
26 }
27
28 var valueTestdata = append(func() (out []valueTestdataEntry) {
29 // Initialize valueTestdata from coderTestdata.
30 for _, td := range coderTestdata {
31 // NOTE: The Compact method preserves the raw formatting of strings,
32 // while the Encoder (by default) does not.
33 if td.name.Name == "ComplicatedString" {
34 td.outCompacted = strings.TrimSpace(td.in)
35 }
36 out = append(out, valueTestdataEntry{
37 name: td.name,
38 in: td.in,
39 wantValid: true,
40 wantCompacted: td.outCompacted,
41 wantIndented: td.outIndented,
42 wantCanonicalized: td.outCanonicalized,
43 })
44 }
45 return out
46 }(), []valueTestdataEntry{{
47 name: jsontest.Name("RFC8785/Primitives"),
48 in: `{
49 "numbers": [333333333.33333329, 1E30, 4.50,
50 2e-3, 0.000000000000000000000000001, -0],
51 "string": "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"\/",
52 "literals": [null, true, false]
53 }`,
54 wantValid: true,
55 wantCompacted: `{"numbers":[333333333.33333329,1E30,4.50,2e-3,0.000000000000000000000000001,-0],"string":"\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"\/","literals":[null,true,false]}`,
56 wantIndented: `{
57 "numbers": [
58 333333333.33333329,
59 1E30,
60 4.50,
61 2e-3,
62 0.000000000000000000000000001,
63 -0
64 ],
65 "string": "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"\/",
66 "literals": [
67 null,
68 true,
69 false
70 ]
71 }`,
72 wantCanonicalized: `{"literals":[null,true,false],"numbers":[333333333.3333333,1e+30,4.5,0.002,1e-27,0],"string":"€$\u000f\nA'B\"\\\\\"/"}`,
73 }, {
74 name: jsontest.Name("RFC8785/ObjectOrdering"),
75 in: `{
76 "\u20ac": "Euro Sign",
77 "\r": "Carriage Return",
78 "\ufb33": "Hebrew Letter Dalet With Dagesh",
79 "1": "One",
80 "\ud83d\ude00": "Emoji: Grinning Face",
81 "\u0080": "Control",
82 "\u00f6": "Latin Small Letter O With Diaeresis"
83 }`,
84 wantValid: true,
85 wantCompacted: `{"\u20ac":"Euro Sign","\r":"Carriage Return","\ufb33":"Hebrew Letter Dalet With Dagesh","1":"One","\ud83d\ude00":"Emoji: Grinning Face","\u0080":"Control","\u00f6":"Latin Small Letter O With Diaeresis"}`,
86 wantIndented: `{
87 "\u20ac": "Euro Sign",
88 "\r": "Carriage Return",
89 "\ufb33": "Hebrew Letter Dalet With Dagesh",
90 "1": "One",
91 "\ud83d\ude00": "Emoji: Grinning Face",
92 "\u0080": "Control",
93 "\u00f6": "Latin Small Letter O With Diaeresis"
94 }`,
95 wantCanonicalized: `{"\r":"Carriage Return","1":"One","€":"Control","ö":"Latin Small Letter O With Diaeresis","€":"Euro Sign","😀":"Emoji: Grinning Face","דּ":"Hebrew Letter Dalet With Dagesh"}`,
96 }, {
97 name: jsontest.Name("LargeIntegers"),
98 in: ` [ -9223372036854775808 , 9223372036854775807 ] `,
99 wantValid: true,
100 wantCompacted: `[-9223372036854775808,9223372036854775807]`,
101 wantIndented: `[
102 -9223372036854775808,
103 9223372036854775807
104 ]`,
105 wantCanonicalized: `[-9223372036854776000,9223372036854776000]`, // NOTE: Loss of precision due to numbers being treated as floats.
106 }, {
107 name: jsontest.Name("InvalidUTF8"),
108 in: ` "living` + "\xde\xad\xbe\xef" + `\ufffd�" `,
109 wantValid: false, // uses RFC 7493 as the definition; which validates UTF-8
110 wantCompacted: `"living` + "\xde\xad\xbe\xef" + `\ufffd�"`,
111 wantCanonicalizeErr: E(jsonwire.ErrInvalidUTF8).withPos(` "living`+"\xde\xad", ""),
112 }, {
113 name: jsontest.Name("InvalidUTF8/SurrogateHalf"),
114 in: `"\ud800"`,
115 wantValid: false, // uses RFC 7493 as the definition; which validates UTF-8
116 wantCompacted: `"\ud800"`,
117 wantCanonicalizeErr: newInvalidEscapeSequenceError(`\ud800"`).withPos(`"`, ""),
118 }, {
119 name: jsontest.Name("UppercaseEscaped"),
120 in: `"\u000B"`,
121 wantValid: true,
122 wantCompacted: `"\u000B"`,
123 wantCanonicalized: `"\u000b"`,
124 }, {
125 name: jsontest.Name("DuplicateNames"),
126 in: ` { "0" : 0 , "1" : 1 , "0" : 0 }`,
127 wantValid: false, // uses RFC 7493 as the definition; which does check for object uniqueness
128 wantCompacted: `{"0":0,"1":1,"0":0}`,
129 wantIndented: `{
130 "0": 0,
131 "1": 1,
132 "0": 0
133 }`,
134 wantCanonicalizeErr: E(ErrDuplicateName).withPos(` { "0" : 0 , "1" : 1 , `, "/0"),
135 }, {
136 name: jsontest.Name("Whitespace"),
137 in: " \n\r\t",
138 wantValid: false,
139 wantCompacted: " \n\r\t",
140 wantCompactErr: E(io.ErrUnexpectedEOF).withPos(" \n\r\t", ""),
141 wantIndentErr: E(io.ErrUnexpectedEOF).withPos(" \n\r\t", ""),
142 wantCanonicalizeErr: E(io.ErrUnexpectedEOF).withPos(" \n\r\t", ""),
143 }}...)
144
145 func TestValueMethods(t *testing.T) {
146 for _, td := range valueTestdata {
147 t.Run(td.name.Name, func(t *testing.T) {
148 if td.wantIndented == "" {
149 td.wantIndented = td.wantCompacted
150 }
151 if td.wantCanonicalized == "" {
152 td.wantCanonicalized = td.wantCompacted
153 }
154 if td.wantCompactErr != nil {
155 td.wantCompacted = td.in
156 }
157 if td.wantIndentErr != nil {
158 td.wantIndented = td.in
159 }
160 if td.wantCanonicalizeErr != nil {
161 td.wantCanonicalized = td.in
162 }
163
164 v := Value(td.in)
165 gotValid := v.IsValid()
166 if gotValid != td.wantValid {
167 t.Errorf("%s: Value.IsValid = %v, want %v", td.name.Where, gotValid, td.wantValid)
168 }
169
170 gotCompacted := Value(td.in)
171 gotCompactErr := gotCompacted.Compact()
172 if string(gotCompacted) != td.wantCompacted {
173 t.Errorf("%s: Value.Compact = %s, want %s", td.name.Where, gotCompacted, td.wantCompacted)
174 }
175 if !equalError(gotCompactErr, td.wantCompactErr) {
176 t.Errorf("%s: Value.Compact error mismatch:\ngot %v\nwant %v", td.name.Where, gotCompactErr, td.wantCompactErr)
177 }
178
179 gotIndented := Value(td.in)
180 gotIndentErr := gotIndented.Indent(WithIndentPrefix("\t"), WithIndent(" "))
181 if string(gotIndented) != td.wantIndented {
182 t.Errorf("%s: Value.Indent = %s, want %s", td.name.Where, gotIndented, td.wantIndented)
183 }
184 if !equalError(gotIndentErr, td.wantIndentErr) {
185 t.Errorf("%s: Value.Indent error mismatch:\ngot %v\nwant %v", td.name.Where, gotIndentErr, td.wantIndentErr)
186 }
187
188 gotCanonicalized := Value(td.in)
189 gotCanonicalizeErr := gotCanonicalized.Canonicalize()
190 if string(gotCanonicalized) != td.wantCanonicalized {
191 t.Errorf("%s: Value.Canonicalize = %s, want %s", td.name.Where, gotCanonicalized, td.wantCanonicalized)
192 }
193 if !equalError(gotCanonicalizeErr, td.wantCanonicalizeErr) {
194 t.Errorf("%s: Value.Canonicalize error mismatch:\ngot %v\nwant %v", td.name.Where, gotCanonicalizeErr, td.wantCanonicalizeErr)
195 }
196 })
197 }
198 }