git.iko.soy/typeable/pandoc
on 465eeec help sign in
174 lines 6.1 kB history raw
1local tasty = require 'tasty'
3local test = tasty.test_case
4local group = tasty.test_group
5local assert = tasty.assert
7function os_is_windows ()
8 return package.config:sub(1,1) == '\\'
9end
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_list = pandoc.List:new {{'one', '1'}, {'two', '2'}}
24 local attr_from_list = pandoc.Attr('', {}, attributes_list:clone())
26 assert.are_same(
27 pandoc.List:new(attr_from_list.attributes),
28 attributes_list
29 )
31 local attributes_table = {one = '1', two = '2'}
32 local attr_from_table = pandoc.Attr('', {}, attributes_table)
34 local assoc_list_from_table =
35 pandoc.List:new(attr_from_table.attributes)
36 -- won't work in general, but does in this special case
37 table.sort(assoc_list_from_table, function(x, y) return x[1]<y[1] end)
38 assert.are_same(
39 assoc_list_from_table,
40 attributes_list
41 )
42 end
43 )
44 },
45 group 'AttributeList' {
46 test('allows access via fields', function ()
47 local attributes = pandoc.Attr('', {}, {{'a', '1'}, {'b', '2'}}).attributes
48 assert.are_equal(attributes.a, '1')
49 assert.are_equal(attributes.b, '2')
50 end),
51 test('allows access to pairs via numerical indexing', function ()
52 local attributes = pandoc.Attr('', {}, {{'a', '1'}, {'b', '2'}}).attributes
53 assert.are_same(attributes[1], {'a', '1'})
54 assert.are_same(attributes[2], {'b', '2'})
55 end),
56 test('adds entries by field name', function ()
57 local attributes = pandoc.Attr('',{}, {{'c', '1'}, {'d', '2'}}).attributes
58 attributes.e = '3'
59 assert.are_same(
60 -- checking the full AttributeList would "duplicate" entries
61 setmetatable(attributes, nil),
62 {{'c', '1'}, {'d', '2'}, {'e', '3'}}
63 )
64 end),
65 test('deletes entries by field name', function ()
66 local attributes = pandoc.Attr('',{}, {a = '1', b = '2'}).attributes
67 attributes.a = nil
68 assert.is_nil(attributes.a)
69 local assoc_list = setmetatable(attributes, nil)
70 assert.are_same(assoc_list, {{'b', '2'}})
71 end),
72 test('remains unchanged if deleted key did not exist', function ()
73 local assoc_list = pandoc.List:new {{'alpha', 'x'}, {'beta', 'y'}}
74 local attributes = pandoc.Attr('', {}, assoc_list:clone()).attributes
75 attributes.a = nil
76 assert.are_same(pandoc.List:new(attributes), assoc_list)
77 end),
78 test('gives key-value pairs when iterated-over', function ()
79 local attributes = {width = '11', height = '22', name = 'test'}
80 local attr = pandoc.Attr('', {}, attributes)
81 local count = 0
82 for k, v in pairs(attr.attributes) do
83 assert.are_equal(attributes[k], v)
84 count = count + 1
85 end
86 assert.are_equal(count, 3)
87 end)
88 },
89 },
91 group 'clone' {
92 test('clones Attr', function ()
93 local attr = pandoc.Attr('test', {'my-class'}, {foo = 'bar'})
94 local cloned = attr:clone()
95 attr.identifier = ''
96 attr.classes = {}
97 attr.attributes = {}
98 assert.are_same(cloned.identifier, 'test')
99 assert.are_same(cloned.classes, {'my-class'})
100 assert.are_same(cloned.attributes.foo, 'bar')
101 end),
102 test('clones ListAttributes', function ()
103 local la = pandoc.ListAttributes(2, pandoc.DefaultStyle, pandoc.Period)
104 local cloned = la:clone()
105 la.start = 9
106 assert.are_same(cloned.start, 2)
107 end),
108 test('clones Para', function ()
109 local para = pandoc.Para {pandoc.Str 'Hello'}
110 local cloned = para:clone()
111 para.content[1].text = 'bye'
112 assert.are_same(cloned, pandoc.Para {pandoc.Str 'Hello'})
113 end),
114 test('clones Str', function ()
115 local str = pandoc.Str 'Hello'
116 local cloned = str:clone()
117 str.text = 'bye'
118 assert.are_same(cloned.text, 'Hello')
119 end),
120 test('clones Citation', function ()
121 local cite = pandoc.Citation('leibniz', pandoc.AuthorInText)
122 local cloned = cite:clone()
123 cite.id = 'newton'
124 assert.are_same(cloned.id, 'leibniz')
125 end),
126 },
128 group 'pipe' {
129 test('external string processing', function ()
130 if os_is_windows() then
131 local pipe_result = pandoc.pipe('find', {'hi'}, 'hi')
132 assert.are_equal('hi', pipe_result:match '%a+')
133 else
134 local pipe_result = pandoc.pipe('tr', {'a', 'b'}, 'abc')
135 assert.are_equal('bbc', pipe_result:match '%a+')
136 end
137 end),
138 test('failing pipe', function ()
139 if os_is_windows() then
140 local success, err = pcall(pandoc.pipe, 'find', {'/a'}, 'hi')
141 assert.is_falsy(success)
142 assert.are_equal('find', err.command)
143 assert.is_truthy(err.error_code ~= 0)
144 else
145 local success, err = pcall(pandoc.pipe, 'false', {}, 'abc')
146 assert.is_falsy(success)
147 assert.are_equal('false', err.command)
148 assert.are_equal(1, err.error_code)
149 assert.are_equal('', err.output)
150 end
151 end)
152 },
154 group 'read' {
155 test('Markdown', function ()
156 local valid_markdown = '*Hello*, World!\n'
157 local expected = pandoc.Pandoc({
158 pandoc.Para {
159 pandoc.Emph { pandoc.Str 'Hello' },
160 pandoc.Str ',',
161 pandoc.Space(),
162 pandoc.Str 'World!'
163 }
164 })
165 assert.are_same(expected, pandoc.read(valid_markdown))
166 end),
167 test('failing read', function ()
168 assert.error_matches(
169 function () pandoc.read('foo', 'nosuchreader') end,
170 'Unknown reader: nosuchreader'
171 )
172 end)
173 },