Skip to content

Commit c642888

Browse files
authored
Merge pull request #585 from biojppm/fix/test_suite
2 parents 119b604 + 52c28c2 commit c642888

32 files changed

+2178
-1443
lines changed

changelog/current.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
- Ensure parse errors for invalid YAML cases:
2+
- [PR#585](https://github.com/biojppm/rapidyaml/pull/585): colon on newline at top level ([play](https://play.yaml.com/?show=xd#c2NhbGFyCjogYmFkCi0tLQpbc2VxXQo6IGJhZAotLS0KW21hcF0KOiBiYWQK)):
3+
```yaml
4+
scalar
5+
: bad
6+
---
7+
[seq]
8+
: bad
9+
---
10+
{map: }
11+
: bad
12+
```
13+
- [PR#586](https://github.com/biojppm/rapidyaml/pull/586): tokens after explicit document endcolon on newline:
14+
```yaml
15+
bad cases:
16+
scalar
17+
: bad
18+
[seq]
19+
: bad
20+
{map: }
21+
: bad
22+
```
23+
- [PR#586](https://github.com/biojppm/rapidyaml/pull/586): tokens after explicit document end:
24+
```yaml
25+
foo: bar
26+
... bad tokens
27+
```

src/c4/yml/event_handler_tree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
115115
{
116116
_RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree != nullptr);
117117
if(m_num_directives && !m_tree->is_stream(m_tree->root_id()))
118-
_RYML_ERR_BASIC_(m_stack.m_callbacks, "directives cannot be used without a document");
118+
_RYML_ERR_PARSE_(m_stack.m_callbacks, m_curr->pos, "directives cannot be used without a document");
119119
this->_stack_finish_parse();
120120
/* This pointer is temporary. Remember that:
121121
*

src/c4/yml/parse_engine.def.hpp

Lines changed: 716 additions & 660 deletions
Large diffs are not rendered by default.

src/c4/yml/parse_engine.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ class ParseEngine
488488
private:
489489

490490
void _handle_map_block();
491+
bool _handle_map_block_qmrk();
492+
bool _handle_map_block_rkcl();
491493
void _handle_seq_block();
492494
void _handle_map_flow();
493495
void _handle_seq_flow();
@@ -507,12 +509,14 @@ class ParseEngine
507509
void _end_seq_blck();
508510
void _end2_map();
509511
void _end2_seq();
512+
void _end_flow_container(size_t orig_indent);
510513
void _flow_container_was_a_key(size_t orig_indent);
511514

512515
void _begin2_doc();
513516
void _begin2_doc_expl();
514517
void _end2_doc();
515518
void _end2_doc_expl();
519+
void _check_doc_end_tokens() const;
516520

517521
void _maybe_begin_doc();
518522
void _maybe_end_doc();
@@ -605,10 +609,10 @@ class ParseEngine
605609

606610
#ifndef RYML_DBG
607611
C4_ALWAYS_INLINE static void add_flags(ParserFlag_t on, ParserState *C4_RESTRICT s) noexcept { s->flags |= on; }
608-
C4_ALWAYS_INLINE static void addrem_flags(ParserFlag_t on, ParserFlag_t off, ParserState *C4_RESTRICT s) noexcept { s->flags &= ~off; s->flags |= on; }
612+
C4_ALWAYS_INLINE static void addrem_flags(ParserFlag_t on, ParserFlag_t off, ParserState *C4_RESTRICT s) noexcept { _RYML_ASSERT_BASIC((on & off) == 0); s->flags &= ~off; s->flags |= on; }
609613
C4_ALWAYS_INLINE static void rem_flags(ParserFlag_t off, ParserState *C4_RESTRICT s) noexcept { s->flags &= ~off; }
610614
C4_ALWAYS_INLINE void add_flags(ParserFlag_t on) noexcept { m_evt_handler->m_curr->flags |= on; }
611-
C4_ALWAYS_INLINE void addrem_flags(ParserFlag_t on, ParserFlag_t off) noexcept { m_evt_handler->m_curr->flags &= ~off; m_evt_handler->m_curr->flags |= on; }
615+
C4_ALWAYS_INLINE void addrem_flags(ParserFlag_t on, ParserFlag_t off) noexcept { _RYML_ASSERT_BASIC((on & off) == 0); m_evt_handler->m_curr->flags &= ~off; m_evt_handler->m_curr->flags |= on; }
612616
C4_ALWAYS_INLINE void rem_flags(ParserFlag_t off) noexcept { m_evt_handler->m_curr->flags &= ~off; }
613617
#else
614618
static void add_flags(ParserFlag_t on, ParserState *C4_RESTRICT s);
@@ -694,7 +698,6 @@ class ParseEngine
694698
Annotation m_pending_anchors;
695699
Annotation m_pending_tags;
696700

697-
bool m_was_inside_qmrk;
698701
bool m_doc_empty = true;
699702
size_t m_prev_colon = npos;
700703

src_extra/c4/yml/extra/event_handler_ints.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,9 @@ struct EventHandlerInts : public c4::yml::EventHandlerStack<EventHandlerInts, Ev
510510
return m_arena_pos;
511511
}
512512

513-
/** Predicate to test if the event buffer successfully accomodated
514-
* all the parse events.
513+
/** Predicate to test if the event and arena buffers successfully
514+
* accomodated all the parse events.
515+
*
515516
* @warning this is valid only until the next parse */
516517
bool fits_buffers() const
517518
{

test/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ endfunction()
9393

9494
ryml_add_test(engine_1_doc)
9595
ryml_add_test(engine_2_seq)
96-
ryml_add_test(engine_3_map)
96+
ryml_add_test(engine_3_map_1)
97+
ryml_add_test(engine_3_map_2)
9798
ryml_add_test(engine_4_anchor)
9899
ryml_add_test(engine_5_tag)
99100
ryml_add_test(engine_6_qmrk_1)
@@ -163,7 +164,8 @@ endif()
163164
if(MSVC)
164165
target_compile_options(ryml-test-engine_1_doc PRIVATE /bigobj)
165166
target_compile_options(ryml-test-engine_2_seq PRIVATE /bigobj)
166-
target_compile_options(ryml-test-engine_3_map PRIVATE /bigobj)
167+
target_compile_options(ryml-test-engine_3_map_1 PRIVATE /bigobj)
168+
target_compile_options(ryml-test-engine_3_map_2 PRIVATE /bigobj)
167169
target_compile_options(ryml-test-engine_4_anchor PRIVATE /bigobj)
168170
target_compile_options(ryml-test-engine_5_tag PRIVATE /bigobj)
169171
target_compile_options(ryml-test-engine_6_qmrk_1 PRIVATE /bigobj)

test/test_anchor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,13 @@ TEST(simple_anchor, resolve_nested)
771771
}
772772
{
773773
Tree t = parse_in_arena(yaml);
774-
ExpectError::check_error_basic(&t, [&]{
774+
ExpectError::check_error_visit(&t, [&]{
775775
t.resolve(true);
776776
});
777777
}
778778
{
779779
Tree t = parse_in_arena(yaml);
780-
ExpectError::check_error_basic(&t, [&]{
780+
ExpectError::check_error_visit(&t, [&]{
781781
t.resolve(false);
782782
});
783783
}

test/test_bom.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void test_bom2(bom2spec const& spec, CreateFn &&createfn, TestFn &&testfn, bom2_
126126
else
127127
{
128128
pfn_error_basic orig = get_callbacks().m_error_basic;
129-
ExpectError::check_error_basic([&]{
129+
ExpectError::check_error_parse([&]{
130130
Parser::handler_type handler;
131131
Parser parser(&handler);
132132
Tree tree;

test/test_emit.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,14 @@ void test_emits(Tree const& t, id_type id, std::string const& expected, std::str
356356
{
357357
EmitOptions optsd = opts;
358358
optsd = optsd.max_depth(0);
359-
ExpectError::check_error_basic(&t, [&]{ return emit2buf([&](substr buf){ return emit_yaml(t, id, optsd, buf); }); });
360-
ExpectError::check_error_basic(&t, [&]{ return emit2buf([&](substr buf){ return emit_json(t, id, optsd, buf); }); });
361-
ExpectError::check_error_basic(&t, [&]{ return emit2file([&](FILE *f){ return emit_yaml(t, id, optsd, f); }); });
362-
ExpectError::check_error_basic(&t, [&]{ return emit2file([&](FILE *f){ return emit_json(t, id, optsd, f); }); });
363-
ExpectError::check_error_basic(&t, [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_yaml(t, id, optsd); }); });
364-
ExpectError::check_error_basic(&t, [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_json(t, id, optsd); }); });
365-
ExpectError::check_error_basic(&t, [&]{ return emitrs_yaml<std::string>(t, id, optsd); });
366-
ExpectError::check_error_basic(&t, [&]{ return emitrs_json<std::string>(t, id, optsd); });
359+
ExpectError::check_error_visit(&t, [&]{ return emit2buf([&](substr buf){ return emit_yaml(t, id, optsd, buf); }); });
360+
ExpectError::check_error_visit(&t, [&]{ return emit2buf([&](substr buf){ return emit_json(t, id, optsd, buf); }); });
361+
ExpectError::check_error_visit(&t, [&]{ return emit2file([&](FILE *f){ return emit_yaml(t, id, optsd, f); }); });
362+
ExpectError::check_error_visit(&t, [&]{ return emit2file([&](FILE *f){ return emit_json(t, id, optsd, f); }); });
363+
ExpectError::check_error_visit(&t, [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_yaml(t, id, optsd); }); });
364+
ExpectError::check_error_visit(&t, [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_json(t, id, optsd); }); });
365+
ExpectError::check_error_visit(&t, [&]{ return emitrs_yaml<std::string>(t, id, optsd); });
366+
ExpectError::check_error_visit(&t, [&]{ return emitrs_json<std::string>(t, id, optsd); });
367367
}
368368
}
369369

@@ -414,14 +414,14 @@ void test_emits(Tree const& t, std::string const& expected, std::string const& e
414414
{
415415
EmitOptions optsd = opts;
416416
optsd = optsd.max_depth(0);
417-
ExpectError::check_error_basic(&t, [&]{ return emit2buf([&](substr buf){ return emit_yaml(t, optsd, buf); }); });
418-
ExpectError::check_error_basic(&t, [&]{ return emit2buf([&](substr buf){ return emit_json(t, optsd, buf); }); });
419-
ExpectError::check_error_basic(&t, [&]{ return emit2file([&](FILE *f){ return emit_yaml(t, optsd, f); }); });
420-
ExpectError::check_error_basic(&t, [&]{ return emit2file([&](FILE *f){ return emit_json(t, optsd, f); }); });
421-
ExpectError::check_error_basic(&t, [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_yaml(ConstNodeRef(&t), optsd); }); });
422-
ExpectError::check_error_basic(&t, [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_json(ConstNodeRef(&t), optsd); }); });
423-
ExpectError::check_error_basic(&t, [&]{ return emitrs_yaml<std::string>(t, optsd); });
424-
ExpectError::check_error_basic(&t, [&]{ return emitrs_json<std::string>(t, optsd); });
417+
ExpectError::check_error_visit(&t, [&]{ return emit2buf([&](substr buf){ return emit_yaml(t, optsd, buf); }); });
418+
ExpectError::check_error_visit(&t, [&]{ return emit2buf([&](substr buf){ return emit_json(t, optsd, buf); }); });
419+
ExpectError::check_error_visit(&t, [&]{ return emit2file([&](FILE *f){ return emit_yaml(t, optsd, f); }); });
420+
ExpectError::check_error_visit(&t, [&]{ return emit2file([&](FILE *f){ return emit_json(t, optsd, f); }); });
421+
ExpectError::check_error_visit(&t, [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_yaml(ConstNodeRef(&t), optsd); }); });
422+
ExpectError::check_error_visit(&t, [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_json(ConstNodeRef(&t), optsd); }); });
423+
ExpectError::check_error_visit(&t, [&]{ return emitrs_yaml<std::string>(t, optsd); });
424+
ExpectError::check_error_visit(&t, [&]{ return emitrs_json<std::string>(t, optsd); });
425425
}
426426
}
427427

