diff options
Diffstat (limited to 'spec/outputs/test/backcall_spec.lua')
| -rw-r--r-- | spec/outputs/test/backcall_spec.lua | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/spec/outputs/test/backcall_spec.lua b/spec/outputs/test/backcall_spec.lua new file mode 100644 index 0000000..1eb9f02 --- /dev/null +++ b/spec/outputs/test/backcall_spec.lua | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | return describe("backcall", function() | ||
| 2 | it("should support basic backcall with <-", function() | ||
| 3 | local results = { } | ||
| 4 | local mock_map | ||
| 5 | mock_map = function(list, fn) | ||
| 6 | for _index_0 = 1, #list do | ||
| 7 | local item = list[_index_0] | ||
| 8 | table.insert(results, fn(item)) | ||
| 9 | end | ||
| 10 | end | ||
| 11 | do | ||
| 12 | mock_map({ | ||
| 13 | 1, | ||
| 14 | 2, | ||
| 15 | 3 | ||
| 16 | }, function(x) | ||
| 17 | return x * 2 | ||
| 18 | end) | ||
| 19 | end | ||
| 20 | return assert.same(results, { | ||
| 21 | 2, | ||
| 22 | 4, | ||
| 23 | 6 | ||
| 24 | }) | ||
| 25 | end) | ||
| 26 | it("should support nested backcalls", function() | ||
| 27 | local results = { } | ||
| 28 | local mock_map | ||
| 29 | mock_map = function(list, fn) | ||
| 30 | for _index_0 = 1, #list do | ||
| 31 | local item = list[_index_0] | ||
| 32 | fn(item) | ||
| 33 | end | ||
| 34 | end | ||
| 35 | mock_map({ | ||
| 36 | 1, | ||
| 37 | 2, | ||
| 38 | 3, | ||
| 39 | 4 | ||
| 40 | }, function(x) | ||
| 41 | if x > 2 then | ||
| 42 | return table.insert(results, x) | ||
| 43 | end | ||
| 44 | end) | ||
| 45 | return assert.same(results, { | ||
| 46 | 3, | ||
| 47 | 4 | ||
| 48 | }) | ||
| 49 | end) | ||
| 50 | return it("should work with method call backcall", function() | ||
| 51 | local results = { } | ||
| 52 | local obj = { | ||
| 53 | process = function(self, fn) | ||
| 54 | return fn(42) | ||
| 55 | end | ||
| 56 | } | ||
| 57 | return obj:process(function(value) | ||
| 58 | table.insert(results, value) | ||
| 59 | return assert.same(results, { | ||
| 60 | 42 | ||
| 61 | }) | ||
| 62 | end) | ||
| 63 | end) | ||
| 64 | end) | ||
