From ef892a15b766ae34c6079805f6192e58a6eb149c Mon Sep 17 00:00:00 2001 From: beder Date: Tue, 13 Sep 2011 01:23:30 -0500 Subject: [PATCH] Copied over all the 2.x tests that are (a) single doc and (b) don't have tags --- test/new-api/spectests.cpp | 205 +++++++++++++++++++++++++++++++++---- test/specexamples.h | 7 +- 2 files changed, 190 insertions(+), 22 deletions(-) diff --git a/test/new-api/spectests.cpp b/test/new-api/spectests.cpp index f1112be..9fe42ff 100644 --- a/test/new-api/spectests.cpp +++ b/test/new-api/spectests.cpp @@ -1,62 +1,231 @@ #include "spectests.h" +#include "specexamples.h" +#include "yaml-cpp/yaml.h" + +#define YAML_ASSERT(cond) do { if(!(cond)) return " Assert failed: " #cond; } while(false) namespace Test { namespace Spec { // 2.1 - TEST SeqScalars() { return " not written yet"; } + TEST SeqScalars() { + YAML::Node doc = YAML::Parse(ex2_1); + YAML_ASSERT(doc.Type() == YAML::NodeType::Sequence); + YAML_ASSERT(doc.size() == 3); + YAML_ASSERT(doc[0].as() == "Mark McGwire"); + YAML_ASSERT(doc[1].as() == "Sammy Sosa"); + YAML_ASSERT(doc[2].as() == "Ken Griffey"); + return true; + } // 2.2 - TEST MappingScalarsToScalars() { return " not written yet"; } + TEST MappingScalarsToScalars() { + YAML::Node doc = YAML::Parse(ex2_2); + YAML_ASSERT(doc.Type() == YAML::NodeType::Map); + YAML_ASSERT(doc.size() == 3); + YAML_ASSERT(doc["hr"].as() == "65"); + YAML_ASSERT(doc["avg"].as() == "0.278"); + YAML_ASSERT(doc["rbi"].as() == "147"); + return true; + } // 2.3 - TEST MappingScalarsToSequences() { return " not written yet"; } + TEST MappingScalarsToSequences() { + YAML::Node doc = YAML::Parse(ex2_3); + YAML_ASSERT(doc.Type() == YAML::NodeType::Map); + YAML_ASSERT(doc.size() == 2); + YAML_ASSERT(doc["american"].size() == 3); + YAML_ASSERT(doc["american"][0].as() == "Boston Red Sox"); + YAML_ASSERT(doc["american"][1].as() == "Detroit Tigers"); + YAML_ASSERT(doc["american"][2].as() == "New York Yankees"); + YAML_ASSERT(doc["national"].size() == 3); + YAML_ASSERT(doc["national"][0].as() == "New York Mets"); + YAML_ASSERT(doc["national"][1].as() == "Chicago Cubs"); + YAML_ASSERT(doc["national"][2].as() == "Atlanta Braves"); + return true; + } // 2.4 - TEST SequenceOfMappings() { return " not written yet"; } + TEST SequenceOfMappings() { + YAML::Node doc = YAML::Parse(ex2_4); + YAML_ASSERT(doc.size() == 2); + YAML_ASSERT(doc[0].size() == 3); + YAML_ASSERT(doc[0]["name"].as() == "Mark McGwire"); + YAML_ASSERT(doc[0]["hr"].as() == "65"); + YAML_ASSERT(doc[0]["avg"].as() == "0.278"); + YAML_ASSERT(doc[1].size() == 3); + YAML_ASSERT(doc[1]["name"].as() == "Sammy Sosa"); + YAML_ASSERT(doc[1]["hr"].as() == "63"); + YAML_ASSERT(doc[1]["avg"].as() == "0.288"); + return true; + } // 2.5 - TEST SequenceOfSequences() { return " not written yet"; } + TEST SequenceOfSequences() { + YAML::Node doc = YAML::Parse(ex2_5); + YAML_ASSERT(doc.size() == 3); + YAML_ASSERT(doc[0].size() == 3); + YAML_ASSERT(doc[0][0].as() == "name"); + YAML_ASSERT(doc[0][1].as() == "hr"); + YAML_ASSERT(doc[0][2].as() == "avg"); + YAML_ASSERT(doc[1].size() == 3); + YAML_ASSERT(doc[1][0].as() == "Mark McGwire"); + YAML_ASSERT(doc[1][1].as() == "65"); + YAML_ASSERT(doc[1][2].as() == "0.278"); + YAML_ASSERT(doc[2].size() == 3); + YAML_ASSERT(doc[2][0].as() == "Sammy Sosa"); + YAML_ASSERT(doc[2][1].as() == "63"); + YAML_ASSERT(doc[2][2].as() == "0.288"); + return true; + } // 2.6 - TEST MappingOfMappings() { return " not written yet"; } + TEST MappingOfMappings() { + YAML::Node doc = YAML::Parse(ex2_6); + YAML_ASSERT(doc.size() == 2); + YAML_ASSERT(doc["Mark McGwire"].size() == 2); + YAML_ASSERT(doc["Mark McGwire"]["hr"].as() == "65"); + YAML_ASSERT(doc["Mark McGwire"]["avg"].as() == "0.278"); + YAML_ASSERT(doc["Sammy Sosa"].size() == 2); + YAML_ASSERT(doc["Sammy Sosa"]["hr"].as() == "63"); + YAML_ASSERT(doc["Sammy Sosa"]["avg"].as() == "0.288"); + return true; + } // 2.7 - TEST TwoDocumentsInAStream() { return " not written yet"; } + TEST TwoDocumentsInAStream() { + return " not written yet"; + } // 2.8 - TEST PlayByPlayFeed() { return " not written yet"; } + TEST PlayByPlayFeed() { + return " not written yet"; + } // 2.9 - TEST SingleDocumentWithTwoComments() { return " not written yet"; } + TEST SingleDocumentWithTwoComments() { + YAML::Node doc = YAML::Parse(ex2_9); + YAML_ASSERT(doc.size() == 2); + YAML_ASSERT(doc["hr"].size() == 2); + YAML_ASSERT(doc["hr"][0].as() == "Mark McGwire"); + YAML_ASSERT(doc["hr"][1].as() == "Sammy Sosa"); + YAML_ASSERT(doc["rbi"].size() == 2); + YAML_ASSERT(doc["rbi"][0].as() == "Sammy Sosa"); + YAML_ASSERT(doc["rbi"][1].as() == "Ken Griffey"); + return true; + } // 2.10 - TEST SimpleAnchor() { return " not written yet"; } + TEST SimpleAnchor() { + YAML::Node doc = YAML::Parse(ex2_10); + YAML_ASSERT(doc.size() == 2); + YAML_ASSERT(doc["hr"].size() == 2); + YAML_ASSERT(doc["hr"][0].as() == "Mark McGwire"); + YAML_ASSERT(doc["hr"][1].as() == "Sammy Sosa"); + YAML_ASSERT(doc["rbi"].size() == 2); + YAML_ASSERT(doc["rbi"][0].as() == "Sammy Sosa"); + YAML_ASSERT(doc["rbi"][1].as() == "Ken Griffey"); + return true; + } // 2.11 - TEST MappingBetweenSequences() { return " not written yet"; } + TEST MappingBetweenSequences() { + YAML::Node doc = YAML::Parse(ex2_11); + + std::vector tigers_cubs; + tigers_cubs.push_back("Detroit Tigers"); + tigers_cubs.push_back("Chicago cubs"); + + std::vector yankees_braves; + yankees_braves.push_back("New York Yankees"); + yankees_braves.push_back("Atlanta Braves"); + + + YAML_ASSERT(doc.size() == 2); + YAML_ASSERT(doc[tigers_cubs].size() == 1); + YAML_ASSERT(doc[tigers_cubs][0].as() == "2001-07-23"); + YAML_ASSERT(doc[yankees_braves].size() == 3); + YAML_ASSERT(doc[yankees_braves][0].as() == "2001-07-02"); + YAML_ASSERT(doc[yankees_braves][1].as() == "2001-08-12"); + YAML_ASSERT(doc[yankees_braves][2].as() == "2001-08-14"); + return true; + } // 2.12 - TEST CompactNestedMapping() { return " not written yet"; } + TEST CompactNestedMapping() { + YAML::Node doc = YAML::Parse(ex2_12); + YAML_ASSERT(doc.size() == 3); + YAML_ASSERT(doc[0].size() == 2); + YAML_ASSERT(doc[0]["item"].as() == "Super Hoop"); + YAML_ASSERT(doc[0]["quantity"].as() == 1); + YAML_ASSERT(doc[1].size() == 2); + YAML_ASSERT(doc[1]["item"].as() == "Basketball"); + YAML_ASSERT(doc[1]["quantity"].as() == 4); + YAML_ASSERT(doc[2].size() == 2); + YAML_ASSERT(doc[2]["item"].as() == "Big Shoes"); + YAML_ASSERT(doc[2]["quantity"].as() == 1); + return true; + } // 2.13 - TEST InLiteralsNewlinesArePreserved() { return " not written yet"; } + TEST InLiteralsNewlinesArePreserved() { + YAML::Node doc = YAML::Parse(ex2_13); + YAML_ASSERT(doc.as() == + "\\//||\\/||\n" + "// || ||__"); + return true; + } // 2.14 - TEST InFoldedScalarsNewlinesBecomeSpaces() { return " not written yet"; } + TEST InFoldedScalarsNewlinesBecomeSpaces() { + YAML::Node doc = YAML::Parse(ex2_14); + YAML_ASSERT(doc.as() == "Mark McGwire's year was crippled by a knee injury."); + return true; + } // 2.15 - TEST FoldedNewlinesArePreservedForMoreIndentedAndBlankLines() { return " not written yet"; } + TEST FoldedNewlinesArePreservedForMoreIndentedAndBlankLines() { + YAML::Node doc = YAML::Parse(ex2_15); + YAML_ASSERT(doc.as() == + "Sammy Sosa completed another fine season with great stats.\n\n" + " 63 Home Runs\n" + " 0.288 Batting Average\n\n" + "What a year!"); + return true; + } // 2.16 - TEST IndentationDeterminesScope() { return " not written yet"; } + TEST IndentationDeterminesScope() { + YAML::Node doc = YAML::Parse(ex2_16); + YAML_ASSERT(doc.size() == 3); + YAML_ASSERT(doc["name"].as() == "Mark McGwire"); + YAML_ASSERT(doc["accomplishment"].as() == "Mark set a major league home run record in 1998.\n"); + YAML_ASSERT(doc["stats"].as() == "65 Home Runs\n0.278 Batting Average\n"); + return true; + } // 2.17 - TEST QuotedScalars() { return " not written yet"; } + TEST QuotedScalars() { + YAML::Node doc = YAML::Parse(ex2_17); + YAML_ASSERT(doc.size() == 6); + YAML_ASSERT(doc["unicode"].as() == "Sosa did fine.\xe2\x98\xba"); + YAML_ASSERT(doc["control"].as() == "\b1998\t1999\t2000\n"); + YAML_ASSERT(doc["hex esc"].as() == "\x0d\x0a is \r\n"); + YAML_ASSERT(doc["single"].as() == "\"Howdy!\" he cried."); + YAML_ASSERT(doc["quoted"].as() == " # Not a 'comment'."); + YAML_ASSERT(doc["tie-fighter"].as() == "|\\-*-/|"); + return true; + } // 2.18 - TEST MultiLineFlowScalars() { return " not written yet"; } + TEST MultiLineFlowScalars() { + YAML::Node doc = YAML::Parse(ex2_18); + YAML_ASSERT(doc.size() == 2); + YAML_ASSERT(doc["plain"].as() == "This unquoted scalar spans many lines."); + YAML_ASSERT(doc["quoted"].as() == "So does this quoted scalar.\n"); + return true; + } // TODO: 2.19 - 2.22 schema tags diff --git a/test/specexamples.h b/test/specexamples.h index b7abee1..3ce7d33 100644 --- a/test/specexamples.h +++ b/test/specexamples.h @@ -441,7 +441,6 @@ namespace Test { "!yaml!str \"foo\""; const char *ex6_17 = - std::string input = "%TAG ! !foo\n" "%TAG ! !foo\n" "bar"; @@ -687,17 +686,17 @@ namespace Test { " \t\n" " detected\n"; - const char *ex8_3_a = + const char *ex8_3a = "- |\n" " \n" " text"; - const char *ex8_3_b = + const char *ex8_3b = "- >\n" " text\n" " text"; - const char *ex8_3_b = + const char *ex8_3c = "- |2\n" " text";