@@ -0,0 +1,111 @@
1+local tasty = require 'tasty'
2+local List = require 'pandoc.List'
3+
4+local assert = tasty.assert
5+local test = tasty.test_case
6+local group = tasty.test_group
7+
8+return {
9+ group 'new' {
10+ test('make table usable as list', function ()
11+ local test = List:new{1, 1, 2, 3, 5}
12+ assert.are_same(
13+ {1, 1, 4, 9, 25},
14+ test:map(function (x) return x^2 end)
15+ )
16+ end),
17+ test('return empty list if no argument is given', function ()
18+ assert.are_same({}, List:new())
19+ end),
20+ test('metatable of result is pandoc.List', function ()
21+ local test = List:new{5}
22+ assert.are_equal(List, getmetatable(test))
23+ end)
24+ },
25+
26+ group 'includes' {
27+ test('finds elements in list', function ()
28+ local lst = List:new {'one', 'two', 'three'}
29+ assert.is_truthy(lst:includes('one'))
30+ assert.is_truthy(lst:includes('two'))
31+ assert.is_truthy(lst:includes('three'))
32+ assert.is_falsy(lst:includes('four'))
33+ end)
34+ },
35+
36+ group 'clone' {
37+ test('changing the clone does not affect original', function ()
38+ local orig = List:new {23, 42}
39+ local copy = orig:clone()
40+ copy[1] = 5
41+ assert.are_same({23, 42}, orig)
42+ assert.are_same({5, 42}, copy)
43+ end),
44+ test('result is a list', function ()
45+ local orig = List:new {23, 42}
46+ assert.are_equal(List, getmetatable(orig:clone()))
47+ end),
48+ },
49+
50+ group 'find' {
51+ test('returns element and index if found', function ()
52+ local list = List:new {5, 23, 71}
53+ local elem, idx = list:find(71)
54+ assert.are_same(71, elem)
55+ assert.are_same(3, idx)
56+ end),
57+ test('respects start index', function ()
58+ local list = List:new {19, 23, 29, 71}
59+ assert.are_equal(23, list:find(23, 1))
60+ assert.are_equal(23, list:find(23, 2))
61+ assert.is_nil(list:find(23, 3))
62+ end),
63+ test('returns nil if element not found', function ()
64+ assert.is_nil((List:new {18, 20, 22, 0, 24}):find('0'))
65+ end),
66+ },
67+
68+ group 'find_if' {
69+ test('returns element and index if found', function ()
70+ local perm_prime = List:new {2, 3, 5, 7, 11, 13, 17, 31, 37, 71}
71+ local elem, idx = perm_prime:find_if(function (x) return x >= 10 end)
72+ assert.are_same(11, elem)
73+ assert.are_same(5, idx)
74+ end),
75+ test('returns nil if element not found', function ()
76+ local is_null = function (n) return List.includes({23,35,46,59}, n) end
77+ assert.is_nil((List:new {18, 20, 22, 24, 27}):find_if(is_null))
78+ end),
79+ },
80+
81+ group 'extend' {
82+ test('extends list with other list', function ()
83+ local primes = List:new {2, 3, 5, 7}
84+ primes:extend {11, 13, 17}
85+ assert.are_same({2, 3, 5, 7, 11, 13, 17}, primes)
86+ end)
87+ },
88+
89+ group 'map' {
90+ test('applies function to elements', function ()
91+ local primes = List:new {2, 3, 5, 7}
92+ local squares = primes:map(function (x) return x^2 end)
93+ assert.are_same({4, 9, 25, 49}, squares)
94+ end),
95+ test('leaves original list unchanged', function ()
96+ local primes = List:new {2, 3, 5, 7}
97+ local squares = primes:map(function (x) return x^2 end)
98+ assert.are_same({2, 3, 5, 7}, primes)
99+ end)
100+ },
101+
102+ group 'filter' {
103+ test('keep elements for which property is truthy', function ()
104+ local is_small_prime = function (x)
105+ return List.includes({2, 3, 5, 7}, x)
106+ end
107+ local numbers = List:new {4, 7, 2, 9, 5, 11}
108+ assert.are_same({7, 2, 5}, numbers:filter(is_small_prime))
109+ end),
110+ },
111+}