Debian Codegraph @

pkg/internal/third_party/go-json-experiment/json/errors_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 json
6
7 import (
8 "archive/tar"
9 "bytes"
10 "errors"
11 "io"
12 "strings"
13 "testing"
14
15 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/internal/jsonwire"
16 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext"
17 )
18
19 func TestSemanticError(t *testing.T) {
20 tests := []struct {
21 err error
22 want string
23 }{{
24 err: &SemanticError{},
25 want: `json: cannot handle`,
26 }, {
27 err: &SemanticError{JSONKind: 'n'},
28 want: `json: cannot handle JSON null`,
29 }, {
30 err: &SemanticError{action: "unmarshal", JSONKind: 't'},
31 want: `json: cannot unmarshal JSON boolean`,
32 }, {
33 err: &SemanticError{action: "unmarshal", JSONKind: 'x'},
34 want: `json: cannot unmarshal`, // invalid token kinds are ignored
35 }, {
36 err: &SemanticError{action: "marshal", JSONKind: '"'},
37 want: `json: cannot marshal JSON string`,
38 }, {
39 err: &SemanticError{GoType: T[bool]()},
40 want: `json: cannot handle Go bool`,
41 }, {
42 err: &SemanticError{action: "marshal", GoType: T[int]()},
43 want: `json: cannot marshal from Go int`,
44 }, {
45 err: &SemanticError{action: "unmarshal", GoType: T[uint]()},
46 want: `json: cannot unmarshal into Go uint`,
47 }, {
48 err: &SemanticError{GoType: T[struct{ Alpha, Bravo, Charlie, Delta, Echo, Foxtrot, Golf, Hotel string }]()},
49 want: `json: cannot handle Go struct`,
50 }, {
51 err: &SemanticError{GoType: T[struct{ Alpha, Bravo, Charlie, Delta, Echo, Foxtrot, Golf, Hotel, x string }]()},
52 want: `json: cannot handle Go json.struct`,
53 }, {
54 err: &SemanticError{JSONKind: '0', GoType: T[tar.Header]()},
55 want: `json: cannot handle JSON number with Go tar.Header`,
56 }, {
57 err: &SemanticError{action: "unmarshal", JSONKind: '0', JSONValue: jsontext.Value(`1e1000`), GoType: T[int]()},
58 want: `json: cannot unmarshal JSON number 1e1000 into Go int`,
59 }, {
60 err: &SemanticError{action: "marshal", JSONKind: '{', GoType: T[bytes.Buffer]()},
61 want: `json: cannot marshal JSON object from Go bytes.Buffer`,
62 }, {
63 err: &SemanticError{action: "unmarshal", JSONKind: ']', GoType: T[strings.Reader]()},
64 want: `json: cannot unmarshal JSON array into Go strings.Reader`,
65 }, {
66 err: &SemanticError{action: "unmarshal", JSONKind: '{', GoType: T[float64](), ByteOffset: 123},
67 want: `json: cannot unmarshal JSON object into Go float64 after offset 123`,
68 }, {
69 err: &SemanticError{action: "marshal", JSONKind: 'f', GoType: T[complex128](), ByteOffset: 123, JSONPointer: "/foo/2/bar/3"},
70 want: `json: cannot marshal JSON boolean from Go complex128 within "/foo/2/bar/3"`,
71 }, {
72 err: &SemanticError{action: "unmarshal", JSONKind: '}', GoType: T[io.Reader](), ByteOffset: 123, JSONPointer: "/foo/2/bar/3", Err: errors.New("some underlying error")},
73 want: `json: cannot unmarshal JSON object into Go io.Reader within "/foo/2/bar/3": some underlying error`,
74 }, {
75 err: &SemanticError{Err: errors.New("some underlying error")},
76 want: `json: cannot handle: some underlying error`,
77 }, {
78 err: &SemanticError{ByteOffset: 123},
79 want: `json: cannot handle after offset 123`,
80 }, {
81 err: &SemanticError{JSONPointer: "/foo/2/bar/3"},
82 want: `json: cannot handle within "/foo/2/bar/3"`,
83 }, {
84 err: &SemanticError{action: "unmarshal", JSONPointer: "/3", GoType: T[struct{ Fizz, Buzz string }](), Err: ErrUnknownName},
85 want: `json: cannot unmarshal into Go struct { Fizz string; Buzz string }: unknown object member name "3"`,
86 }, {
87 err: &SemanticError{action: "unmarshal", JSONPointer: "/foo/2/bar/3", GoType: T[struct{ Foo string }](), Err: ErrUnknownName},
88 want: `json: cannot unmarshal into Go struct { Foo string }: unknown object member name "3" within "/foo/2/bar"`,
89 }, {
90 err: &SemanticError{JSONPointer: "/foo/bar", ByteOffset: 16, GoType: T[string](), Err: &jsontext.SyntacticError{JSONPointer: "/foo/bar/baz", ByteOffset: 53, Err: jsonwire.ErrInvalidUTF8}},
91 want: `json: cannot handle Go string: invalid UTF-8 within "/foo/bar/baz" after offset 53`,
92 }, {
93 err: &SemanticError{JSONPointer: "/fizz/bar", ByteOffset: 16, GoType: T[string](), Err: &jsontext.SyntacticError{JSONPointer: "/foo/bar/baz", ByteOffset: 53, Err: jsonwire.ErrInvalidUTF8}},
94 want: `json: cannot handle Go string within "/fizz/bar": invalid UTF-8 within "/foo/bar/baz" after offset 53`,
95 }, {
96 err: &SemanticError{ByteOffset: 16, GoType: T[string](), Err: &jsontext.SyntacticError{JSONPointer: "/foo/bar/baz", ByteOffset: 53, Err: jsonwire.ErrInvalidUTF8}},
97 want: `json: cannot handle Go string: invalid UTF-8 within "/foo/bar/baz" after offset 53`,
98 }, {
99 err: &SemanticError{ByteOffset: 85, GoType: T[string](), Err: &jsontext.SyntacticError{JSONPointer: "/foo/bar/baz", ByteOffset: 53, Err: jsonwire.ErrInvalidUTF8}},
100 want: `json: cannot handle Go string after offset 85: invalid UTF-8 within "/foo/bar/baz" after offset 53`,
101 }}
102
103 for _, tt := range tests {
104 got := tt.err.Error()
105 // Cleanup the error of non-deterministic rendering effects.
106 if strings.HasPrefix(got, errorPrefix+"unable to ") {
107 got = errorPrefix + "cannot " + strings.TrimPrefix(got, errorPrefix+"unable to ")
108 }
109 if got != tt.want {
110 t.Errorf("%#v.Error mismatch:\ngot %v\nwant %v", tt.err, got, tt.want)
111 }
112 }
113 }