git.iko.soy/typeable/pandoc
on master help sign in
Lua pandoc module: better tests for Attr and AttributeList
03b9f3dAlbert Krewinkel
test/lua/module/pandoc.lua+65
@@ -9,6 +9,71 @@ function os_is_windows ()
9end
9end
10
10
11return {
11return {
12+ group 'Attr' {
13+ group 'Constructor' {
14+ test('returns null-Attr if no arguments are given', function ()
15+ local attr = pandoc.Attr()
16+ assert.are_equal(attr.identifier, '')
17+ assert.are_same(attr.classes, {})
18+ assert.are_same(attr.attributes, {})
19+ end),
20+ test(
21+ 'accepts string-indexed table or list of pairs as attributes',
22+ function ()
23+ local attributes_table = {one = '1', two = '2'}
24+ local attributes_list = pandoc.List:new {{'one', '1'}, {'two', '2'}}
25+ local attr_from_table = pandoc.Attr('', {}, attributes_table)
26+ local attr_from_list = pandoc.Attr('', {}, attributes_list:clone())
27+ assert.are_same(
28+ pandoc.List:new(attr_from_table.attributes),
29+ attributes_list
30+ )
31+ assert.are_same(
32+ pandoc.List:new(attr_from_list.attributes),
33+ attributes_list
34+ )
35+ end
36+ )
37+ },
38+ group 'AttributeList' {
39+ test('allows access via fields', function ()
40+ local attributes = pandoc.Attr('', {}, {{'a', '1'}, {'b', '2'}}).attributes
41+ assert.are_equal(attributes.a, '1')
42+ assert.are_equal(attributes.b, '2')
43+ end),
44+ test('allows access to pairs via numerical indexing', function ()
45+ local attributes = pandoc.Attr('', {}, {{'a', '1'}, {'b', '2'}}).attributes
46+ assert.are_same(attributes[1], {'a', '1'})
47+ assert.are_same(attributes[2], {'b', '2'})
48+ end),
49+ test('adds entries by field name', function ()
50+ local attributes = pandoc.Attr('',{}, {{'c', '1'}, {'d', '2'}}).attributes
51+ attributes.e = '3'
52+ assert.are_same(
53+ -- checking the full AttributeList would "duplicate" entries
54+ setmetatable(attributes, nil),
55+ {{'c', '1'}, {'d', '2'}, {'e', '3'}}
56+ )
57+ end),
58+ test('deletes entries by field name', function ()
59+ local attributes = pandoc.Attr('',{}, {a = '1', b = '2'}).attributes
60+ attributes.a = nil
61+ assert.is_nil(attributes.a)
62+ local assoc_list = setmetatable(attributes, nil)
63+ assert.are_same(assoc_list, {{'b', '2'}})
64+ end),
65+ test('gives key-value pairs when iterated-over', function ()
66+ local attributes = {width = '11', height = '22', name = 'test'}
67+ local attr = pandoc.Attr('', {}, attributes)
68+ local count = 0
69+ for k, v in pairs(attr.attributes) do
70+ assert.are_equal(attributes[k], v)
71+ count = count + 1
72+ end
73+ assert.are_equal(count, 3)
74+ end)
75+ },
76+ },
12 group 'pipe' {
77 group 'pipe' {
13 test('external string processing', function ()
78 test('external string processing', function ()
14 if os_is_windows() then
79 if os_is_windows() then