Add tests, reworked key_to_string function

This commit is contained in:
beda 2019-02-07 12:54:44 +01:00
parent 88ac439b47
commit 7d2094ce95
4 changed files with 77 additions and 8 deletions

View File

@ -47,7 +47,7 @@ inline Node::Node(const Node& rhs)
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) {}
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>
std::string key_to_string(const Key& key) {
std::stringstream ss;
if (is_streamable<std::stringstream, Key>::value) {
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();
} else {
}
};
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);
}
// indexing

View File

@ -8,7 +8,6 @@
#endif
#include <stdexcept>
#include <string>
#include "yaml-cpp/dll.h"
#include "yaml-cpp/emitterstyle.h"
@ -117,7 +116,7 @@ class YAML_CPP_API Node {
private:
enum Zombie { ZombieNode };
explicit Node(Zombie);
explicit Node(Zombie, std::string);
explicit Node(Zombie, const std::string&);
explicit Node(detail::node& node, detail::shared_memory_holder pMemory);
void EnsureNodeExists() const;
@ -132,6 +131,7 @@ class YAML_CPP_API Node {
private:
bool m_isValid;
// 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

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

View File

@ -843,4 +843,9 @@ const char *ex8_22 =
" - nested\n"
"mapping: !!map\n"
" foo: bar\n";
const char *ex9_1 =
"first:\n"
" second: 1\n"
" third: 2\n";
}