git.iko.soy/typeable/pandoc
on 31b650b help sign in
typeable/pandoc/test/lua/module/pandoc-utils.lua
159 lines 4.8 kB history raw
1local tasty = require 'tasty'
2local utils = require 'pandoc.utils'
4local assert = tasty.assert
5local test = tasty.test_case
6local group = tasty.test_group
8return {
9 group 'blocks_to_inlines' {
10 test('default separator', function ()
11 local blocks = {
12 pandoc.Para { pandoc.Str 'Paragraph1' },
13 pandoc.Para { pandoc.Emph { pandoc.Str 'Paragraph2' } }
14 }
15 local expected = {
16 pandoc.Str 'Paragraph1',
17 pandoc.Space(), pandoc.Str '¶', pandoc.Space(),
18 pandoc.Emph { pandoc.Str 'Paragraph2' }
19 }
20 assert.are_same(
21 expected,
22 utils.blocks_to_inlines(blocks)
23 )
24 end),
25 test('custom separator', function ()
26 local blocks = {
27 pandoc.Para{ pandoc.Str 'Paragraph1' },
28 pandoc.Para{ pandoc.Emph 'Paragraph2' }
29 }
30 local expected = {
31 pandoc.Str 'Paragraph1',
32 pandoc.LineBreak(),
33 pandoc.Emph { pandoc.Str 'Paragraph2' }
34 }
35 assert.are_same(
36 expected,
37 utils.blocks_to_inlines(blocks, { pandoc.LineBreak() })
38 )
39 end)
40 },
42 group 'make_sections' {
43 test('sanity check', function ()
44 local blks = {
45 pandoc.Header(1, {pandoc.Str 'First'}),
46 pandoc.Header(2, {pandoc.Str 'Second'}),
47 pandoc.Header(2, {pandoc.Str 'Third'}),
48 }
49 local hblks = utils.make_sections(true, 1, blks)
50 assert.are_equal('Div', hblks[1].t)
51 assert.are_equal('Header', hblks[1].content[1].t)
52 assert.are_equal('1', hblks[1].content[1].attributes['number'])
53 end)
54 },
56 group 'normalize_date' {
57 test('09 Nov 1989', function ()
58 assert.are_equal('1989-11-09', utils.normalize_date '09 Nov 1989')
59 end),
60 test('12/31/2017', function ()
61 assert.are_equal('2017-12-31', utils.normalize_date '12/31/2017')
62 end),
63 },
65 group 'sha1' {
66 test('hashing', function ()
67 local ref_hash = '0a0a9f2a6772942557ab5355d76af442f8f65e01'
68 assert.are_equal(ref_hash, utils.sha1 'Hello, World!')
69 end)
70 },
72 group 'stringify' {
73 test('inlines', function ()
74 local inline = pandoc.Emph{
75 pandoc.Str 'Cogito',
76 pandoc.Space(),
77 pandoc.Str 'ergo',
78 pandoc.Space(),
79 pandoc.Str 'sum.',
80 }
81 assert.are_equal('Cogito ergo sum.', utils.stringify(inline))
82 end)
83 },
85 group 'to_roman_numeral' {
86 test('convertes number', function ()
87 assert.are_equal('MDCCCLXXXVIII', utils.to_roman_numeral(1888))
88 end),
89 test('fails on non-convertible argument', function ()
90 assert.is_falsy(pcall(utils.to_roman_numeral, 'not a number'))
91 end)
92 },
94 group 'to_simple_table' {
95 test('convertes Table', function ()
96 function simple_cell (blocks)
97 return {
98 attr = pandoc.Attr(),
99 alignment = "AlignDefault",
100 contents = blocks,
101 col_span = 1,
102 row_span = 1,
103 }
104 end
105 local tbl = pandoc.Table(
106 {long = {pandoc.Plain {
107 pandoc.Str "the", pandoc.Space(), pandoc.Str "caption"}}},
108 {{pandoc.AlignDefault, nil}},
109 {pandoc.Attr(), {{pandoc.Attr(), {simple_cell{pandoc.Plain "head1"}}}}},
110 {{
111 attr = pandoc.Attr(),
112 body = {{pandoc.Attr(), {simple_cell{pandoc.Plain "cell1"}}}},
113 head = {},
114 row_head_columns = 0
115 }},
116 {pandoc.Attr(), {}},
117 pandoc.Attr()
118 )
119 local stbl = utils.to_simple_table(tbl)
120 assert.are_equal('SimpleTable', stbl.t)
121 assert.are_equal('head1', utils.stringify(stbl.headers[1]))
122 assert.are_equal('cell1', utils.stringify(stbl.rows[1][1]))
123 assert.are_equal('the caption', utils.stringify(pandoc.Span(stbl.caption)))
124 end),
125 test('fails on para', function ()
126 assert.is_falsy(pcall(utils.to_simple_table, pandoc.Para "nope"))
127 end),
128 },
129 group 'from_simple_table' {
130 test('converts SimpleTable to Table', function ()
131 local caption = {pandoc.Str "Overview"}
132 local aligns = {pandoc.AlignDefault, pandoc.AlignDefault}
133 local widths = {0, 0} -- let pandoc determine col widths
134 local headers = {
135 {pandoc.Plain "Language"},
136 {pandoc.Plain "Typing"}
137 }
138 local rows = {
139 {{pandoc.Plain "Haskell"}, {pandoc.Plain "static"}},
140 {{pandoc.Plain "Lua"}, {pandoc.Plain "Dynamic"}},
141 }
142 local simple_table = pandoc.SimpleTable(
143 caption,
144 aligns,
145 widths,
146 headers,
147 rows
148 )
149 local tbl = utils.from_simple_table(simple_table)
150 assert.are_equal("Table", tbl.t)
151 assert.are_same(
152 {pandoc.Plain(caption)},
153 tbl.caption.long
154 )
155 -- reversible
156 assert.are_same(simple_table, utils.to_simple_table(tbl))
157 end),
158 }