Skip to content

Commit 95ca255

Browse files
committed
t/200-md4c-reader: refactor; add tests of Markdown in special blocks
- Refactor common code into helpers - Print the method name at the beginning of each test to make it easier to read the logs - Add tests of Markdown in special blocks (#23)
1 parent 8813e86 commit 95ca255

File tree

1 file changed

+86
-61
lines changed

1 file changed

+86
-61
lines changed

t/200-md4c-reader-t.vala

+86-61
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// TODO find or write a deep-comparison library for Vala!
55

66
using My;
7+
using My.Cmp;
78

89
// === Helpers =============================================================
910

@@ -60,43 +61,69 @@ private void default_node_checker(GLib.Node<Elem> node)
6061
}
6162

6263
/**
63-
* Parse a string that contains exactly one block, and check its children
64+
* Parse a string that contains exactly one block, and check its first child
6465
* @param text See is_contents
6566
* @param is_contents If false, text is a filename with respect to the
6667
* same directory as this file.
6768
* If true, text is the contents themselves.
6869
* @param block_type What type the block must be
69-
* @param span_type What type the span must be
70+
* @param child_type What type the child (usually a span) must be
7071
* @param block_checker Function to check the block
71-
* @param span_checker Function to check the span
72+
* @param child_checker Function to check the child
7273
*/
73-
void test_block_span(string text, bool is_contents,
74-
Elem.Type block_type, Elem.Type span_type,
74+
void test_block_child(string text, bool is_contents,
75+
Elem.Type block_type, Elem.Type child_type,
7576

7677
NodeChecker block_checker = default_node_checker,
77-
NodeChecker span_checker = default_node_checker)
78+
NodeChecker child_checker = default_node_checker)
7879
{
80+
diag(GLib.Log.METHOD);
7981
read_and_test(text, (doc)=> {
8082
unowned GLib.Node<Elem> node0 = doc.root.nth_child(0);
81-
assert_true(node0.n_children() >= 1);
83+
assert_nonnull(node0);
84+
if(node0 == null) {
85+
return; // can't test anything else
86+
}
87+
assert_cmpuint(node0.n_children(), GE, 1);
8288
assert_true(node0.data.ty == block_type);
8389
block_checker(node0);
8490
unowned GLib.Node<Elem> node1 = node0.nth_child(0);
85-
assert_true(node1.n_children() < 2);
86-
assert_true(node1.data.ty == span_type);
87-
span_checker(node1);
91+
assert_nonnull(node1);
92+
if(node1 != null) {
93+
assert_cmpuint(node1.n_children(), LT, 2);
94+
assert_true(node1.data.ty == child_type);
95+
child_checker(node1);
96+
}
8897
}, is_contents);
8998
}
99+
100+
/**
101+
* Check for a single child span.
102+
*
103+
* For use in {NodeChecker} functions.
104+
*/
105+
void assert_has_one_span_child(GLib.Node<Elem> node, Elem.Type span_type,
106+
string span_text)
107+
{
108+
assert_cmpuint(node.n_children(), EQ, 1);
109+
unowned GLib.Node<Elem> snode = node.nth_child(0);
110+
assert_true(snode.data.ty == span_type);
111+
assert_true(snode.data.text == span_text);
112+
assert_cmpuint(snode.n_children(), EQ, 0);
113+
}
114+
90115
// === General tests =======================================================
91116

92117
void test_misc()
93118
{
119+
diag(GLib.Log.METHOD);
94120
var md = new MarkdownMd4cReader();
95121
assert_true(!md.meta); // for coverage
96122
}
97123

