|
| 1 | +local jestGlobals = require('@pkg/@jsdotlua/jest-globals') |
| 2 | + |
| 3 | +local reverseFindIndex = require('../reverseFindIndex') |
| 4 | + |
| 5 | +local expect = jestGlobals.expect |
| 6 | +local it = jestGlobals.it |
| 7 | + |
| 8 | +it('finds the index of the last element matching the predicate', function() |
| 9 | + local array = { 1, 2, 3, 4, 5 } |
| 10 | + local result = reverseFindIndex(array, function(element) |
| 11 | + return element % 2 == 0 |
| 12 | + end) |
| 13 | + |
| 14 | + expect(result).toBe(4) |
| 15 | +end) |
| 16 | + |
| 17 | +it('returns nil when no element matches', function() |
| 18 | + local array = { 1, 3, 5, 7, 9 } |
| 19 | + local result = reverseFindIndex(array, function(element) |
| 20 | + return element % 2 == 0 |
| 21 | + end) |
| 22 | + |
| 23 | + expect(result).toBe(nil) |
| 24 | +end) |
| 25 | + |
| 26 | +it('starts from the given index', function() |
| 27 | + local array = { 1, 2, 3, 4, 5 } |
| 28 | + local result = reverseFindIndex(array, function(element) |
| 29 | + return element % 2 == 0 |
| 30 | + end, 3) |
| 31 | + |
| 32 | + expect(result).toBe(2) |
| 33 | +end) |
| 34 | + |
| 35 | +it('handles empty array', function() |
| 36 | + local array = {} |
| 37 | + local result = reverseFindIndex(array, function(_element) |
| 38 | + return true |
| 39 | + end) |
| 40 | + |
| 41 | + expect(result).toBe(nil) |
| 42 | +end) |
| 43 | + |
| 44 | +return nil |
0 commit comments