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/.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 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 = { diff --git a/spec/app_spec.lua b/spec/app_spec.lua new file mode 100644 index 00000000..3b1f2909 --- /dev/null +++ b/spec/app_spec.lua @@ -0,0 +1,27 @@ +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) + +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/spec/date_spec.lua b/spec/date_spec.lua new file mode 100644 index 00000000..1032de25 --- /dev/null +++ b/spec/date_spec.lua @@ -0,0 +1,48 @@ +local Date = require("pl.Date") + +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) + + end) + +end) 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/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/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/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-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 94bd661a..6a91a098 100644 --- a/tests/test-date.lua +++ b/tests/test-date.lua @@ -1,44 +1,10 @@ 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 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)) @@ -119,10 +85,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 - print(app.lua()) - if not utils.execute ("TZ='Europe/London' "..app.lua().." tests/test-date2.lua") 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) - - - 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) 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') - 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'}) 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') -