98124
void test_loadfile()
99125
{
126+
diag(GLib.Log.METHOD);
100127
read_and_test("basic.md", (doc)=> {
101128
assert_true(doc.root.n_children() == 2);
102129

@@ -147,14 +174,16 @@ void test_image_bad()
147174

148175
void test_header()
149176
{
150-
test_block_span("# H1", true, BLOCK_HEADER, SPAN_PLAIN,
177+
diag(GLib.Log.METHOD);
178+
test_block_child("# H1", true, BLOCK_HEADER, SPAN_PLAIN,
151179
(bnode)=> { assert_true(bnode.data.header_level == 1); },
152180
(snode)=> { assert_true(snode.data.text == "H1"); }
153181
);
154182
}
155183

156184
void test_quote()
157185
{
186+
diag(GLib.Log.METHOD);
158187
read_and_test("> Raven", (doc)=> {
159188
unowned GLib.Node<Elem> node0 = doc.root.nth_child(0);
160189
assert_true(node0.n_children() >= 1);
@@ -174,114 +203,109 @@ void test_quote()
174203

175204
void test_codeblock()
176205
{
177-
test_block_span("200-codeblock.md", false, BLOCK_CODE, SPAN_PLAIN,
206+
diag(GLib.Log.METHOD);
207+
test_block_child("200-codeblock.md", false, BLOCK_CODE, SPAN_PLAIN,
178208
(bnode)=> { assert_true(bnode.data.info_string == ""); },
179-
(snode)=> {
180-
assert_true(substr(snode.data.text, 0, "Line 1".length) == "Line 1");
181-
}
209+
(snode)=> { assert_true(substr(snode.data.text, 0, "Line 1".length) == "Line 1"); }
182210
);
183211
}
184212

185213
void test_special()
186214
{
187-
test_block_span("200-special.md", false, BLOCK_SPECIAL, SPAN_PLAIN,
215+
diag(GLib.Log.METHOD);
216+
test_block_child("200-special.md", false, BLOCK_SPECIAL, BLOCK_COPY,
188217
(bnode)=> { assert_true(bnode.data.info_string == "specialblock"); },
189-
(snode)=> { assert_true(snode.data.text == "Hello"); }
218+
(cnode)=> { assert_has_one_span_child(cnode, SPAN_PLAIN, "Hello"); }
190219
);
191220
}
192221

193222
void test_special_nocmd()
194223
{
195-
test_block_span("200-special-nocmd.md", false, BLOCK_SPECIAL, SPAN_PLAIN,
224+
diag(GLib.Log.METHOD);
225+
test_block_child("200-special-nocmd.md", false, BLOCK_SPECIAL, BLOCK_COPY,
196226
(bnode)=> { assert_true(bnode.data.info_string == ""); },
197-
(snode)=> { assert_true(snode.data.text == "No command"); }
227+
(cnode)=> {
228+
assert_has_one_span_child(cnode, SPAN_PLAIN, "No command");
229+
}
230+
);
231+
}
232+
233+
void test_special_with_formatting()
234+
{
235+
diag(GLib.Log.METHOD);
236+
test_block_child("```pfft:specialblock\n**Formatted**\n```", true,
237+
BLOCK_SPECIAL, BLOCK_COPY,
238+
(bnode)=> { assert_true(bnode.data.info_string == "specialblock"); },
239+
(copy_node)=> {
240+
assert_true(copy_node.n_children() == 1);
241+
unowned GLib.Node<Elem> snode = copy_node.nth_child(0);
242+
assert_true(snode.data.ty == SPAN_STRONG);
243+
assert_has_one_span_child(snode, SPAN_PLAIN, "Formatted");
244+
}
198245
);
199246
}
200247

201248
// span_plain is tested plenty of places herein
202249

203250
void test_italics()
204251
{
205-
test_block_span("*Italics*", true, BLOCK_COPY, SPAN_EM,
252+
diag(GLib.Log.METHOD);
253+
test_block_child("*Italics*", true, BLOCK_COPY, SPAN_EM,
206254
default_node_checker,
207-
(snode)=>{
208-
assert_true(snode.n_children() == 1);
209-
unowned GLib.Node<Elem> node1 = snode.nth_child(0);
210-
assert_true(node1.n_children() == 0);
211-
assert_true(node1.data.ty == SPAN_PLAIN);
212-
assert_true(node1.data.text == "Italics");
213-
}
255+
(cnode)=>{ assert_has_one_span_child(cnode, SPAN_PLAIN, "Italics"); }
214256
);
215257
}
216258

217259
void test_bold()
218260
{
219-
test_block_span("**Bold**", true, BLOCK_COPY, SPAN_STRONG,
261+
diag(GLib.Log.METHOD);
262+
test_block_child("**Bold**", true, BLOCK_COPY, SPAN_STRONG,
220263
default_node_checker,
221-
(snode)=>{
222-
assert_true(snode.n_children() == 1);
223-
unowned GLib.Node<Elem> node1 = snode.nth_child(0);
224-
assert_true(node1.n_children() == 0);
225-
assert_true(node1.data.ty == SPAN_PLAIN);
226-
assert_true(node1.data.text == "Bold");
227-
}
264+
(cnode)=>{ assert_has_one_span_child(cnode, SPAN_PLAIN, "Bold"); }
228265
);
229266
}
230267

231268
void test_inline_code()
232269
{
233-
test_block_span("`31337`", true, BLOCK_COPY, SPAN_CODE,
270+
diag(GLib.Log.METHOD);
271+
test_block_child("`31337`", true, BLOCK_COPY, SPAN_CODE,
234272
default_node_checker,
235-
(snode)=>{
236-
assert_true(snode.n_children() == 1);
237-
unowned GLib.Node<Elem> node1 = snode.nth_child(0);
238-
assert_true(node1.n_children() == 0);
239-
assert_true(node1.data.ty == SPAN_PLAIN);
240-
assert_true(node1.data.text == "31337");
241-
}
273+
(cnode)=>{ assert_has_one_span_child(cnode, SPAN_PLAIN, "31337"); }
242274
);
243275
}
244276

245277
void test_strike()
246278
{
247-
test_block_span("~not really~", true, BLOCK_COPY, SPAN_STRIKE,
279+
diag(GLib.Log.METHOD);
280+
test_block_child("~not really~", true, BLOCK_COPY, SPAN_STRIKE,
248281
default_node_checker,
249-
(snode)=>{
250-
assert_true(snode.n_children() == 1);
251-
unowned GLib.Node<Elem> node1 = snode.nth_child(0);
252-
assert_true(node1.n_children() == 0);
253-
assert_true(node1.data.ty == SPAN_PLAIN);
254-
assert_true(node1.data.text == "not really");
255-
}
282+
(cnode)=>{ assert_has_one_span_child(cnode, SPAN_PLAIN, "not really"); }
256283
);
257284
}
258285

259286
void test_underline()
260287
{
261-
test_block_span("_NZ_", true, BLOCK_COPY, SPAN_UNDERLINE,
288+
diag(GLib.Log.METHOD);
289+
test_block_child("_NZ_", true, BLOCK_COPY, SPAN_UNDERLINE,
262290
default_node_checker,
263-
(snode)=>{
264-
assert_true(snode.n_children() == 1);
265-
unowned GLib.Node<Elem> node1 = snode.nth_child(0);
266-
assert_true(node1.n_children() == 0);
267-
assert_true(node1.data.ty == SPAN_PLAIN);
268-
assert_true(node1.data.text == "NZ");
269-
}
291+
(cnode)=>{ assert_has_one_span_child(cnode, SPAN_PLAIN, "NZ"); }
270292
);
271293
}
272294

273295
void test_image()
274296
{
297+
diag(GLib.Log.METHOD);
275298
NodeChecker scheck = (snode)=>{assert_true(snode.data.href == "image.png");};
276-
test_block_span("200-image.md", false, BLOCK_COPY, SPAN_IMAGE,
299+
test_block_child("200-image.md", false, BLOCK_COPY, SPAN_IMAGE,
277300
default_node_checker, scheck);
278-
test_block_span("![](image.png)", true, BLOCK_COPY, SPAN_IMAGE,
301+
test_block_child("![](image.png)", true, BLOCK_COPY, SPAN_IMAGE,
279302
default_node_checker, scheck);
280303
}
281304

282305
public static int main (string[] args)
283306
{
284307
// run the tests
308+
My.App.init_before_run();
285309
Test.init (ref args);
286310
Test.set_nonfatal_assertions();
287311
Test.add_func("/200-md4c-reader/misc", test_misc);
@@ -294,6 +318,7 @@ public static int main (string[] args)
294318
Test.add_func("/200-md4c-reader/codeblock", test_codeblock);
295319
Test.add_func("/200-md4c-reader/special", test_special);
296320
Test.add_func("/200-md4c-reader/special_nocmd", test_special_nocmd);
321+
Test.add_func("/200-md4c-reader/special_with_formatting", test_special_with_formatting);
297322
Test.add_func("/200-md4c-reader/italics", test_italics);
298323
Test.add_func("/200-md4c-reader/bold", test_bold);
299324
Test.add_func("/200-md4c-reader/inline_code", test_inline_code);

0 commit comments

Comments
 (0)