|
| 1 | +---@diagnostic disable: undefined-global |
| 2 | + |
| 3 | +local Set = require("azrar.vendor.Set") |
| 4 | +local deepcopy = require("pl.tablex").deepcopy |
| 5 | + |
| 6 | +local upper = string.upper |
| 7 | + |
| 8 | +describe("azrar.vendor.Set", function() |
| 9 | + ([[ _____ ___de _b_d_ _bcd_ a_c__ __c__ |
| 10 | + a_c_e a___e abc__ abcd_ abcde |
| 11 | + ab_de ABC__ ]]):gsub("%S+", function(p) |
| 12 | + local set = Set() |
| 13 | + p:gsub(".", function(c) |
| 14 | + set[c] = c ~= "_" and true or nil |
| 15 | + end) |
| 16 | + _G[p] = set |
| 17 | + end) |
| 18 | + |
| 19 | + -- stylua: ignore |
| 20 | + local tests = { |
| 21 | + --------------fname-------------|--set--|--arg--|-expected-|-same_ref?--- |
| 22 | + { "add" , _b_d_ , "c" , _bcd_ , true }, |
| 23 | + { "clear" , abcde , nil , {} , true }, |
| 24 | + { "copy" , abcde , nil , abcde , }, |
| 25 | + { "difference_update" , a_c_e , _bcd_ , a___e , true }, |
| 26 | + { "difference" , a_c_e , _bcd_ , a___e , }, |
| 27 | + { "discard" , _bcd_ , "c" , _b_d_ , true }, |
| 28 | + { "intersection_update" , a_c_e , _bcd_ , __c__ , true }, |
| 29 | + { "intersection" , a_c_e , _bcd_ , __c__ , }, |
| 30 | + { "isdisjoint" , _b_d_ , ___de , false , }, |
| 31 | + { "isdisjoint" , _b_d_ , a___e , true , }, |
| 32 | + { "isempty" , _____ , nil , true , }, |
| 33 | + { "isempty" , abcde , nil , false , }, |
| 34 | + { "issubset" , abcd_ , abcde , true , }, |
| 35 | + { "issubset" , abcde , abcd_ , false , }, |
| 36 | + { "issuperset" , abcd_ , abcde , false , }, |
| 37 | + { "issuperset" , abcde , abcd_ , true , }, |
| 38 | + { "len" , _____ , nil , 0 , }, |
| 39 | + { "len" , a_c__ , nil , 2 , }, |
| 40 | + { "map" , abc__ , upper , ABC__ , }, |
| 41 | + { "pop" , _____ , nil , nil , }, |
| 42 | + { "remove" , _bcd_ , "c" , _b_d_ , true }, |
| 43 | + { "symmetric_difference_update" , a_c_e , _bcd_ , ab_de , true }, |
| 44 | + { "symmetric_difference" , a_c_e , _bcd_ , ab_de , }, |
| 45 | + { "union" , abc__ , ___de , abcde , }, |
| 46 | + { "update" , abc__ , ___de , abcde , true }, |
| 47 | + { "values" , _____ , nil , {} , }, |
| 48 | + } |
| 49 | + |
| 50 | + for _, t in ipairs(tests) do |
| 51 | + local fname, set, arg_, expected, same_ref = table.unpack(t) |
| 52 | + it(fname .. "() returns correct value", function() |
| 53 | + set = deepcopy(set) |
| 54 | + local res = set[fname](set, arg_) |
| 55 | + assert.are_same(expected, res) |
| 56 | + if same_ref then |
| 57 | + assert.are_equal(set, res, "Expected same set instance") |
| 58 | + else |
| 59 | + assert.not_equal(set, res, "Expected different set instances") |
| 60 | + end |
| 61 | + end) |
| 62 | + end |
| 63 | + |
| 64 | + describe("__call", function() |
| 65 | + it("constructs a set from a list", function() |
| 66 | + local s = Set({ "a", "b", "c" }) |
| 67 | + assert.is_true(getmetatable(s) == Set) |
| 68 | + assert.are_same({ a = true, b = true, c = true }, s) |
| 69 | + end) |
| 70 | + |
| 71 | + it("handles duplicate values", function() |
| 72 | + local s = Set({ "a", "b", "a", "c" }) |
| 73 | + assert.are_same({ a = true, b = true, c = true }, s) |
| 74 | + end) |
| 75 | + end) |
| 76 | + |
| 77 | + it("pop() removes and returns an arbitrary element", function() |
| 78 | + local s = Set({ "a", "b", "c" }) |
| 79 | + local v = s:pop() |
| 80 | + assert.is_true(v == "a" or v == "b" or v == "c") |
| 81 | + assert.is_nil(s[v]) |
| 82 | + end) |
| 83 | + |
| 84 | + it("values() returns all values as a list", function() |
| 85 | + local ls = { "a", "b", "c" } |
| 86 | + local res = Set(ls):values() |
| 87 | + assert.are_same(ls, res:sort()) |
| 88 | + end) |
| 89 | +end) |
0 commit comments