diff options
Diffstat (limited to 'spec/outputs/test/return_spec.lua')
| -rw-r--r-- | spec/outputs/test/return_spec.lua | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/spec/outputs/test/return_spec.lua b/spec/outputs/test/return_spec.lua new file mode 100644 index 0000000..9479773 --- /dev/null +++ b/spec/outputs/test/return_spec.lua | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | return describe("return", function() | ||
| 2 | it("should return from comprehension", function() | ||
| 3 | local fn | ||
| 4 | fn = function() | ||
| 5 | local _accum_0 = { } | ||
| 6 | local _len_0 = 1 | ||
| 7 | for x = 1, 5 do | ||
| 8 | _accum_0[_len_0] = x * 2 | ||
| 9 | _len_0 = _len_0 + 1 | ||
| 10 | end | ||
| 11 | return _accum_0 | ||
| 12 | end | ||
| 13 | local result = fn() | ||
| 14 | return assert.same(result, { | ||
| 15 | 2, | ||
| 16 | 4, | ||
| 17 | 6, | ||
| 18 | 8, | ||
| 19 | 10 | ||
| 20 | }) | ||
| 21 | end) | ||
| 22 | it("should return from table comprehension", function() | ||
| 23 | local fn | ||
| 24 | fn = function() | ||
| 25 | local _tbl_0 = { } | ||
| 26 | for k, v in pairs({ | ||
| 27 | a = 1, | ||
| 28 | b = 2 | ||
| 29 | }) do | ||
| 30 | _tbl_0[k] = v | ||
| 31 | end | ||
| 32 | return _tbl_0 | ||
| 33 | end | ||
| 34 | local result = fn() | ||
| 35 | return assert.same(type(result), "table") | ||
| 36 | end) | ||
| 37 | it("should return from nested if", function() | ||
| 38 | local fn | ||
| 39 | fn = function(a, b) | ||
| 40 | if a then | ||
| 41 | if b then | ||
| 42 | return "both" | ||
| 43 | else | ||
| 44 | return "only a" | ||
| 45 | end | ||
| 46 | else | ||
| 47 | return "neither" | ||
| 48 | end | ||
| 49 | end | ||
| 50 | assert.same(fn(true, true), "both") | ||
| 51 | assert.same(fn(true, false), "only a") | ||
| 52 | return assert.same(fn(false, false), "neither") | ||
| 53 | end) | ||
| 54 | it("should return from switch", function() | ||
| 55 | local fn | ||
| 56 | fn = function(value) | ||
| 57 | if 1 == value then | ||
| 58 | return "one" | ||
| 59 | elseif 2 == value then | ||
| 60 | return "two" | ||
| 61 | else | ||
| 62 | return "other" | ||
| 63 | end | ||
| 64 | end | ||
| 65 | assert.same(fn(1), "one") | ||
| 66 | assert.same(fn(2), "two") | ||
| 67 | return assert.same(fn(3), "other") | ||
| 68 | end) | ||
| 69 | it("should return table literal", function() | ||
| 70 | local fn | ||
| 71 | fn = function() | ||
| 72 | return { | ||
| 73 | value = 42, | ||
| 74 | name = "test" | ||
| 75 | } | ||
| 76 | end | ||
| 77 | local result = fn() | ||
| 78 | assert.same(result.value, 42) | ||
| 79 | return assert.same(result.name, "test") | ||
| 80 | end) | ||
| 81 | it("should return array literal", function() | ||
| 82 | local fn | ||
| 83 | fn = function() | ||
| 84 | return { | ||
| 85 | 1, | ||
| 86 | 2, | ||
| 87 | 3 | ||
| 88 | } | ||
| 89 | end | ||
| 90 | local result = fn() | ||
| 91 | return assert.same(result, { | ||
| 92 | 1, | ||
| 93 | 2, | ||
| 94 | 3 | ||
| 95 | }) | ||
| 96 | end) | ||
| 97 | it("should return from with statement", function() | ||
| 98 | local fn | ||
| 99 | fn = function(obj) | ||
| 100 | local result = obj.value | ||
| 101 | return result | ||
| 102 | end | ||
| 103 | return assert.same(fn({ | ||
| 104 | value = 100 | ||
| 105 | }), 100) | ||
| 106 | end) | ||
| 107 | it("should return nil implicitly", function() | ||
| 108 | local fn | ||
| 109 | fn = function() | ||
| 110 | local _ = "no return" | ||
| 111 | end | ||
| 112 | return assert.same(fn(), nil) | ||
| 113 | end) | ||
| 114 | it("should return multiple values", function() | ||
| 115 | local fn | ||
| 116 | fn = function() | ||
| 117 | return 1, 2, 3 | ||
| 118 | end | ||
| 119 | local a, b, c = fn() | ||
| 120 | assert.same(a, 1) | ||
| 121 | assert.same(b, 2) | ||
| 122 | return assert.same(c, 3) | ||
| 123 | end) | ||
| 124 | it("should return from function call", function() | ||
| 125 | local fn | ||
| 126 | fn = function() | ||
| 127 | local inner | ||
| 128 | inner = function() | ||
| 129 | return 42 | ||
| 130 | end | ||
| 131 | return inner() | ||
| 132 | end | ||
| 133 | return assert.same(fn(), 42) | ||
| 134 | end) | ||
| 135 | return it("should handle return in expression context", function() | ||
| 136 | local fn | ||
| 137 | fn = function(cond) | ||
| 138 | if cond then | ||
| 139 | return "yes" | ||
| 140 | else | ||
| 141 | return "no" | ||
| 142 | end | ||
| 143 | end | ||
| 144 | assert.same(fn(true), "yes") | ||
| 145 | return assert.same(fn(false), "no") | ||
| 146 | end) | ||
| 147 | end) | ||
