Skip to content

Commit c496f74

Browse files
authored
adds tests for ensureMove (#1199)
ref #1192
1 parent 31edd0a commit c496f74

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

tests/nimony/arc/tensuremove.nim

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
import std/[syncio, assertions]
2+
3+
block:
4+
block:
5+
type
6+
JsonNode = ref object
7+
8+
proc foo(d: JsonNode) =
9+
discard
10+
11+
proc test_something()=
12+
var a = JsonNode()
13+
foo ensureMove(a)
14+
15+
test_something()
16+
17+
block:
18+
type
19+
JsonNode = object
20+
data: int
21+
22+
proc foo(d: JsonNode) =
23+
discard
24+
25+
proc test_something()=
26+
var a = JsonNode()
27+
foo ensureMove(a)
28+
29+
test_something()
30+
31+
block:
32+
type
33+
JsonNode = object
34+
data: int
35+
36+
proc `=destroy`(x: JsonNode) = discard
37+
38+
proc foo(d: JsonNode) =
39+
discard
40+
41+
proc test_something()=
42+
var a = JsonNode()
43+
foo ensureMove(a)
44+
45+
test_something()
46+
47+
48+
# TODO: move it into the block below
49+
type
50+
X = object
51+
s: string
52+
53+
block:
54+
proc `=copy`(x: var X, y: X) =
55+
x.s = "copied " & y.s
56+
57+
proc `=sink`(x: var X, y: X) =
58+
`=destroy`(x)
59+
`=wasMoved`(x)
60+
x.s = "moved " & y.s
61+
62+
proc consume(x: sink X) =
63+
discard x.s
64+
65+
proc main =
66+
let m = "abcdefg"
67+
var x = X(s: ensureMove m)
68+
consume(ensureMove x)
69+
70+
main()
71+
72+
block:
73+
type
74+
String = object
75+
id: string
76+
77+
proc hello =
78+
var s = String(id: "1")
79+
var m = ensureMove s
80+
assert m.id == "1"
81+
82+
hello()
83+
84+
block:
85+
type
86+
String = object
87+
id: string
88+
89+
proc hello =
90+
var n = "1"
91+
var s = String(id: ensureMove n)
92+
var m = ensureMove s
93+
assert m.id == "1"
94+
95+
hello()
96+
97+
block:
98+
type
99+
String = object
100+
id: string
101+
102+
proc hello =
103+
var n = "1"
104+
var s = [ensureMove n]
105+
var m = ensureMove s
106+
assert m[0] == "1"
107+
108+
hello()
109+
110+
block:
111+
type
112+
String = object
113+
id: string
114+
115+
proc hello =
116+
var n = "1"
117+
var s = @[ensureMove n]
118+
var m = ensureMove s
119+
assert m[0] == "1"
120+
121+
hello()
122+
123+
block:
124+
type
125+
String = object
126+
id: string
127+
128+
proc hello =
129+
var s = String(id: "1")
130+
var m = ensureMove s.id
131+
assert m == "1"
132+
133+
hello()
134+
135+
block:
136+
proc foo =
137+
var x = 1
138+
let y = ensureMove x # move
139+
140+
assert y == 1
141+
142+
foo()
143+
144+
block:
145+
proc foo =
146+
var x = 1
147+
let y = ensureMove x # move
148+
assert y == 1
149+
foo()
150+
151+
# TODO:
152+
# block:
153+
# proc foo =
154+
# var x = @[1, 2, 3]
155+
# let y = ensureMove x[0] # move
156+
# assert y == 1
157+
# # when not defined(js):
158+
# # assert x == @[0, 2, 3]
159+
# foo()
160+
161+
block:
162+
proc foo =
163+
var x = [1, 2, 3]
164+
let y = ensureMove x[0] # move
165+
assert y == 1
166+
# when not defined(js):
167+
# assert x == @[0, 2, 3]
168+
foo()
169+
170+
# TODO:
171+
# block:
172+
# proc foo =
173+
# var x = @["1", "2", "3"]
174+
# let y = ensureMove x[0] # move
175+
# assert y == "1"
176+
# foo()
177+

0 commit comments

Comments
 (0)