| 1 |
local util = require 'lua_util' |
| 2 |
|
| 3 |
context("Lua util - callback_from_string", function() |
| 4 |
local cases = { |
| 5 |
{'return function', 'return function(a, b) return a + b end'}, |
| 6 |
{'function', 'function(a, b) return a + b end'}, |
| 7 |
{'plain ops', 'local c = select(1, ...)\nreturn c + select(2, ...)'}, |
| 8 |
} |
| 9 |
local fail_cases = { |
| 10 |
nil, |
| 11 |
'', |
| 12 |
'return function(a, b) ( end', |
| 13 |
'function(a, b) ( end', |
| 14 |
'return a + b' |
| 15 |
} |
| 16 |
|
| 17 |
for _,c in ipairs(cases) do |
| 18 |
test('Success case: ' .. c[1], function() |
| 19 |
local ret,f = util.callback_from_string(c[2]) |
| 20 |
assert_true(ret, f) |
| 21 |
assert_equal(f(2, 2), 4) |
| 22 |
end) |
| 23 |
end |
| 24 |
for i,c in ipairs(fail_cases) do |
| 25 |
test('Failure case: ' .. tostring(i), function() |
| 26 |
local ret,f = util.callback_from_string(c) |
| 27 |
assert_false(ret) |
| 28 |
end) |
| 29 |
end |
| 30 |
end) |
| 31 |
|
| 32 |
context("Lua util - str_endswith", function() |
| 33 |
local ending = { |
| 34 |
{'a', 'a'}, |
| 35 |
{'ab', 'b'}, |
| 36 |
{'ab', 'ab'}, |
| 37 |
{'abc', 'bc'}, |
| 38 |
{'any', ''}, |
| 39 |
} |
| 40 |
local not_ending = { |
| 41 |
{'a', 'b'}, |
| 42 |
{'', 'a'}, |
| 43 |
{'ab', 'a'}, |
| 44 |
{'ab', 'ba'}, |
| 45 |
{'ab', 'lab'}, |
| 46 |
{'abc', 'ab'}, |
| 47 |
{'abcd', 'bc'}, |
| 48 |
{'a', 'A'}, |
| 49 |
{'aB', 'b'}, |
| 50 |
} |
| 51 |
for _, c in ipairs(ending) do |
| 52 |
test(string.format('True case: str_endswith("%s", "%s")', c[1], c[2]), function() |
| 53 |
assert_true(util.str_endswith(c[1], c[2])) |
| 54 |
end) |
| 55 |
end |
| 56 |
for _, c in ipairs(not_ending) do |
| 57 |
test(string.format('False case: str_endswith("%s", "%s")', c[1], c[2]), function() |
| 58 |
assert_false(util.str_endswith(c[1], c[2])) |
| 59 |
end) |
| 60 |
end |
| 61 |
end) |