aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/do_statement_spec.yue
blob: 0adad202907dfb516ade52e6ae6a2aea56a0a8b2 (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
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
describe "do statement", ->
	it "should create new scope", ->
		x = 10
		do
			local x = 20
			assert.same x, 20
		assert.same x, 10

	it "should return value from do block", ->
		result = do
			x = 5
			x * 2
		assert.same result, 10

	it "should work with multiple statements", ->
		result = do
			a = 1
			b = 2
			c = 3
			a + b + c
		assert.same result, 6

	it "should handle nested do blocks", ->
		result = do
			x = 10
			y = do
				z = 5
				z * 2
			x + y
		assert.same result, 20

	it "should support conditional in do block", ->
		result = do
			value = 5
			if value > 3
				value * 2
			else
				value
		assert.same result, 10

	it "should work with loops in do block", ->
		result = do
			sum = 0
			for i = 1, 5
				sum += i
			sum
		assert.same result, 15

	it "should handle table operations", ->
		result = do
			tb = {1, 2, 3}
			table.insert tb, 4
			#tb
		assert.same result, 4

	it "should work with function definition", ->
		result = do
			fn = (x) -> x * 2
			fn 5
		assert.same result, 10

	it "should support variable shadowing", ->
		x = "outer"
		result = do
			x = "inner"
			x
		assert.same result, "inner"
		assert.same x, "outer"

	it "should work with method calls", ->
		obj =
			value: 10
			double: => @value * 2

		result = do
			with obj
				\double!
		assert.same result, 20

	it "should handle comprehensions in do block", ->
		result = do
			items = [1, 2, 3, 4, 5]
			[item * 2 for item in *items]
		assert.same result, {2, 4, 6, 8, 10}

	it "should work with try-catch", ->
		result = do
			success = try
				error "test error"
				false
			catch err
				true
		assert.is_true success

	it "should support return statement", ->
		fn = ->
			do
				x = 10
				return x * 2
			"never reached"

		result = fn!
		assert.same result, 20

	it "should work with assignment", ->
		result = do
			a, b, c = 1, 2, 3
			a + b + c
		assert.same result, 6

	it "should handle destructuring", ->
		result = do
			tb = {x: 10, y: 20}
			{:x, :y} = tb
			x + y
		assert.same result, 30

	it "should work with string interpolation", ->
		name = "world"
		result = do
			greeting = "hello"
			"#{greeting} #{name}"
		assert.same result, "hello world"

	it "should support implicit return", ->
		result = do
			value = 42
		assert.same result, 42

	it "should handle empty do block", ->
		result = do
		assert.same result, nil

	it "should work with backcalls", ->
		result = do
			items = [1, 2, 3]
			(x) <- map _, items
			x * 2
		assert.same result, {2, 4, 6}