diff --git a/include/yaml-cpp/exceptions.h b/include/yaml-cpp/exceptions.h index e49a32a..25523c9 100644 --- a/include/yaml-cpp/exceptions.h +++ b/include/yaml-cpp/exceptions.h @@ -123,7 +123,7 @@ inline const std::string BAD_SUBSCRIPT_WITH_KEY( inline const std::string BAD_SUBSCRIPT_WITH_KEY(const std::string& key) { std::stringstream stream; - stream << BAD_SUBSCRIPT << " (key: '" << key << "')"; + stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")"; return stream.str(); } @@ -131,7 +131,7 @@ template inline const std::string BAD_SUBSCRIPT_WITH_KEY( const T& key, typename enable_if>::type* = 0) { std::stringstream stream; - stream << BAD_SUBSCRIPT << " (key: '" << key << "')"; + stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")"; return stream.str(); } @@ -140,7 +140,7 @@ inline const std::string INVALID_NODE_WITH_KEY(const std::string& key) { if (key.empty()) { return INVALID_NODE; } - stream << "invalid node - first invalid key: '" << key << "'"; + stream << "invalid node; first invalid key: \"" << key << "\""; return stream.str(); } } diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 52f6f5d..60120cf 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -374,25 +374,9 @@ inline typename to_value_t::return_type to_value(const T& t) { } } -template -struct key_to_string_impl { - static std::string impl(const Key& key) { - std::stringstream ss; - ss << key; - return ss.str(); - } -}; - -template -struct key_to_string_impl { - static std::string impl(const Key&) { - return std::string(); - } -}; - template std::string key_to_string(const Key& key) { - return key_to_string_impl::value>().impl(key); + return streamable_to_string::value>().impl(key); } // indexing diff --git a/include/yaml-cpp/node/node.h b/include/yaml-cpp/node/node.h index cc54d17..49af58e 100644 --- a/include/yaml-cpp/node/node.h +++ b/include/yaml-cpp/node/node.h @@ -132,7 +132,7 @@ class YAML_CPP_API Node { private: bool m_isValid; - // string representation of invalid key, if the node is invalid + // String representation of invalid key, if the node is invalid. std::string m_invalidKey; mutable detail::shared_memory_holder m_pMemory; mutable detail::node* m_pNode; diff --git a/include/yaml-cpp/traits.h b/include/yaml-cpp/traits.h index e7079d7..f3cda64 100644 --- a/include/yaml-cpp/traits.h +++ b/include/yaml-cpp/traits.h @@ -9,6 +9,8 @@ #include #include +#include +#include namespace YAML { template @@ -104,7 +106,7 @@ struct disable_if : public disable_if_c {}; } template -class is_streamable { +struct is_streamable { template static auto test(int) -> decltype(std::declval() << std::declval(), std::true_type()); @@ -112,8 +114,22 @@ class is_streamable { template static auto test(...) -> std::false_type; - public: static const bool value = decltype(test(0))::value; }; +template +struct streamable_to_string { + static std::string impl(const Key& key) { + std::stringstream ss; + ss << key; + return ss.str(); + } +}; + +template +struct streamable_to_string { + static std::string impl(const Key&) { + return std::string(); + } +}; #endif // TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/test/integration/error_messages_test.cpp b/test/integration/error_messages_test.cpp index a9d09c8..64ab6b9 100644 --- a/test/integration/error_messages_test.cpp +++ b/test/integration/error_messages_test.cpp @@ -1,4 +1,3 @@ -#include "specexamples.h" #include "yaml-cpp/yaml.h" // IWYU pragma: keep #include "gtest/gtest.h" @@ -14,15 +13,19 @@ namespace YAML { namespace { -TEST(ErrorMessageTest, Ex9_1_BadSubscriptErrorMessage) { - Node doc = Load(ex9_1); +TEST(ErrorMessageTest, BadSubscriptErrorMessage) { + const char *example_yaml = "first:\n" + " second: 1\n" + " third: 2\n"; + + Node doc = Load(example_yaml); // Test that printable key is part of error message EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"]["fourth"], - "operator[] call on a scalar (key: 'fourth')"); + "operator[] call on a scalar (key: \"fourth\")"); EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"][37], - "operator[] call on a scalar (key: '37')"); + "operator[] call on a scalar (key: \"37\")"); // Non-printable key is not included in error message @@ -35,14 +38,18 @@ TEST(ErrorMessageTest, Ex9_1_BadSubscriptErrorMessage) { } TEST(ErrorMessageTest, Ex9_1_InvalidNodeErrorMessage) { - const Node doc = Load(ex9_1); + const char *example_yaml = "first:\n" + " second: 1\n" + " third: 2\n"; + + const Node doc = Load(example_yaml); // Test that printable key is part of error message EXPECT_THROW_EXCEPTION(YAML::InvalidNode, doc["first"]["fourth"].as(), - "invalid node - first invalid key: 'fourth'"); + "invalid node; first invalid key: \"fourth\""); EXPECT_THROW_EXCEPTION(YAML::InvalidNode, doc["first"][37].as(), - "invalid node - first invalid key: '37'"); + "invalid node; first invalid key: \"37\""); // Non-printable key is not included in error message EXPECT_THROW_EXCEPTION(YAML::InvalidNode, diff --git a/test/specexamples.h b/test/specexamples.h index e4e85ef..3c81c77 100644 --- a/test/specexamples.h +++ b/test/specexamples.h @@ -843,9 +843,4 @@ const char *ex8_22 = " - nested\n" "mapping: !!map\n" " foo: bar\n"; - -const char *ex9_1 = - "first:\n" - " second: 1\n" - " third: 2\n"; }