-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclone_test.go
228 lines (196 loc) · 6.94 KB
/
clone_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// 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 worksheets
import (
"github.com/stretchr/testify/require"
)
var cloneDefs = `
type dup_me worksheet {
1: value text
2: v_slice []number[0]
3: r_slice []dup_me
4: ref1 dup_me
5: ref2 dup_me
}`
func (s *Zuite) TestClone_simple() {
ws := s.cloneDefs.MustNewWorksheet("dup_me")
ws.MustSet("value", NewText("Mary had a little lamb"))
ws.data[indexVersion] = NewNumberFromInt(666)
dup := ws.Clone()
require.True(s.T(), ws != dup, "dup must be a different instance than ws")
require.NotEqual(s.T(), ws.Id(), dup.Id())
require.Equal(s.T(), 1, dup.Version())
require.Len(s.T(), dup.orig, 0)
require.Equal(s.T(), map[int]Value{
indexId: NewText(dup.Id()),
indexVersion: NewNumberFromInt(1),
1: NewText("Mary had a little lamb"),
}, dup.data)
require.Len(s.T(), dup.parents, 0)
}
func (s *Zuite) TestClone_withOneWsRef() {
ws1 := s.cloneDefs.MustNewWorksheet("dup_me")
ws2 := s.cloneDefs.MustNewWorksheet("dup_me")
ws1.MustSet("ref1", ws2)
dup1 := ws1.Clone()
dup2 := dup1.MustGet("ref1").(*Worksheet)
require.True(s.T(), ws1 != dup1, "dup1 must be a different instance than ws1")
require.True(s.T(), ws2 != dup2, "dup2 must be a different instance than ws2")
require.NotEqual(s.T(), ws1.Id(), dup1.Id())
require.NotEqual(s.T(), ws2.Id(), dup2.Id())
require.Len(s.T(), dup1.orig, 0)
require.Len(s.T(), dup2.orig, 0)
require.Equal(s.T(), map[int]Value{
indexId: NewText(dup1.Id()),
indexVersion: NewNumberFromInt(1),
4: dup2,
}, dup1.data)
require.Equal(s.T(), map[int]Value{
indexId: NewText(dup2.Id()),
indexVersion: NewNumberFromInt(1),
}, dup2.data)
require.Len(s.T(), dup1.parents, 0)
require.Equal(s.T(), parentsRefs(map[string]map[int]map[string]*Worksheet{
"dup_me": {
4: {
dup1.Id(): dup1,
},
},
}), dup2.parents)
}
func (s *Zuite) TestClone_withTwoWsRefToSameWs() {
ws1 := s.cloneDefs.MustNewWorksheet("dup_me")
ws2 := s.cloneDefs.MustNewWorksheet("dup_me")
ws1.MustSet("ref1", ws2)
ws1.MustSet("ref2", ws2)
dup1 := ws1.Clone()
dup2a := dup1.MustGet("ref1").(*Worksheet)
dup2b := dup1.MustGet("ref2").(*Worksheet)
require.True(s.T(), ws1 != dup1, "dup1 must be a different instance than ws1")
require.True(s.T(), ws2 != dup2a, "dup2a must be a different instance than ws2")
require.True(s.T(), ws2 != dup2b, "dup2b must be a different instance than ws2")
require.True(s.T(), dup2a == dup2b, "dup2a must be the same instance as dup2b")
dup2 := dup2a // == dup2b
require.NotEqual(s.T(), ws1.Id(), dup1.Id())
require.NotEqual(s.T(), ws2.Id(), dup2.Id())
require.Len(s.T(), dup1.orig, 0)
require.Len(s.T(), dup2a.orig, 0)
require.Equal(s.T(), map[int]Value{
indexId: NewText(dup1.Id()),
indexVersion: NewNumberFromInt(1),
4: dup2,
5: dup2,
}, dup1.data)
require.Equal(s.T(), map[int]Value{
indexId: NewText(dup2.Id()),
indexVersion: NewNumberFromInt(1),
}, dup2.data)
require.Len(s.T(), dup1.parents, 0)
require.Equal(s.T(), parentsRefs(map[string]map[int]map[string]*Worksheet{
"dup_me": {
4: {
dup1.Id(): dup1,
},
5: {
dup1.Id(): dup1,
},
},
}), dup2.parents)
}
func (s *Zuite) TestClone_withSliceOfValues() {
ws := s.cloneDefs.MustNewWorksheet("dup_me")
ws.MustAppend("v_slice", NewNumberFromInt(2))
ws.MustAppend("v_slice", NewNumberFromInt(3))
ws.MustAppend("v_slice", NewNumberFromInt(3))
ws.MustAppend("v_slice", NewNumberFromInt(5))
ws.MustAppend("v_slice", NewNumberFromInt(8))
ws.MustAppend("v_slice", NewNumberFromInt(13))
ws.MustDel("v_slice", 2)
wsSlice := ws.data[2].(*Slice)
dup := ws.Clone()
require.True(s.T(), ws != dup, "dup must be a different instance than ws")
require.NotEqual(s.T(), ws.Id(), dup.Id())
require.Len(s.T(), dup.orig, 0)
require.Len(s.T(), dup.data, 3)
require.Len(s.T(), dup.parents, 0)
// Highlighting that dupSlice is a fresh new slice, where elements have been
// set to exactly what is in wsSlice at the time of doing the cloning, i.e.
// the lastRank is not preserved, because we are doing a clean slate copy.
// For wsSlice lastRank is 6, since we've added 6 elements, and removed one,
// but for dupSlice, the lastRank is 5, since we're only copying the 5
// remaining elements when cloning.
require.Equal(s.T(), 6, wsSlice.lastRank)
dupSlice := dup.data[2].(*Slice)
require.NotEqual(s.T(), wsSlice.id, dupSlice.id)
require.Equal(s.T(), wsSlice.typ, dupSlice.typ)
require.Equal(s.T(), 5, dupSlice.lastRank)
require.Equal(s.T(), []sliceElement{
{1, NewNumberFromInt(2)},
{2, NewNumberFromInt(3)},
{3, NewNumberFromInt(5)},
{4, NewNumberFromInt(8)},
{5, NewNumberFromInt(13)},
}, dupSlice.elements)
}
func (s *Zuite) TestClone_withSliceOfRefs() {
ws := s.cloneDefs.MustNewWorksheet("dup_me")
child1 := s.cloneDefs.MustNewWorksheet("dup_me")
child2 := s.cloneDefs.MustNewWorksheet("dup_me")
child3 := s.cloneDefs.MustNewWorksheet("dup_me")
ws.MustAppend("r_slice", child1)
ws.MustAppend("r_slice", child2)
ws.MustAppend("r_slice", child3)
ws.MustSet("ref1", child1)
ws.MustSet("ref2", child2)
dup := ws.Clone()
// Since we're testing cloning behavior in other tests, we're only checking
// that all pointers are set properly here.
dupSlice := dup.data[3].(*Slice)
dupChild1 := dupSlice.elements[0].value.(*Worksheet)
dupChild2 := dupSlice.elements[1].value.(*Worksheet)
dupChild3 := dupSlice.elements[2].value.(*Worksheet)
require.True(s.T(), ws != dup, "dup must be a different instance than ws")
require.True(s.T(), child1 != dupChild1, "dupChild1 must be a different instance than child1")
require.True(s.T(), child2 != dupChild2, "dupChild2 must be a different instance than child2")
require.True(s.T(), child3 != dupChild3, "dupChild3 must be a different instance than child3")
require.True(s.T(), dup.data[4] == dupChild1, "r_slice[0] should point to ref1")
require.True(s.T(), dup.data[5] == dupChild2, "r_slice[1] should point to ref2")
// Parents.
require.Len(s.T(), dup.parents, 0)
require.Equal(s.T(), parentsRefs(map[string]map[int]map[string]*Worksheet{
"dup_me": {
3: {
dup.Id(): dup,
},
4: {
dup.Id(): dup,
},
},
}), dupChild1.parents)
require.Equal(s.T(), parentsRefs(map[string]map[int]map[string]*Worksheet{
"dup_me": {
3: {
dup.Id(): dup,
},
5: {
dup.Id(): dup,
},
},
}), dupChild2.parents)
require.Equal(s.T(), parentsRefs(map[string]map[int]map[string]*Worksheet{
"dup_me": {
3: {
dup.Id(): dup,
},
},
}), dupChild3.parents)
}