From e4c0e30daeb489e664af3d9308d48ccd5f7fd844 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Wed, 30 Sep 2020 17:01:53 +0300 Subject: [PATCH 01/10] chore: Add configuration for Busted test framework --- .busted | 6 ++++++ .luacheckrc | 1 + penlight-dev-1.rockspec | 8 ++++++++ 3 files changed, 15 insertions(+) create mode 100644 .busted diff --git a/.busted b/.busted new file mode 100644 index 00000000..ed6f8878 --- /dev/null +++ b/.busted @@ -0,0 +1,6 @@ +return { + default = { + lpath = "./lua/?.lua;./lua/?/init.lua", + } +} +-- vim: ft=lua diff --git a/.luacheckrc b/.luacheckrc index 0da34feb..53fc740e 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -14,6 +14,7 @@ not_globals = { include_files = { "**/*.lua", "*.rockspec", + ".busted", ".luacheckrc", } diff --git a/penlight-dev-1.rockspec b/penlight-dev-1.rockspec index 73246a29..b02570f5 100644 --- a/penlight-dev-1.rockspec +++ b/penlight-dev-1.rockspec @@ -26,6 +26,14 @@ dependencies = { "luafilesystem" } +test_dependencies = { + "busted", +} + +test = { + type = "busted", +} + build = { type = "builtin", modules = { From 5dd27beb1388480050a0b1ffb40a9646c60c2b57 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 1 Oct 2020 16:40:01 +0300 Subject: [PATCH 02/10] ci: Add busted based tests to Travis & Appveyor jobs --- .travis.yml | 2 ++ appveyor.yml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index cb2bf6d9..32033341 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,11 +16,13 @@ before_install: - hererocks here -r^ --$LUA - source here/bin/activate - luarocks install luacov-coveralls + - luarocks install busted install: - luarocks make script: + - busted -c -v - lua run.lua tests --luacov - lua run.lua examples diff --git a/appveyor.yml b/appveyor.yml index 6233d941..3bc26dba 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -20,11 +20,13 @@ before_build: - call here\bin\activate - "if \"%LUA%\"==\"lua 5.4\" ( luarocks install bit32 )" - luarocks install luacov-coveralls + - luarocks install busted build_script: - luarocks make test_script: + - busted -c -v - lua run.lua tests --luacov - lua run.lua examples From 4849d387eef103f74a40d718575eb731507eccbc Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 1 Oct 2020 16:20:03 +0300 Subject: [PATCH 03/10] test: Port date serialization test to Busted --- spec/date_spec.lua | 11 +++++++++++ tests/test-date.lua | 2 +- tests/test-date2.lua | 10 ---------- 3 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 spec/date_spec.lua delete mode 100644 tests/test-date2.lua diff --git a/spec/date_spec.lua b/spec/date_spec.lua new file mode 100644 index 00000000..0a027449 --- /dev/null +++ b/spec/date_spec.lua @@ -0,0 +1,11 @@ +local Date = require("pl.Date") + +describe("pl.Date:__tostring()", function () + + it("should be suitable for serialization", function () + local df = Date.Format() + local du = df:parse("2008-07-05") + assert.is.equal(du, du:toUTC()) + end) + +end) diff --git a/tests/test-date.lua b/tests/test-date.lua index 94bd661a..3f718539 100644 --- a/tests/test-date.lua +++ b/tests/test-date.lua @@ -122,7 +122,7 @@ asserteq(tostring(now - utc),'zero') if app.platform() ~= 'Windows' then print(app.lua()) - if not utils.execute ("TZ='Europe/London' "..app.lua().." tests/test-date2.lua") then + if not utils.execute (app.lua().." -e 'print(1)'") then error "buggered!" end end diff --git a/tests/test-date2.lua b/tests/test-date2.lua deleted file mode 100644 index 230fb183..00000000 --- a/tests/test-date2.lua +++ /dev/null @@ -1,10 +0,0 @@ -local Date = require 'pl.Date' -local test = require 'pl.test' -local df = Date.Format() -local dl = df:parse '2008-07-05' -local du = dl:toUTC() - -test.asserteq(dl,du) - - - From 117bf7df3072c9428fbe49d901828f56c63a0eda Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 1 Oct 2020 16:46:07 +0300 Subject: [PATCH 04/10] test: Port text template test to Busted --- spec/text_spec.lua | 11 +++++++++++ tests/test-text2.lua | 8 -------- 2 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 spec/text_spec.lua delete mode 100644 tests/test-text2.lua diff --git a/spec/text_spec.lua b/spec/text_spec.lua new file mode 100644 index 00000000..cd42f061 --- /dev/null +++ b/spec/text_spec.lua @@ -0,0 +1,11 @@ +local text = require("pl.text") + +describe("pl.text.Template", function () + + it("replaces placeholders", function () + local tempalte = text.Template("${here} is the $answer") + local out = tempalte:substitute({ here = 'one', answer = 'two' }) + assert.is.equal('one is the two', out) + end) + +end) diff --git a/tests/test-text2.lua b/tests/test-text2.lua deleted file mode 100644 index c22edbe9..00000000 --- a/tests/test-text2.lua +++ /dev/null @@ -1,8 +0,0 @@ -local text = require 'pl.text' -local Template = text.Template -local asserteq = require 'pl.test' . asserteq - - -local t = Template('${here} is the $answer') -asserteq(t:substitute {here = 'one', answer = 'two'} , 'one is the two') - From dfc8fd9b1e48c03b256b6614b7cd2e9da301f2e7 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 1 Oct 2020 17:35:06 +0300 Subject: [PATCH 05/10] test: Port test for app invocation info to Busted --- spec/app_spec.lua | 15 +++++++++++++++ tests/test-date.lua | 5 ----- 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 spec/app_spec.lua diff --git a/spec/app_spec.lua b/spec/app_spec.lua new file mode 100644 index 00000000..a107352d --- /dev/null +++ b/spec/app_spec.lua @@ -0,0 +1,15 @@ +local app = require("pl.app") + +describe("pl.app.lua", function () + + local invocation = app.lua() + + it("should pick up the arguments used to run this test", function () + assert.is.truthy(invocation:match("lua.+package.+busted")) + end) + + it("should be reusable to invoke Lua", function () + assert.is.truthy(os.execute(app.lua()..' -e "n=1;os.exit(n-1)"')) + end) + +end) diff --git a/tests/test-date.lua b/tests/test-date.lua index 3f718539..5652f0e3 100644 --- a/tests/test-date.lua +++ b/tests/test-date.lua @@ -1,6 +1,5 @@ local test = require 'pl.test' local app = require 'pl.app' -local utils = require 'pl.utils' local asserteq, assertmatch = test.asserteq, test.assertmatch local dump = require 'pl.pretty'.dump local T = require 'pl.test'.tuple @@ -121,8 +120,4 @@ local now,utc = Date(), Date 'utc' asserteq(tostring(now - utc),'zero') if app.platform() ~= 'Windows' then - print(app.lua()) - if not utils.execute (app.lua().." -e 'print(1)'") then - error "buggered!" - end end From aa489514e5385ca26b67d64717f87d42ed85b2f3 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 1 Oct 2020 17:53:31 +0300 Subject: [PATCH 06/10] test: Port app platform detection to Busted --- spec/app_spec.lua | 12 ++++++++++++ tests/test-app.lua | 1 - tests/test-date.lua | 4 ---- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/spec/app_spec.lua b/spec/app_spec.lua index a107352d..3b1f2909 100644 --- a/spec/app_spec.lua +++ b/spec/app_spec.lua @@ -13,3 +13,15 @@ describe("pl.app.lua", function () end) end) + +describe("pl.app.platform", function () + + -- TODO: Find a reliable alternate way to determine platform to check that + -- this is returning the right answer, not just any old answer. + it("should at least return a valid platform", function () + local platforms = { Linux = true, OSX = true, Windows = true } + local detected = app.platform() + assert.is.truthy(platforms[detected]) + end) + +end) diff --git a/tests/test-app.lua b/tests/test-app.lua index 20ce6294..f5066cc2 100644 --- a/tests/test-app.lua +++ b/tests/test-app.lua @@ -260,4 +260,3 @@ do -- app.parse_args asserteq(s, {}) end - diff --git a/tests/test-date.lua b/tests/test-date.lua index 5652f0e3..31d11125 100644 --- a/tests/test-date.lua +++ b/tests/test-date.lua @@ -1,5 +1,4 @@ local test = require 'pl.test' -local app = require 'pl.app' local asserteq, assertmatch = test.asserteq, test.assertmatch local dump = require 'pl.pretty'.dump local T = require 'pl.test'.tuple @@ -118,6 +117,3 @@ asserteq(tostring(nxt - d), '1 month ') --- Can explicitly get UTC date; these of course refer to same time local now,utc = Date(), Date 'utc' asserteq(tostring(now - utc),'zero') - -if app.platform() ~= 'Windows' then -end From c151dadc6b012789172f595e2bcc22a5d08456c7 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 1 Oct 2020 18:31:43 +0300 Subject: [PATCH 07/10] test: Port MultiMap test to Busted --- spec/multimap_spec.lua | 14 ++++++++++++++ tests/test-multimap.lua | 11 ----------- 2 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 spec/multimap_spec.lua delete mode 100644 tests/test-multimap.lua diff --git a/spec/multimap_spec.lua b/spec/multimap_spec.lua new file mode 100644 index 00000000..a83d1500 --- /dev/null +++ b/spec/multimap_spec.lua @@ -0,0 +1,14 @@ +local MultiMap = require("pl.MultiMap") + +describe("pl.MultiMap", function () + + it("should hold multiple values per key", function () + local map = MultiMap() + map:set('foo', 1) + map:set('bar', 3) + map:set('foo', 2) + local expected = { foo = { 1, 2 }, bar = { 3 } } + assert.is.same(expected, map) + end) + +end) diff --git a/tests/test-multimap.lua b/tests/test-multimap.lua deleted file mode 100644 index 1cb29258..00000000 --- a/tests/test-multimap.lua +++ /dev/null @@ -1,11 +0,0 @@ -local asserteq = require 'pl.test' . asserteq -local MultiMap = require 'pl.MultiMap' - -m = MultiMap() -m:set('john',1) -m:set('jane',3) -m:set('john',2) - -local ms = MultiMap{john={1,2},jane={3}} - -asserteq(m,ms) From c6e05a59f93ac1178e974c68ae3b2f33e86d3a7c Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 1 Oct 2020 20:34:27 +0300 Subject: [PATCH 08/10] test: Port pretty number format test to Busted --- spec/pretty_spec.lua | 40 ++++++++++++++++++++++++++++++++++++ tests/test-pretty-number.lua | 33 ----------------------------- 2 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 spec/pretty_spec.lua delete mode 100644 tests/test-pretty-number.lua diff --git a/spec/pretty_spec.lua b/spec/pretty_spec.lua new file mode 100644 index 00000000..85e37706 --- /dev/null +++ b/spec/pretty_spec.lua @@ -0,0 +1,40 @@ +local pretty = require("pl.pretty") + +describe("pl.pretty.number", function () + + it("should format memory", function () + local function assert_memory (expected, input) + assert.is.equal(expected, pretty.number(input, "M")) + end + assert_memory("123B", 123) + assert_memory("1.2KiB", 1234) + assert_memory("10.0KiB", 10*1024) + assert_memory("1.0MiB", 1024*1024) + assert_memory("1.0GiB", 1024*1024*1024) + end) + + it("should format postfixes", function () + local function assert_postfix(expected, input) + assert.is.equal(expected, pretty.number(input, "N", 2)) + end + assert_postfix("123", 123) + assert_postfix("1.23K", 1234) + assert_postfix("10.24K", 10*1024) + assert_postfix("1.05M", 1024*1024) + assert_postfix("1.07B", 1024*1024*1024) + end) + + it("should format postfixes", function () + local function assert_separator(expected, input) + assert.is.equal(expected, pretty.number(input, "T")) + end + assert_separator('123', 123) + assert_separator('1,234', 1234) + assert_separator('12,345', 12345) + assert_separator('123,456', 123456) + assert_separator('1,234,567', 1234567) + assert_separator('12,345,678', 12345678) + end) + + +end) diff --git a/tests/test-pretty-number.lua b/tests/test-pretty-number.lua deleted file mode 100644 index 347a5cf5..00000000 --- a/tests/test-pretty-number.lua +++ /dev/null @@ -1,33 +0,0 @@ -local test = require 'pl.test' -local pretty = require 'pl.pretty' - -function testm(x,s) - test.asserteq(pretty.number(x,'M'),s) -end - -testm(123,'123B') -testm(1234,'1.2KiB') -testm(10*1024,'10.0KiB') -testm(1024*1024,'1.0MiB') - -function testn(x,s) - test.asserteq(pretty.number(x,'N',2),s) -end - -testn(123,'123') -testn(1234,'1.23K') -testn(10*1024,'10.24K') -testn(1024*1024,'1.05M') -testn(1024*1024*1024,'1.07B') - -function testc(x,s) - test.asserteq(pretty.number(x,'T'),s) -end - -testc(123,'123') -testc(1234,'1,234') -testc(12345,'12,345') -testc(123456,'123,456') -testc(1234567,'1,234,567') -testc(12345678,'12,345,678') - From 86281b93d93efd126a40957bdf27f6268a0c9692 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 1 Oct 2020 21:50:12 +0300 Subject: [PATCH 09/10] test: Port Set test to Busted --- spec/set_spec.lua | 84 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test-set.lua | 37 -------------------- 2 files changed, 84 insertions(+), 37 deletions(-) create mode 100644 spec/set_spec.lua delete mode 100644 tests/test-set.lua diff --git a/spec/set_spec.lua b/spec/set_spec.lua new file mode 100644 index 00000000..02febcaa --- /dev/null +++ b/spec/set_spec.lua @@ -0,0 +1,84 @@ +local Set = require("pl.Set") + +describe("pl.Set", function () + + local s = Set() + local s1_2 = Set({ 1, 2 }) + local s1_2_3 = Set({ 1, 2, 3 }) + local s1_3 = Set({ 1, 3 }) + local s2 = Set({ 2 }) + local s2_1 = Set({ 2, 1 }) + local s2_3 = Set({ 2, 3 }) + local s3 = Set({ 3 }) + local sm = Set({ "foo", "bar" }) + + it("should produce a set object", function () + assert.is.same({ true, true }, s1_2) + end) + + it("should produce identical sets for any ordered input", function () + assert.is.same(s1_2, s2_1) + end) + + describe("should have an operator for", function () + + it("union", function () + assert.is.same(s1_2_3, s1_2 + s3) + assert.is.same(s1_2_3, s1_2 + 3) + end) + + it("intersection", function () + assert.is.same(s2, s1_2 * s2_3) + end) + + it("difference", function () + assert.is.same(s2_1, s1_2_3 - s3) + assert.is.same(s2_3, s1_2_3 - 1) + end) + + it("symmetric difference", function () + assert.is.same(s1_3, s1_2 ^ s2_3) + end) + + it("tostring", function () + -- Cannot test multi-entry sets because of non-deterministic key order + assert.is.same('[2]', tostring(s2)) + end) + + end) + + describe("should provide functions", function () + + it("isempty", function () + assert.is.truthy(Set.isempty(s)) + assert.is.falsy(Set.isempty(s3)) + end) + + it("set", function () + local m = Set() + Set.set(m, 'foo', true) + m.bar = true + assert.is.same(m, sm) + assert.is_not.same(m, s1_2) + end) + + end) + + describe("should have a comparison operator for", function () + + it("supersets/subsets than", function () + assert.is.truthy(s1_2 > s2) + assert.is.falsy(s1_3 > s2) + assert.is.falsy(s1_2 > s2_3) + assert.is.truthy(s1_2 < s1_2_3) + assert.is.falsy(s1_2_3 < s1_2) + end) + + it("equality", function () + assert.is.truthy(s1_2 == s2_1) + assert.is.falsy(s1_2 == s2_3) + end) + + end) + +end) diff --git a/tests/test-set.lua b/tests/test-set.lua deleted file mode 100644 index 6c123217..00000000 --- a/tests/test-set.lua +++ /dev/null @@ -1,37 +0,0 @@ -local Set = require 'pl.Set' -local asserteq = require 'pl.test' . asserteq - -local s1 = Set{1,2} -local s2 = Set{1,2} --- equality -asserteq(s1,s2) --- union -asserteq(Set{1,2} + Set{2,3}, Set{1,2,3}) -asserteq(Set{1,2} + 3, Set{1,2,3}) --- intersection -asserteq(Set{1,2} * Set{2,3}, Set{2}) --- difference -local fruit = Set{'apple','banana','orange','apricots'} -local tropical = Set{'banana','orange'} - -asserteq(fruit - tropical, Set{'apple','apricots'}) -asserteq(tropical - 'orange', Set{'banana'}) - --- symmetric_difference -asserteq(Set{1,2} ^ Set{2,3}, Set{1,3}) --- tostring - illustrative, because these assertions may or may not work, --- due to no ordering in set elements ---asserteq(tostring(S{1,2}),'[1,2]') ---asserteq(tostring(S{1,S{2,3}}),'[1,[2,3]]') - -local s3 = Set() -asserteq(Set.isempty(s3),true) - -local s4 = Set{1,2,3} - --- subsets/supersets -asserteq(s4 > s1,true) - -Set.set(s3,'one',true) -s3.two = true -asserteq(s3,Set{'one','two'}) From a2d59f1d77f74aadf431b5cc89ca0ea0de678a99 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Fri, 2 Oct 2020 00:36:29 +0300 Subject: [PATCH 10/10] test: Port Date format tests to Busted --- spec/date_spec.lua | 47 ++++++++++++++++++++++++++++++++++++++++----- tests/test-date.lua | 32 ------------------------------ 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/spec/date_spec.lua b/spec/date_spec.lua index 0a027449..1032de25 100644 --- a/spec/date_spec.lua +++ b/spec/date_spec.lua @@ -1,11 +1,48 @@ local Date = require("pl.Date") -describe("pl.Date:__tostring()", function () +describe("pl.Date", function () + + describe("function", function () + + describe("Format()", function () + + it("should output parsable inputs", function () + local function assert_date_format(expected, format) + local df = Date.Format(format) + local d = df:parse(expected) + assert.is.equal(expected, df:tostring(d)) + end + assert_date_format('02/04/10', 'dd/mm/yy') + assert_date_format('04/02/2010', 'mm/dd/yyyy') + assert_date_format('2011-02-20', 'yyyy-mm-dd') + assert_date_format('20070320', 'yyyymmdd') + assert_date_format('23:10', 'HH:MM') + end) + + it("should parse 'slack' fields", function () + local df = Date.Format("m/d/yy") + -- TODO: Re-enable when issue #359 fixed + -- assert.is.equal('01/05/99', df:tostring(df:parse('1/5/99'))) + assert.is.equal('01/05/01', df:tostring(df:parse('1/5/01'))) + assert.is.equal('01/05/32', df:tostring(df:parse('1/5/32'))) + end) + + end) + + end) + + describe("meta method", function () + + describe("__tostring()", function () + + it("should be suitable for serialization", function () + local df = Date.Format() + local du = df:parse("2008-07-05") + assert.is.equal(du, du:toUTC()) + end) + + end) - it("should be suitable for serialization", function () - local df = Date.Format() - local du = df:parse("2008-07-05") - assert.is.equal(du, du:toUTC()) end) end) diff --git a/tests/test-date.lua b/tests/test-date.lua index 31d11125..6a91a098 100644 --- a/tests/test-date.lua +++ b/tests/test-date.lua @@ -5,38 +5,6 @@ local T = require 'pl.test'.tuple local Date = require 'pl.Date' ---[[ -d = Date() -print(d) -print(d:year()) -d:day(20) -print(d) -d:add {day = 2} -print(d:day()) -d = Date() -- 'now' -print(d:last_day():day()) -print(d:month(7):last_day()) ---]] - -function check_df(fmt,str,no_check) - local df = Date.Format(fmt) - local d = df:parse(str) - --print(str,d) - if not no_check then - asserteq(df:tostring(d),str) - end -end - -check_df('dd/mm/yy','02/04/10') -check_df('mm/dd/yyyy','04/02/2010') -check_df('yyyy-mm-dd','2011-02-20') -check_df('yyyymmdd','20070320') - --- use single fields for 'slack' parsing -check_df('m/d/yyyy','1/5/2001',true) - -check_df('HH:MM','23:10') - iso = Date.Format 'yyyy-mm-dd' -- ISO date d = iso:parse '2010-04-10' asserteq(T(d:day(),d:month(),d:year()),T(10,4,2010))