Skip to content

Commit

Permalink
added test cases for uncovered lines/branches in the deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
fktn-k committed May 27, 2024
1 parent 20acbde commit 2d7356c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 54 deletions.
30 changes: 4 additions & 26 deletions include/fkYAML/detail/input/deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,7 @@ class basic_deserializer {
}
}

bool do_continue = true;
switch (type) {
case lexical_token_t::SEQUENCE_BLOCK_PREFIX: {
if (type == lexical_token_t::SEQUENCE_BLOCK_PREFIX) {
// a key separator preceeding block sequence entries
*mp_current_node = node_type::sequence();
apply_directive_set(*mp_current_node);
Expand All @@ -372,32 +370,12 @@ class basic_deserializer {
cur_context.line = line;
cur_context.indent = indent;
cur_context.state = context_state_t::BLOCK_SEQUENCE;
do_continue = false;
break;
}
case lexical_token_t::EXPLICIT_KEY_PREFIX:
// a key separator for a explicit block mapping key.
// defer the handling of the explicit key prefix token until the next loop.
break;
// defer checking the existence of a key separator after the scalar until a deserialize_scalar()
// call.
case lexical_token_t::NULL_VALUE:
case lexical_token_t::BOOLEAN_VALUE:
case lexical_token_t::INTEGER_VALUE:
case lexical_token_t::FLOAT_NUMBER_VALUE:
case lexical_token_t::STRING_VALUE:
// defer handling these tokens until the next loop.
case lexical_token_t::MAPPING_FLOW_BEGIN:
case lexical_token_t::SEQUENCE_FLOW_BEGIN:
break;
default: // LCOV_EXCL_LINE
break; // LCOV_EXCL_LINE
}

if (do_continue) {
continue;
}
break;
// defer checking the existence of a key separator after the following scalar until the next
// deserialize_scalar() call.
continue;
}

// handle explicit mapping key separators.
Expand Down
30 changes: 4 additions & 26 deletions single_include/fkYAML/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4570,9 +4570,7 @@ class basic_deserializer {
}
}

bool do_continue = true;
switch (type) {
case lexical_token_t::SEQUENCE_BLOCK_PREFIX: {
if (type == lexical_token_t::SEQUENCE_BLOCK_PREFIX) {
// a key separator preceeding block sequence entries
*mp_current_node = node_type::sequence();
apply_directive_set(*mp_current_node);
Expand All @@ -4581,32 +4579,12 @@ class basic_deserializer {
cur_context.line = line;
cur_context.indent = indent;
cur_context.state = context_state_t::BLOCK_SEQUENCE;
do_continue = false;
break;
}
case lexical_token_t::EXPLICIT_KEY_PREFIX:
// a key separator for a explicit block mapping key.
// defer the handling of the explicit key prefix token until the next loop.
break;
// defer checking the existence of a key separator after the scalar until a deserialize_scalar()
// call.
case lexical_token_t::NULL_VALUE:
case lexical_token_t::BOOLEAN_VALUE:
case lexical_token_t::INTEGER_VALUE:
case lexical_token_t::FLOAT_NUMBER_VALUE:
case lexical_token_t::STRING_VALUE:
// defer handling these tokens until the next loop.
case lexical_token_t::MAPPING_FLOW_BEGIN:
case lexical_token_t::SEQUENCE_FLOW_BEGIN:
break;
default: // LCOV_EXCL_LINE
break; // LCOV_EXCL_LINE
}

if (do_continue) {
continue;
}
break;
// defer checking the existence of a key separator after the following scalar until the next
// deserialize_scalar() call.
continue;
}

// handle explicit mapping key separators.
Expand Down
16 changes: 14 additions & 2 deletions test/unit_test/test_deserializer_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,8 @@ TEST_CASE("Deserializer_FlowSequence") {
}

SECTION("lack the beginning of a flow sequence") {
REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter("test: {]}")), fkyaml::parse_error);
auto input = GENERATE(std::string("test: {]}"), std::string("test: {foo: bar]}"));
REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter(input)), fkyaml::parse_error);
}

SECTION("root flow sequence") {
Expand Down Expand Up @@ -1435,6 +1436,11 @@ TEST_CASE("Deserializer_FlowSequence") {
std::string("[123, {foo: true bar: 3.14}]"));
REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter(input)), fkyaml::parse_error);
}

SECTION("too many value separators") {
std::string input = "[123,,true]";
REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter(input)), fkyaml::parse_error);
}
}

TEST_CASE("Deserializer_FlowMapping") {
Expand Down Expand Up @@ -1495,7 +1501,8 @@ TEST_CASE("Deserializer_FlowMapping") {
}

SECTION("lack the beginning of a flow mapping") {
REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter("test: [}]")), fkyaml::parse_error);
auto input = GENERATE(std::string("test: [}]"), std::string("test: [true}]"));
REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter(input)), fkyaml::parse_error);
}

SECTION("flow mapping with child flow sequence") {
Expand Down Expand Up @@ -1727,6 +1734,11 @@ TEST_CASE("Deserializer_FlowMapping") {
std::string("{foo: 123, child: [bar: true baz: 3.14]}"));
REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter(input)), fkyaml::parse_error);
}

SECTION("too many value separators") {
std::string input = "{foo: 123,,bar: true}";
REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter(input)), fkyaml::parse_error);
}
}

TEST_CASE("Deserializer_InputWithComment") {
Expand Down

0 comments on commit 2d7356c

Please sign in to comment.