aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/goto_spec.yue
blob: 9dad0a7b35b0a2c6a3c161146649f0ac23243a11 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
describe "goto", ->
	it "should support basic goto and label", ->
		a = 0
		::start::
		a += 1
		if a < 5
			goto start
		assert.same a, 5

	it "should support conditional goto", ->
		a = 0
		::loop::
		a += 1
		goto done if a == 3
		goto loop
		::done::
		assert.same a, 3

	it "should support goto in nested loops", ->
		count = 0
		for x = 1, 3
			for y = 1, 3
				count += 1
				if x == 2 and y == 2
					goto found
		::found::
		assert.same count, 5  -- (1,1), (1,2), (1,3), (2,1), (2,2)

	it "should support multiple labels", ->
		a = 0
		::first::
		a += 1
		goto second if a == 2
		goto first
		::second::
		assert.same a, 2

	it "should work with for loops", ->
		sum = 0
		for i = 1, 10
			sum += i
			goto done if i == 5
		::done::
		assert.same sum, 15  -- 1+2+3+4+5

	it "should work with while loops", ->
		count = 0
		while true
			count += 1
			goto endwhile if count == 3
		::endwhile::
		assert.same count, 3

	it "should skip rest of loop with goto", ->
		values = {}
		for i = 1, 5
			goto continue if i % 2 == 0
			table.insert values, i
			::continue::
		assert.same values, {1, 3, 5}

	it "should support goto with switch", ->
		result = "default"
		value = 2
		switch value
			when 1
				goto case_one
			when 2
				goto case_two
		goto default_label
		::case_one::
		result = "one"
		goto finish
		::case_two::
		result = "two"
		goto finish
		::default_label::
		result = "default"
		::finish::
		assert.same result, "two"