aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/return_spec.yue
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-26 06:38:38 +0000
committerLi Jin <dragon-fly@qq.com>2026-01-26 06:38:38 +0000
commit5d5b657f606b5939062983b1f90c3359d542672e (patch)
tree32132fd8908d6a8920d59362c572815a949f1a1f /spec/inputs/test/return_spec.yue
parentf5006f449a7be1a2f655f1b178ecf1d2f0569dd5 (diff)
downloadyuescript-5d5b657f606b5939062983b1f90c3359d542672e.tar.gz
yuescript-5d5b657f606b5939062983b1f90c3359d542672e.tar.bz2
yuescript-5d5b657f606b5939062983b1f90c3359d542672e.zip
Fixed compiler improvements and added comprehensive test suite
- Fixed makefile preprocessor macro definitions (removed spaces in -D flags) - Added null pointer check in compiler class declaration handling - Added comprehensive test specifications for various language features: - attrib, backcall, cond, config, existential, export, goto - import, literals, macro, metatable, operators, return - string, switch, vararg, with Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'spec/inputs/test/return_spec.yue')
-rw-r--r--spec/inputs/test/return_spec.yue85
1 files changed, 85 insertions, 0 deletions
diff --git a/spec/inputs/test/return_spec.yue b/spec/inputs/test/return_spec.yue
new file mode 100644
index 0000000..3bf0bed
--- /dev/null
+++ b/spec/inputs/test/return_spec.yue
@@ -0,0 +1,85 @@
1describe "return", ->
2 it "should return from comprehension", ->
3 fn = ->
4 return [x * 2 for x = 1, 5]
5 result = fn!
6 assert.same result, {2, 4, 6, 8, 10}
7
8 it "should return from table comprehension", ->
9 fn = ->
10 return {k, v for k, v in pairs {a: 1, b: 2}}
11 result = fn!
12 assert.same type(result), "table"
13
14 it "should return from nested if", ->
15 fn = (a, b) ->
16 if a
17 if b
18 return "both"
19 else
20 return "only a"
21 else
22 return "neither"
23 assert.same fn(true, true), "both"
24 assert.same fn(true, false), "only a"
25 assert.same fn(false, false), "neither"
26
27 it "should return from switch", ->
28 fn = (value) ->
29 return switch value
30 when 1 then "one"
31 when 2 then "two"
32 else "other"
33 assert.same fn(1), "one"
34 assert.same fn(2), "two"
35 assert.same fn(3), "other"
36
37 it "should return table literal", ->
38 fn = ->
39 return
40 value: 42
41 name: "test"
42 result = fn!
43 assert.same result.value, 42
44 assert.same result.name, "test"
45
46 it "should return array literal", ->
47 fn = ->
48 return
49 * 1
50 * 2
51 * 3
52 result = fn!
53 assert.same result, {1, 2, 3}
54
55 it "should return from with statement", ->
56 fn = (obj) ->
57 result = obj.value
58 return result
59 assert.same fn({value: 100}), 100
60
61 it "should return nil implicitly", ->
62 fn -> print "no return"
63 assert.same fn!, nil
64
65 it "should return multiple values", ->
66 fn -> 1, 2, 3
67 a, b, c = fn!
68 assert.same a, 1
69 assert.same b, 2
70 assert.same c, 3
71
72 it "should return from function call", ->
73 fn = ->
74 inner = -> 42
75 return inner!
76 assert.same fn!, 42
77
78 it "should handle return in expression context", ->
79 fn = (cond) ->
80 if cond
81 return "yes"
82 else
83 return "no"
84 assert.same fn(true), "yes"
85 assert.same fn(false), "no"