Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Busted test framework and start porting tests #358

Merged
merged 10 commits into from
Oct 9, 2020
6 changes: 6 additions & 0 deletions .busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
return {
default = {
lpath = "./lua/?.lua;./lua/?/init.lua",
}
}
-- vim: ft=lua
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ not_globals = {
include_files = {
"**/*.lua",
"*.rockspec",
".busted",
".luacheckrc",
}

Expand Down
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions penlight-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ dependencies = {
"luafilesystem"
}

test_dependencies = {
"busted",
}

test = {
type = "busted",
}

build = {
type = "builtin",
modules = {
Expand Down
27 changes: 27 additions & 0 deletions spec/app_spec.lua
Original file line number Diff line number Diff line change
@@ -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"))
Tieske marked this conversation as resolved.
Show resolved Hide resolved
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)
48 changes: 48 additions & 0 deletions spec/date_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions spec/multimap_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
40 changes: 40 additions & 0 deletions spec/pretty_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
84 changes: 84 additions & 0 deletions spec/set_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions spec/text_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
1 change: 0 additions & 1 deletion tests/test-app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,3 @@ do -- app.parse_args
asserteq(s, {})

end

41 changes: 0 additions & 41 deletions tests/test-date.lua
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -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
10 changes: 0 additions & 10 deletions tests/test-date2.lua

This file was deleted.

11 changes: 0 additions & 11 deletions tests/test-multimap.lua

This file was deleted.

Loading