-
Notifications
You must be signed in to change notification settings - Fork 2
/
cloning_test.go
172 lines (142 loc) · 4.18 KB
/
cloning_test.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package valix
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestConditions_Clone(t *testing.T) {
src := Conditions{"foo"}
dst := src.Clone()
require.Equal(t, len(src), len(dst))
src[0] = "x"
require.NotEqual(t, src[0], dst[0])
src = append(src, "bar")
require.NotEqual(t, len(src), len(dst))
src = nil
dst = src.Clone()
require.Nil(t, dst)
}
func TestConstraints_Clone(t *testing.T) {
constraint := &StringNotEmpty{}
src := Constraints{constraint, nil}
dst := src.Clone()
require.Equal(t, len(src), len(dst))
// the actual constraint items are NOT cloned...
require.Equal(t, src[0], dst[0])
constraint.Message = "Fooey"
require.Equal(t, "Fooey", src[0].(*StringNotEmpty).Message)
require.Equal(t, "Fooey", dst[0].(*StringNotEmpty).Message)
src = nil
dst = src.Clone()
require.Nil(t, dst)
}
func TestOasInfo_Clone(t *testing.T) {
src := &OasInfo{Title: "foo"}
dst := src.Clone()
require.Equal(t, src, dst)
require.Equal(t, src.Title, dst.Title)
dst.Title = "bar"
require.NotEqual(t, src.Title, dst.Title)
dst2 := cloneOasInfo(src)
require.NotNil(t, dst2)
require.NotEqual(t, dst, dst2)
src = nil
dst = cloneOasInfo(src)
require.Nil(t, dst)
}
func TestOthersExpr_Clone(t *testing.T) {
item := &OtherProperty{Name: "foo"}
src := OthersExpr{item}
dst := src.Clone()
require.Equal(t, len(src), len(dst))
// the actual items are NOT cloned...
require.Equal(t, src[0], dst[0])
src = nil
dst = src.Clone()
require.Nil(t, dst)
}
func TestConditionalVariant_Clone(t *testing.T) {
src := &ConditionalVariant{
WhenConditions: Conditions{"foo"},
}
dst := src.Clone()
require.False(t, src == dst)
require.Equal(t, len(src.WhenConditions), len(dst.WhenConditions))
src.WhenConditions = append(src.WhenConditions, "bar")
require.NotEqual(t, len(src.WhenConditions), len(dst.WhenConditions))
dst2 := cloneConditionalVariant(src)
require.NotNil(t, dst2)
require.False(t, dst == dst2)
src = nil
dst = cloneConditionalVariant(src)
require.Nil(t, dst)
}
func TestConditionalVariants_Clone(t *testing.T) {
variant := &ConditionalVariant{
WhenConditions: Conditions{"foo"},
}
src := ConditionalVariants{variant}
dst := src.Clone()
require.Equal(t, len(src), len(dst))
require.Equal(t, len(src[0].WhenConditions), len(dst[0].WhenConditions))
src[0].WhenConditions = append(src[0].WhenConditions, "bar")
require.NotEqual(t, len(src[0].WhenConditions), len(dst[0].WhenConditions))
require.Equal(t, src[0].WhenConditions[0], dst[0].WhenConditions[0])
src[0].WhenConditions[0] = "bar"
require.NotEqual(t, src[0].WhenConditions[0], dst[0].WhenConditions[0])
src = nil
dst = src.Clone()
require.Nil(t, dst)
}
func TestProperties_Clone(t *testing.T) {
src := Properties{
"foo": {
Type: JsonString,
},
"bar": nil,
}
dst := src.Clone()
require.Equal(t, len(src), len(dst))
src["baz"] = nil
require.NotEqual(t, len(src), len(dst))
dst["foo"].Type = JsonAny
require.Equal(t, JsonString, src["foo"].Type)
src = nil
dst = src.Clone()
require.Nil(t, dst)
}
func TestPropertyValidator_Clone(t *testing.T) {
src := &PropertyValidator{
Mandatory: true,
WhenConditions: Conditions{"foo"},
}
dst := src.Clone()
require.False(t, src == dst)
require.Equal(t, len(src.WhenConditions), len(dst.WhenConditions))
require.Equal(t, src.Mandatory, dst.Mandatory)
src.WhenConditions = append(src.WhenConditions, "bar")
src.Mandatory = false
require.NotEqual(t, len(src.WhenConditions), len(dst.WhenConditions))
require.NotEqual(t, src.Mandatory, dst.Mandatory)
}
func TestValidator_Clone(t *testing.T) {
src := &Validator{
IgnoreUnknownProperties: true,
Properties: Properties{
"foo": nil,
},
}
dst := src.Clone()
require.False(t, src == dst)
require.Equal(t, src.IgnoreUnknownProperties, dst.IgnoreUnknownProperties)
require.Equal(t, len(src.Properties), len(dst.Properties))
src.IgnoreUnknownProperties = !src.IgnoreUnknownProperties
src.Properties["bar"] = nil
require.NotEqual(t, src.IgnoreUnknownProperties, dst.IgnoreUnknownProperties)
require.NotEqual(t, len(src.Properties), len(dst.Properties))
dst2 := cloneValidator(src)
require.NotNil(t, dst2)
require.False(t, dst == dst2)
src = nil
dst = cloneValidator(src)
require.Nil(t, dst)
}