git.iko.soy/typeable/pandoc
on 467ea4e help sign in
253 lines 8.5 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 group 'HTML-like attribute tables' {
90 test('in element constructor', function ()
91 local html_attributes = {
92 id = 'the-id',
93 class = 'class1 class2',
94 width = '11',
95 height = '12'
96 }
97 local attr = pandoc.Span('test', html_attributes).attr
98 assert.are_equal(attr.identifier, 'the-id')
99 assert.are_equal(attr.classes[1], 'class1')
100 assert.are_equal(attr.classes[2], 'class2')
101 assert.are_equal(attr.attributes.width, '11')
102 assert.are_equal(attr.attributes.height, '12')
103 end),
104 test('element attr setter', function ()
105 local html_attributes = {
106 id = 'the-id',
107 class = 'class1 class2',
108 width = "11",
109 height = "12"
110 }
111 local span = pandoc.Span 'test'
112 span.attr = html_attributes
113 assert.are_equal(span.attr.identifier, 'the-id')
114 assert.are_equal(span.attr.classes[1], 'class1')
115 assert.are_equal(span.attr.classes[2], 'class2')
116 assert.are_equal(span.attr.attributes.width, '11')
117 assert.are_equal(span.attr.attributes.height, '12')
118 end),
119 test('element attrbutes setter', function ()
120 local attributes = {
121 width = "11",
122 height = "12"
123 }
124 local span = pandoc.Span 'test'
125 span.attributes = attributes
126 assert.are_equal(span.attr.attributes.width, '11')
127 assert.are_equal(span.attr.attributes.height, '12')
128 end)
129 }
130 },
132 group 'clone' {
133 test('clones Attr', function ()
134 local attr = pandoc.Attr('test', {'my-class'}, {foo = 'bar'})
135 local cloned = attr:clone()
136 attr.identifier = ''
137 attr.classes = {}
138 attr.attributes = {}
139 assert.are_same(cloned.identifier, 'test')
140 assert.are_same(cloned.classes, {'my-class'})
141 assert.are_same(cloned.attributes.foo, 'bar')
142 end),
143 test('clones ListAttributes', function ()
144 local la = pandoc.ListAttributes(2, pandoc.DefaultStyle, pandoc.Period)
145 local cloned = la:clone()
146 la.start = 9
147 assert.are_same(cloned.start, 2)
148 end),
149 test('clones Para', function ()
150 local para = pandoc.Para {pandoc.Str 'Hello'}
151 local cloned = para:clone()
152 para.content[1].text = 'bye'
153 assert.are_same(cloned, pandoc.Para {pandoc.Str 'Hello'})
154 end),
155 test('clones Str', function ()
156 local str = pandoc.Str 'Hello'
157 local cloned = str:clone()
158 str.text = 'bye'
159 assert.are_same(cloned.text, 'Hello')
160 end),
161 test('clones Citation', function ()
162 local cite = pandoc.Citation('leibniz', pandoc.AuthorInText)
163 local cloned = cite:clone()
164 cite.id = 'newton'
165 assert.are_same(cloned.id, 'leibniz')
166 end),
167 },
169 group 'pipe' {
170 test('external string processing', function ()
171 if os_is_windows() then
172 local pipe_result = pandoc.pipe('find', {'hi'}, 'hi')
173 assert.are_equal('hi', pipe_result:match '%a+')
174 else
175 local pipe_result = pandoc.pipe('tr', {'a', 'b'}, 'abc')
176 assert.are_equal('bbc', pipe_result:match '%a+')
177 end
178 end),
179 test('failing pipe', function ()
180 if os_is_windows() then
181 local success, err = pcall(pandoc.pipe, 'find', {'/a'}, 'hi')
182 assert.is_falsy(success)
183 assert.are_equal('find', err.command)
184 assert.is_truthy(err.error_code ~= 0)
185 else
186 local success, err = pcall(pandoc.pipe, 'false', {}, 'abc')
187 assert.is_falsy(success)
188 assert.are_equal('false', err.command)
189 assert.are_equal(1, err.error_code)
190 assert.are_equal('', err.output)
191 end
192 end)
193 },
195 group 'read' {
196 test('Markdown', function ()
197 local valid_markdown = '*Hello*, World!\n'
198 local expected = pandoc.Pandoc({
199 pandoc.Para {
200 pandoc.Emph { pandoc.Str 'Hello' },
201 pandoc.Str ',',
202 pandoc.Space(),
203 pandoc.Str 'World!'
204 }
205 })
206 assert.are_same(expected, pandoc.read(valid_markdown))
207 end),
208 test('failing read', function ()
209 assert.error_matches(
210 function () pandoc.read('foo', 'nosuchreader') end,
211 'Unknown reader: nosuchreader'
212 )
213 end)
214 },
216 group 'walk_block' {
217 test('block walking order', function ()
218 local acc = {}
219 local nested_nums = pandoc.Div {
220 pandoc.Para{pandoc.Str'1'},
221 pandoc.Div{
222 pandoc.Para{pandoc.Str'2'},
223 pandoc.Para{pandoc.Str'3'}
224 },
225 pandoc.Para{pandoc.Str'4'}
226 }
227 pandoc.walk_block(
228 nested_nums,
229 {Para = function (p) table.insert(acc, p.content[1].text) end}
230 )
231 assert.are_equal('1234', table.concat(acc))
232 end)
233 },
235 group 'walk_inline' {
236 test('inline walking order', function ()
237 local acc = {}
238 local nested_nums = pandoc.Span {
239 pandoc.Str'1',
240 pandoc.Emph {
241 pandoc.Str'2',
242 pandoc.Str'3'
243 },
244 pandoc.Str'4'
245 }
246 pandoc.walk_inline(
247 nested_nums,
248 {Str = function (s) table.insert(acc, s.text) end}
249 )
250 assert.are_equal('1234', table.concat(acc))
251 end)
252 }