diff --git a/yaml-reader/spectests.cpp b/yaml-reader/spectests.cpp new file mode 100644 index 0000000..3169b43 --- /dev/null +++ b/yaml-reader/spectests.cpp @@ -0,0 +1,91 @@ +#include "spectests.h" +#include "yaml.h" +#include +#include +#include +#include + +namespace Test { + namespace { + void RunSpecTest(bool (*test)(), const std::string& index, const std::string& name, bool& passed) { + std::string error; + bool ok = true; + try { + ok = test(); + } catch(const YAML::Exception& e) { + ok = false; + error = e.msg; + } + if(ok) { + std::cout << "Spec test " << index << " passed: " << name << "\n"; + } else { + passed = false; + std::cout << "Spec test " << index << " failed: " << name << "\n"; + if(error != "") + std::cout << "Caught exception: " << error << "\n"; + } + } + } + + namespace Spec { + bool SeqScalars() { + std::string input = + "- Mark McGwire\n" + "- Sammy Sosa\n" + "- Ken Griffey"; + std::stringstream stream(input); + + YAML::Parser parser(stream); + YAML::Node doc; + parser.GetNextDocument(doc); + + if(doc.size() != 3) + return false; + std::string output; + doc[0] >> output; + if(output != "Mark McGwire") + return false; + doc[1] >> output; + if(output != "Sammy Sosa") + return false; + doc[2] >> output; + if(output != "Ken Griffey") + return false; + return true; + } + + bool MappingScalarsToScalars() { + std::string input = + "hr: 65 # Home runs\n" + "avg: 0.278 # Batting average\n" + "rbi: 147 # Runs Batted In"; + std::stringstream stream(input); + + YAML::Parser parser(stream); + YAML::Node doc; + parser.GetNextDocument(doc); + + std::string output; + doc["hr"] >> output; + if(output != "65") + return false; + doc["avg"] >> output; + if(output != "0.278") + return false; + doc["rbi"] >> output; + if(output != "147") + return false; + return true; + } + } + + bool RunSpecTests() + { + bool passed = true; + RunSpecTest(&Spec::SeqScalars, "2.1", "Sequence of Scalars", passed); + RunSpecTest(&Spec::MappingScalarsToScalars, "2.2", "Mapping Scalars to Scalars", passed); + return passed; + } + +} + diff --git a/yaml-reader/spectests.h b/yaml-reader/spectests.h new file mode 100644 index 0000000..fcb1fb4 --- /dev/null +++ b/yaml-reader/spectests.h @@ -0,0 +1,11 @@ +#pragma once + +#ifndef SPECTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define SPECTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +namespace Test { + bool RunSpecTests(); +} + +#endif // SPECTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + diff --git a/yaml-reader/tests.cpp b/yaml-reader/tests.cpp index 89d7c20..8bf33ba 100644 --- a/yaml-reader/tests.cpp +++ b/yaml-reader/tests.cpp @@ -1,5 +1,6 @@ -#include "yaml.h" #include "tests.h" +#include "spectests.h" +#include "yaml.h" #include #include #include @@ -12,6 +13,9 @@ namespace Test bool passed = true; if(!RunParserTests()) passed = false; + + if(!RunSpecTests()) + passed = false; if(!RunEmitterTests()) passed = false;