summaryrefslogtreecommitdiff
path: root/spec/list_spec.lua
blob: d7b588982af5b7d60ecf6a4cc848dcc58a12d62b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)