Repository navigation
Expand file tree
/
Copy pathrobust_test.go
More file actions
112 lines (99 loc) · 2.84 KB
/
Copy pathrobust_test.go
File metadata and controls
112 lines (99 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jsonutils
import (
"testing"
)
func TestUnmarshalTargetMustBeWritable(t *testing.T) {
type S struct {
A int `json:"a"`
}
jo, err := ParseString(`{"a":1}`)
if err != nil {
t.Fatalf("parse: %v", err)
}
// a struct value is not addressable, there is nowhere to write to
if err := jo.Unmarshal(S{}); err == nil {
t.Error("expect an error for a struct value")
}
var nilPtr *S
if err := jo.Unmarshal(nilPtr); err == nil {
t.Error("expect an error for a nil pointer")
}
var nilMap map[string]string
if err := jo.Unmarshal(nilMap); err == nil {
t.Error("expect an error for a nil map")
}
// a pointer still works
var s S
if err := jo.Unmarshal(&s); err != nil {
t.Fatalf("unmarshal into a pointer: %v", err)
}
if s.A != 1 {
t.Errorf("A = %d, want 1", s.A)
}
// a non nil map is a reference type, it still works
m := make(map[string]string)
if err := jo.Unmarshal(m); err != nil {
t.Fatalf("unmarshal into a map: %v", err)
}
if m["a"] != "1" {
t.Errorf("m[a] = %q, want %q", m["a"], "1")
}
}
func TestUnmarshalDanglingReference(t *testing.T) {
type Inner struct {
B int `json:"b"`
}
type S struct {
A *Inner `json:"a"`
C *Inner `json:"c"`
}
jo, err := ParseTrustedString(`{"c":<999>}`)
if err != nil {
t.Fatalf("parse: %v", err)
}
var s S
if err := jo.Unmarshal(&s); err == nil {
t.Errorf("expect an error for a reference with no node, got %+v", s)
}
// a resolvable reference is unaffected
jo, err = ParseTrustedString(`{"a":{"___jnid_":1,"b":5},"c":<1>}`)
if err != nil {
t.Fatalf("parse: %v", err)
}
var s2 S
if err := jo.Unmarshal(&s2); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if s2.A == nil || s2.C == nil || s2.A.B != 5 {
t.Errorf("unexpected result: %+v %+v", s2.A, s2.C)
}
}
func TestParseQueryStringErrorPropagation(t *testing.T) {
// a is already a scalar, a.b has nowhere to go
if _, err := ParseQueryString("a=1&a.b=2"); err == nil {
t.Error("expect an error when a scalar conflicts with a nested key")
}
jo, err := ParseQueryString("a.b=2&a.c=3")
if err != nil {
t.Fatalf("ParseQueryString: %v", err)
}
if v, err := jo.Int("a", "b"); err != nil || v != 2 {
t.Errorf("a.b = %d, %v; want 2", v, err)
}
if v, err := jo.Int("a", "c"); err != nil || v != 3 {
t.Errorf("a.c = %d, %v; want 3", v, err)
}
}