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) == '\\'
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, {})
21 'accepts string-indexed table or list of pairs as attributes',
23 local attributes_list = pandoc.List:new {{'one', '1'}, {'two', '2'}}
24 local attr_from_list = pandoc.Attr('', {}, attributes_list:clone())
27 pandoc.List:new(attr_from_list.attributes),
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)
39 assoc_list_from_table,
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')
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'})
56 test('adds entries by field name', function ()
57 local attributes = pandoc.Attr('',{}, {{'c', '1'}, {'d', '2'}}).attributes
60 -- checking the full AttributeList would "duplicate" entries
61 setmetatable(attributes, nil),
62 {{'c', '1'}, {'d', '2'}, {'e', '3'}}
65 test('deletes entries by field name', function ()
66 local attributes = pandoc.Attr('',{}, {a = '1', b = '2'}).attributes
68 assert.is_nil(attributes.a)
69 local assoc_list = setmetatable(attributes, nil)
70 assert.are_same(assoc_list, {{'b', '2'}})
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
76 assert.are_same(pandoc.List:new(attributes), assoc_list)
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)
82 for k, v in pairs(attr.attributes) do
83 assert.are_equal(attributes[k], v)
86 assert.are_equal(count, 3)
89 group 'HTML-like attribute tables' {
90 test('in element constructor', function ()
91 local html_attributes = {
93 class = 'class1 class2',
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')
104 test('element attr setter', function ()
105 local html_attributes = {
107 class = 'class1 class2',
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')
119 test('element attrbutes setter', function ()
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')
133 test('clones Attr', function ()
134 local attr = pandoc.Attr('test', {'my-class'}, {foo = 'bar'})
135 local cloned = attr:clone()
139 assert.are_same(cloned.identifier, 'test')
140 assert.are_same(cloned.classes, {'my-class'})
141 assert.are_same(cloned.attributes.foo, 'bar')
143 test('clones ListAttributes', function ()
144 local la = pandoc.ListAttributes(2, pandoc.DefaultStyle, pandoc.Period)
145 local cloned = la:clone()
147 assert.are_same(cloned.start, 2)
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'})
155 test('clones Str', function ()
156 local str = pandoc.Str 'Hello'
157 local cloned = str:clone()
159 assert.are_same(cloned.text, 'Hello')
161 test('clones Citation', function ()
162 local cite = pandoc.Citation('leibniz', pandoc.AuthorInText)
163 local cloned = cite:clone()
165 assert.are_same(cloned.id, 'leibniz')
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+')
175 local pipe_result = pandoc.pipe('tr', {'a', 'b'}, 'abc')
176 assert.are_equal('bbc', pipe_result:match '%a+')
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)
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)
196 test('Markdown', function ()
197 local valid_markdown = '*Hello*, World!\n'
198 local expected = pandoc.Pandoc({
200 pandoc.Emph { pandoc.Str 'Hello' },
206 assert.are_same(expected, pandoc.read(valid_markdown))
208 test('failing read', function ()
209 assert.error_matches(
210 function () pandoc.read('foo', 'nosuchreader') end,
211 'Unknown reader: nosuchreader'
217 test('block walking order', function ()
219 local nested_nums = pandoc.Div {
220 pandoc.Para{pandoc.Str'1'},
222 pandoc.Para{pandoc.Str'2'},
223 pandoc.Para{pandoc.Str'3'}
225 pandoc.Para{pandoc.Str'4'}
229 {Para = function (p) table.insert(acc, p.content[1].text) end}
231 assert.are_equal('1234', table.concat(acc))
235 group 'walk_inline' {
236 test('inline walking order', function ()
238 local nested_nums = pandoc.Span {
248 {Str = function (s) table.insert(acc, s.text) end}
250 assert.are_equal('1234', table.concat(acc))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list