@@ -471,14 +471,14 @@ void test_emits(ConstNodeRef n, std::string const& expected, std::string const&
471471
{
472472
EmitOptions optsd = opts;
473473
optsd = optsd.max_depth(0);
474-
ExpectError::check_error_basic(n.tree(), [&]{ return emit2buf([&](substr buf){ return emit_yaml(n, optsd, buf); }); });
475-
ExpectError::check_error_basic(n.tree(), [&]{ return emit2buf([&](substr buf){ return emit_json(n, optsd, buf); }); });
476-
ExpectError::check_error_basic(n.tree(), [&]{ return emit2file([&](FILE *f){ return emit_yaml(n, optsd, f); }); });
477-
ExpectError::check_error_basic(n.tree(), [&]{ return emit2file([&](FILE *f){ return emit_json(n, optsd, f); }); });
478-
ExpectError::check_error_basic(n.tree(), [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_yaml(n, optsd); }); });
479-
ExpectError::check_error_basic(n.tree(), [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_json(n, optsd); }); });
480-
ExpectError::check_error_basic(n.tree(), [&]{ return emitrs_yaml<std::string>(n, optsd); });
481-
ExpectError::check_error_basic(n.tree(), [&]{ return emitrs_json<std::string>(n, optsd); });
474+
ExpectError::check_error_visit(n.tree(), [&]{ return emit2buf([&](substr buf){ return emit_yaml(n, optsd, buf); }); });
475+
ExpectError::check_error_visit(n.tree(), [&]{ return emit2buf([&](substr buf){ return emit_json(n, optsd, buf); }); });
476+
ExpectError::check_error_visit(n.tree(), [&]{ return emit2file([&](FILE *f){ return emit_yaml(n, optsd, f); }); });
477+
ExpectError::check_error_visit(n.tree(), [&]{ return emit2file([&](FILE *f){ return emit_json(n, optsd, f); }); });
478+
ExpectError::check_error_visit(n.tree(), [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_yaml(n, optsd); }); });
479+
ExpectError::check_error_visit(n.tree(), [&]{ return emit2stream([&](std::ostringstream &oss){ oss << as_json(n, optsd); }); });
480+
ExpectError::check_error_visit(n.tree(), [&]{ return emitrs_yaml<std::string>(n, optsd); });
481+
ExpectError::check_error_visit(n.tree(), [&]{ return emitrs_json<std::string>(n, optsd); });
482482
}
483483
}
484484
}

