Software / code / prosody
Comparison
spec/util_array_spec.lua @ 10100:5ca9c4917885
util.array: Add tests
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Mon, 29 Jul 2019 00:51:03 +0200 |
| child | 10397:921e8b00778e |
comparison
equal
deleted
inserted
replaced
| 10099:7e3196e0263e | 10100:5ca9c4917885 |
|---|---|
| 1 local array = require "util.array"; | |
| 2 describe("util.array", function () | |
| 3 describe("creation", function () | |
| 4 describe("from tablle", function () | |
| 5 it("works", function () | |
| 6 local a = array({"a", "b", "c"}); | |
| 7 assert.same({"a", "b", "c"}, a); | |
| 8 end); | |
| 9 end); | |
| 10 | |
| 11 describe("from iterator", function () | |
| 12 it("works", function () | |
| 13 -- collects the first value, ie the keys | |
| 14 local a = array(ipairs({true, true, true})); | |
| 15 assert.same({1, 2, 3}, a); | |
| 16 end); | |
| 17 end); | |
| 18 | |
| 19 describe("collect", function () | |
| 20 it("works", function () | |
| 21 -- collects the first value, ie the keys | |
| 22 local a = array.collect(ipairs({true, true, true})); | |
| 23 assert.same({1, 2, 3}, a); | |
| 24 end); | |
| 25 end); | |
| 26 | |
| 27 end); | |
| 28 | |
| 29 describe("metatable", function () | |
| 30 describe("operator", function () | |
| 31 describe("addition", function () | |
| 32 it("works", function () | |
| 33 local a = array({ "a", "b" }); | |
| 34 local b = array({ "c", "d" }); | |
| 35 assert.same({"a", "b", "c", "d"}, a + b); | |
| 36 end); | |
| 37 end); | |
| 38 | |
| 39 describe("equality", function () | |
| 40 it("works", function () | |
| 41 local a1 = array({ "a", "b" }); | |
| 42 local a2 = array({ "a", "b" }); | |
| 43 local b = array({ "c", "d" }); | |
| 44 assert.truthy(a1 == a2); | |
| 45 assert.falsy(a1 == b); | |
| 46 end); | |
| 47 end); | |
| 48 | |
| 49 describe("division", function () | |
| 50 it("works", function () | |
| 51 local a = array({ "a", "b", "c" }); | |
| 52 local b = a / function (i) if i ~= "b" then return i .. "x" end end; | |
| 53 assert.same({ "ax", "cx" }, b); | |
| 54 end); | |
| 55 end); | |
| 56 | |
| 57 end); | |
| 58 end); | |
| 59 | |
| 60 describe("methods", function () | |
| 61 describe("map", function () | |
| 62 it("works", function () | |
| 63 local a = array({ "a", "b", "c" }); | |
| 64 local b = a:map(string.upper); | |
| 65 assert.same({ "A", "B", "C" }, b); | |
| 66 end); | |
| 67 end); | |
| 68 | |
| 69 describe("filter", function () | |
| 70 it("works", function () | |
| 71 local a = array({ "a", "b", "c" }); | |
| 72 a:filter(function (i) return i ~= "b" end); | |
| 73 assert.same({ "a", "c" }, a); | |
| 74 end); | |
| 75 end); | |
| 76 | |
| 77 describe("sort", function () | |
| 78 it("works", function () | |
| 79 local a = array({ 5, 4, 3, 1, 2, }); | |
| 80 a:sort(); | |
| 81 assert.same({ 1, 2, 3, 4, 5, }, a); | |
| 82 end); | |
| 83 end); | |
| 84 | |
| 85 describe("unique", function () | |
| 86 it("works", function () | |
| 87 local a = array({ "a", "b", "c", "c", "a", "b" }); | |
| 88 a:unique(); | |
| 89 assert.same({ "a", "b", "c" }, a); | |
| 90 end); | |
| 91 end); | |
| 92 | |
| 93 describe("pluck", function () | |
| 94 it("works", function () | |
| 95 local a = array({ { a = 1, b = -1 }, { a = 2, b = -2 }, }); | |
| 96 a:pluck("a"); | |
| 97 assert.same({ 1, 2 }, a); | |
| 98 end); | |
| 99 end); | |
| 100 | |
| 101 | |
| 102 describe("reverse", function () | |
| 103 it("works", function () | |
| 104 local a = array({ "a", "b", "c" }); | |
| 105 a:reverse(); | |
| 106 assert.same({ "c", "b", "a" }, a); | |
| 107 end); | |
| 108 end); | |
| 109 | |
| 110 -- TODO :shuffle | |
| 111 | |
| 112 describe("append", function () | |
| 113 it("works", function () | |
| 114 local a = array({ "a", "b", "c" }); | |
| 115 a:append(array({ "d", "e", })); | |
| 116 assert.same({ "a", "b", "c", "d", "e" }, a); | |
| 117 end); | |
| 118 end); | |
| 119 | |
| 120 describe("push", function () | |
| 121 it("works", function () | |
| 122 local a = array({ "a", "b", "c" }); | |
| 123 a:push("d"):push("e"); | |
| 124 assert.same({ "a", "b", "c", "d", "e" }, a); | |
| 125 end); | |
| 126 end); | |
| 127 | |
| 128 describe("pop", function () | |
| 129 it("works", function () | |
| 130 local a = array({ "a", "b", "c" }); | |
| 131 assert.equal("c", a:pop()); | |
| 132 assert.same({ "a", "b", }, a); | |
| 133 end); | |
| 134 end); | |
| 135 | |
| 136 describe("concat", function () | |
| 137 it("works", function () | |
| 138 local a = array({ "a", "b", "c" }); | |
| 139 assert.equal("a,b,c", a:concat(",")); | |
| 140 end); | |
| 141 end); | |
| 142 | |
| 143 describe("length", function () | |
| 144 it("works", function () | |
| 145 local a = array({ "a", "b", "c" }); | |
| 146 assert.equal(3, a:length()); | |
| 147 end); | |
| 148 end); | |
| 149 | |
| 150 end); | |
| 151 | |
| 152 -- TODO The various array.foo(array ina, array outa) functions | |
| 153 end); | |
| 154 |