From b4d677797a952efd213969b9cb31f5278f2e61e3 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 14 Feb 2015 13:38:01 +0100 Subject: [PATCH] awful.util.unittest: Move into spec/ This gets rid of awful.util.unittest and instead creates an automatic test for it under spec/awful/util_spec.lua. In the process, it also fixes the test to actually test the right thing and I took the liberty to add some more tests. Signed-off-by: Uli Schlachter --- lib/awful/util.lua.in | 11 ------ spec/awful/util_spec.lua | 80 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 spec/awful/util_spec.lua diff --git a/lib/awful/util.lua.in b/lib/awful/util.lua.in index d62971956..61d8c342c 100644 --- a/lib/awful/util.lua.in +++ b/lib/awful/util.lua.in @@ -505,17 +505,6 @@ function util.query_to_pattern(q) return s end -function util.unittest() - local function testquery(q, s) - print('testquery("'..q..'", "'..s..'")') - local p = util.query_to_pattern("downlow") - local s = "DownLow" - assert(s:match(p)) - end - testquery("downlow", "DownLow") - testquery("%word", "%word") -end - return util -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/spec/awful/util_spec.lua b/spec/awful/util_spec.lua new file mode 100644 index 000000000..a06f1db74 --- /dev/null +++ b/spec/awful/util_spec.lua @@ -0,0 +1,80 @@ +--------------------------------------------------------------------------- +-- @author Uli Schlachter +-- @copyright 2015 Uli Schlachter +--------------------------------------------------------------------------- + +local util = require("awful.util") +local say = require("say") +local assert_util = require("luassert.util") + +describe("awful.util", function() + it("table.keys_filter", function() + local t = { "a", 1, function() end, false} + assert.is.same(util.table.keys_filter(t, "number", "function"), { 2, 3 }) + end) + + it("table.reverse", function() + local t = { "a", "b", c = "c", "d" } + assert.is.same(util.table.reverse(t), { "d", "b", "a", c = "c" }) + end) + + describe("table.iterate", function() + it("no filter", function() + local t = { "a", "b", c = "c", "d" } + local f = util.table.iterate(t, function() return true end) + assert.is.equal(f(), "a") + assert.is.equal(f(), "b") + assert.is.equal(f(), "d") + assert.is.equal(f(), nil) + assert.is.equal(f(), nil) + end) + + it("b filter", function() + local t = { "a", "b", c = "c", "d" } + local f = util.table.iterate(t, function(i) return i == "b" end) + assert.is.equal(f(), "b") + assert.is.equal(f(), nil) + assert.is.equal(f(), nil) + end) + + it("with offset", function() + local t = { "a", "b", c = "c", "d" } + local f = util.table.iterate(t, function() return true end, 2) + assert.is.equal(f(), "b") + assert.is.equal(f(), "d") + assert.is.equal(f(), "a") + assert.is.equal(f(), nil) + assert.is.equal(f(), nil) + end) + end) + + describe("quote_pattern", function() + it("text", function() + assert.is.equal(util.quote_pattern("text"), "text") + end) + + it("do.t", function() + assert.is.equal(util.quote_pattern("do.t"), "do%.t") + end) + + it("per%cen[tage", function() + assert.is.equal(util.quote_pattern("per%cen[tage"), "per%%cen%[tage") + end) + end) + + describe("query_to_pattern", function() + it("DownLow", function() + assert.is.equal(string.match("DownLow", util.query_to_pattern("downlow")), "DownLow") + end) + + it("%word", function() + assert.is.equal(string.match("%word", util.query_to_pattern("%word")), "%word") + end) + + it("Substring of DownLow", function() + assert.is.equal(string.match("DownLow", util.query_to_pattern("ownl")), "ownL") + end) + end) +end) + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80