From 91e85f9809ed406d2120e424aaa2f249ee18e2c2 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 1 Jan 2024 23:07:21 +0100 Subject: Implemented first and easy part of reading package lists --- spec/list_spec.lua | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 spec/list_spec.lua (limited to 'spec') 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) -- cgit v1.1