test/test_engine_1_doc.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,131 @@ ENGINE_TEST(DocEmptyTerm4_2,
174174
}
175175

176176

177+
ENGINE_TEST(DocEmptyTermErr,
178+
"...\n...\n"
179+
,
180+
""
181+
,
182+
"+STR\n"
183+
"-STR\n")
184+
{
185+
___(ps.begin_stream());
186+
___(ps.end_stream());
187+
}
188+
189+
190+
//-----------------------------------------------------------------------------
191+
192+
ENGINE_TEST_ERRLOC(DocTerm_MapBlck_err0,
193+
Location(2, 5),
194+
"foo: bar\n"
195+
"... bad\n"
196+
)
197+
ENGINE_TEST_ERRLOC(DocTerm_MapBlck_err1,
198+
Location(2, 5),
199+
"foo: bar\n"
200+
"... [bad]\n"
201+
)
202+
ENGINE_TEST_ERRLOC(DocTerm_MapBlck_err2,
203+
Location(2, 5),
204+
"foo: bar\n"
205+
"... {bad: yes}\n"
206+
)
207+
ENGINE_TEST_ERRLOC(DocTerm_MapBlck_err3,
208+
Location(2, 5),
209+
"foo:\n"
210+
"... bad\n"
211+
)
212+
ENGINE_TEST_ERRLOC(DocTerm_MapBlck_err4,
213+
Location(2, 5),
214+
"foo:\n"
215+
"... bad\n"
216+
)
217+
ENGINE_TEST_ERRLOC(DocTerm_MapBlck_err5,
218+
Location(2, 5),
219+
"?\n"
220+
"... bad\n"
221+
)
222+
ENGINE_TEST_ERRLOC(DocTerm_MapBlck_err6,
223+
Location(2, 5),
224+
"? foo\n"
225+
"... bad\n"
226+
)
227+
ENGINE_TEST(DocTerm_MapBlck_ok,
228+
"foo: bar\n"
229+
"... #bad\n"
230+
,
231+
"foo: bar\n"
232+
,
233+
"+STR\n"
234+
"+DOC\n"
235+
"+MAP\n"
236+
"=VAL :foo\n"
237+
"=VAL :bar\n"
238+
"-MAP\n"
239+
"-DOC ...\n"
240+
"-STR\n")
241+
{
242+
___(ps.begin_stream());
243+
___(ps.begin_doc());
244+
___(ps.begin_map_val_block());
245+
___(ps.set_key_scalar_plain("foo"));
246+
___(ps.set_val_scalar_plain("bar"));
247+
___(ps.end_map_block());
248+
___(ps.end_doc_expl());
249+
___(ps.end_stream());
250+
}
251+
252+
ENGINE_TEST_ERRLOC(DocTerm_SeqBlck_err0,
253+
Location(2, 5),
254+
"- foo\n"
255+
"... bad\n"
256+
)
257+
ENGINE_TEST_ERRLOC(DocTerm_SeqBlck_err1,
258+
Location(2, 5),
259+
"-\n"
260+
"... bad\n"
261+
)
262+
263+
ENGINE_TEST_ERRLOC(DocTerm_MapFlow_err0,
264+
Location(2, 5),
265+
"{key: val}\n"
266+
"... bad\n"
267+
)
268+
269+
ENGINE_TEST_ERRLOC(DocTerm_SeqFlow_err0,
270+
Location(2, 5),
271+
"{key: val}\n"
272+
"... bad\n"
273+
)
274+
275+
ENGINE_TEST_ERRLOC(DocTerm_ScalarPlain_err0,
276+
Location(2, 5),
277+
"scalar\n"
278+
"... bad\n"
279+
)
280+
ENGINE_TEST_ERRLOC(DocTerm_ScalarSquo_err0,
281+
Location(2, 5),
282+
"'scalar'\n"
283+
"... bad\n"
284+
)
285+
ENGINE_TEST_ERRLOC(DocTerm_ScalarDquo_err0,
286+
Location(2, 5),
287+
"\"scalar\"\n"
288+
"... bad\n"
289+
)
290+
ENGINE_TEST_ERRLOC(DocTerm_ScalarLit_err0,
291+
Location(3, 5),
292+
"|\n scalar\n"
293+
"... bad\n"
294+
)
295+
ENGINE_TEST_ERRLOC(DocTerm_FoldedLit_err0,
296+
Location(3, 5),
297+
">\n scalar\n"
298+
"... bad\n"
299+
)
300+
301+
177302
//-----------------------------------------------------------------------------
178303

179304
ENGINE_TEST(DocEmptyExplMult,

0 commit comments

Comments
 (0)