@@ -0,0 +1,111 @@
1+local tasty = require 'tasty'
2+local List = require 'pandoc.List'
4+local assert = tasty.assert
5+local test = tasty.test_case
6+local group = tasty.test_group
10+ test('make table usable as list', function ()
11+ local test = List:new{1, 1, 2, 3, 5}
14+ test:map(function (x) return x^2 end)
17+ test('return empty list if no argument is given', function ()
18+ assert.are_same({}, List:new())
20+ test('metatable of result is pandoc.List', function ()
21+ local test = List:new{5}
22+ assert.are_equal(List, getmetatable(test))
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'))
37+ test('changing the clone does not affect original', function ()
38+ local orig = List:new {23, 42}
39+ local copy = orig:clone()
41+ assert.are_same({23, 42}, orig)
42+ assert.are_same({5, 42}, copy)
44+ test('result is a list', function ()
45+ local orig = List:new {23, 42}
46+ assert.are_equal(List, getmetatable(orig:clone()))
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)
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))
63+ test('returns nil if element not found', function ()
64+ assert.is_nil((List:new {18, 20, 22, 0, 24}):find('0'))
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)
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))
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)
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)
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)
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)
107+ local numbers = List:new {4, 7, 2, 9, 5, 11}
108+ assert.are_same({7, 2, 5}, numbers:filter(is_small_prime))