Skip to content

Commit

Permalink
added missing errorx tests for conversion from string scalar node to …
Browse files Browse the repository at this point in the history
…compatible string type
  • Loading branch information
fktn-k committed Apr 20, 2024
1 parent 294f1d6 commit 1458ff9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion include/fkYAML/detail/conversions/from_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ template <
conjunction<
is_basic_node<BasicNodeType>,
negation<std::is_same<CompatibleStringType, typename BasicNodeType::string_type>>,
std::is_constructible<CompatibleStringType, const typename BasicNodeType::string_type&>>::value,
disjunction<
std::is_constructible<CompatibleStringType, const typename BasicNodeType::string_type&>,
std::is_assignable<CompatibleStringType, const typename BasicNodeType::string_type&>>>::value,
int> = 0>
inline void from_node(const BasicNodeType& n, CompatibleStringType& s)
{
Expand Down
4 changes: 3 additions & 1 deletion single_include/fkYAML/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8212,7 +8212,9 @@ template <
conjunction<
is_basic_node<BasicNodeType>,
negation<std::is_same<CompatibleStringType, typename BasicNodeType::string_type>>,
std::is_constructible<CompatibleStringType, const typename BasicNodeType::string_type&>>::value,
disjunction<
std::is_constructible<CompatibleStringType, const typename BasicNodeType::string_type&>,
std::is_assignable<CompatibleStringType, const typename BasicNodeType::string_type&>>>::value,
int> = 0>
inline void from_node(const BasicNodeType& n, CompatibleStringType& s)
{
Expand Down
25 changes: 25 additions & 0 deletions test/unit_test/test_node_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2501,8 +2501,20 @@ TEST_CASE("Node_AddTagName")
// test cases for value getters (copy)
//

struct string_wrap
{
string_wrap() = default;
string_wrap& operator=(const std::string& _str)
{
str = _str;
return *this;
}
std::string str;
};

TEST_CASE("Node_GetValue")
{

SECTION("sequence")
{
fkyaml::node node(fkyaml::node::sequence_type {fkyaml::node(true), fkyaml::node(false)});
Expand All @@ -2525,6 +2537,7 @@ TEST_CASE("Node_GetValue")
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::integer_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::float_number_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::string_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<string_wrap>(), fkyaml::type_error);
}
}

Expand Down Expand Up @@ -2552,6 +2565,7 @@ TEST_CASE("Node_GetValue")
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::integer_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::float_number_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::string_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<string_wrap>(), fkyaml::type_error);
}
}

Expand All @@ -2573,6 +2587,7 @@ TEST_CASE("Node_GetValue")
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::integer_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::float_number_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::string_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<string_wrap>(), fkyaml::type_error);
}
}

Expand All @@ -2593,6 +2608,7 @@ TEST_CASE("Node_GetValue")
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::integer_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::float_number_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::string_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<string_wrap>(), fkyaml::type_error);
}
}

Expand Down Expand Up @@ -2620,6 +2636,7 @@ TEST_CASE("Node_GetValue")
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::boolean_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::float_number_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::string_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<string_wrap>(), fkyaml::type_error);
}

SECTION("non-integer node value")
Expand Down Expand Up @@ -2660,6 +2677,7 @@ TEST_CASE("Node_GetValue")
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::boolean_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::integer_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<fkyaml::node::string_type>(), fkyaml::type_error);
REQUIRE_THROWS_AS(node.get_value<string_wrap>(), fkyaml::type_error);
}

SECTION("non-float-number node value")
Expand Down Expand Up @@ -2692,6 +2710,13 @@ TEST_CASE("Node_GetValue")
REQUIRE(str == "test");
}

SECTION("compatible string value")
{
auto str_wrap = node.get_value<string_wrap>();
REQUIRE(str_wrap.str.size() == 4);
REQUIRE(str_wrap.str == "test");
}

#ifdef FK_YAML_HAS_CXX_17
SECTION("string view")
{
Expand Down

0 comments on commit 1458ff9

Please sign in to comment.