|
| 1 | +---@diagnostic disable: param-type-mismatch |
| 2 | + |
| 3 | +local ops = require("mods.operator") |
| 4 | + |
| 5 | +describe("mods.operator", function() |
| 6 | + -- stylua: ignore start |
| 7 | + local function sum(a, b) return a + b end |
| 8 | + local tbl = { a = 1 } |
| 9 | + local tests = { |
| 10 | + ---operator--|--------args------|-expected--- |
| 11 | + { "add" , { 3, 4 } , 7 }, |
| 12 | + { "sub" , { 3, 4 } , -1 }, |
| 13 | + { "mul" , { 3, 4 } , 12 }, |
| 14 | + { "div" , { 10, 4 } , 2.5 }, |
| 15 | + { "idiv" , { 5, 2 } , 2 }, |
| 16 | + { "mod" , { 5, 2 } , 1 }, |
| 17 | + { "pow" , { 2, 4 } , 16 }, |
| 18 | + { "unm" , { -3 } , 3 }, |
| 19 | + |
| 20 | + { "eq" , { 1, 1 } , true }, |
| 21 | + { "neq" , { 1, 2 } , true }, |
| 22 | + { "lt" , { 1, 2 } , true }, |
| 23 | + { "le" , { 2, 2 } , true }, |
| 24 | + { "gt" , { 3, 2 } , true }, |
| 25 | + { "ge" , { 2, 2 } , true }, |
| 26 | + |
| 27 | + { "land" , { false, true } , false }, |
| 28 | + { "land" , { true, true } , true }, |
| 29 | + { "lor" , { false, true } , true }, |
| 30 | + { "lor" , { false, false } , false }, |
| 31 | + { "lnot" , { true } , false }, |
| 32 | + |
| 33 | + { "concat" , { "a", "b" } , "ab" }, |
| 34 | + { "len" , { "abc" } , 3 }, |
| 35 | + |
| 36 | + { "index" , { tbl, "a" } , 1 }, |
| 37 | + { "index" , { tbl, "b" } , nil }, |
| 38 | + { "call" , { sum, 1, 2 } , 3 }, |
| 39 | + { "setindex" , { tbl, "a", 2 } , 2 }, |
| 40 | + } |
| 41 | + -- stylua: ignore end |
| 42 | + |
| 43 | + for i = 1, #tests do |
| 44 | + local op, args, expected = unpack(tests[i]) |
| 45 | + it(op .. "() returns correct value", function() |
| 46 | + assert.are_equal(expected, ops[op](unpack(args))) |
| 47 | + end) |
| 48 | + end |
| 49 | +end) |
0 commit comments