diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/list_spec.lua | 62 |
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) |