code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(55 lines)
1 # Tests for ensuring immutable value nature in various contexts
2 struct Inner(xs:[Int32])
4 struct Outer(inner:Inner)
6 enum HoldsList(HasList(xs:[Int32]))
8 func sneaky(outer:Outer)
9 (&outer.inner.xs)[1] = 99
11 func sneaky2(outer:&Outer)
12 (&outer.inner.xs)[1] = 99
14 func main()
15 do
16 xs := [10, 20, 30]
17 copy := xs
18 (&xs)[1] = 99
19 assert xs == [99, 20, 30]
20 assert copy == [10, 20, 30]
22 do
23 t := {"A":10, "B":20}
24 copy := t
25 (&t)["A"] = 99
26 assert t == {"A":99, "B":20}
27 assert copy == {"A":10, "B":20}
29 do
30 foo := Outer(Inner([10, 20, 30]))
31 copy := foo
32 (&foo.inner.xs)[1] = 99
33 assert foo.inner.xs == [99, 20, 30]
34 assert copy.inner.xs == [10, 20, 30]
36 do
37 foo := Outer(Inner([10, 20, 30]))
38 copy := foo
39 sneaky(foo)
40 assert foo.inner.xs == [10, 20, 30]
41 assert copy.inner.xs == [10, 20, 30]
43 do
44 foo := Outer(Inner([10, 20, 30]))
45 copy := foo
46 sneaky2(&foo)
47 assert foo.inner.xs == [99, 20, 30]
48 assert copy.inner.xs == [10, 20, 30]
50 do
51 x := HoldsList.HasList([10, 20, 30])
52 when x is HasList(list)
53 (&list)[1] = 99
55 assert x == HoldsList.HasList([10, 20, 30])