diff options
Diffstat (limited to 'spec/outputs/test/functions_advanced_spec.lua')
| -rw-r--r-- | spec/outputs/test/functions_advanced_spec.lua | 259 |
1 files changed, 259 insertions, 0 deletions
diff --git a/spec/outputs/test/functions_advanced_spec.lua b/spec/outputs/test/functions_advanced_spec.lua new file mode 100644 index 0000000..4e99023 --- /dev/null +++ b/spec/outputs/test/functions_advanced_spec.lua | |||
| @@ -0,0 +1,259 @@ | |||
| 1 | return describe("advanced functions", function() | ||
| 2 | it("should support fat arrow with self", function() | ||
| 3 | local obj = { | ||
| 4 | value = 10, | ||
| 5 | getValue = function(self) | ||
| 6 | return self.value | ||
| 7 | end | ||
| 8 | } | ||
| 9 | return assert.same(obj:getValue(), 10) | ||
| 10 | end) | ||
| 11 | it("should work with argument defaults", function() | ||
| 12 | local fn | ||
| 13 | fn = function(name, height) | ||
| 14 | if name == nil then | ||
| 15 | name = "something" | ||
| 16 | end | ||
| 17 | if height == nil then | ||
| 18 | height = 100 | ||
| 19 | end | ||
| 20 | return tostring(name) .. ", " .. tostring(height) | ||
| 21 | end | ||
| 22 | assert.same(fn(), "something, 100") | ||
| 23 | assert.same(fn("test"), "test, 100") | ||
| 24 | return assert.same(fn("test", 50), "test, 50") | ||
| 25 | end) | ||
| 26 | it("should handle defaults with previous arguments", function() | ||
| 27 | local fn | ||
| 28 | fn = function(x, y) | ||
| 29 | if x == nil then | ||
| 30 | x = 100 | ||
| 31 | end | ||
| 32 | if y == nil then | ||
| 33 | y = x + 1000 | ||
| 34 | end | ||
| 35 | return x + y | ||
| 36 | end | ||
| 37 | assert.same(fn(), 1200) | ||
| 38 | return assert.same(fn(50), 1100) | ||
| 39 | end) | ||
| 40 | it("should work with multi-line arguments", function() | ||
| 41 | local my_func | ||
| 42 | my_func = function(a, b, c, d, e, f) | ||
| 43 | return a + b + c + d + e + f | ||
| 44 | end | ||
| 45 | local result = my_func(5, 4, 3, 8, 9, 10) | ||
| 46 | return assert.same(result, 39) | ||
| 47 | end) | ||
| 48 | it("should support nested function calls", function() | ||
| 49 | local another_func | ||
| 50 | another_func = function(a, b, c, d, e, f) | ||
| 51 | return a + b + c + d + e + f | ||
| 52 | end | ||
| 53 | local my_func | ||
| 54 | my_func = function(a, b, c, d, e, f, g) | ||
| 55 | return a + b + c + d + e + f + g | ||
| 56 | end | ||
| 57 | local result = my_func(5, 6, 7, 6, another_func(6, 7, 8, 9, 1, 2), 5, 4) | ||
| 58 | return assert.same(result, 66) | ||
| 59 | end) | ||
| 60 | it("should handle implicit return", function() | ||
| 61 | local sum | ||
| 62 | sum = function(x, y) | ||
| 63 | return x + y | ||
| 64 | end | ||
| 65 | return assert.same(sum(10, 20), 30) | ||
| 66 | end) | ||
| 67 | it("should work with explicit return", function() | ||
| 68 | local difference | ||
| 69 | difference = function(x, y) | ||
| 70 | return x - y | ||
| 71 | end | ||
| 72 | return assert.same(difference(20, 10), 10) | ||
| 73 | end) | ||
| 74 | it("should support multiple return values", function() | ||
| 75 | local mystery | ||
| 76 | mystery = function(x, y) | ||
| 77 | return x + y, x - y | ||
| 78 | end | ||
| 79 | local a, b = mystery(10, 20) | ||
| 80 | assert.same(a, 30) | ||
| 81 | return assert.same(b, -10) | ||
| 82 | end) | ||
| 83 | it("should work with function as argument", function() | ||
| 84 | local apply | ||
| 85 | apply = function(fn, x, y) | ||
| 86 | return fn(x, y) | ||
| 87 | end | ||
| 88 | local result = apply((function(a, b) | ||
| 89 | return a + b | ||
| 90 | end), 5, 10) | ||
| 91 | return assert.same(result, 15) | ||
| 92 | end) | ||
| 93 | it("should handle function returning function", function() | ||
| 94 | local create_adder | ||
| 95 | create_adder = function(x) | ||
| 96 | return function(y) | ||
| 97 | return x + y | ||
| 98 | end | ||
| 99 | end | ||
| 100 | local add_five = create_adder(5) | ||
| 101 | return assert.same(add_five(10), 15) | ||
| 102 | end) | ||
| 103 | it("should support immediately invoked function", function() | ||
| 104 | local result = (function(x) | ||
| 105 | return x * 2 | ||
| 106 | end)(5) | ||
| 107 | return assert.same(result, 10) | ||
| 108 | end) | ||
| 109 | it("should work with varargs", function() | ||
| 110 | local sum_all | ||
| 111 | sum_all = function(...) | ||
| 112 | local total = 0 | ||
| 113 | for i = 1, select('#', ...) do | ||
| 114 | if type(select(i, ...)) == "number" then | ||
| 115 | total = total + select(i, ...) | ||
| 116 | end | ||
| 117 | end | ||
| 118 | return total | ||
| 119 | end | ||
| 120 | return assert.same(sum_all(1, 2, 3, 4, 5), 15) | ||
| 121 | end) | ||
| 122 | it("should handle named varargs", function() | ||
| 123 | local fn | ||
| 124 | fn = function(...) | ||
| 125 | local t = { | ||
| 126 | n = select("#", ...), | ||
| 127 | ... | ||
| 128 | } | ||
| 129 | local count = 0 | ||
| 130 | for i = 1, t.n do | ||
| 131 | count = count + 1 | ||
| 132 | end | ||
| 133 | return count | ||
| 134 | end | ||
| 135 | return assert.same(fn(1, 2, 3), 3) | ||
| 136 | end) | ||
| 137 | it("should support prefixed return", function() | ||
| 138 | local findValue | ||
| 139 | findValue = function() | ||
| 140 | local items = { | ||
| 141 | 1, | ||
| 142 | 2, | ||
| 143 | 3 | ||
| 144 | } | ||
| 145 | for _index_0 = 1, #items do | ||
| 146 | local item = items[_index_0] | ||
| 147 | if item == 5 then | ||
| 148 | return item | ||
| 149 | end | ||
| 150 | end | ||
| 151 | return "not found" | ||
| 152 | end | ||
| 153 | local result = findValue() | ||
| 154 | return assert.same(result, "not found") | ||
| 155 | end) | ||
| 156 | it("should work with parameter destructuring", function() | ||
| 157 | local fn | ||
| 158 | fn = function(_arg_0) | ||
| 159 | local a, b, c | ||
| 160 | a, b, c = _arg_0.a, _arg_0.b, _arg_0.c | ||
| 161 | return a + b + c | ||
| 162 | end | ||
| 163 | return assert.same(fn({ | ||
| 164 | a = 1, | ||
| 165 | b = 2, | ||
| 166 | c = 3 | ||
| 167 | }), 6) | ||
| 168 | end) | ||
| 169 | it("should handle default values in destructuring", function() | ||
| 170 | local fn | ||
| 171 | fn = function(_arg_0) | ||
| 172 | local a1, b | ||
| 173 | a1, b = _arg_0.a, _arg_0.b | ||
| 174 | if a1 == nil then | ||
| 175 | a1 = 123 | ||
| 176 | end | ||
| 177 | if b == nil then | ||
| 178 | b = 'abc' | ||
| 179 | end | ||
| 180 | return a1 .. " " .. b | ||
| 181 | end | ||
| 182 | assert.same(fn({ }), "123 abc") | ||
| 183 | return assert.same(fn({ | ||
| 184 | a = 456 | ||
| 185 | }), "456 abc") | ||
| 186 | end) | ||
| 187 | it("should support empty function body", function() | ||
| 188 | local empty_fn | ||
| 189 | empty_fn = function() end | ||
| 190 | return assert.same(empty_fn(), nil) | ||
| 191 | end) | ||
| 192 | it("should work with function in table", function() | ||
| 193 | local tb = { | ||
| 194 | value = 10, | ||
| 195 | double = function(self) | ||
| 196 | return self.value * 2 | ||
| 197 | end | ||
| 198 | } | ||
| 199 | return assert.same(tb:double(), 20) | ||
| 200 | end) | ||
| 201 | it("should handle function with no arguments", function() | ||
| 202 | local fn | ||
| 203 | fn = function() | ||
| 204 | return "result" | ||
| 205 | end | ||
| 206 | assert.same(fn(), "result") | ||
| 207 | return assert.same(fn(), "result") | ||
| 208 | end) | ||
| 209 | it("should support calling function with !", function() | ||
| 210 | local fn | ||
| 211 | fn = function() | ||
| 212 | return 42 | ||
| 213 | end | ||
| 214 | return assert.same(fn(), 42) | ||
| 215 | end) | ||
| 216 | it("should work with nested functions", function() | ||
| 217 | local outer | ||
| 218 | outer = function(x) | ||
| 219 | local inner | ||
| 220 | inner = function(y) | ||
| 221 | return x + y | ||
| 222 | end | ||
| 223 | return inner | ||
| 224 | end | ||
| 225 | local add_five = outer(5) | ||
| 226 | return assert.same(add_five(10), 15) | ||
| 227 | end) | ||
| 228 | it("should handle function in expression", function() | ||
| 229 | local result | ||
| 230 | if (function(x) | ||
| 231 | return x > 10 | ||
| 232 | end)(15) then | ||
| 233 | result = "large" | ||
| 234 | else | ||
| 235 | result = "small" | ||
| 236 | end | ||
| 237 | return assert.same(result, "large") | ||
| 238 | end) | ||
| 239 | return it("should support function as return value", function() | ||
| 240 | local get_operation | ||
| 241 | get_operation = function(op) | ||
| 242 | if "add" == op then | ||
| 243 | return function(a, b) | ||
| 244 | return a + b | ||
| 245 | end | ||
| 246 | elseif "subtract" == op then | ||
| 247 | return function(a, b) | ||
| 248 | return a - b | ||
| 249 | end | ||
| 250 | else | ||
| 251 | return function() | ||
| 252 | return 0 | ||
| 253 | end | ||
| 254 | end | ||
| 255 | end | ||
| 256 | local add = get_operation("add") | ||
| 257 | return assert.same(add(5, 3), 8) | ||
| 258 | end) | ||
| 259 | end) | ||
