aboutsummaryrefslogtreecommitdiff
path: root/test/structs.tm
blob: 1768cf71463c38513aae27a1fc485dadd7d6df00 (plain)
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

struct Single(x:Int)
struct Pair(x,y:Int)
struct Mixed(x:Int, text:Text)
struct LinkedList(x:Int, next:@LinkedList?=none)
struct Password(text:Text; secret)

struct CorecursiveA(other:@CorecursiveB?)
struct CorecursiveB(other:@CorecursiveA?=none)

func test_literals()
	assert Single(123) == Single(123)
	x := Pair(10, 20)
	assert x == Pair(x=10, y=20)
	y := Pair(y=20, 10)
	assert y == Pair(x=10, y=20)
	assert x == y
	assert x != Pair(-1, -2)

func test_metamethods()
	>> x := Pair(10, 20)
	>> y := Pair(100, 200)
	assert x == y == no
	assert x == Pair(10, 20)
	assert x != Pair(10, 30)

	assert x < Pair(11, 20)
	>> set := {x: yes}
	assert set.has(x) == yes
	assert set.has(y) == no

func test_mixed()
	>> x := Mixed(10, "Hello")
	>> y := Mixed(99, "Hello")
	assert x == y == no
	assert x == Mixed(10, "Hello")
	assert x != Mixed(10, "Bye")
	assert x < Mixed(11, "Hello")
	>> set := {x: yes}
	assert set.has(x) == yes
	assert set.has(y) == no

func test_text()
	>> b := @CorecursiveB()
	>> a := @CorecursiveA(b)
	>> b.other = a
	>> a
	# = @CorecursiveA(@CorecursiveB(@~1))

func main()
	test_literals()
	test_metamethods()
	test_mixed()
	test_text()

	>> @LinkedList(10, @LinkedList(20))

	>> my_pass := Password("Swordfish")
	assert my_pass == Password("Swordfish")
	assert "$my_pass" == "Password(...)"
	>> users_by_password := {my_pass: "User1", Password("xxx"): "User2"}
	assert "$users_by_password" == '{Password(...): "User1", Password(...): "User2"}'
	assert users_by_password[my_pass]! == "User1"

	>> CorecursiveA(@CorecursiveB())