|
| 1 | +---@diagnostic disable: undefined-field, param-type-mismatch, need-check-nil |
| 2 | + |
| 3 | +local is = require("mods.is") |
| 4 | +local fmt = string.format |
| 5 | + |
| 6 | +describe("mods.is", function() |
| 7 | + local f = function() end |
| 8 | + local co = coroutine.create(f) |
| 9 | + local ct = setmetatable({}, { __call = f }) |
| 10 | + local nct = setmetatable({}, { __call = true }) |
| 11 | + |
| 12 | + it("is is callable via __call", function() |
| 13 | + assert.is_callable(is) |
| 14 | + end) |
| 15 | + |
| 16 | + -- stylua: ignore |
| 17 | + local tests = { |
| 18 | + -----type----|----ok-----|-not-ok--- |
| 19 | + { "Boolean" , false , 123 }, |
| 20 | + { "Boolean" , true , nil }, |
| 21 | + { "Callable" , ct , nct }, |
| 22 | + { "Callable" , f , {} }, |
| 23 | + { "False" , false , true }, |
| 24 | + { "Falsy" , false , true }, |
| 25 | + { "Falsy" , nil , 123 }, |
| 26 | + { "Function" , f , "abc" }, |
| 27 | + { "Integer" , 123 , 13.4 }, |
| 28 | + { "Integer" , 123 , nil }, |
| 29 | + { "Nil" , nil , 123 }, |
| 30 | + { "Number" , 123 , "123" }, |
| 31 | + { "String" , "abc" , true }, |
| 32 | + { "Table" , {} , false }, |
| 33 | + { "Thread" , co , f }, |
| 34 | + { "True" , true , false }, |
| 35 | + { "Truthy" , 123 , nil }, |
| 36 | + { "Truthy" , true , false }, |
| 37 | + { "Userdata" , io.stdout , {} }, |
| 38 | + } |
| 39 | + |
| 40 | + for i = 1, #tests do |
| 41 | + local tp, v1, v2 = unpack(tests[i], 1, 3) |
| 42 | + |
| 43 | + it(fmt("exposes is.%s", tp), function() |
| 44 | + assert.is_function(is[tp]) |
| 45 | + end) |
| 46 | + |
| 47 | + it(fmt("exposes is.%s", tp:lower()), function() |
| 48 | + assert.is_function(is[tp:lower()]) |
| 49 | + end) |
| 50 | + |
| 51 | + it(fmt("is.%s returns true for %s", tp, pretty(v1)), function() |
| 52 | + assert.is_true(is[tp](v1)) |
| 53 | + end) |
| 54 | + |
| 55 | + it(fmt("is.%s returns false for %s", tp, pretty(v2)), function() |
| 56 | + assert.is_false(is[tp](v2)) |
| 57 | + end) |
| 58 | + |
| 59 | + it(fmt("is(%q, %s) returns true", tp, pretty(v1)), function() |
| 60 | + assert.is_true(is(v1, tp)) |
| 61 | + end) |
| 62 | + |
| 63 | + it(fmt("is(%q, %s) returns false", tp, pretty(v2)), function() |
| 64 | + assert.is_false(is(v2, tp)) |
| 65 | + end) |
| 66 | + end |
| 67 | +end) |
0 commit comments