mirror of
https://github.com/biojppm/rapidyaml.git
synced 2026-01-18 21:41:18 +01:00
[fix] ensure events emitter exits on error: prevent a forever loop
This commit is contained in:
@@ -130,13 +130,18 @@ if(RYML_TEST_TOOLS)
|
||||
function(ryml_add_event_tool_test name expect_success contents)
|
||||
ryml_create_file(${name}.yml "${contents}" file)
|
||||
add_test(NAME ryml-test-tool-events-${name} COMMAND ${RYML_TGT_EVENTS} ${name}.yml)
|
||||
if(UNIX)
|
||||
#add_test(NAME ryml-test-tool-events-${name}-stdin COMMAND ${CMAKE_COMMAND} -E echo '${contents}' | ${RYML_TGT_EVENTS} -)
|
||||
endif()
|
||||
if(NOT expect_success)
|
||||
set_tests_properties(ryml-test-tool-events-${name} PROPERTIES WILL_FAIL TRUE)
|
||||
#set_tests_properties(ryml-test-tool-events-${name}-stdin PROPERTIES WILL_FAIL TRUE)
|
||||
endif()
|
||||
endfunction()
|
||||
ryml_get_target_exe(ryml-yaml-events RYML_TGT_EVENTS)
|
||||
ryml_add_event_tool_test(success TRUE "{foo: bar, baz: [exactly]")
|
||||
ryml_add_event_tool_test(failure FALSE "foo: 'bar")
|
||||
ryml_add_event_tool_test(fail_squo FALSE "foo: 'bar")
|
||||
ryml_add_event_tool_test(fail_badseq FALSE "[ a, b, c ] ]")
|
||||
endif()
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,17 @@
|
||||
namespace c4 {
|
||||
namespace yml {
|
||||
|
||||
TEST(simple_seq, bad_seq)
|
||||
{
|
||||
Tree tree;
|
||||
ExpectError::do_check(&tree, [&]{
|
||||
parse_in_arena(R"(
|
||||
---
|
||||
[ a, b, c ] ]
|
||||
)", &tree);
|
||||
});
|
||||
}
|
||||
|
||||
TEST(simple_seq, two_nested_flow_seqs)
|
||||
{
|
||||
Tree tree = parse_in_arena("[[]]");
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <test_suite/test_suite_events.hpp>
|
||||
#include <c4/fs/fs.hpp>
|
||||
#include <cstdio>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace c4;
|
||||
using namespace c4::yml;
|
||||
@@ -23,28 +24,25 @@ int main(int argc, const char *argv[])
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// do not abort on error
|
||||
int exit_status = 0;
|
||||
Callbacks callbacks = {};
|
||||
callbacks.m_user_data = &exit_status;
|
||||
callbacks.m_error = [](const char *msg, size_t msg_len, Location location, void *status)
|
||||
callbacks.m_error = [](const char *msg, size_t msg_len, Location location, void *)
|
||||
{
|
||||
report_error(msg, msg_len, location, stderr);
|
||||
*(int*)status = 1;
|
||||
throw std::runtime_error({msg, msg_len});
|
||||
};
|
||||
|
||||
Tree tree(callbacks);
|
||||
csubstr filename = to_csubstr(argv[1]);
|
||||
std::string evt, src = load_file(filename);
|
||||
tree.reserve(to_substr(src).count('\n'));
|
||||
parse_in_place(filename, to_substr(src), &tree);
|
||||
if(exit_status)
|
||||
return exit_status;
|
||||
emit_events(&evt, tree);
|
||||
if(exit_status)
|
||||
return exit_status;
|
||||
std::fwrite(evt.data(), 1, evt.size(), stdout);
|
||||
try {
|
||||
Tree tree(callbacks);
|
||||
csubstr filename = to_csubstr(argv[1]);
|
||||
std::string evt, src = load_file(filename);
|
||||
tree.reserve(to_substr(src).count('\n'));
|
||||
parse_in_place(filename, to_substr(src), &tree);
|
||||
emit_events(&evt, tree);
|
||||
std::fwrite(evt.data(), 1, evt.size(), stdout);
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -60,7 +58,6 @@ void usage(const char *exename)
|
||||
)", exename, exename);
|
||||
}
|
||||
|
||||
|
||||
std::string load_file(csubstr filename)
|
||||
{
|
||||
if(filename == "-") // read from stdin
|
||||
|
||||
Reference in New Issue
Block a user