From ad0a3311a7bc8d541e3fa347f31fa3fd568de398 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Sun, 23 Mar 2014 19:21:13 -0500 Subject: [PATCH] Convert node tests to gtest --- test/integration/load_node_test.cpp | 191 +++++++++ test/main.cpp | 3 - test/new-api/nodetests.cpp | 600 ---------------------------- test/node/node_test.cpp | 249 +++++++++++- test/nodetests.h | 14 - test/tests.cpp | 18 - test/tests.h | 56 --- test/teststruct.h | 20 - 8 files changed, 439 insertions(+), 712 deletions(-) create mode 100644 test/integration/load_node_test.cpp delete mode 100644 test/new-api/nodetests.cpp delete mode 100644 test/nodetests.h delete mode 100644 test/tests.cpp delete mode 100644 test/tests.h delete mode 100644 test/teststruct.h diff --git a/test/integration/load_node_test.cpp b/test/integration/load_node_test.cpp new file mode 100644 index 0000000..4c4c826 --- /dev/null +++ b/test/integration/load_node_test.cpp @@ -0,0 +1,191 @@ +#include "yaml-cpp/yaml.h" // IWYU pragma: keep + +#include "gtest/gtest.h" + +namespace YAML { +namespace { +TEST(LoadNodeTest, Reassign) { + YAML::Node node = YAML::Load("foo"); + node = YAML::Node(); +} + +TEST(LoadNodeTest, FallbackValues) { + YAML::Node node = YAML::Load("foo: bar\nx: 2"); + EXPECT_EQ("bar", node["foo"].as()); + EXPECT_EQ("bar", node["foo"].as("hello")); + EXPECT_EQ("hello", node["baz"].as("hello")); + EXPECT_EQ(2, node["x"].as()); + EXPECT_EQ(2, node["x"].as(5)); + EXPECT_EQ(5, node["y"].as(5)); +} + +TEST(LoadNodeTest, NumericConversion) { + YAML::Node node = YAML::Load("[1.5, 1, .nan, .inf, -.inf, 0x15, 015]"); + EXPECT_EQ(1.5f, node[0].as()); + EXPECT_EQ(1.5, node[0].as()); + EXPECT_THROW(node[0].as(), YAML::TypedBadConversion); + EXPECT_EQ(1, node[1].as()); + EXPECT_EQ(1.0f, node[1].as()); + EXPECT_NE(node[2].as(), node[2].as()); + EXPECT_EQ(std::numeric_limits::infinity(), node[3].as()); + EXPECT_EQ(-std::numeric_limits::infinity(), node[4].as()); + EXPECT_EQ(21, node[5].as()); + EXPECT_EQ(13, node[6].as()); +} + +TEST(LoadNodeTest, Binary) { + YAML::Node node = YAML::Load( + "[!!binary \"SGVsbG8sIFdvcmxkIQ==\", !!binary " + "\"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieS" + "B0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIG" + "x1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbi" + "B0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZG" + "dlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS" + "4K\"]"); + EXPECT_EQ( + YAML::Binary(reinterpret_cast("Hello, World!"), 13), + node[0].as()); + EXPECT_EQ(YAML::Binary(reinterpret_cast( + "Man is distinguished, not only by his reason, " + "but by this singular passion from other " + "animals, which is a lust of the mind, that by " + "a perseverance of delight in the continued and " + "indefatigable generation of knowledge, exceeds " + "the short vehemence of any carnal pleasure.\n"), + 270), + node[1].as()); +} + +TEST(LoadNodeTest, IterateSequence) { + YAML::Node node = YAML::Load("[1, 3, 5, 7]"); + int seq[] = {1, 3, 5, 7}; + int i = 0; + for (YAML::const_iterator it = node.begin(); it != node.end(); ++it) { + EXPECT_TRUE(i < 4); + int x = seq[i++]; + EXPECT_EQ(x, it->as()); + } + EXPECT_EQ(4, i); +} + +TEST(LoadNodeTest, IterateMap) { + YAML::Node node = YAML::Load("{a: A, b: B, c: C}"); + int i = 0; + for (YAML::const_iterator it = node.begin(); it != node.end(); ++it) { + EXPECT_TRUE(i < 3); + i++; + EXPECT_EQ(it->second.as(), it->first.as() + 'A' - 'a'); + } + EXPECT_EQ(3, i); +} + +#ifdef BOOST_FOREACH +TEST(LoadNodeTest, ForEach) { + YAML::Node node = YAML::Load("[1, 3, 5, 7]"); + int seq[] = {1, 3, 5, 7}; + int i = 0; + BOOST_FOREACH(const YAML::Node & item, node) { + int x = seq[i++]; + EXPECT_EQ(x, item.as()); + } +} + +TEST(LoadNodeTest, ForEachMap) { + YAML::Node node = YAML::Load("{a: A, b: B, c: C}"); + BOOST_FOREACH(const YAML::const_iterator::value_type & p, node) { + EXPECT_EQ(p.second.as(), p.first.as() + 'A' - 'a'); + } +} +#endif + +TEST(LoadNodeTest, CloneScalar) { + YAML::Node node = YAML::Load("!foo monkey"); + YAML::Node clone = Clone(node); + EXPECT_FALSE(clone == node); + EXPECT_EQ(clone.as(), node.as()); + EXPECT_EQ(clone.Tag(), node.Tag()); +} + +TEST(LoadNodeTest, CloneSeq) { + YAML::Node node = YAML::Load("[1, 3, 5, 7]"); + YAML::Node clone = Clone(node); + EXPECT_FALSE(clone == node); + EXPECT_EQ(YAML::NodeType::Sequence, clone.Type()); + EXPECT_EQ(clone.size(), node.size()); + for (std::size_t i = 0; i < node.size(); i++) { + EXPECT_EQ(clone[i].as(), node[i].as()); + } +} + +TEST(LoadNodeTest, CloneMap) { + YAML::Node node = YAML::Load("{foo: bar}"); + YAML::Node clone = Clone(node); + EXPECT_FALSE(clone == node); + EXPECT_EQ(YAML::NodeType::Map, clone.Type()); + EXPECT_EQ(clone.size(), node.size()); + EXPECT_EQ(clone["foo"].as(), node["foo"].as()); +} + +TEST(LoadNodeTest, CloneAlias) { + YAML::Node node = YAML::Load("&foo [*foo]"); + YAML::Node clone = Clone(node); + EXPECT_FALSE(clone == node); + EXPECT_EQ(YAML::NodeType::Sequence, clone.Type()); + EXPECT_EQ(clone.size(), node.size()); + EXPECT_EQ(clone[0], clone); +} + +TEST(LoadNodeTest, ForceInsertIntoMap) { + YAML::Node node; + node["a"] = "b"; + node.force_insert("x", "y"); + node.force_insert("a", 5); + EXPECT_EQ(3, node.size()); + EXPECT_EQ(YAML::NodeType::Map, node.Type()); + bool ab = false; + bool a5 = false; + bool xy = false; + for (YAML::const_iterator it = node.begin(); it != node.end(); ++it) { + if (it->first.as() == "a") { + if (it->second.as() == "b") + ab = true; + else if (it->second.as() == "5") + a5 = true; + } else if (it->first.as() == "x" && + it->second.as() == "y") + xy = true; + } + EXPECT_TRUE(ab); + EXPECT_TRUE(a5); + EXPECT_TRUE(xy); +} + +TEST(LoadNodeTest, ResetNode) { + YAML::Node node = YAML::Load("[1, 2, 3]"); + EXPECT_TRUE(!node.IsNull()); + YAML::Node other = node; + node.reset(); + EXPECT_TRUE(node.IsNull()); + EXPECT_TRUE(!other.IsNull()); + node.reset(other); + EXPECT_TRUE(!node.IsNull()); + EXPECT_EQ(node, other); +} + +TEST(LoadNodeTest, DereferenceIteratorError) { + YAML::Node node = YAML::Load("[{a: b}, 1, 2]"); + EXPECT_THROW(node.begin()->first.as(), YAML::InvalidNode); + EXPECT_EQ(true, (*node.begin()).IsMap()); + EXPECT_EQ(true, node.begin()->IsMap()); + EXPECT_THROW((*node.begin()->begin()).IsDefined(), YAML::InvalidNode); + EXPECT_THROW(node.begin()->begin()->IsDefined(), YAML::InvalidNode); +} + +TEST(NodeTest, EmitEmptyNode) { + YAML::Node node; + YAML::Emitter emitter; + emitter << node; + EXPECT_EQ("", std::string(emitter.c_str())); +} +} +} diff --git a/test/main.cpp b/test/main.cpp index dd65123..443e2db 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,9 +1,6 @@ -#include "tests.h" - #include "gtest/gtest.h" int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - Test::RunAll(); return RUN_ALL_TESTS(); } diff --git a/test/new-api/nodetests.cpp b/test/new-api/nodetests.cpp deleted file mode 100644 index 7c47674..0000000 --- a/test/new-api/nodetests.cpp +++ /dev/null @@ -1,600 +0,0 @@ -#include -#include - -#include "nodetests.h" -#include "yaml-cpp/yaml.h" // IWYU pragma: keep - -namespace YAML { -class InvalidNode; -template class TypedBadConversion; -} // namespace YAML - -#if BOOST_VERSION >= 103400 -#include -#endif - -namespace { -struct TEST { - TEST() : ok(false) {} - TEST(bool ok_) : ok(ok_) {} - TEST(const char* error_) : ok(false), error(error_) {} - TEST(const std::string& error_) : ok(false), error(error_) {} - - bool ok; - std::string error; -}; -} - -#define YAML_ASSERT(cond) \ - do { \ - if (!(cond)) \ - return " Assert failed: " #cond; \ - } while (false) - -#define YAML_ASSERT_THROWS(cond, exc) \ - do { \ - try { \ - (cond); \ - return " Expression did not throw: " #cond; \ - } \ - catch (const exc&) { \ - } \ - catch (const std::runtime_error& e) { \ - std::stringstream stream; \ - stream << " Expression threw runtime error ther than " #exc \ - ":\n " #cond "\n " << e.what(); \ - return stream.str(); \ - } \ - catch (...) { \ - return " Expression threw unknown exception, other than " #exc \ - ":\n " #cond; \ - } \ - } while (false) - -namespace Test { -namespace Node { -TEST SimpleScalar() { - YAML::Node node = YAML::Node("Hello, World!"); - YAML_ASSERT(node.IsScalar()); - YAML_ASSERT(node.as() == "Hello, World!"); - return true; -} - -TEST IntScalar() { - YAML::Node node = YAML::Node(15); - YAML_ASSERT(node.IsScalar()); - YAML_ASSERT(node.as() == 15); - return true; -} - -TEST SimpleAppendSequence() { - YAML::Node node; - node.push_back(10); - node.push_back("foo"); - node.push_back("monkey"); - YAML_ASSERT(node.IsSequence()); - YAML_ASSERT(node.size() == 3); - YAML_ASSERT(node[0].as() == 10); - YAML_ASSERT(node[1].as() == "foo"); - YAML_ASSERT(node[2].as() == "monkey"); - YAML_ASSERT(node.IsSequence()); - return true; -} - -TEST SimpleAssignSequence() { - YAML::Node node; - node[0] = 10; - node[1] = "foo"; - node[2] = "monkey"; - YAML_ASSERT(node.IsSequence()); - YAML_ASSERT(node.size() == 3); - YAML_ASSERT(node[0].as() == 10); - YAML_ASSERT(node[1].as() == "foo"); - YAML_ASSERT(node[2].as() == "monkey"); - YAML_ASSERT(node.IsSequence()); - return true; -} - -TEST SimpleMap() { - YAML::Node node; - node["key"] = "value"; - YAML_ASSERT(node.IsMap()); - YAML_ASSERT(node["key"].as() == "value"); - YAML_ASSERT(node.size() == 1); - return true; -} - -TEST MapWithUndefinedValues() { - YAML::Node node; - node["key"] = "value"; - node["undefined"]; - YAML_ASSERT(node.IsMap()); - YAML_ASSERT(node["key"].as() == "value"); - YAML_ASSERT(node.size() == 1); - - node["undefined"] = "monkey"; - YAML_ASSERT(node["undefined"].as() == "monkey"); - YAML_ASSERT(node.size() == 2); - - return true; -} - -TEST MapIteratorWithUndefinedValues() { - YAML::Node node; - node["key"] = "value"; - node["undefined"]; - - std::size_t count = 0; - for (YAML::const_iterator it = node.begin(); it != node.end(); ++it) - count++; - YAML_ASSERT(count == 1); - return true; -} - -TEST SimpleSubkeys() { - YAML::Node node; - node["device"]["udid"] = "12345"; - node["device"]["name"] = "iPhone"; - node["device"]["os"] = "4.0"; - node["username"] = "monkey"; - YAML_ASSERT(node["device"]["udid"].as() == "12345"); - YAML_ASSERT(node["device"]["name"].as() == "iPhone"); - YAML_ASSERT(node["device"]["os"].as() == "4.0"); - YAML_ASSERT(node["username"].as() == "monkey"); - return true; -} - -TEST StdVector() { - std::vector primes; - primes.push_back(2); - primes.push_back(3); - primes.push_back(5); - primes.push_back(7); - primes.push_back(11); - primes.push_back(13); - - YAML::Node node; - node["primes"] = primes; - YAML_ASSERT(node["primes"].as >() == primes); - return true; -} - -TEST StdList() { - std::list primes; - primes.push_back(2); - primes.push_back(3); - primes.push_back(5); - primes.push_back(7); - primes.push_back(11); - primes.push_back(13); - - YAML::Node node; - node["primes"] = primes; - YAML_ASSERT(node["primes"].as >() == primes); - return true; -} - -TEST StdMap() { - std::map squares; - squares[0] = 0; - squares[1] = 1; - squares[2] = 4; - squares[3] = 9; - squares[4] = 16; - - YAML::Node node; - node["squares"] = squares; - YAML_ASSERT((node["squares"].as >() == squares)); - return true; -} - -TEST StdPair() { - std::pair p; - p.first = 5; - p.second = "five"; - - YAML::Node node; - node["pair"] = p; - YAML_ASSERT((node["pair"].as >() == p)); - return true; -} - -TEST SimpleAlias() { - YAML::Node node; - node["foo"] = "value"; - node["bar"] = node["foo"]; - YAML_ASSERT(node["foo"].as() == "value"); - YAML_ASSERT(node["bar"].as() == "value"); - YAML_ASSERT(node["foo"] == node["bar"]); - YAML_ASSERT(node.size() == 2); - return true; -} - -TEST AliasAsKey() { - YAML::Node node; - node["foo"] = "value"; - YAML::Node value = node["foo"]; - node[value] = "foo"; - YAML_ASSERT(node["foo"].as() == "value"); - YAML_ASSERT(node[value].as() == "foo"); - YAML_ASSERT(node["value"].as() == "foo"); - YAML_ASSERT(node.size() == 2); - return true; -} - -TEST SelfReferenceSequence() { - YAML::Node node; - node[0] = node; - YAML_ASSERT(node.IsSequence()); - YAML_ASSERT(node.size() == 1); - YAML_ASSERT(node[0] == node); - YAML_ASSERT(node[0][0] == node); - YAML_ASSERT(node[0][0] == node[0]); - return true; -} - -TEST ValueSelfReferenceMap() { - YAML::Node node; - node["key"] = node; - YAML_ASSERT(node.IsMap()); - YAML_ASSERT(node.size() == 1); - YAML_ASSERT(node["key"] == node); - YAML_ASSERT(node["key"]["key"] == node); - YAML_ASSERT(node["key"]["key"] == node["key"]); - return true; -} - -TEST KeySelfReferenceMap() { - YAML::Node node; - node[node] = "value"; - YAML_ASSERT(node.IsMap()); - YAML_ASSERT(node.size() == 1); - YAML_ASSERT(node[node].as() == "value"); - return true; -} - -TEST SelfReferenceMap() { - YAML::Node node; - node[node] = node; - YAML_ASSERT(node.IsMap()); - YAML_ASSERT(node.size() == 1); - YAML_ASSERT(node[node] == node); - YAML_ASSERT(node[node][node] == node); - YAML_ASSERT(node[node][node] == node[node]); - return true; -} - -TEST TempMapVariable() { - YAML::Node node; - YAML::Node tmp = node["key"]; - tmp = "value"; - YAML_ASSERT(node.IsMap()); - YAML_ASSERT(node.size() == 1); - YAML_ASSERT(node["key"].as() == "value"); - return true; -} - -TEST TempMapVariableAlias() { - YAML::Node node; - YAML::Node tmp = node["key"]; - tmp = node["other"]; - node["other"] = "value"; - YAML_ASSERT(node.IsMap()); - YAML_ASSERT(node.size() == 2); - YAML_ASSERT(node["key"].as() == "value"); - YAML_ASSERT(node["other"].as() == "value"); - YAML_ASSERT(node["other"] == node["key"]); - return true; -} - -TEST Bool() { - YAML::Node node; - node[true] = false; - YAML_ASSERT(node.IsMap()); - YAML_ASSERT(node[true].as() == false); - return true; -} - -TEST AutoBoolConversion() { - YAML::Node node; - node["foo"] = "bar"; - YAML_ASSERT(static_cast(node["foo"])); - YAML_ASSERT(!node["monkey"]); - YAML_ASSERT(!!node["foo"]); - return true; -} - -TEST Reassign() { - YAML::Node node = YAML::Load("foo"); - node = YAML::Node(); - return true; -} - -TEST FallbackValues() { - YAML::Node node = YAML::Load("foo: bar\nx: 2"); - YAML_ASSERT(node["foo"].as() == "bar"); - YAML_ASSERT(node["foo"].as("hello") == "bar"); - YAML_ASSERT(node["baz"].as("hello") == "hello"); - YAML_ASSERT(node["x"].as() == 2); - YAML_ASSERT(node["x"].as(5) == 2); - YAML_ASSERT(node["y"].as(5) == 5); - return true; -} - -TEST NumericConversion() { - YAML::Node node = YAML::Load("[1.5, 1, .nan, .inf, -.inf, 0x15, 015]"); - YAML_ASSERT(node[0].as() == 1.5f); - YAML_ASSERT(node[0].as() == 1.5); - YAML_ASSERT_THROWS(node[0].as(), YAML::TypedBadConversion); - YAML_ASSERT(node[1].as() == 1); - YAML_ASSERT(node[1].as() == 1.0f); - YAML_ASSERT(node[2].as() != node[2].as()); - YAML_ASSERT(node[3].as() == std::numeric_limits::infinity()); - YAML_ASSERT(node[4].as() == -std::numeric_limits::infinity()); - YAML_ASSERT(node[5].as() == 21); - YAML_ASSERT(node[6].as() == 13); - return true; -} - -TEST Binary() { - YAML::Node node = YAML::Load( - "[!!binary \"SGVsbG8sIFdvcmxkIQ==\", !!binary " - "\"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieS" - "B0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIG" - "x1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbi" - "B0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZG" - "dlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS" - "4K\"]"); - YAML_ASSERT(node[0].as() == - YAML::Binary( - reinterpret_cast("Hello, World!"), 13)); - YAML_ASSERT(node[1].as() == - YAML::Binary(reinterpret_cast( - "Man is distinguished, not only by his reason, " - "but by this singular passion from other " - "animals, which is a lust of the mind, that by " - "a perseverance of delight in the continued and " - "indefatigable generation of knowledge, exceeds " - "the short vehemence of any carnal pleasure.\n"), - 270)); - return true; -} - -TEST IterateSequence() { - YAML::Node node = YAML::Load("[1, 3, 5, 7]"); - int seq[] = {1, 3, 5, 7}; - int i = 0; - for (YAML::const_iterator it = node.begin(); it != node.end(); ++it) { - YAML_ASSERT(i < 4); - int x = seq[i++]; - YAML_ASSERT(it->as() == x); - } - YAML_ASSERT(i == 4); - return true; -} - -TEST IterateMap() { - YAML::Node node = YAML::Load("{a: A, b: B, c: C}"); - int i = 0; - for (YAML::const_iterator it = node.begin(); it != node.end(); ++it) { - YAML_ASSERT(i < 3); - i++; - YAML_ASSERT(it->first.as() + 'A' - 'a' == it->second.as()); - } - YAML_ASSERT(i == 3); - return true; -} - -#ifdef BOOST_FOREACH -TEST ForEach() { - YAML::Node node = YAML::Load("[1, 3, 5, 7]"); - int seq[] = {1, 3, 5, 7}; - int i = 0; - BOOST_FOREACH(const YAML::Node & item, node) { - int x = seq[i++]; - YAML_ASSERT(item.as() == x); - } - return true; -} - -TEST ForEachMap() { - YAML::Node node = YAML::Load("{a: A, b: B, c: C}"); - BOOST_FOREACH(const YAML::const_iterator::value_type & p, node) { - YAML_ASSERT(p.first.as() + 'A' - 'a' == p.second.as()); - } - return true; -} -#endif - -TEST CloneScalar() { - YAML::Node node = YAML::Load("!foo monkey"); - YAML::Node clone = Clone(node); - YAML_ASSERT(!(node == clone)); - YAML_ASSERT(node.as() == clone.as()); - YAML_ASSERT(node.Tag() == clone.Tag()); - return true; -} - -TEST CloneSeq() { - YAML::Node node = YAML::Load("[1, 3, 5, 7]"); - YAML::Node clone = Clone(node); - YAML_ASSERT(!(node == clone)); - YAML_ASSERT(clone.Type() == YAML::NodeType::Sequence); - YAML_ASSERT(node.size() == clone.size()); - for (std::size_t i = 0; i < node.size(); i++) - YAML_ASSERT(node[i].as() == clone[i].as()); - return true; -} - -TEST CloneMap() { - YAML::Node node = YAML::Load("{foo: bar}"); - YAML::Node clone = Clone(node); - YAML_ASSERT(!(node == clone)); - YAML_ASSERT(clone.Type() == YAML::NodeType::Map); - YAML_ASSERT(node.size() == clone.size()); - YAML_ASSERT(node["foo"].as() == clone["foo"].as()); - return true; -} - -TEST CloneAlias() { - YAML::Node node = YAML::Load("&foo [*foo]"); - YAML::Node clone = Clone(node); - YAML_ASSERT(!(node == clone)); - YAML_ASSERT(clone.Type() == YAML::NodeType::Sequence); - YAML_ASSERT(node.size() == clone.size()); - YAML_ASSERT(clone == clone[0]); - return true; -} - -TEST ForceInsertIntoMap() { - YAML::Node node; - node["a"] = "b"; - node.force_insert("x", "y"); - node.force_insert("a", 5); - YAML_ASSERT(node.size() == 3); - YAML_ASSERT(node.Type() == YAML::NodeType::Map); - bool ab = false; - bool a5 = false; - bool xy = false; - for (YAML::const_iterator it = node.begin(); it != node.end(); ++it) { - if (it->first.as() == "a") { - if (it->second.as() == "b") - ab = true; - else if (it->second.as() == "5") - a5 = true; - } else if (it->first.as() == "x" && - it->second.as() == "y") - xy = true; - } - YAML_ASSERT(ab); - YAML_ASSERT(a5); - YAML_ASSERT(xy); - return true; -} - -TEST ResetNode() { - YAML::Node node = YAML::Load("[1, 2, 3]"); - YAML_ASSERT(!node.IsNull()); - YAML::Node other = node; - node.reset(); - YAML_ASSERT(node.IsNull()); - YAML_ASSERT(!other.IsNull()); - node.reset(other); - YAML_ASSERT(!node.IsNull()); - YAML_ASSERT(other == node); - return true; -} - -TEST DereferenceIteratorError() { - YAML::Node node = YAML::Load("[{a: b}, 1, 2]"); - YAML_ASSERT_THROWS(node.begin()->first.as(), YAML::InvalidNode); - YAML_ASSERT((*node.begin()).IsMap() == true); - YAML_ASSERT(node.begin()->IsMap() == true); - YAML_ASSERT_THROWS((*node.begin()->begin()).IsDefined(), YAML::InvalidNode); - YAML_ASSERT_THROWS(node.begin()->begin()->IsDefined(), YAML::InvalidNode); - return true; -} - -TEST FloatingPrecision() { - const double x = 0.123456789; - YAML::Node node = YAML::Node(x); - YAML_ASSERT(node.as() == x); - return true; -} - -TEST EmitEmptyNode() { - YAML::Node node; - YAML::Emitter emitter; - emitter << node; - YAML_ASSERT(std::string(emitter.c_str()) == ""); - return true; -} - -TEST SpaceChar() { - YAML::Node node = YAML::Node(' '); - YAML_ASSERT(node.as() == ' '); - return true; -} -} - -void RunNodeTest(TEST (*test)(), const std::string& name, int& passed, - int& total) { - TEST ret; - try { - ret = test(); - } - catch (const std::exception& e) { - ret.ok = false; - ret.error = e.what(); - } - if (ret.ok) { - passed++; - } else { - std::cout << "Node test failed: " << name << "\n"; - if (ret.error != "") - std::cout << ret.error << "\n"; - } - total++; -} - -bool RunNodeTests() { - int passed = 0; - int total = 0; - - RunNodeTest(&Node::SimpleScalar, "simple scalar", passed, total); - RunNodeTest(&Node::IntScalar, "int scalar", passed, total); - RunNodeTest(&Node::SimpleAppendSequence, "simple append sequence", passed, - total); - RunNodeTest(&Node::SimpleAssignSequence, "simple assign sequence", passed, - total); - RunNodeTest(&Node::SimpleMap, "simple map", passed, total); - RunNodeTest(&Node::MapWithUndefinedValues, "map with undefined values", - passed, total); - RunNodeTest(&Node::MapIteratorWithUndefinedValues, - "map iterator with undefined values", passed, total); - RunNodeTest(&Node::SimpleSubkeys, "simple subkey", passed, total); - RunNodeTest(&Node::StdVector, "std::vector", passed, total); - RunNodeTest(&Node::StdList, "std::list", passed, total); - RunNodeTest(&Node::StdMap, "std::map", passed, total); - RunNodeTest(&Node::StdPair, "std::pair", passed, total); - RunNodeTest(&Node::SimpleAlias, "simple alias", passed, total); - RunNodeTest(&Node::AliasAsKey, "alias as key", passed, total); - RunNodeTest(&Node::SelfReferenceSequence, "self reference sequence", passed, - total); - RunNodeTest(&Node::ValueSelfReferenceMap, "value self reference map", passed, - total); - RunNodeTest(&Node::KeySelfReferenceMap, "key self reference map", passed, - total); - RunNodeTest(&Node::SelfReferenceMap, "self reference map", passed, total); - RunNodeTest(&Node::TempMapVariable, "temp map variable", passed, total); - RunNodeTest(&Node::TempMapVariableAlias, "temp map variable alias", passed, - total); - RunNodeTest(&Node::Bool, "bool", passed, total); - RunNodeTest(&Node::AutoBoolConversion, "auto bool conversion", passed, total); - RunNodeTest(&Node::Reassign, "reassign", passed, total); - RunNodeTest(&Node::FallbackValues, "fallback values", passed, total); - RunNodeTest(&Node::NumericConversion, "numeric conversion", passed, total); - RunNodeTest(&Node::Binary, "binary", passed, total); - RunNodeTest(&Node::IterateSequence, "iterate sequence", passed, total); - RunNodeTest(&Node::IterateMap, "iterate map", passed, total); -#ifdef BOOST_FOREACH - RunNodeTest(&Node::ForEach, "for each", passed, total); - RunNodeTest(&Node::ForEachMap, "for each map", passed, total); -#endif - RunNodeTest(&Node::CloneScalar, "clone scalar", passed, total); - RunNodeTest(&Node::CloneSeq, "clone seq", passed, total); - RunNodeTest(&Node::CloneMap, "clone map", passed, total); - RunNodeTest(&Node::CloneAlias, "clone alias", passed, total); - RunNodeTest(&Node::ForceInsertIntoMap, "force insert into map", passed, - total); - RunNodeTest(&Node::ResetNode, "reset node", passed, total); - RunNodeTest(&Node::DereferenceIteratorError, "dereference iterator error", - passed, total); - RunNodeTest(&Node::FloatingPrecision, "floating precision", passed, total); - RunNodeTest(&Node::EmitEmptyNode, "emit empty node", passed, total); - RunNodeTest(&Node::SpaceChar, "space char", passed, total); - - std::cout << "Node tests: " << passed << "/" << total << " passed\n"; - return passed == total; -} -} diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index 0830e0e..830ddd7 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -1,8 +1,255 @@ -#include "gtest/gtest.h" #include "yaml-cpp/node/node.h" +#include "yaml-cpp/node/impl.h" +#include "yaml-cpp/node/convert.h" +#include "yaml-cpp/node/iterator.h" +#include "yaml-cpp/node/detail/impl.h" + +#include "gtest/gtest.h" namespace YAML { namespace { +TEST(NodeTest, SimpleScalar) { + YAML::Node node = YAML::Node("Hello, World!"); + EXPECT_TRUE(node.IsScalar()); + EXPECT_EQ("Hello, World!", node.as()); +} + +TEST(NodeTest, IntScalar) { + YAML::Node node = YAML::Node(15); + EXPECT_TRUE(node.IsScalar()); + EXPECT_EQ(15, node.as()); +} + +TEST(NodeTest, SimpleAppendSequence) { + YAML::Node node; + node.push_back(10); + node.push_back("foo"); + node.push_back("monkey"); + EXPECT_TRUE(node.IsSequence()); + EXPECT_EQ(3, node.size()); + EXPECT_EQ(10, node[0].as()); + EXPECT_EQ("foo", node[1].as()); + EXPECT_EQ("monkey", node[2].as()); + EXPECT_TRUE(node.IsSequence()); +} + +TEST(NodeTest, SimpleAssignSequence) { + YAML::Node node; + node[0] = 10; + node[1] = "foo"; + node[2] = "monkey"; + EXPECT_TRUE(node.IsSequence()); + EXPECT_EQ(3, node.size()); + EXPECT_EQ(10, node[0].as()); + EXPECT_EQ("foo", node[1].as()); + EXPECT_EQ("monkey", node[2].as()); + EXPECT_TRUE(node.IsSequence()); +} + +TEST(NodeTest, SimpleMap) { + YAML::Node node; + node["key"] = "value"; + EXPECT_TRUE(node.IsMap()); + EXPECT_EQ("value", node["key"].as()); + EXPECT_EQ(1, node.size()); +} + +TEST(NodeTest, MapWithUndefinedValues) { + YAML::Node node; + node["key"] = "value"; + node["undefined"]; + EXPECT_TRUE(node.IsMap()); + EXPECT_EQ("value", node["key"].as()); + EXPECT_EQ(1, node.size()); + + node["undefined"] = "monkey"; + EXPECT_EQ("monkey", node["undefined"].as()); + EXPECT_EQ(2, node.size()); +} + +TEST(NodeTest, MapIteratorWithUndefinedValues) { + YAML::Node node; + node["key"] = "value"; + node["undefined"]; + + std::size_t count = 0; + for (const_iterator it = node.begin(); it != node.end(); ++it) + count++; + EXPECT_EQ(1, count); +} + +TEST(NodeTest, SimpleSubkeys) { + YAML::Node node; + node["device"]["udid"] = "12345"; + node["device"]["name"] = "iPhone"; + node["device"]["os"] = "4.0"; + node["username"] = "monkey"; + EXPECT_EQ("12345", node["device"]["udid"].as()); + EXPECT_EQ("iPhone", node["device"]["name"].as()); + EXPECT_EQ("4.0", node["device"]["os"].as()); + EXPECT_EQ("monkey", node["username"].as()); +} + +TEST(NodeTest, StdVector) { + std::vector primes; + primes.push_back(2); + primes.push_back(3); + primes.push_back(5); + primes.push_back(7); + primes.push_back(11); + primes.push_back(13); + + YAML::Node node; + node["primes"] = primes; + EXPECT_EQ(primes, node["primes"].as >()); +} + +TEST(NodeTest, StdList) { + std::list primes; + primes.push_back(2); + primes.push_back(3); + primes.push_back(5); + primes.push_back(7); + primes.push_back(11); + primes.push_back(13); + + YAML::Node node; + node["primes"] = primes; + EXPECT_EQ(primes, node["primes"].as >()); +} + +TEST(NodeTest, StdMap) { + std::map squares; + squares[0] = 0; + squares[1] = 1; + squares[2] = 4; + squares[3] = 9; + squares[4] = 16; + + YAML::Node node; + node["squares"] = squares; + std::map actualSquares = node["squares"].as >(); + EXPECT_EQ(squares, actualSquares); +} + +TEST(NodeTest, StdPair) { + std::pair p; + p.first = 5; + p.second = "five"; + + YAML::Node node; + node["pair"] = p; + std::pair actualP = + node["pair"].as >(); + EXPECT_EQ(p, actualP); +} + +TEST(NodeTest, SimpleAlias) { + YAML::Node node; + node["foo"] = "value"; + node["bar"] = node["foo"]; + EXPECT_EQ("value", node["foo"].as()); + EXPECT_EQ("value", node["bar"].as()); + EXPECT_EQ(node["bar"], node["foo"]); + EXPECT_EQ(2, node.size()); +} + +TEST(NodeTest, AliasAsKey) { + YAML::Node node; + node["foo"] = "value"; + YAML::Node value = node["foo"]; + node[value] = "foo"; + EXPECT_EQ("value", node["foo"].as()); + EXPECT_EQ("foo", node[value].as()); + EXPECT_EQ("foo", node["value"].as()); + EXPECT_EQ(2, node.size()); +} + +TEST(NodeTest, SelfReferenceSequence) { + YAML::Node node; + node[0] = node; + EXPECT_TRUE(node.IsSequence()); + EXPECT_EQ(1, node.size()); + EXPECT_EQ(node, node[0]); + EXPECT_EQ(node, node[0][0]); + EXPECT_EQ(node[0], node[0][0]); +} + +TEST(NodeTest, ValueSelfReferenceMap) { + YAML::Node node; + node["key"] = node; + EXPECT_TRUE(node.IsMap()); + EXPECT_EQ(1, node.size()); + EXPECT_EQ(node, node["key"]); + EXPECT_EQ(node, node["key"]["key"]); + EXPECT_EQ(node["key"], node["key"]["key"]); +} + +TEST(NodeTest, KeySelfReferenceMap) { + YAML::Node node; + node[node] = "value"; + EXPECT_TRUE(node.IsMap()); + EXPECT_EQ(1, node.size()); + EXPECT_EQ("value", node[node].as()); +} + +TEST(NodeTest, SelfReferenceMap) { + YAML::Node node; + node[node] = node; + EXPECT_TRUE(node.IsMap()); + EXPECT_EQ(1, node.size()); + EXPECT_EQ(node, node[node]); + EXPECT_EQ(node, node[node][node]); + EXPECT_EQ(node[node], node[node][node]); +} + +TEST(NodeTest, TempMapVariable) { + YAML::Node node; + YAML::Node tmp = node["key"]; + tmp = "value"; + EXPECT_TRUE(node.IsMap()); + EXPECT_EQ(1, node.size()); + EXPECT_EQ("value", node["key"].as()); +} + +TEST(NodeTest, TempMapVariableAlias) { + YAML::Node node; + YAML::Node tmp = node["key"]; + tmp = node["other"]; + node["other"] = "value"; + EXPECT_TRUE(node.IsMap()); + EXPECT_EQ(2, node.size()); + EXPECT_EQ("value", node["key"].as()); + EXPECT_EQ("value", node["other"].as()); + EXPECT_EQ(node["key"], node["other"]); +} + +TEST(NodeTest, Bool) { + YAML::Node node; + node[true] = false; + EXPECT_TRUE(node.IsMap()); + EXPECT_EQ(false, node[true].as()); +} + +TEST(NodeTest, AutoBoolConversion) { + YAML::Node node; + node["foo"] = "bar"; + EXPECT_TRUE(static_cast(node["foo"])); + EXPECT_TRUE(!node["monkey"]); + EXPECT_TRUE(!!node["foo"]); +} + +TEST(NodeTest, FloatingPrecision) { + const double x = 0.123456789; + YAML::Node node = YAML::Node(x); + EXPECT_EQ(x, node.as()); +} + +TEST(NodeTest, SpaceChar) { + YAML::Node node = YAML::Node(' '); + EXPECT_EQ(' ', node.as()); +} + TEST(NodeTest, CloneNull) { Node node; Node clone = Clone(node); diff --git a/test/nodetests.h b/test/nodetests.h deleted file mode 100644 index 6728758..0000000 --- a/test/nodetests.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef NODETESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#define NODETESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - -#if defined(_MSC_VER) || \ - (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ - (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 -#pragma once -#endif - -namespace Test { -bool RunNodeTests(); -} - -#endif // NODETESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A6666 diff --git a/test/tests.cpp b/test/tests.cpp deleted file mode 100644 index b675941..0000000 --- a/test/tests.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include -#include -#include - -#include "nodetests.h" -#include "tests.h" - -namespace Test { -void RunAll() { - bool passed = true; - if (!RunNodeTests()) - passed = false; - - if (passed) - std::cout << "All tests passed!\n"; -} -} diff --git a/test/tests.h b/test/tests.h deleted file mode 100644 index 72c87c1..0000000 --- a/test/tests.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef TESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#define TESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - -#if defined(_MSC_VER) || \ - (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ - (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 -#pragma once -#endif - -#include - -namespace Test { -void RunAll(); - -namespace Parser { -// scalar tests -void SimpleScalar(std::string& inputScalar, std::string& desiredOutput); -void MultiLineScalar(std::string& inputScalar, std::string& desiredOutput); -void LiteralScalar(std::string& inputScalar, std::string& desiredOutput); -void FoldedScalar(std::string& inputScalar, std::string& desiredOutput); -void ChompedFoldedScalar(std::string& inputScalar, std::string& desiredOutput); -void ChompedLiteralScalar(std::string& inputScalar, std::string& desiredOutput); -void FoldedScalarWithIndent(std::string& inputScalar, - std::string& desiredOutput); -void ColonScalar(std::string& inputScalar, std::string& desiredOutput); -void QuotedScalar(std::string& inputScalar, std::string& desiredOutput); -void CommaScalar(std::string& inputScalar, std::string& desiredOutput); -void DashScalar(std::string& inputScalar, std::string& desiredOutput); -void URLScalar(std::string& inputScalar, std::string& desiredOutput); - -// misc tests -bool SimpleSeq(); -bool SimpleMap(); -bool FlowSeq(); -bool FlowMap(); -bool FlowMapWithOmittedKey(); -bool FlowMapWithOmittedValue(); -bool FlowMapWithSoloEntry(); -bool FlowMapEndingWithSoloEntry(); -bool QuotedSimpleKeys(); -bool CompressedMapAndSeq(); -bool NullBlockSeqEntry(); -bool NullBlockMapKey(); -bool NullBlockMapValue(); -bool SimpleAlias(); -bool AliasWithNull(); -bool AnchorInSimpleKey(); -bool AliasAsSimpleKey(); -bool ExplicitDoc(); -bool MultipleDocs(); -bool ExplicitEndDoc(); -bool MultipleDocsWithSomeExplicitIndicators(); -} -} - -#endif // TESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/test/teststruct.h b/test/teststruct.h deleted file mode 100644 index 60596cf..0000000 --- a/test/teststruct.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include - -#define YAML_ASSERT(cond) \ - do { \ - if (!(cond)) \ - return " Assert failed: " #cond; \ - } while (false) - -namespace Test { -struct TEST { - TEST() : ok(false) {} - TEST(bool ok_) : ok(ok_) {} - TEST(const char *error_) : ok(false), error(error_) {} - - bool ok; - std::string error; -}; -}