Update tests, change is_streamable

This commit is contained in:
beda 2019-02-18 10:21:29 +01:00
parent 06209f8c2e
commit 5e60c73fbb
6 changed files with 38 additions and 36 deletions

View File

@ -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 <typename T>
inline const std::string BAD_SUBSCRIPT_WITH_KEY(
const T& key, typename enable_if<is_numeric<T>>::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();
}
}

View File

@ -374,25 +374,9 @@ inline typename to_value_t<T>::return_type to_value(const T& t) {
}
}
template<typename Key, bool Streamable>
struct key_to_string_impl {
static std::string impl(const Key& key) {
std::stringstream ss;
ss << key;
return ss.str();
}
};
template<typename Key>
struct key_to_string_impl<Key, false> {
static std::string impl(const Key&) {
return std::string();
}
};
template<typename Key>
std::string key_to_string(const Key& key) {
return key_to_string_impl<Key, is_streamable<std::stringstream, Key>::value>().impl(key);
return streamable_to_string<Key, is_streamable<std::stringstream, Key>::value>().impl(key);
}
// indexing

View File

@ -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;

View File

@ -9,6 +9,8 @@
#include <type_traits>
#include <utility>
#include <string>
#include <sstream>
namespace YAML {
template <typename>
@ -104,7 +106,7 @@ struct disable_if : public disable_if_c<Cond::value, T> {};
}
template <typename S, typename T>
class is_streamable {
struct is_streamable {
template <typename SS, typename TT>
static auto test(int)
-> decltype(std::declval<SS&>() << std::declval<TT>(), std::true_type());
@ -112,8 +114,22 @@ class is_streamable {
template <typename, typename>
static auto test(...) -> std::false_type;
public:
static const bool value = decltype(test<S, T>(0))::value;
};
template<typename Key, bool Streamable>
struct streamable_to_string {
static std::string impl(const Key& key) {
std::stringstream ss;
ss << key;
return ss.str();
}
};
template<typename Key>
struct streamable_to_string<Key, false> {
static std::string impl(const Key&) {
return std::string();
}
};
#endif // TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@ -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<int>(),
"invalid node - first invalid key: 'fourth'");
"invalid node; first invalid key: \"fourth\"");
EXPECT_THROW_EXCEPTION(YAML::InvalidNode, doc["first"][37].as<int>(),
"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,

View File

@ -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";
}