Update tests, change is_streamable
This commit is contained in:
parent
06209f8c2e
commit
5e60c73fbb
@ -123,7 +123,7 @@ inline const std::string BAD_SUBSCRIPT_WITH_KEY(
|
|||||||
|
|
||||||
inline const std::string BAD_SUBSCRIPT_WITH_KEY(const std::string& key) {
|
inline const std::string BAD_SUBSCRIPT_WITH_KEY(const std::string& key) {
|
||||||
std::stringstream stream;
|
std::stringstream stream;
|
||||||
stream << BAD_SUBSCRIPT << " (key: '" << key << "')";
|
stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")";
|
||||||
return stream.str();
|
return stream.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +131,7 @@ template <typename T>
|
|||||||
inline const std::string BAD_SUBSCRIPT_WITH_KEY(
|
inline const std::string BAD_SUBSCRIPT_WITH_KEY(
|
||||||
const T& key, typename enable_if<is_numeric<T>>::type* = 0) {
|
const T& key, typename enable_if<is_numeric<T>>::type* = 0) {
|
||||||
std::stringstream stream;
|
std::stringstream stream;
|
||||||
stream << BAD_SUBSCRIPT << " (key: '" << key << "')";
|
stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")";
|
||||||
return stream.str();
|
return stream.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +140,7 @@ inline const std::string INVALID_NODE_WITH_KEY(const std::string& key) {
|
|||||||
if (key.empty()) {
|
if (key.empty()) {
|
||||||
return INVALID_NODE;
|
return INVALID_NODE;
|
||||||
}
|
}
|
||||||
stream << "invalid node - first invalid key: '" << key << "'";
|
stream << "invalid node; first invalid key: \"" << key << "\"";
|
||||||
return stream.str();
|
return stream.str();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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>
|
template<typename Key>
|
||||||
std::string key_to_string(const Key& 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
|
// indexing
|
||||||
|
|||||||
@ -132,7 +132,7 @@ class YAML_CPP_API Node {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_isValid;
|
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;
|
std::string m_invalidKey;
|
||||||
mutable detail::shared_memory_holder m_pMemory;
|
mutable detail::shared_memory_holder m_pMemory;
|
||||||
mutable detail::node* m_pNode;
|
mutable detail::node* m_pNode;
|
||||||
|
|||||||
@ -9,6 +9,8 @@
|
|||||||
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
||||||
template <typename>
|
template <typename>
|
||||||
@ -104,7 +106,7 @@ struct disable_if : public disable_if_c<Cond::value, T> {};
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename S, typename T>
|
template <typename S, typename T>
|
||||||
class is_streamable {
|
struct is_streamable {
|
||||||
template <typename SS, typename TT>
|
template <typename SS, typename TT>
|
||||||
static auto test(int)
|
static auto test(int)
|
||||||
-> decltype(std::declval<SS&>() << std::declval<TT>(), std::true_type());
|
-> decltype(std::declval<SS&>() << std::declval<TT>(), std::true_type());
|
||||||
@ -112,8 +114,22 @@ class is_streamable {
|
|||||||
template <typename, typename>
|
template <typename, typename>
|
||||||
static auto test(...) -> std::false_type;
|
static auto test(...) -> std::false_type;
|
||||||
|
|
||||||
public:
|
|
||||||
static const bool value = decltype(test<S, T>(0))::value;
|
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
|
#endif // TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
#include "specexamples.h"
|
|
||||||
#include "yaml-cpp/yaml.h" // IWYU pragma: keep
|
#include "yaml-cpp/yaml.h" // IWYU pragma: keep
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
@ -14,15 +13,19 @@
|
|||||||
namespace YAML {
|
namespace YAML {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
TEST(ErrorMessageTest, Ex9_1_BadSubscriptErrorMessage) {
|
TEST(ErrorMessageTest, BadSubscriptErrorMessage) {
|
||||||
Node doc = Load(ex9_1);
|
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
|
// Test that printable key is part of error message
|
||||||
EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"]["fourth"],
|
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],
|
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
|
// Non-printable key is not included in error message
|
||||||
@ -35,14 +38,18 @@ TEST(ErrorMessageTest, Ex9_1_BadSubscriptErrorMessage) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(ErrorMessageTest, Ex9_1_InvalidNodeErrorMessage) {
|
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
|
// Test that printable key is part of error message
|
||||||
EXPECT_THROW_EXCEPTION(YAML::InvalidNode, doc["first"]["fourth"].as<int>(),
|
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>(),
|
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
|
// Non-printable key is not included in error message
|
||||||
EXPECT_THROW_EXCEPTION(YAML::InvalidNode,
|
EXPECT_THROW_EXCEPTION(YAML::InvalidNode,
|
||||||
|
|||||||
@ -843,9 +843,4 @@ const char *ex8_22 =
|
|||||||
" - nested\n"
|
" - nested\n"
|
||||||
"mapping: !!map\n"
|
"mapping: !!map\n"
|
||||||
" foo: bar\n";
|
" foo: bar\n";
|
||||||
|
|
||||||
const char *ex9_1 =
|
|
||||||
"first:\n"
|
|
||||||
" second: 1\n"
|
|
||||||
" third: 2\n";
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user