Add tests, reworked key_to_string function
This commit is contained in:
parent
88ac439b47
commit
7d2094ce95
@ -47,7 +47,7 @@ inline Node::Node(const Node& rhs)
|
|||||||
|
|
||||||
inline Node::Node(Zombie) : m_isValid(false), m_pNode(NULL) {}
|
inline Node::Node(Zombie) : m_isValid(false), m_pNode(NULL) {}
|
||||||
|
|
||||||
inline Node::Node(Zombie, std::string key)
|
inline Node::Node(Zombie, const std::string& key)
|
||||||
: m_isValid(false), m_invalidKey(key), m_pNode(NULL) {}
|
: m_isValid(false), m_invalidKey(key), m_pNode(NULL) {}
|
||||||
|
|
||||||
inline Node::Node(detail::node& node, detail::shared_memory_holder pMemory)
|
inline Node::Node(detail::node& node, detail::shared_memory_holder pMemory)
|
||||||
@ -374,15 +374,25 @@ inline typename to_value_t<T>::return_type to_value(const T& t) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Key>
|
template<typename Key, bool Streamable>
|
||||||
std::string key_to_string(const Key& key) {
|
struct key_to_string_impl {
|
||||||
std::stringstream ss;
|
static std::string impl(const Key& key) {
|
||||||
if (is_streamable<std::stringstream, Key>::value) {
|
std::stringstream ss;
|
||||||
ss << key;
|
ss << key;
|
||||||
return ss.str();
|
return ss.str();
|
||||||
} else {
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Key>
|
||||||
|
struct key_to_string_impl<Key, false> {
|
||||||
|
static std::string impl(const Key&) {
|
||||||
return std::string();
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// indexing
|
// indexing
|
||||||
|
|||||||
@ -8,7 +8,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "yaml-cpp/dll.h"
|
#include "yaml-cpp/dll.h"
|
||||||
#include "yaml-cpp/emitterstyle.h"
|
#include "yaml-cpp/emitterstyle.h"
|
||||||
@ -117,7 +116,7 @@ class YAML_CPP_API Node {
|
|||||||
private:
|
private:
|
||||||
enum Zombie { ZombieNode };
|
enum Zombie { ZombieNode };
|
||||||
explicit Node(Zombie);
|
explicit Node(Zombie);
|
||||||
explicit Node(Zombie, std::string);
|
explicit Node(Zombie, const std::string&);
|
||||||
explicit Node(detail::node& node, detail::shared_memory_holder pMemory);
|
explicit Node(detail::node& node, detail::shared_memory_holder pMemory);
|
||||||
|
|
||||||
void EnsureNodeExists() const;
|
void EnsureNodeExists() const;
|
||||||
@ -132,6 +131,7 @@ class YAML_CPP_API Node {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_isValid;
|
bool m_isValid;
|
||||||
|
// 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;
|
||||||
|
|||||||
54
test/integration/error_messages_test.cpp
Normal file
54
test/integration/error_messages_test.cpp
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include "specexamples.h"
|
||||||
|
#include "yaml-cpp/yaml.h" // IWYU pragma: keep
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
#define EXPECT_THROW_EXCEPTION(exception_type, statement, message) \
|
||||||
|
ASSERT_THROW(statement, exception_type); \
|
||||||
|
try { \
|
||||||
|
statement; \
|
||||||
|
} catch (const exception_type& e) { \
|
||||||
|
EXPECT_EQ(e.msg, message); \
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace YAML {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
TEST(ErrorMessageTest, Ex9_1_BadSubscriptErrorMessage) {
|
||||||
|
Node doc = Load(ex9_1);
|
||||||
|
|
||||||
|
// 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')");
|
||||||
|
|
||||||
|
EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"][37],
|
||||||
|
"operator[] call on a scalar (key: '37')");
|
||||||
|
|
||||||
|
|
||||||
|
// Non-printable key is not included in error message
|
||||||
|
EXPECT_THROW_EXCEPTION(YAML::BadSubscript,
|
||||||
|
doc["first"]["second"][std::vector<int>()],
|
||||||
|
"operator[] call on a scalar");
|
||||||
|
|
||||||
|
EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"][Node()],
|
||||||
|
"operator[] call on a scalar");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ErrorMessageTest, Ex9_1_InvalidNodeErrorMessage) {
|
||||||
|
const Node doc = Load(ex9_1);
|
||||||
|
|
||||||
|
// 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'");
|
||||||
|
|
||||||
|
EXPECT_THROW_EXCEPTION(YAML::InvalidNode, doc["first"][37].as<int>(),
|
||||||
|
"invalid node - first invalid key: '37'");
|
||||||
|
|
||||||
|
// Non-printable key is not included in error message
|
||||||
|
EXPECT_THROW_EXCEPTION(YAML::InvalidNode,
|
||||||
|
doc["first"][std::vector<int>()].as<int>(),
|
||||||
|
"invalid node; this may result from using a map "
|
||||||
|
"iterator as a sequence iterator, or vice-versa");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -843,4 +843,9 @@ 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