-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeTest.nim
277 lines (229 loc) · 7.53 KB
/
theTest.nim
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import unittest
import unpack
suite "Sequence-like unpacking with unpackSeq/aUnpackSeq macro":
setup:
let testSeq = @[2, 4, 6]
let testArray = [2, 4, 6]
let testTuple = (2, 4, 6)
let testString = "246"
test "should unpack sequence":
testSeq.unpackSeq(a, b, c)
check [a, c, b] == [2, 6, 4]
# is expanded into:
# let
# a = testSeq[0]
# b = testSeq[1]
# c = testSeq[2]
test "should ignore '_' in sequence":
testSeq.unpackSeq(a, _, c)
check [a, c] == [2, 6]
# is expanded into:
# let
# a = testSeq[0]
# c = testSeq[2]
test "should unpack array":
testArray.unpackSeq(a, b, c)
check [a, c, b] == [2, 6, 4]
test "should unpack tuple":
testTuple.unpackSeq(a, b, c)
check [a, c, b] == [2, 6, 4]
## testTuple.unpackObject(d, e, f, g) <- will cause IndexError at runtime
test "should unpack string":
testString.unpackSeq(a, b, c)
check [a, c, b] == ['2', '6', '4']
test "should unpack from index 0 to arbitrary number":
testTuple.unpackSeq(a, b)
check [a, b] == [2, 4]
test "unpackSeq with var before first item should create variables with var":
testTuple.unpackSeq(var a, b)
check [a, b] == [2, 4]
a = 13
check a == 13
test "unpackSeq without var before first item defines symbols with let":
testTuple.unpackSeq(a, b)
check [a, b] == [2, 4]
test "aUnpackSeq should assign data to existing variables":
var a, b = 1
testTuple.aUnpackSeq(a, _, b)
check [a, b] == [2, 6]
suite "Sequence unpacking with arrow operators":
setup:
let testSeq = @[2, 4, 6]
let testArray = [2, 4, 6]
let testTuple = (2, 4, 6)
test "should unpack sequence":
[a, b, c] <- testSeq
check [a, c, b] == [2, 6, 4]
# is expanded into:
# let
# a = testSeq[0]
# b = testSeq[1]
# c = testSeq[2]
test "should unpack array":
[a, b, c] <- testArray
check [a, c, b] == [2, 6, 4]
test "should unpack tuple":
[a, b, c] <- testTuple
check [a, c, b] == [2, 6, 4]
## [d, e, f, g] <- testTuple <- will cause IndexError at runtime
test "should unpack from index 0 to arbitrary number":
[a, b] <- testTuple
check [a, b] == [2, 4]
test "adding var before first item should create mutable variables":
[var a, b] <- testTuple
check [a, b] == [2, 4]
a = 13
check a == 13
test "<- without var defines symbols with let":
[a, b] <- testTuple
check [a, b] == [2, 4]
test "<-- should assign data to existing variables":
var a, b = 1
[a, b] <-- testTuple
check [a, b] == [2, 4]
suite "Object meber unpacking unpackObject/aUnpackObject":
type
Person = object
name, job: string
PersonRef = ref Person
setup:
let timName = "Tim"
let fluffer = "Fluffer"
let johnName = "John"
let tim = Person(name: timName, job: fluffer)
let timRef = new(Person)
timRef.name = timName
timRef.job = fluffer
var secreteCounter = 0
proc colleague(p: Person; name: string): Person =
secreteCounter += 1
result = p
result.name = name
test "should unpack ordinary objects":
tim.unpackObject(name, job)
check name == timName
check job == fluffer
test "should unpack object refs":
timRef.unpackObject(name, job)
check name == timName
check job == fluffer
test "should unpack object pointers":
let timPtr = unsafeAddr(tim)
timPtr.unpackObject(job, name)
check name == timName
check job == fluffer
test "should not call proc multiple times when invoked after a chain of calls":
tim.colleague(johnName).unpackObject(name, job)
# is expanded into:
# let someUniqueSym1_212_498 = tim.colleague(johnName)
# let
# name = someUniqueSym1_212_498.name
# job = someUniqueSym1_212_498.job
check name == johnName
check job == fluffer
check secreteCounter == 1
test "should be able to rename object member with '=' sign":
tim.unpackObject(name as otherName)
check otherName == timName
tim.unpackObject(job, name as yetAnotherName) # and is order-agnostic.
check yetAnotherName == timName
check job == fluffer
test "adding var before first item should create new variables":
tim.unpackObject(var name as otherName, job)
check otherName == timName
check job == fluffer
otherName = johnName
check otherName == johnName
test "aUnpackObject should assign to existing variables":
var otherName, yetAnotherName, job = ""
tim.aUnpackObject(name as otherName)
check otherName == timName
tim.aUnpackObject(job, name as yetAnotherName) # and is order-agnostic.
check yetAnotherName == timName
check job == fluffer
suite "Object meber unpacking with arrow operators":
type
Person = object
name, job: string
PersonRef = ref Person
setup:
let timName = "Tim"
let fluffer = "Fluffer"
let johnName = "John"
let poofer = "Poofer"
let tim = Person(name: timName, job: fluffer)
let timRef = new(Person)
timRef.name = timName
timRef.job = fluffer
var secreteCounter = 0
proc colleague(p: Person; name: string): Person =
secreteCounter += 1
result = p
result.name = name
test "should unpack ordinary objects":
{name, job} <- tim
check name == timName
check job == fluffer
test "should unpack object refs":
{name, job} <- timRef
check name == timName
check job == fluffer
test "should unpack object pointers":
let timPtr = unsafeAddr(tim)
{job, name} <- timPtr
check name == timName
check job == fluffer
test "should not call proc multiple times when invoked after a chain of calls":
{name, job} <- tim.colleague(johnName)
# is expanded into:
# let someUniqueSym1_212_498 = tim.colleague(johnName)
# let
# name = someUniqueSym1_212_498.name
# job = someUniqueSym1_212_498.job
check name == johnName
check job == fluffer
check secreteCounter == 1
test "should be able to rename object member with ':' sign":
{name as otherName} <- tim
check otherName == timName
{job, name as yetAnotherName} <- tim # and is order-agnostic.
check yetAnotherName == timName
check job == fluffer
test "adding var before first item should create all symbol as mutable variables":
{var name as otherName, job} <- tim
check otherName == timName
check job == fluffer
job = poofer
otherName = johnName
check job == poofer
check otherName == johnName
test "<-- should assign data to existing variables":
{var name, job} <- tim
check name == timName
check job == fluffer
{name, job} <-- tim.colleague(johnName)
check name == johnName
suite "Named tuple unpacking with arrow operators":
setup:
type
Animal = tuple[numLegs: int; name, genus: string]
let carolyn = "Carolyn"
let felis = "Felis"
let equus = "Equus"
let bojack = "BoJack"
let cat = (numLegs: 2, name: carolyn, genus: felis).Animal
let horse = (numLegs: 2, name: bojack, genus: equus).Animal
test "should be unpackable with [] like unpacking sequences":
[var l, n, g] <- cat
check (l, n, g) == (2, carolyn, felis)
[l, n] <-- horse
check (l, n, g) == (2, bojack, felis)
[_, _, g] <-- horse
check (l, n, g) == (2, bojack, equus)
test "should be unpackable with {} like unpacking objects":
{var numLegs as l, name as n, genus as g} <- cat
check (l, n, g) == (2, carolyn, felis)
{name as n, numLegs as l} <-- horse
check (l, n, g) == (2, bojack, felis)
{genus as g} <-- horse
check (l, n, g) == (2, bojack, equus)