summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2024-01-01 23:07:21 +0100
committerAki <please@ignore.pl>2024-01-01 23:07:21 +0100
commit91e85f9809ed406d2120e424aaa2f249ee18e2c2 (patch)
treee35c522db195727308d0795ee0b4f9b50b9b3c29 /spec
downloadensure-91e85f9809ed406d2120e424aaa2f249ee18e2c2.zip
ensure-91e85f9809ed406d2120e424aaa2f249ee18e2c2.tar.gz
ensure-91e85f9809ed406d2120e424aaa2f249ee18e2c2.tar.bz2
Implemented first and easy part of reading package lists
Diffstat (limited to 'spec')
-rw-r--r--spec/list_spec.lua62
1 files changed, 62 insertions, 0 deletions
diff --git a/spec/list_spec.lua b/spec/list_spec.lua
new file mode 100644
index 0000000..d7b5889
--- /dev/null
+++ b/spec/list_spec.lua
@@ -0,0 +1,62 @@
+local list = require "ensure.list"
+
+
+local function handler_with_errors ()
+ local errors = {}
+ local handler = {
+ identifier = function (_, name)
+ return name
+ end,
+ invalid = function (_, text)
+ table.insert(errors, text)
+ end,
+ }
+ return handler, errors
+end
+
+
+describe("List reader", function()
+ it("should translate empty file into an empty table", function()
+ assert.are.same({}, list.read"")
+ end)
+
+ it("should match valid package names", function()
+ local samples = {"name", "@name", "+name", "_name", "name-1.45"}
+ for _, name in pairs(samples) do
+ assert.are.same({name}, list.read(name))
+ end
+ end)
+
+ it("should split package names on whitespace", function()
+ assert.are.same(
+ {"one", "two", "three", "four", "five"},
+ list.read [[one two three
+ four five]]
+ )
+ end)
+
+ it("should ignore leading and trailing whitespace", function()
+ assert.are.same({"name"}, list.read" name ")
+ end)
+
+ it("should not match invalid package names", function()
+ assert.are.same({}, list.read".hey")
+ assert.are.same({}, list.read"a=3")
+ end)
+
+ it("should allow custom handlers", function()
+ local handler = {}
+ stub(handler, "identifier")
+ stub(handler, "invalid")
+ local _ = list.read("correct .incorrect", handler)
+ assert.stub(handler.identifier).was.called()
+ assert.stub(handler.invalid).was.called()
+ end)
+
+ it("should match valid and invalid names together", function()
+ local handler, errors = handler_with_errors()
+ local output = list.read("correct .incorrect another -and-not", handler)
+ assert.are.same({"correct", "another"}, output)
+ assert.are.same({".incorrect", "-and-not"}, errors)
+ end)
+end)