4
4
// TODO find or write a deep-comparison library for Vala!
5
5
6
6
using My ;
7
+ using My.Cmp ;
7
8
8
9
// === Helpers =============================================================
9
10
@@ -60,43 +61,69 @@ private void default_node_checker(GLib.Node<Elem> node)
60
61
}
61
62
62
63
/**
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
64
65
* @param text See is_contents
65
66
* @param is_contents If false, text is a filename with respect to the
66
67
* same directory as this file.
67
68
* If true, text is the contents themselves.
68
69
* @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
70
71
* @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
72
73
*/
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 ,
75
76
76
77
NodeChecker block_checker = default_node_checker,
77
- NodeChecker span_checker = default_node_checker)
78
+ NodeChecker child_checker = default_node_checker)
78
79
{
80
+ diag(GLib . Log . METHOD );
79
81
read_and_test(text, (doc)= > {
80
82
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 );
82
88
assert_true(node0. data. ty == block_type);
83
89
block_checker(node0);
84
90
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
+ }
88
97
}, is_contents);
89
98
}
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
+
90
115
// === General tests =======================================================
91
116
92
117
void test_misc()
93
118
{
119
+ diag(GLib . Log . METHOD );
94
120
var md = new MarkdownMd4cReader ();
95
121
assert_true(! md. meta); // for coverage
96
122
}
97
123
98
124
void test_loadfile()
99
125
{
126
+ diag(GLib . Log . METHOD );
100
127
read_and_test(" basic.md" , (doc)= > {
101
128
assert_true(doc. root. n_children() == 2 );
102
129
@@ -147,14 +174,16 @@ void test_image_bad()
147
174
148
175
void test_header()
149
176
{
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 ,
151
179
(bnode)= > { assert_true(bnode. data. header_level == 1 ); },
152
180
(snode)= > { assert_true(snode. data. text == " H1" ); }
153
181
);
154
182
}
155
183
156
184
void test_quote()
157
185
{
186
+ diag(GLib . Log . METHOD );
158
187
read_and_test(" > Raven" , (doc)= > {
159
188
unowned GLib . Node<Elem > node0 = doc. root. nth_child(0 );
160
189
assert_true(node0. n_children() >= 1 );
@@ -174,114 +203,109 @@ void test_quote()
174
203
175
204
void test_codeblock()
176
205
{
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 ,
178
208
(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" ); }
182
210
);
183
211
}
184
212
185
213
void test_special()
186
214
{
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 ,
188
217
(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" ); }
190
219
);
191
220
}
192
221
193
222
void test_special_nocmd()
194
223
{
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 ,
196
226
(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
+ }
198
245
);
199
246
}
200
247
201
248
// span_plain is tested plenty of places herein
202
249
203
250
void test_italics()
204
251
{
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 ,
206
254
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" ); }
214
256
);
215
257
}
216
258
217
259
void test_bold()
218
260
{
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 ,
220
263
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" ); }
228
265
);
229
266
}
230
267
231
268
void test_inline_code()
232
269
{
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 ,
234
272
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" ); }
242
274
);
243
275
}
244
276
245
277
void test_strike()
246
278
{
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 ,
248
281
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" ); }
256
283
);
257
284
}
258
285
259
286
void test_underline()
260
287
{
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 ,
262
290
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" ); }
270
292
);
271
293
}
272
294
273
295
void test_image()
274
296
{
297
+ diag(GLib . Log . METHOD );
275
298
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 ,
277
300
default_node_checker, scheck);
278
- test_block_span (" " , true , BLOCK_COPY , SPAN_IMAGE ,
301
+ test_block_child (" " , true , BLOCK_COPY , SPAN_IMAGE ,
279
302
default_node_checker, scheck);
280
303
}
281
304
282
305
public static int main (string [] args)
283
306
{
284
307
// run the tests
308
+ My . App . init_before_run();
285
309
Test . init (ref args);
286
310
Test . set_nonfatal_assertions();
287
311
Test . add_func(" /200-md4c-reader/misc" , test_misc);
@@ -294,6 +318,7 @@ public static int main (string[] args)
294
318
Test . add_func(" /200-md4c-reader/codeblock" , test_codeblock);
295
319
Test . add_func(" /200-md4c-reader/special" , test_special);
296
320
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);
297
322
Test . add_func(" /200-md4c-reader/italics" , test_italics);
298
323
Test . add_func(" /200-md4c-reader/bold" , test_bold);
299
324
Test . add_func(" /200-md4c-reader/inline_code" , test_inline_code);
0 commit comments