aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/do_statement_spec.yue
diff options
context:
space:
mode:
Diffstat (limited to 'spec/inputs/test/do_statement_spec.yue')
-rw-r--r--spec/inputs/test/do_statement_spec.yue139
1 files changed, 139 insertions, 0 deletions
diff --git a/spec/inputs/test/do_statement_spec.yue b/spec/inputs/test/do_statement_spec.yue
new file mode 100644
index 0000000..0adad20
--- /dev/null
+++ b/spec/inputs/test/do_statement_spec.yue
@@ -0,0 +1,139 @@
1describe "do statement", ->
2 it "should create new scope", ->
3 x = 10
4 do
5 local x = 20
6 assert.same x, 20
7 assert.same x, 10
8
9 it "should return value from do block", ->
10 result = do
11 x = 5
12 x * 2
13 assert.same result, 10
14
15 it "should work with multiple statements", ->
16 result = do
17 a = 1
18 b = 2
19 c = 3
20 a + b + c
21 assert.same result, 6
22
23 it "should handle nested do blocks", ->
24 result = do
25 x = 10
26 y = do
27 z = 5
28 z * 2
29 x + y
30 assert.same result, 20
31
32 it "should support conditional in do block", ->
33 result = do
34 value = 5
35 if value > 3
36 value * 2
37 else
38 value
39 assert.same result, 10
40
41 it "should work with loops in do block", ->
42 result = do
43 sum = 0
44 for i = 1, 5
45 sum += i
46 sum
47 assert.same result, 15
48
49 it "should handle table operations", ->
50 result = do
51 tb = {1, 2, 3}
52 table.insert tb, 4
53 #tb
54 assert.same result, 4
55
56 it "should work with function definition", ->
57 result = do
58 fn = (x) -> x * 2
59 fn 5
60 assert.same result, 10
61
62 it "should support variable shadowing", ->
63 x = "outer"
64 result = do
65 x = "inner"
66 x
67 assert.same result, "inner"
68 assert.same x, "outer"
69
70 it "should work with method calls", ->
71 obj =
72 value: 10
73 double: => @value * 2
74
75 result = do
76 with obj
77 \double!
78 assert.same result, 20
79
80 it "should handle comprehensions in do block", ->
81 result = do
82 items = [1, 2, 3, 4, 5]
83 [item * 2 for item in *items]
84 assert.same result, {2, 4, 6, 8, 10}
85
86 it "should work with try-catch", ->
87 result = do
88 success = try
89 error "test error"
90 false
91 catch err
92 true
93 assert.is_true success
94
95 it "should support return statement", ->
96 fn = ->
97 do
98 x = 10
99 return x * 2
100 "never reached"
101
102 result = fn!
103 assert.same result, 20
104
105 it "should work with assignment", ->
106 result = do
107 a, b, c = 1, 2, 3
108 a + b + c
109 assert.same result, 6
110
111 it "should handle destructuring", ->
112 result = do
113 tb = {x: 10, y: 20}
114 {:x, :y} = tb
115 x + y
116 assert.same result, 30
117
118 it "should work with string interpolation", ->
119 name = "world"
120 result = do
121 greeting = "hello"
122 "#{greeting} #{name}"
123 assert.same result, "hello world"
124
125 it "should support implicit return", ->
126 result = do
127 value = 42
128 assert.same result, 42
129
130 it "should handle empty do block", ->
131 result = do
132 assert.same result, nil
133
134 it "should work with backcalls", ->
135 result = do
136 items = [1, 2, 3]
137 (x) <- map _, items
138 x * 2
139 assert.same result, {2, 4, 6}