-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathlinked_list_spec.lua
72 lines (55 loc) · 2.24 KB
/
linked_list_spec.lua
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
63
64
65
66
67
68
69
70
71
72
local linked_list = require 'apicast.linked_list'
describe('linked_list', function()
describe('readonly', function()
it('returns a list that finds elements in the correct order', function()
local list = linked_list.readonly(
{ a = 1 }, linked_list.readonly({ b = 2 }, { c = 3, a = 10 }))
assert.equals(1, list.a) -- appears twice; returns the first
assert.equals(2, list.b)
assert.equals(3, list.c)
end)
it('returns a list that cannot be modified', function()
local list = linked_list.readonly({ a = 1 })
assert.has_error(function() list.abc = 123 end, 'readonly list')
assert.is_nil(list.abc)
end)
it('returns a list that has pointers to current and next elements', function()
local list = linked_list.readonly(
{ a = 1 }, linked_list.readonly({ b = 2 }, { c = 3 }))
assert.same({ a = 1 }, list.current)
assert.same({ b = 2 }, list.next.current)
assert.same({ c = 3 }, list.next.next)
end)
it('takes false over nil', function()
local list = linked_list.readonly({ a = false }, { a = 'value' })
assert.is_false(list.a)
end)
end)
describe('readwrite', function()
it('returns a list that finds elements in the correct order', function()
local list = linked_list.readwrite(
{ a = 1 }, linked_list.readwrite({ b = 2 }, { c = 3, a = 10 }))
assert.equals(1, list.a) -- appears twice; returns the first
assert.equals(2, list.b)
assert.equals(3, list.c)
end)
it('returns a list that can be modified', function()
local list = linked_list.readwrite({ a = 1 })
list.abc = 123
assert.equals(123, list.abc)
assert.same({ a = 1, abc = 123 }, list.current)
end)
it('returns a list that has pointers to current and next elements', function()
local list = linked_list.readwrite(
{ a = 1 }, linked_list.readwrite({ b = 2 }, { c = 3 }))
assert.same({ a = 1 }, list.current)
assert.same({ b = 2 }, list.next.current)
assert.same({ c = 3 }, list.next.next)
end)
it('can override values with false', function()
local list = linked_list.readwrite({ }, { a = 'value' })
list.a = false
assert.is_false(list.a)
end)
end)
end)