mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Compare commits
4 Commits
release-0.
...
release-0.
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5217149ed4 | ||
![]() |
e7ac6b3bf1 | ||
![]() |
9a1f4f9a0d | ||
![]() |
07443495c8 |
@@ -10,36 +10,34 @@
|
|||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
template <typename T>
|
inline bool Convert(const std::string& input, std::string& output) {
|
||||||
struct Converter {
|
|
||||||
static bool Convert(const std::string& input, T& output);
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
bool Convert(const std::string& input, T& output) {
|
|
||||||
return Converter<T>::Convert(input, output);
|
|
||||||
}
|
|
||||||
|
|
||||||
// this is the one to specialize
|
|
||||||
template <typename T>
|
|
||||||
inline bool Converter<T>::Convert(const std::string& input, T& output) {
|
|
||||||
std::stringstream stream(input);
|
|
||||||
stream >> output;
|
|
||||||
return !stream.fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
// specializations
|
|
||||||
template <>
|
|
||||||
inline bool Converter<std::string>::Convert(const std::string& input, std::string& output) {
|
|
||||||
output = input;
|
output = input;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
|
||||||
bool Converter<bool>::Convert(const std::string& input, bool& output);
|
|
||||||
|
|
||||||
template <>
|
bool Convert(const std::string& input, bool& output);
|
||||||
bool Converter<_Null>::Convert(const std::string& input, _Null& output);
|
bool Convert(const std::string& input, _Null& output);
|
||||||
|
|
||||||
|
#define YAML_MAKE_STREAM_CONVERT(type) \
|
||||||
|
inline bool Convert(const std::string& input, type& output) { \
|
||||||
|
std::stringstream stream(input); \
|
||||||
|
stream >> output; \
|
||||||
|
return !stream.fail(); \
|
||||||
|
}
|
||||||
|
|
||||||
|
YAML_MAKE_STREAM_CONVERT(char)
|
||||||
|
YAML_MAKE_STREAM_CONVERT(unsigned char)
|
||||||
|
YAML_MAKE_STREAM_CONVERT(int)
|
||||||
|
YAML_MAKE_STREAM_CONVERT(unsigned int)
|
||||||
|
YAML_MAKE_STREAM_CONVERT(short)
|
||||||
|
YAML_MAKE_STREAM_CONVERT(unsigned short)
|
||||||
|
YAML_MAKE_STREAM_CONVERT(long)
|
||||||
|
YAML_MAKE_STREAM_CONVERT(unsigned long)
|
||||||
|
YAML_MAKE_STREAM_CONVERT(float)
|
||||||
|
YAML_MAKE_STREAM_CONVERT(double)
|
||||||
|
YAML_MAKE_STREAM_CONVERT(long double)
|
||||||
|
|
||||||
|
#undef YAML_MAKE_STREAM_CONVERT
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
#endif // CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
@@ -53,6 +53,9 @@ namespace YAML
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const T Read() const;
|
const T Read() const;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
operator T() const;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
friend void operator >> (const Node& node, T& value);
|
friend void operator >> (const Node& node, T& value);
|
||||||
@@ -109,8 +112,27 @@ namespace YAML
|
|||||||
const Node *m_pIdentity;
|
const Node *m_pIdentity;
|
||||||
mutable bool m_referenced;
|
mutable bool m_referenced;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// comparisons with auto-conversion
|
||||||
|
template <typename T>
|
||||||
|
bool operator == (const T& value, const Node& node);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool operator == (const Node& node, const T& value);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool operator != (const T& value, const Node& node);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool operator != (const Node& node, const T& value);
|
||||||
|
|
||||||
|
bool operator == (const char *value, const Node& node);
|
||||||
|
bool operator == (const Node& node, const char *value);
|
||||||
|
bool operator != (const char *value, const Node& node);
|
||||||
|
bool operator != (const Node& node, const char *value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "nodeimpl.h"
|
#include "nodeimpl.h"
|
||||||
|
#include "nodereadimpl.h"
|
||||||
|
|
||||||
#endif // NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
#endif // NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
@@ -9,15 +9,6 @@
|
|||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
// implementation of templated things
|
// implementation of templated things
|
||||||
template <typename T>
|
|
||||||
inline bool Node::Read(T& value) const {
|
|
||||||
std::string scalar;
|
|
||||||
if(!GetScalar(scalar))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return Convert(scalar, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline const T Node::Read() const {
|
inline const T Node::Read() const {
|
||||||
T value;
|
T value;
|
||||||
@@ -25,9 +16,14 @@ namespace YAML
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Node::operator T() const {
|
||||||
|
return Read<T>();
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline void operator >> (const Node& node, T& value) {
|
inline void operator >> (const Node& node, T& value) {
|
||||||
if(!node.Read(value))
|
if(!ConvertScalar(node, value))
|
||||||
throw InvalidScalar(node.m_mark);
|
throw InvalidScalar(node.m_mark);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,6 +76,43 @@ namespace YAML
|
|||||||
inline const Node& Node::operator [] (const char *key) const {
|
inline const Node& Node::operator [] (const char *key) const {
|
||||||
return GetValue(std::string(key));
|
return GetValue(std::string(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline bool operator == (const T& value, const Node& node) {
|
||||||
|
return value == node.Read<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline bool operator == (const Node& node, const T& value) {
|
||||||
|
return value == node.Read<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline bool operator != (const T& value, const Node& node) {
|
||||||
|
return value != node.Read<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline bool operator != (const Node& node, const T& value) {
|
||||||
|
return value != node.Read<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool operator == (const char *value, const Node& node) {
|
||||||
|
return std::string(value) == node;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool operator == (const Node& node, const char *value) {
|
||||||
|
return std::string(value) == node;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool operator != (const char *value, const Node& node) {
|
||||||
|
return std::string(value) != node;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool operator != (const Node& node, const char *value) {
|
||||||
|
return std::string(value) != node;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // NODEIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
#endif // NODEIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
65
include/nodereadimpl.h
Normal file
65
include/nodereadimpl.h
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace YAML
|
||||||
|
{
|
||||||
|
// implementation for Node::Read
|
||||||
|
// (the goal is to call ConvertScalar if we can, and fall back to operator >> if not)
|
||||||
|
// thanks to litb from stackoverflow.com
|
||||||
|
// http://stackoverflow.com/questions/1386183/how-to-call-a-templated-function-if-it-exists-and-something-else-otherwise/1386390#1386390
|
||||||
|
|
||||||
|
template<bool>
|
||||||
|
struct read_impl;
|
||||||
|
|
||||||
|
// ConvertScalar available
|
||||||
|
template<>
|
||||||
|
struct read_impl<true> {
|
||||||
|
template<typename T>
|
||||||
|
static bool read(const Node& node, T& value) {
|
||||||
|
return ConvertScalar(node, value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ConvertScalar not available
|
||||||
|
template<>
|
||||||
|
struct read_impl<false> {
|
||||||
|
template<typename T>
|
||||||
|
static bool read(const Node& node, T& value) {
|
||||||
|
try {
|
||||||
|
node >> value;
|
||||||
|
} catch(const Exception&) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace fallback {
|
||||||
|
// sizeof > 1
|
||||||
|
struct flag { char c[2]; };
|
||||||
|
flag Convert(...);
|
||||||
|
|
||||||
|
char (& operator,(flag, flag) )[1];
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void operator,(flag, T const&);
|
||||||
|
|
||||||
|
char (& operator,(char(&)[1], flag) )[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline bool Node::Read(T& value) const {
|
||||||
|
using namespace fallback;
|
||||||
|
|
||||||
|
return read_impl<sizeof (fallback::flag(), Convert(std::string(), value), fallback::flag()) != 1>::read(*this, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// the main conversion function
|
||||||
|
template <typename T>
|
||||||
|
inline bool ConvertScalar(const Node& node, T& value) {
|
||||||
|
std::string scalar;
|
||||||
|
if(!node.GetScalar(scalar))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return Convert(scalar, value);
|
||||||
|
}
|
||||||
|
}
|
@@ -49,8 +49,7 @@ namespace
|
|||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
template <>
|
bool Convert(const std::string& input, bool& b)
|
||||||
bool Converter<bool>::Convert(const std::string& input, bool& b)
|
|
||||||
{
|
{
|
||||||
// we can't use iostream bool extraction operators as they don't
|
// we can't use iostream bool extraction operators as they don't
|
||||||
// recognize all possible values in the table below (taken from
|
// recognize all possible values in the table below (taken from
|
||||||
@@ -82,8 +81,7 @@ namespace YAML
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
bool Convert(const std::string& input, _Null& /*output*/)
|
||||||
bool Converter<_Null>::Convert(const std::string& input, _Null& /*output*/)
|
|
||||||
{
|
{
|
||||||
return input.empty() || input == "~" || input == "null" || input == "Null" || input == "NULL";
|
return input.empty() || input == "~" || input == "null" || input == "Null" || input == "NULL";
|
||||||
}
|
}
|
||||||
|
24
src/map.cpp
24
src/map.cpp
@@ -53,6 +53,11 @@ namespace YAML
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::size_t Map::GetSize() const
|
||||||
|
{
|
||||||
|
return m_data.size();
|
||||||
|
}
|
||||||
|
|
||||||
void Map::Parse(Scanner *pScanner, const ParserState& state)
|
void Map::Parse(Scanner *pScanner, const ParserState& state)
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
@@ -117,24 +122,21 @@ namespace YAML
|
|||||||
pScanner->pop();
|
pScanner->pop();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// now it better be a key
|
|
||||||
if(token.type != Token::KEY)
|
|
||||||
throw ParserException(token.mark, ErrorMsg::END_OF_MAP_FLOW);
|
|
||||||
|
|
||||||
pScanner->pop();
|
|
||||||
|
|
||||||
std::auto_ptr <Node> pKey(new Node), pValue(new Node);
|
std::auto_ptr <Node> pKey(new Node), pValue(new Node);
|
||||||
|
|
||||||
// grab key
|
// grab key (if non-null)
|
||||||
pKey->Parse(pScanner, state);
|
if(token.type == Token::KEY) {
|
||||||
|
pScanner->pop();
|
||||||
|
pKey->Parse(pScanner, state);
|
||||||
|
}
|
||||||
|
|
||||||
// now grab value (optional)
|
// now grab value (optional)
|
||||||
if(!pScanner->empty() && pScanner->peek().type == Token::VALUE) {
|
if(!pScanner->empty() && pScanner->peek().type == Token::VALUE) {
|
||||||
pScanner->pop();
|
pScanner->pop();
|
||||||
pValue->Parse(pScanner, state);
|
pValue->Parse(pScanner, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
// now eat the separator (or could be a map end, which we ignore - but if it's neither, then it's a bad node)
|
// now eat the separator (or could be a map end, which we ignore - but if it's neither, then it's a bad node)
|
||||||
Token& nextToken = pScanner->peek();
|
Token& nextToken = pScanner->peek();
|
||||||
if(nextToken.type == Token::FLOW_ENTRY)
|
if(nextToken.type == Token::FLOW_ENTRY)
|
||||||
|
@@ -26,6 +26,7 @@ namespace YAML
|
|||||||
|
|
||||||
virtual bool GetBegin(std::map <Node *, Node *, ltnode>::const_iterator& it) const;
|
virtual bool GetBegin(std::map <Node *, Node *, ltnode>::const_iterator& it) const;
|
||||||
virtual bool GetEnd(std::map <Node *, Node *, ltnode>::const_iterator& it) const;
|
virtual bool GetEnd(std::map <Node *, Node *, ltnode>::const_iterator& it) const;
|
||||||
|
virtual std::size_t GetSize() const;
|
||||||
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
||||||
virtual void Write(Emitter& out) const;
|
virtual void Write(Emitter& out) const;
|
||||||
|
|
||||||
|
@@ -182,9 +182,6 @@ namespace YAML
|
|||||||
// Value
|
// Value
|
||||||
void Scanner::ScanValue()
|
void Scanner::ScanValue()
|
||||||
{
|
{
|
||||||
// just in case we have an empty key
|
|
||||||
InsertPotentialSimpleKey();
|
|
||||||
|
|
||||||
// and check that simple key
|
// and check that simple key
|
||||||
bool isSimpleKey = VerifySimpleKey();
|
bool isSimpleKey = VerifySimpleKey();
|
||||||
|
|
||||||
|
@@ -5,76 +5,332 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
struct TEST {
|
||||||
|
TEST(): ok(false) {}
|
||||||
|
TEST(bool ok_): ok(ok_) {}
|
||||||
|
TEST(const char *error_): ok(false), error(error_) {}
|
||||||
|
|
||||||
|
bool ok;
|
||||||
|
std::string error;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#define YAML_ASSERT(cond) do { if(!(cond)) return "Assert failed: " #cond; } while(false)
|
||||||
|
|
||||||
namespace Test {
|
namespace Test {
|
||||||
namespace {
|
namespace {
|
||||||
void RunSpecTest(bool (*test)(), const std::string& index, const std::string& name, bool& passed) {
|
void RunSpecTest(TEST (*test)(), const std::string& index, const std::string& name, bool& passed) {
|
||||||
std::string error;
|
TEST ret;
|
||||||
bool ok = true;
|
|
||||||
try {
|
try {
|
||||||
ok = test();
|
ret = test();
|
||||||
} catch(const YAML::Exception& e) {
|
} catch(const YAML::Exception& e) {
|
||||||
ok = false;
|
ret.ok = false;
|
||||||
error = e.msg;
|
ret.error = e.msg;
|
||||||
}
|
}
|
||||||
if(ok) {
|
|
||||||
|
if(ret.ok) {
|
||||||
std::cout << "Spec test " << index << " passed: " << name << "\n";
|
std::cout << "Spec test " << index << " passed: " << name << "\n";
|
||||||
} else {
|
} else {
|
||||||
passed = false;
|
passed = false;
|
||||||
std::cout << "Spec test " << index << " failed: " << name << "\n";
|
std::cout << "Spec test " << index << " failed: " << name << "\n";
|
||||||
if(error != "")
|
std::cout << ret.error << "\n";
|
||||||
std::cout << "Caught exception: " << error << "\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace Spec {
|
namespace Spec {
|
||||||
bool SeqScalars() {
|
TEST SeqScalars() {
|
||||||
std::string input =
|
std::string input =
|
||||||
"- Mark McGwire\n"
|
"- Mark McGwire\n"
|
||||||
"- Sammy Sosa\n"
|
"- Sammy Sosa\n"
|
||||||
"- Ken Griffey";
|
"- Ken Griffey";
|
||||||
std::stringstream stream(input);
|
std::stringstream stream(input);
|
||||||
|
|
||||||
YAML::Parser parser(stream);
|
YAML::Parser parser(stream);
|
||||||
YAML::Node doc;
|
YAML::Node doc;
|
||||||
parser.GetNextDocument(doc);
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
if(doc.size() != 3)
|
YAML_ASSERT(doc.size() == 3);
|
||||||
return false;
|
YAML_ASSERT(doc[0] == "Mark McGwire");
|
||||||
std::string output;
|
YAML_ASSERT(doc[1] == "Sammy Sosa");
|
||||||
doc[0] >> output;
|
YAML_ASSERT(doc[2] == "Ken Griffey");
|
||||||
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MappingScalarsToScalars() {
|
TEST MappingScalarsToScalars() {
|
||||||
std::string input =
|
std::string input =
|
||||||
"hr: 65 # Home runs\n"
|
"hr: 65 # Home runs\n"
|
||||||
"avg: 0.278 # Batting average\n"
|
"avg: 0.278 # Batting average\n"
|
||||||
"rbi: 147 # Runs Batted In";
|
"rbi: 147 # Runs Batted In";
|
||||||
std::stringstream stream(input);
|
std::stringstream stream(input);
|
||||||
|
|
||||||
YAML::Parser parser(stream);
|
YAML::Parser parser(stream);
|
||||||
YAML::Node doc;
|
YAML::Node doc;
|
||||||
parser.GetNextDocument(doc);
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
std::string output;
|
YAML_ASSERT(doc.size() == 3);
|
||||||
doc["hr"] >> output;
|
YAML_ASSERT(doc["hr"] == "65");
|
||||||
if(output != "65")
|
YAML_ASSERT(doc["avg"] == "0.278");
|
||||||
return false;
|
YAML_ASSERT(doc["rbi"] == "147");
|
||||||
doc["avg"] >> output;
|
return true;
|
||||||
if(output != "0.278")
|
}
|
||||||
return false;
|
|
||||||
doc["rbi"] >> output;
|
TEST MappingScalarsToSequences() {
|
||||||
if(output != "147")
|
std::string input =
|
||||||
return false;
|
"american:\n"
|
||||||
|
"- Boston Red Sox\n"
|
||||||
|
"- Detroit Tigers\n"
|
||||||
|
"- New York Yankees\n"
|
||||||
|
"national:\n"
|
||||||
|
"- New York Mets\n"
|
||||||
|
"- Chicago Cubs\n"
|
||||||
|
"- Atlanta Braves";
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
YAML_ASSERT(doc.size() == 2);
|
||||||
|
YAML_ASSERT(doc["american"].size() == 3);
|
||||||
|
YAML_ASSERT(doc["american"][0] == "Boston Red Sox");
|
||||||
|
YAML_ASSERT(doc["american"][1] == "Detroit Tigers");
|
||||||
|
YAML_ASSERT(doc["american"][2] == "New York Yankees");
|
||||||
|
YAML_ASSERT(doc["national"].size() == 3);
|
||||||
|
YAML_ASSERT(doc["national"][0] == "New York Mets");
|
||||||
|
YAML_ASSERT(doc["national"][1] == "Chicago Cubs");
|
||||||
|
YAML_ASSERT(doc["national"][2] == "Atlanta Braves");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST SequenceOfMappings()
|
||||||
|
{
|
||||||
|
std::string input =
|
||||||
|
"-\n"
|
||||||
|
" name: Mark McGwire\n"
|
||||||
|
" hr: 65\n"
|
||||||
|
" avg: 0.278\n"
|
||||||
|
"-\n"
|
||||||
|
" name: Sammy Sosa\n"
|
||||||
|
" hr: 63\n"
|
||||||
|
" avg: 0.288";
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
YAML_ASSERT(doc.size() == 2);
|
||||||
|
YAML_ASSERT(doc[0].size() == 3);
|
||||||
|
YAML_ASSERT(doc[0]["name"] == "Mark McGwire");
|
||||||
|
YAML_ASSERT(doc[0]["hr"] == "65");
|
||||||
|
YAML_ASSERT(doc[0]["avg"] == "0.278");
|
||||||
|
YAML_ASSERT(doc[1].size() == 3);
|
||||||
|
YAML_ASSERT(doc[1]["name"] == "Sammy Sosa");
|
||||||
|
YAML_ASSERT(doc[1]["hr"] == "63");
|
||||||
|
YAML_ASSERT(doc[1]["avg"] == "0.288");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST SequenceOfSequences()
|
||||||
|
{
|
||||||
|
std::string input =
|
||||||
|
"- [name , hr, avg ]\n"
|
||||||
|
"- [Mark McGwire, 65, 0.278]\n"
|
||||||
|
"- [Sammy Sosa , 63, 0.288]";
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
YAML_ASSERT(doc.size() == 3);
|
||||||
|
YAML_ASSERT(doc[0].size() == 3);
|
||||||
|
YAML_ASSERT(doc[0][0] == "name");
|
||||||
|
YAML_ASSERT(doc[0][1] == "hr");
|
||||||
|
YAML_ASSERT(doc[0][2] == "avg");
|
||||||
|
YAML_ASSERT(doc[1].size() == 3);
|
||||||
|
YAML_ASSERT(doc[1][0] == "Mark McGwire");
|
||||||
|
YAML_ASSERT(doc[1][1] == "65");
|
||||||
|
YAML_ASSERT(doc[1][2] == "0.278");
|
||||||
|
YAML_ASSERT(doc[2].size() == 3);
|
||||||
|
YAML_ASSERT(doc[2][0] == "Sammy Sosa");
|
||||||
|
YAML_ASSERT(doc[2][1] == "63");
|
||||||
|
YAML_ASSERT(doc[2][2] == "0.288");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST MappingOfMappings()
|
||||||
|
{
|
||||||
|
std::string input =
|
||||||
|
"Mark McGwire: {hr: 65, avg: 0.278}\n"
|
||||||
|
"Sammy Sosa: {\n"
|
||||||
|
" hr: 63,\n"
|
||||||
|
" avg: 0.288\n"
|
||||||
|
" }";
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
YAML_ASSERT(doc.size() == 2);
|
||||||
|
YAML_ASSERT(doc["Mark McGwire"].size() == 2);
|
||||||
|
YAML_ASSERT(doc["Mark McGwire"]["hr"] == "65");
|
||||||
|
YAML_ASSERT(doc["Mark McGwire"]["avg"] == "0.278");
|
||||||
|
YAML_ASSERT(doc["Sammy Sosa"].size() == 2);
|
||||||
|
YAML_ASSERT(doc["Sammy Sosa"]["hr"] == "63");
|
||||||
|
YAML_ASSERT(doc["Sammy Sosa"]["avg"] == "0.288");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST TwoDocumentsInAStream()
|
||||||
|
{
|
||||||
|
std::string input =
|
||||||
|
"# Ranking of 1998 home runs\n"
|
||||||
|
"---\n"
|
||||||
|
"- Mark McGwire\n"
|
||||||
|
"- Sammy Sosa\n"
|
||||||
|
"- Ken Griffey\n"
|
||||||
|
"\n"
|
||||||
|
"# Team ranking\n"
|
||||||
|
"---\n"
|
||||||
|
"- Chicago Cubs\n"
|
||||||
|
"- St Louis Cardinals";
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
YAML_ASSERT(doc.size() == 3);
|
||||||
|
YAML_ASSERT(doc[0] == "Mark McGwire");
|
||||||
|
YAML_ASSERT(doc[1] == "Sammy Sosa");
|
||||||
|
YAML_ASSERT(doc[2] == "Ken Griffey");
|
||||||
|
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
YAML_ASSERT(doc.size() == 2);
|
||||||
|
YAML_ASSERT(doc[0] == "Chicago Cubs");
|
||||||
|
YAML_ASSERT(doc[1] == "St Louis Cardinals");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST PlayByPlayFeed()
|
||||||
|
{
|
||||||
|
std::string input =
|
||||||
|
"---\n"
|
||||||
|
"time: 20:03:20\n"
|
||||||
|
"player: Sammy Sosa\n"
|
||||||
|
"action: strike (miss)\n"
|
||||||
|
"...\n"
|
||||||
|
"---\n"
|
||||||
|
"time: 20:03:47\n"
|
||||||
|
"player: Sammy Sosa\n"
|
||||||
|
"action: grand slam\n"
|
||||||
|
"...";
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
YAML_ASSERT(doc.size() == 3);
|
||||||
|
YAML_ASSERT(doc["time"] == "20:03:20");
|
||||||
|
YAML_ASSERT(doc["player"] == "Sammy Sosa");
|
||||||
|
YAML_ASSERT(doc["action"] == "strike (miss)");
|
||||||
|
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
YAML_ASSERT(doc.size() == 3);
|
||||||
|
YAML_ASSERT(doc["time"] == "20:03:47");
|
||||||
|
YAML_ASSERT(doc["player"] == "Sammy Sosa");
|
||||||
|
YAML_ASSERT(doc["action"] == "grand slam");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST SingleDocumentWithTwoComments()
|
||||||
|
{
|
||||||
|
std::string input =
|
||||||
|
"---\n"
|
||||||
|
"hr: # 1998 hr ranking\n"
|
||||||
|
" - Mark McGwire\n"
|
||||||
|
" - Sammy Sosa\n"
|
||||||
|
"rbi:\n"
|
||||||
|
" # 1998 rbi ranking\n"
|
||||||
|
" - Sammy Sosa\n"
|
||||||
|
" - Ken Griffey";
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
YAML_ASSERT(doc.size() == 2);
|
||||||
|
YAML_ASSERT(doc["hr"].size() == 2);
|
||||||
|
YAML_ASSERT(doc["hr"][0] == "Mark McGwire");
|
||||||
|
YAML_ASSERT(doc["hr"][1] == "Sammy Sosa");
|
||||||
|
YAML_ASSERT(doc["rbi"].size() == 2);
|
||||||
|
YAML_ASSERT(doc["rbi"][0] == "Sammy Sosa");
|
||||||
|
YAML_ASSERT(doc["rbi"][1] == "Ken Griffey");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST SimpleAnchor()
|
||||||
|
{
|
||||||
|
std::string input =
|
||||||
|
"---\n"
|
||||||
|
"hr:\n"
|
||||||
|
" - Mark McGwire\n"
|
||||||
|
" # Following node labeled SS\n"
|
||||||
|
" - &SS Sammy Sosa\n"
|
||||||
|
"rbi:\n"
|
||||||
|
" - *SS # Subsequent occurrence\n"
|
||||||
|
" - Ken Griffey";
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
YAML_ASSERT(doc.size() == 2);
|
||||||
|
YAML_ASSERT(doc["hr"].size() == 2);
|
||||||
|
YAML_ASSERT(doc["hr"][0] == "Mark McGwire");
|
||||||
|
YAML_ASSERT(doc["hr"][1] == "Sammy Sosa");
|
||||||
|
YAML_ASSERT(doc["rbi"].size() == 2);
|
||||||
|
YAML_ASSERT(doc["rbi"][0] == "Sammy Sosa");
|
||||||
|
YAML_ASSERT(doc["rbi"][1] == "Ken Griffey");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Pair {
|
||||||
|
Pair() {}
|
||||||
|
Pair(const std::string& f, const std::string& s): first(f), second(s) {}
|
||||||
|
std::string first, second;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool operator == (const Pair& p, const Pair& q) {
|
||||||
|
return p.first == q.first && p.second == q.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator >> (const YAML::Node& node, Pair& p) {
|
||||||
|
node[0] >> p.first;
|
||||||
|
node[1] >> p.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST MappingBetweenSequences()
|
||||||
|
{
|
||||||
|
std::string input =
|
||||||
|
"? - Detroit Tigers\n"
|
||||||
|
" - Chicago cubs\n"
|
||||||
|
":\n"
|
||||||
|
" - 2001-07-23\n"
|
||||||
|
"\n"
|
||||||
|
"? [ New York Yankees,\n"
|
||||||
|
" Atlanta Braves ]\n"
|
||||||
|
": [ 2001-07-02, 2001-08-12,\n"
|
||||||
|
" 2001-08-14 ]";
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
YAML_ASSERT(doc.size() == 2);
|
||||||
|
YAML_ASSERT(doc[Pair("Detroit Tigers", "Chicago cubs")].size() == 1);
|
||||||
|
YAML_ASSERT(doc[Pair("Detroit Tigers", "Chicago cubs")][0] == "2001-07-23");
|
||||||
|
YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")].size() == 3);
|
||||||
|
YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")][0] == "2001-07-02");
|
||||||
|
YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")][1] == "2001-08-12");
|
||||||
|
YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")][2] == "2001-08-14");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,6 +340,15 @@ namespace Test {
|
|||||||
bool passed = true;
|
bool passed = true;
|
||||||
RunSpecTest(&Spec::SeqScalars, "2.1", "Sequence of Scalars", passed);
|
RunSpecTest(&Spec::SeqScalars, "2.1", "Sequence of Scalars", passed);
|
||||||
RunSpecTest(&Spec::MappingScalarsToScalars, "2.2", "Mapping Scalars to Scalars", passed);
|
RunSpecTest(&Spec::MappingScalarsToScalars, "2.2", "Mapping Scalars to Scalars", passed);
|
||||||
|
RunSpecTest(&Spec::MappingScalarsToSequences, "2.3", "Mapping Scalars to Sequences", passed);
|
||||||
|
RunSpecTest(&Spec::SequenceOfMappings, "2.4", "Sequence of Mappings", passed);
|
||||||
|
RunSpecTest(&Spec::SequenceOfSequences, "2.5", "Sequence of Sequences", passed);
|
||||||
|
RunSpecTest(&Spec::MappingOfMappings, "2.6", "Mapping of Mappings", passed);
|
||||||
|
RunSpecTest(&Spec::TwoDocumentsInAStream, "2.7", "Two Documents in a Stream", passed);
|
||||||
|
RunSpecTest(&Spec::PlayByPlayFeed, "2.8", "Play by Play Feed from a Game", passed);
|
||||||
|
RunSpecTest(&Spec::SingleDocumentWithTwoComments, "2.9", "Single Document with Two Comments", passed);
|
||||||
|
RunSpecTest(&Spec::SimpleAnchor, "2.10", "Node for \"Sammy Sosa\" appears twice in this document", passed);
|
||||||
|
RunSpecTest(&Spec::MappingBetweenSequences, "2.11", "Mapping between Sequences", passed);
|
||||||
return passed;
|
return passed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -14,12 +14,12 @@ namespace Test
|
|||||||
if(!RunParserTests())
|
if(!RunParserTests())
|
||||||
passed = false;
|
passed = false;
|
||||||
|
|
||||||
if(!RunSpecTests())
|
|
||||||
passed = false;
|
|
||||||
|
|
||||||
if(!RunEmitterTests())
|
if(!RunEmitterTests())
|
||||||
passed = false;
|
passed = false;
|
||||||
|
|
||||||
|
if(!RunSpecTests())
|
||||||
|
passed = false;
|
||||||
|
|
||||||
if(passed)
|
if(passed)
|
||||||
std::cout << "All tests passed!\n";
|
std::cout << "All tests passed!\n";
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user