Compare commits

...

35 Commits

Author SHA1 Message Date
Jesse Beder
beb524489c Small refactoring 2009-10-22 21:51:32 +00:00
Jesse Beder
4ffb93c12b Switch to flow map when emitting an empty block map 2009-10-22 14:21:12 +00:00
Jesse Beder
ae06a40fe6 Switch to flow sequence when emitting an empty sequence 2009-10-22 14:17:12 +00:00
Jesse Beder
315b00065b Fixed bug in plain scalar folding 2009-10-20 14:47:16 +00:00
Jesse Beder
6f02f7556e Added a bunch of tests, simplified the testing code 2009-10-20 14:43:24 +00:00
Jesse Beder
fa0af88dfe Merged r270:HEAD of the emitting-unicode branch 2009-10-19 23:31:11 +00:00
Jesse Beder
bce845bb1f Fixed little bug in parser commit 2009-10-19 22:42:30 +00:00
Jesse Beder
ed570b9f7c Added default constructor to Parser, and cleaned it up a bit 2009-10-19 22:40:46 +00:00
Jesse Beder
59b0e986bf Update CMakeLists.txt to append, not overwrite CMAKE_CXX_FLAGS 2009-10-19 22:32:26 +00:00
Jesse Beder
cffb98d15b Patched for optional building of tests and tools 2009-10-12 05:21:00 +00:00
Jesse Beder
3e1ba0f3b4 Refactored the UTF-8 emitting 2009-10-08 21:05:56 +00:00
Jesse Beder
d0b5bf4b7b Fixed the emitter unicode output 2009-10-07 06:46:05 +00:00
Jesse Beder
7db39e66b8 Updated signature of Parser::GetNextDocument (issue 45) 2009-09-29 18:25:11 +00:00
Jesse Beder
94eb7f1dbd Modified old gcc version patch so it still uses the new Node::Read in Visual Studio. Also broke up the \uNNNN characters in the spec tests into \xNN-type strings. 2009-09-16 05:31:28 +00:00
Jesse Beder
5733b77b84 Patched for gcc version <= 3.3 (just fall back to original version of Node::Read) 2009-09-16 04:01:40 +00:00
Jesse Beder
98bebfb628 Tagged version 0.2.2 2009-09-09 01:37:23 +00:00
Jesse Beder
7c4cc9bf5f Cleaned up the read template overloads (per litb's update); it seems the old version didn't compile in VS2008. Also updated the VS project files. 2009-09-08 20:57:18 +00:00
Jesse Beder
fa885d1813 More tests, found bug in implicit keys in flow sequence 2009-09-08 05:35:39 +00:00
Jesse Beder
da4614eb8b Fixed flow folding, and made the separation slightly cleaner (but the whole scanscalar thing could use a major refactoring) 2009-09-08 05:24:06 +00:00
Jesse Beder
4dcd222d1f Tests through 6.29, skipping directives and tags 2009-09-08 04:16:45 +00:00
Jesse Beder
7bdd31b34b (Actually) fixed the folding newline bug, but it's a bit messy, and we don't accurately make the distinction between block folding and flow folding 2009-09-07 23:29:04 +00:00
Jesse Beder
a4b8521efe Fixed newlines in folded scalars bug 2009-09-07 22:48:32 +00:00
Jesse Beder
7037562998 Simplified testing output 2009-09-07 22:17:02 +00:00
Jesse Beder
f3ff6ffc55 Fixed bugs with tab as non-content whitespace 2009-09-07 17:12:45 +00:00
Jesse Beder
e3ff87ecde Fixed bugs in escape characters (both parsing and emitting) 2009-09-07 16:31:23 +00:00
Jesse Beder
45ac700fff Fixed error in test 2009-09-07 06:56:05 +00:00
Jesse Beder
2aab5acab4 Fixed last newline of folded scalar bug 2009-09-07 06:54:38 +00:00
Jesse Beder
e9d760eea9 Fixed bug in trailing newlines of plain scalars 2009-09-07 06:42:03 +00:00
Jesse Beder
d485d0a834 Added spec tests (minus tags, directives, and BOM) up through example 5.12 - this exposed an error in line folding 2009-09-07 06:35:37 +00:00
Jesse Beder
973ac4b3bd Added spec tests through example 2.13 2009-09-06 22:17:53 +00:00
Jesse Beder
e91a152e06 Tagged release 0.2.1 for patch with complex keys 2009-09-06 22:02:59 +00:00
Jesse Beder
5217149ed4 Fixed bug with complex keys (and simplified the parsing for flow maps) 2009-09-06 21:52:56 +00:00
Jesse Beder
e7ac6b3bf1 Refactored the operator >> and Node::Read default functions, as well as the conversion functions, to more easily read new types as keys (this uncovered an error, in example 2.11 of the spec) 2009-09-06 20:52:45 +00:00
Jesse Beder
9a1f4f9a0d Added spec tests through 2.10 2009-09-06 17:02:24 +00:00
Jesse Beder
07443495c8 Added templated casting to nodes, as well as operator == and != (for quick checks, especially to help in testing). Implemented size() on a map node to return the number of key/value pairs (as in std::map) 2009-09-06 15:54:11 +00:00
33 changed files with 2234 additions and 649 deletions

View File

@@ -10,16 +10,18 @@ if(IPHONE)
endif(IPHONE)
if(CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_CXX_FLAGS "-O2 -Wall -pedantic -Wextra")
set(CMAKE_CXX_FLAGS "-O2 -Wall -pedantic -Wextra ${CMAKE_CXX_FLAGS}")
endif(CMAKE_COMPILER_IS_GNUCC)
set(YAML_CPP_VERSION_MAJOR "0")
set(YAML_CPP_VERSION_MINOR "2")
set(YAML_CPP_VERSION_PATCH "0")
set(YAML_CPP_VERSION_PATCH "2")
set(YAML_CPP_VERSION "${YAML_CPP_VERSION_MAJOR}.${YAML_CPP_VERSION_MINOR}.${YAML_CPP_VERSION_PATCH}")
enable_testing()
option(YAML_CPP_BUILD_TOOLS "Enables or disables yaml-reader and parse tools" true)
if(WIN32)
set(_library_dir bin) # .dll are in PATH, like executables
else(WIN32)
@@ -64,5 +66,7 @@ if(UNIX)
install(FILES ${PC_FILE} DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
endif(UNIX)
add_subdirectory (yaml-reader)
add_subdirectory (util)
if(YAML_CPP_BUILD_TOOLS)
add_subdirectory (yaml-reader)
add_subdirectory (util)
endif(YAML_CPP_BUILD_TOOLS)

View File

@@ -10,36 +10,34 @@
namespace YAML
{
template <typename T>
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) {
inline bool Convert(const std::string& input, std::string& output) {
output = input;
return true;
}
template <>
bool Converter<bool>::Convert(const std::string& input, bool& output);
template <>
bool Converter<_Null>::Convert(const std::string& input, _Null& output);
bool Convert(const std::string& input, bool& 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

View File

@@ -29,6 +29,7 @@ namespace YAML
const std::string GetLastError() const;
// global setters
bool SetOutputCharset(EMITTER_MANIP value);
bool SetStringFormat(EMITTER_MANIP value);
bool SetBoolFormat(EMITTER_MANIP value);
bool SetIntBase(EMITTER_MANIP value);

View File

@@ -11,6 +11,10 @@ namespace YAML
enum EMITTER_MANIP {
// general manipulators
Auto,
// output character set
EmitNonAscii,
EscapeNonAscii,
// string manipulators
// Auto, // duplicate

View File

@@ -53,6 +53,9 @@ namespace YAML
template <typename T>
const T Read() const;
template <typename T>
operator T() const;
template <typename T>
friend void operator >> (const Node& node, T& value);
@@ -109,8 +112,27 @@ namespace YAML
const Node *m_pIdentity;
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 "nodereadimpl.h"
#endif // NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@@ -9,15 +9,6 @@
namespace YAML
{
// 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>
inline const T Node::Read() const {
T value;
@@ -25,9 +16,14 @@ namespace YAML
return value;
}
template <typename T>
Node::operator T() const {
return Read<T>();
}
template <typename T>
inline void operator >> (const Node& node, T& value) {
if(!node.Read(value))
if(!ConvertScalar(node, value))
throw InvalidScalar(node.m_mark);
}
@@ -80,6 +76,43 @@ namespace YAML
inline const Node& Node::operator [] (const char *key) const {
return GetValue(std::string(key));
}
template <typename T>
inline bool operator == (const T& value, const Node& node) {
return value == node.operator T();
}
template <typename T>
inline bool operator == (const Node& node, const T& value) {
return value == node.operator T();
}
template <typename T>
inline bool operator != (const T& value, const Node& node) {
return value != node.operator T();
}
template <typename T>
inline bool operator != (const Node& node, const T& value) {
return value != node.operator 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

77
include/nodereadimpl.h Normal file
View File

@@ -0,0 +1,77 @@
#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
// Note: this doesn't work on gcc 3.2, but does on gcc 3.4 and above. I'm not sure about 3.3.
#if __GNUC__ && (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ <= 3))
// trick doesn't work? Just fall back to ConvertScalar.
// This means that we can't use any user-defined types as keys in a map
template <typename T>
inline bool Node::Read(T& value) const {
return ConvertScalar(*this, value);
}
#else
// usual case: the trick!
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(...);
int operator,(flag, flag);
template<typename T>
void operator,(flag, T const&);
char operator,(int, flag);
}
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);
}
#endif // done with trick
// 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);
}
}

View File

@@ -4,28 +4,31 @@
#define PARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#include "node.h"
#include "parserstate.h"
#include "noncopyable.h"
#include <ios>
#include <string>
#include <vector>
#include <map>
#include "node.h"
#include "parserstate.h"
#include <memory>
namespace YAML
{
class Scanner;
struct Token;
class Parser
class Parser: private noncopyable
{
public:
Parser();
Parser(std::istream& in);
~Parser();
operator bool() const;
void Load(std::istream& in);
void GetNextDocument(Node& document);
bool GetNextDocument(Node& document);
void PrintTokens(std::ostream& out);
private:
@@ -35,12 +38,7 @@ namespace YAML
void HandleTagDirective(Token *pToken);
private:
// can't copy this
Parser(const Parser&) {}
Parser& operator = (const Parser&) { return *this; }
private:
Scanner *m_pScanner;
std::auto_ptr<Scanner> m_pScanner;
ParserState m_state;
};
}

View File

@@ -49,8 +49,7 @@ namespace
namespace YAML
{
template <>
bool Converter<bool>::Convert(const std::string& input, bool& b)
bool Convert(const std::string& input, bool& b)
{
// we can't use iostream bool extraction operators as they don't
// recognize all possible values in the table below (taken from
@@ -82,8 +81,7 @@ namespace YAML
return false;
}
template <>
bool Converter<_Null>::Convert(const std::string& input, _Null& /*output*/)
bool Convert(const std::string& input, _Null& /*output*/)
{
return input.empty() || input == "~" || input == "null" || input == "Null" || input == "NULL";
}

View File

@@ -37,6 +37,11 @@ namespace YAML
}
// global setters
bool Emitter::SetOutputCharset(EMITTER_MANIP value)
{
return m_pState->SetOutputCharset(value, GLOBAL);
}
bool Emitter::SetStringFormat(EMITTER_MANIP value)
{
return m_pState->SetStringFormat(value, GLOBAL);
@@ -344,12 +349,18 @@ namespace YAML
EMITTER_STATE curState = m_pState->GetCurState();
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
if(flowType == FT_BLOCK)
assert(curState == ES_DONE_WITH_BLOCK_SEQ_ENTRY);
else if(flowType == FT_FLOW) {
m_stream << "]";
if(flowType == FT_BLOCK) {
// Note: block sequences are *not* allowed to be empty, but we convert it
// to a flow sequence if it is
assert(curState == ES_DONE_WITH_BLOCK_SEQ_ENTRY || curState == ES_WAITING_FOR_BLOCK_SEQ_ENTRY);
if(curState == ES_WAITING_FOR_BLOCK_SEQ_ENTRY) {
unsigned curIndent = m_pState->GetCurIndent();
m_stream << IndentTo(curIndent) << "[]";
}
} else if(flowType == FT_FLOW) {
// Note: flow sequences are allowed to be empty
assert(curState == ES_DONE_WITH_FLOW_SEQ_ENTRY || curState == ES_WAITING_FOR_FLOW_SEQ_ENTRY);
m_stream << "]";
} else
assert(false);
@@ -399,12 +410,18 @@ namespace YAML
EMITTER_STATE curState = m_pState->GetCurState();
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
if(flowType == FT_BLOCK)
assert(curState == ES_DONE_WITH_BLOCK_MAP_VALUE);
else if(flowType == FT_FLOW) {
m_stream << "}";
if(flowType == FT_BLOCK) {
// Note: block sequences are *not* allowed to be empty, but we convert it
// to a flow sequence if it is
assert(curState == ES_DONE_WITH_BLOCK_MAP_VALUE || curState == ES_WAITING_FOR_BLOCK_MAP_ENTRY);
if(curState == ES_WAITING_FOR_BLOCK_MAP_ENTRY) {
unsigned curIndent = m_pState->GetCurIndent();
m_stream << IndentTo(curIndent) << "{}";
}
} else if(flowType == FT_FLOW) {
// Note: flow maps are allowed to be empty
assert(curState == ES_DONE_WITH_FLOW_MAP_VALUE || curState == ES_WAITING_FOR_FLOW_MAP_ENTRY);
m_stream << "}";
} else
assert(false);
@@ -485,13 +502,14 @@ namespace YAML
PreAtomicWrite();
EmitSeparationIfNecessary();
bool escapeNonAscii = m_pState->GetOutputCharset() == EscapeNonAscii;
EMITTER_MANIP strFmt = m_pState->GetStringFormat();
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
unsigned curIndent = m_pState->GetCurIndent();
switch(strFmt) {
case Auto:
Utils::WriteString(m_stream, str, flowType == FT_FLOW);
Utils::WriteString(m_stream, str, flowType == FT_FLOW, escapeNonAscii);
break;
case SingleQuoted:
if(!Utils::WriteSingleQuotedString(m_stream, str)) {
@@ -500,11 +518,11 @@ namespace YAML
}
break;
case DoubleQuoted:
Utils::WriteDoubleQuotedString(m_stream, str);
Utils::WriteDoubleQuotedString(m_stream, str, escapeNonAscii);
break;
case Literal:
if(flowType == FT_FLOW)
Utils::WriteString(m_stream, str, flowType == FT_FLOW);
Utils::WriteString(m_stream, str, flowType == FT_FLOW, escapeNonAscii);
else
Utils::WriteLiteralString(m_stream, str, curIndent + m_pState->GetIndent());
break;
@@ -679,3 +697,4 @@ namespace YAML
return *this;
}
}

View File

@@ -9,6 +9,7 @@ namespace YAML
m_stateStack.push(ES_WAITING_FOR_DOC);
// set default global manipulators
m_charset.set(EmitNonAscii);
m_strFmt.set(Auto);
m_boolFmt.set(TrueFalseBool);
m_boolLengthFmt.set(LongBool);
@@ -43,6 +44,7 @@ namespace YAML
// . Only the ones that make sense will be accepted
void EmitterState::SetLocalValue(EMITTER_MANIP value)
{
SetOutputCharset(value, LOCAL);
SetStringFormat(value, LOCAL);
SetBoolFormat(value, LOCAL);
SetBoolCaseFormat(value, LOCAL);
@@ -132,6 +134,18 @@ namespace YAML
{
m_modifiedSettings.clear();
}
bool EmitterState::SetOutputCharset(EMITTER_MANIP value, FMT_SCOPE scope)
{
switch(value) {
case EmitNonAscii:
case EscapeNonAscii:
_Set(m_charset, value, scope);
return true;
default:
return false;
}
}
bool EmitterState::SetStringFormat(EMITTER_MANIP value, FMT_SCOPE scope)
{

View File

@@ -108,6 +108,9 @@ namespace YAML
void ClearModifiedSettings();
// formatters
bool SetOutputCharset(EMITTER_MANIP value, FMT_SCOPE scope);
EMITTER_MANIP GetOutputCharset() const { return m_charset.get(); }
bool SetStringFormat(EMITTER_MANIP value, FMT_SCOPE scope);
EMITTER_MANIP GetStringFormat() const { return m_strFmt.get(); }
@@ -149,6 +152,7 @@ namespace YAML
// other state
std::stack <EMITTER_STATE> m_stateStack;
Setting <EMITTER_MANIP> m_charset;
Setting <EMITTER_MANIP> m_strFmt;
Setting <EMITTER_MANIP> m_boolFmt;
Setting <EMITTER_MANIP> m_boolLengthFmt;

View File

@@ -11,11 +11,123 @@ namespace YAML
namespace Utils
{
namespace {
bool IsPrintable(char ch) {
return (0x20 <= ch && ch <= 0x7E);
enum {REPLACEMENT_CHARACTER = 0xFFFD};
bool IsAnchorChar(int ch) { // test for ns-anchor-char
switch (ch) {
case ',': case '[': case ']': case '{': case '}': // c-flow-indicator
case ' ': case '\t': // s-white
case 0xFEFF: // c-byte-order-mark
case 0xA: case 0xD: // b-char
return false;
case 0x85:
return true;
}
if (ch < 0x20)
return false;
if (ch < 0x7E)
return true;
if (ch < 0xA0)
return false;
if (ch >= 0xD800 && ch <= 0xDFFF)
return false;
if ((ch & 0xFFFE) == 0xFFFE)
return false;
if ((ch >= 0xFDD0) && (ch <= 0xFDEF))
return false;
if (ch > 0x10FFFF)
return false;
return true;
}
bool IsValidPlainScalar(const std::string& str, bool inFlow) {
int Utf8BytesIndicated(char ch) {
int byteVal = static_cast<unsigned char>(ch);
switch (byteVal >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
return 1;
case 12: case 13:
return 2;
case 14:
return 3;
case 15:
return 4;
default:
return -1;
}
}
bool IsTrailingByte(char ch) {
return (ch & 0xC0) == 0x80;
}
bool GetNextCodePointAndAdvance(int& codePoint, std::string::const_iterator& first, std::string::const_iterator last) {
if (first == last)
return false;
int nBytes = Utf8BytesIndicated(*first);
if (nBytes < 1) {
// Bad lead byte
++first;
codePoint = REPLACEMENT_CHARACTER;
return true;
}
if (nBytes == 1) {
codePoint = *first++;
return true;
}
// Gather bits from trailing bytes
codePoint = static_cast<unsigned char>(*first) & ~(0xFF << (7 - nBytes));
++first;
--nBytes;
for (; nBytes > 0; ++first, --nBytes) {
if ((first == last) || !IsTrailingByte(*first)) {
codePoint = REPLACEMENT_CHARACTER;
break;
}
codePoint <<= 6;
codePoint |= *first & 0x3F;
}
// Check for illegal code points
if (codePoint > 0x10FFFF)
codePoint = REPLACEMENT_CHARACTER;
else if (codePoint >= 0xD800 && codePoint <= 0xDFFF)
codePoint = REPLACEMENT_CHARACTER;
else if ((codePoint & 0xFFFE) == 0xFFFE)
codePoint = REPLACEMENT_CHARACTER;
else if (codePoint >= 0xFDD0 && codePoint <= 0xFDEF)
codePoint = REPLACEMENT_CHARACTER;
return true;
}
void WriteCodePoint(ostream& out, int codePoint) {
if (codePoint < 0 || codePoint > 0x10FFFF) {
codePoint = REPLACEMENT_CHARACTER;
}
if (codePoint < 0x7F) {
out << static_cast<char>(codePoint);
} else if (codePoint < 0x7FF) {
out << static_cast<char>(0xC0 | (codePoint >> 6))
<< static_cast<char>(0x80 | (codePoint & 0x3F));
} else if (codePoint < 0xFFFF) {
out << static_cast<char>(0xE0 | (codePoint >> 12))
<< static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F))
<< static_cast<char>(0x80 | (codePoint & 0x3F));
} else {
out << static_cast<char>(0xF0 | (codePoint >> 18))
<< static_cast<char>(0x80 | ((codePoint >> 12) & 0x3F))
<< static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F))
<< static_cast<char>(0x80 | (codePoint & 0x3F));
}
}
bool IsValidPlainScalar(const std::string& str, bool inFlow, bool allowOnlyAscii) {
// first check the start
const RegEx& start = (inFlow ? Exp::PlainScalarInFlow : Exp::PlainScalar);
if(!start.Matches(str))
@@ -28,64 +140,109 @@ namespace YAML
// then check until something is disallowed
const RegEx& disallowed = (inFlow ? Exp::EndScalarInFlow : Exp::EndScalar)
|| (Exp::BlankOrBreak + Exp::Comment)
|| (!Exp::Printable)
|| Exp::NotPrintable
|| Exp::Utf8_ByteOrderMark
|| Exp::Break
|| Exp::Tab;
StringCharSource buffer(str.c_str(), str.size());
while(buffer) {
if(disallowed.Matches(buffer))
return false;
if(allowOnlyAscii && (0x7F < static_cast<unsigned char>(buffer[0])))
return false;
++buffer;
}
return true;
}
void WriteDoubleQuoteEscapeSequence(ostream& out, int codePoint) {
static const char hexDigits[] = "0123456789abcdef";
char escSeq[] = "\\U00000000";
int digits = 8;
if (codePoint < 0xFF) {
escSeq[1] = 'x';
digits = 2;
} else if (codePoint < 0xFFFF) {
escSeq[1] = 'u';
digits = 4;
}
// Write digits into the escape sequence
int i = 2;
for (; digits > 0; --digits, ++i) {
escSeq[i] = hexDigits[(codePoint >> (4 * (digits - 1))) & 0xF];
}
escSeq[i] = 0; // terminate with NUL character
out << escSeq;
}
bool WriteAliasName(ostream& out, const std::string& str) {
int codePoint;
for(std::string::const_iterator i = str.begin();
GetNextCodePointAndAdvance(codePoint, i, str.end());
)
{
if (!IsAnchorChar(codePoint))
return false;
WriteCodePoint(out, codePoint);
}
return true;
}
}
bool WriteString(ostream& out, const std::string& str, bool inFlow)
bool WriteString(ostream& out, const std::string& str, bool inFlow, bool escapeNonAscii)
{
if(IsValidPlainScalar(str, inFlow)) {
if(IsValidPlainScalar(str, inFlow, escapeNonAscii)) {
out << str;
return true;
} else
return WriteDoubleQuotedString(out, str);
return WriteDoubleQuotedString(out, str, escapeNonAscii);
}
bool WriteSingleQuotedString(ostream& out, const std::string& str)
{
out << "'";
for(std::size_t i=0;i<str.size();i++) {
char ch = str[i];
if(!IsPrintable(ch))
return false;
if(ch == '\'')
int codePoint;
for(std::string::const_iterator i = str.begin();
GetNextCodePointAndAdvance(codePoint, i, str.end());
)
{
if (codePoint == '\n')
return false; // We can't handle a new line and the attendant indentation yet
if (codePoint == '\'')
out << "''";
else
out << ch;
WriteCodePoint(out, codePoint);
}
out << "'";
return true;
}
bool WriteDoubleQuotedString(ostream& out, const std::string& str)
bool WriteDoubleQuotedString(ostream& out, const std::string& str, bool escapeNonAscii)
{
out << "\"";
for(std::size_t i=0;i<str.size();i++) {
char ch = str[i];
if(IsPrintable(ch)) {
if(ch == '\"')
out << "\\\"";
else if(ch == '\\')
out << "\\\\";
else
out << ch;
} else {
// TODO: for the common escaped characters, give their usual symbol
std::stringstream str;
str << "\\x" << std::hex << std::setfill('0') << std::setw(2) << static_cast <int>(ch);
out << str.str();
}
int codePoint;
for(std::string::const_iterator i = str.begin();
GetNextCodePointAndAdvance(codePoint, i, str.end());
)
{
if (codePoint == '\"')
out << "\\\"";
else if (codePoint == '\\')
out << "\\\\";
else if (codePoint < 0x20 || (codePoint >= 0x80 && codePoint <= 0xA0)) // Control characters and non-breaking space
WriteDoubleQuoteEscapeSequence(out, codePoint);
else if (codePoint == 0xFEFF) // Byte order marks (ZWNS) should be escaped (YAML 1.2, sec. 5.2)
WriteDoubleQuoteEscapeSequence(out, codePoint);
else if (escapeNonAscii && codePoint > 0x7E)
WriteDoubleQuoteEscapeSequence(out, codePoint);
else
WriteCodePoint(out, codePoint);
}
out << "\"";
return true;
@@ -95,11 +252,15 @@ namespace YAML
{
out << "|\n";
out << IndentTo(indent);
for(std::size_t i=0;i<str.size();i++) {
if(str[i] == '\n')
out << "\n" << IndentTo(indent);
int codePoint;
for(std::string::const_iterator i = str.begin();
GetNextCodePointAndAdvance(codePoint, i, str.end());
)
{
if (codePoint == '\n')
out << "\n" << IndentTo(indent);
else
out << str[i];
WriteCodePoint(out, codePoint);
}
return true;
}
@@ -108,11 +269,15 @@ namespace YAML
{
unsigned curIndent = out.col();
out << "#" << Indentation(postCommentIndent);
for(std::size_t i=0;i<str.size();i++) {
if(str[i] == '\n')
int codePoint;
for(std::string::const_iterator i = str.begin();
GetNextCodePointAndAdvance(codePoint, i, str.end());
)
{
if(codePoint == '\n')
out << "\n" << IndentTo(curIndent) << "#" << Indentation(postCommentIndent);
else
out << str[i];
WriteCodePoint(out, codePoint);
}
return true;
}
@@ -120,25 +285,13 @@ namespace YAML
bool WriteAlias(ostream& out, const std::string& str)
{
out << "*";
for(std::size_t i=0;i<str.size();i++) {
if(!IsPrintable(str[i]) || str[i] == ' ' || str[i] == '\t' || str[i] == '\n' || str[i] == '\r')
return false;
out << str[i];
}
return true;
return WriteAliasName(out, str);
}
bool WriteAnchor(ostream& out, const std::string& str)
{
out << "&";
for(std::size_t i=0;i<str.size();i++) {
if(!IsPrintable(str[i]) || str[i] == ' ' || str[i] == '\t' || str[i] == '\n' || str[i] == '\r')
return false;
out << str[i];
}
return true;
return WriteAliasName(out, str);
}
}
}

View File

@@ -11,9 +11,9 @@ namespace YAML
{
namespace Utils
{
bool WriteString(ostream& out, const std::string& str, bool inFlow);
bool WriteString(ostream& out, const std::string& str, bool inFlow, bool escapeNonAscii);
bool WriteSingleQuotedString(ostream& out, const std::string& str);
bool WriteDoubleQuotedString(ostream& out, const std::string& str);
bool WriteDoubleQuotedString(ostream& out, const std::string& str, bool escapeNonAscii);
bool WriteLiteralString(ostream& out, const std::string& str, int indent);
bool WriteComment(ostream& out, const std::string& str, int postCommentIndent);
bool WriteAlias(ostream& out, const std::string& str);

View File

@@ -28,9 +28,9 @@ namespace YAML
return value;
}
std::string Str(char ch)
std::string Str(unsigned ch)
{
return std::string("") + ch;
return std::string(1, static_cast<char>(ch));
}
// Escape
@@ -83,7 +83,7 @@ namespace YAML
// now do the slash (we're not gonna check if it's a slash - you better pass one!)
switch(ch) {
case '0': return "\0";
case '0': return std::string(1, '\x00');
case 'a': return "\x07";
case 'b': return "\x08";
case 't':
@@ -97,8 +97,9 @@ namespace YAML
case '\"': return "\"";
case '\'': return "\'";
case '\\': return "\\";
case 'N': return "\xC2\x85"; // NEL (#x85)
case '_': return "\xC2\xA0"; // #xA0
case '/': return "/";
case 'N': return "\x85";
case '_': return "\xA0";
case 'L': return "\xE2\x80\xA8"; // LS (#x2028)
case 'P': return "\xE2\x80\xA9"; // PS (#x2029)
case 'x': return Escape(in, 2);

View File

@@ -26,7 +26,12 @@ namespace YAML
const RegEx Alpha = RegEx('a', 'z') || RegEx('A', 'Z');
const RegEx AlphaNumeric = Alpha || Digit;
const RegEx Hex = Digit || RegEx('A', 'F') || RegEx('a', 'f');
const RegEx Printable = RegEx(0x20, 0x7E);
// Valid Unicode code points that are not part of c-printable (YAML 1.2, sec. 5.1)
const RegEx NotPrintable = RegEx(0) ||
RegEx("\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x7F", REGEX_OR) ||
RegEx(0x0E, 0x1F) ||
(RegEx('\xC2') + (RegEx('\x80', '\x84') || RegEx('\x86', '\x9F')));
const RegEx Utf8_ByteOrderMark = RegEx("\xEF\xBB\xBF");
// actual tags

View File

@@ -53,6 +53,11 @@ namespace YAML
return true;
}
std::size_t Map::GetSize() const
{
return m_data.size();
}
void Map::Parse(Scanner *pScanner, const ParserState& state)
{
Clear();
@@ -117,24 +122,21 @@ namespace YAML
pScanner->pop();
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);
// grab key
pKey->Parse(pScanner, state);
// grab key (if non-null)
if(token.type == Token::KEY) {
pScanner->pop();
pKey->Parse(pScanner, state);
}
// now grab value (optional)
if(!pScanner->empty() && pScanner->peek().type == Token::VALUE) {
pScanner->pop();
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)
Token& nextToken = pScanner->peek();
if(nextToken.type == Token::FLOW_ENTRY)

View File

@@ -26,6 +26,7 @@ namespace YAML
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 std::size_t GetSize() const;
virtual void Parse(Scanner *pScanner, const ParserState& state);
virtual void Write(Emitter& out) const;

View File

@@ -8,33 +8,38 @@
namespace YAML
{
Parser::Parser(std::istream& in): m_pScanner(0)
Parser::Parser()
{
}
Parser::Parser(std::istream& in)
{
Load(in);
}
Parser::~Parser()
{
delete m_pScanner;
}
Parser::operator bool() const
{
return !m_pScanner->empty();
return m_pScanner.get() && !m_pScanner->empty();
}
void Parser::Load(std::istream& in)
{
delete m_pScanner;
m_pScanner = new Scanner(in);
m_pScanner.reset(new Scanner(in));
m_state.Reset();
}
// GetNextDocument
// . Reads the next document in the queue (of tokens).
// . Throws a ParserException on error.
void Parser::GetNextDocument(Node& document)
bool Parser::GetNextDocument(Node& document)
{
if(!m_pScanner.get())
return false;
// clear node
document.Clear();
@@ -43,14 +48,14 @@ namespace YAML
// we better have some tokens in the queue
if(m_pScanner->empty())
return;
return false;
// first eat doc start (optional)
if(m_pScanner->peek().type == Token::DOC_START)
m_pScanner->pop();
// now parse our root node
document.Parse(m_pScanner, m_state);
document.Parse(m_pScanner.get(), m_state);
// and finally eat any doc ends we see
while(!m_pScanner->empty() && m_pScanner->peek().type == Token::DOC_END)
@@ -58,6 +63,8 @@ namespace YAML
// clear anchors from the scanner, which are no longer relevant
m_pScanner->ClearAnchors();
return true;
}
// ParseDirectives
@@ -126,6 +133,9 @@ namespace YAML
void Parser::PrintTokens(std::ostream& out)
{
if(!m_pScanner.get())
return;
while(1) {
if(m_pScanner->empty())
break;

View File

@@ -171,8 +171,11 @@ namespace YAML
{
while(1) {
// first eat whitespace
while(INPUT && IsWhitespaceToBeEaten(INPUT.peek()))
while(INPUT && IsWhitespaceToBeEaten(INPUT.peek())) {
if(InBlockContext() && Exp::Tab.Matches(INPUT))
m_simpleKeyAllowed = false;
INPUT.eat(1);
}
// then eat a comment
if(Exp::Comment.Matches(INPUT)) {
@@ -202,18 +205,19 @@ namespace YAML
// Misc. helpers
// IsWhitespaceToBeEaten
// . We can eat whitespace if:
// 1. It's a space
// 2. It's a tab, and we're either:
// a. In the flow context
// b. In the block context but not where a simple key could be allowed
// (i.e., not at the beginning of a line, or following '-', '?', or ':')
// . We can eat whitespace if it's a space or tab
// . Note: originally tabs in block context couldn't be eaten
// "where a simple key could be allowed
// (i.e., not at the beginning of a line, or following '-', '?', or ':')"
// I think this is wrong, since tabs can be non-content whitespace; it's just
// that they can't contribute to indentation, so once you've seen a tab in a
// line, you can't start a simple key
bool Scanner::IsWhitespaceToBeEaten(char ch)
{
if(ch == ' ')
return true;
if(ch == '\t' && (InFlowContext() || !m_simpleKeyAllowed))
if(ch == '\t')
return true;
return false;

View File

@@ -19,14 +19,19 @@ namespace YAML
// and different places in the above flow.
std::string ScanScalar(Stream& INPUT, ScanScalarParams& params)
{
bool foundNonEmptyLine = false, pastOpeningBreak = false;
bool foundNonEmptyLine = false;
bool pastOpeningBreak = (params.fold == FOLD_FLOW);
bool emptyLine = false, moreIndented = false;
int foldedNewlineCount = 0;
bool foldedNewlineStartedMoreIndented = false;
std::string scalar;
params.leadingSpaces = false;
while(INPUT) {
// ********************************
// Phase #1: scan until line ending
std::size_t lastNonWhitespaceChar = scalar.size();
while(!params.end.Matches(INPUT) && !Exp::Break.Matches(INPUT)) {
if(!INPUT)
break;
@@ -46,17 +51,22 @@ namespace YAML
if(params.escape == '\\' && Exp::EscBreak.Matches(INPUT)) {
int n = Exp::EscBreak.Match(INPUT);
INPUT.eat(n);
lastNonWhitespaceChar = scalar.size();
continue;
}
// escape this?
if(INPUT.peek() == params.escape) {
scalar += Exp::Escape(INPUT);
lastNonWhitespaceChar = scalar.size();
continue;
}
// otherwise, just add the damn character
scalar += INPUT.get();
char ch = INPUT.get();
scalar += ch;
if(ch != ' ' && ch != '\t')
lastNonWhitespaceChar = scalar.size();
}
// eof? if we're looking to eat something, then we throw
@@ -77,7 +87,11 @@ namespace YAML
INPUT.eat(n);
break;
}
// do we remove trailing whitespace?
if(params.fold == FOLD_FLOW)
scalar.erase(lastNonWhitespaceChar);
// ********************************
// Phase #2: eat line ending
n = Exp::Break.Match(INPUT);
@@ -108,19 +122,38 @@ namespace YAML
// was this an empty line?
bool nextEmptyLine = Exp::Break.Matches(INPUT);
bool nextMoreIndented = (INPUT.peek() == ' ');
bool nextMoreIndented = Exp::Blank.Matches(INPUT);
if(params.fold == FOLD_BLOCK && foldedNewlineCount == 0 && nextEmptyLine)
foldedNewlineStartedMoreIndented = moreIndented;
// for block scalars, we always start with a newline, so we should ignore it (not fold or keep)
bool useNewLine = pastOpeningBreak;
// and for folded scalars, we don't fold the very last newline to a space
if(params.fold && !emptyLine && INPUT.column() < params.indent)
useNewLine = false;
if(useNewLine) {
if(params.fold && !emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented)
scalar += " ";
else
scalar += "\n";
if(pastOpeningBreak) {
switch(params.fold) {
case DONT_FOLD:
scalar += "\n";
break;
case FOLD_BLOCK:
if(!emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented && INPUT.column() >= params.indent)
scalar += " ";
else if(nextEmptyLine)
foldedNewlineCount++;
else
scalar += "\n";
if(!nextEmptyLine && foldedNewlineCount > 0) {
scalar += std::string(foldedNewlineCount - 1, '\n');
if(foldedNewlineStartedMoreIndented || nextMoreIndented)
scalar += "\n";
foldedNewlineCount = 0;
}
break;
case FOLD_FLOW:
if(nextEmptyLine)
scalar += "\n";
else if(!emptyLine && !nextEmptyLine)
scalar += " ";
break;
}
}
emptyLine = nextEmptyLine;
@@ -141,11 +174,11 @@ namespace YAML
scalar.erase(pos + 1);
}
if(params.chomp <= 0) {
if(params.chomp == STRIP || params.chomp == CLIP) {
std::size_t pos = scalar.find_last_not_of('\n');
if(params.chomp == 0 && pos + 1 < scalar.size())
if(params.chomp == CLIP && pos + 1 < scalar.size())
scalar.erase(pos + 2);
else if(params.chomp == -1 && pos < scalar.size())
else if(params.chomp == STRIP && pos < scalar.size())
scalar.erase(pos + 1);
}

View File

@@ -12,9 +12,10 @@ namespace YAML
{
enum CHOMP { STRIP = -1, CLIP, KEEP };
enum ACTION { NONE, BREAK, THROW };
enum FOLD { DONT_FOLD, FOLD_BLOCK, FOLD_FLOW };
struct ScanScalarParams {
ScanScalarParams(): eatEnd(false), indent(0), detectIndent(false), eatLeadingWhitespace(0), escape(0), fold(false),
ScanScalarParams(): eatEnd(false), indent(0), detectIndent(false), eatLeadingWhitespace(0), escape(0), fold(DONT_FOLD),
trimTrailingSpaces(0), chomp(CLIP), onDocIndicator(NONE), onTabInIndentation(NONE), leadingSpaces(false) {}
// input:
@@ -24,7 +25,7 @@ namespace YAML
bool detectIndent; // should we try to autodetect the indent?
bool eatLeadingWhitespace; // should we continue eating this delicious indentation after 'indent' spaces?
char escape; // what character do we escape on (i.e., slash or single quote) (0 for none)
bool fold; // do we fold line ends?
FOLD fold; // how do we fold line ends?
bool trimTrailingSpaces; // do we remove all trailing spaces (at the very end)
CHOMP chomp; // do we strip, clip, or keep trailing newlines (at the very end)
// Note: strip means kill all, clip means keep at most one, keep means keep all

View File

@@ -182,9 +182,6 @@ namespace YAML
// Value
void Scanner::ScanValue()
{
// just in case we have an empty key
InsertPotentialSimpleKey();
// and check that simple key
bool isSimpleKey = VerifySimpleKey();
@@ -290,10 +287,10 @@ namespace YAML
params.end = (InFlowContext() ? Exp::EndScalarInFlow : Exp::EndScalar) || (Exp::BlankOrBreak + Exp::Comment);
params.eatEnd = false;
params.indent = (InFlowContext() ? 0 : GetTopIndent() + 1);
params.fold = true;
params.fold = FOLD_FLOW;
params.eatLeadingWhitespace = true;
params.trimTrailingSpaces = true;
params.chomp = CLIP;
params.chomp = STRIP;
params.onDocIndicator = BREAK;
params.onTabInIndentation = THROW;
@@ -330,7 +327,7 @@ namespace YAML
params.eatEnd = true;
params.escape = (single ? '\'' : '\\');
params.indent = 0;
params.fold = true;
params.fold = FOLD_FLOW;
params.eatLeadingWhitespace = true;
params.trimTrailingSpaces = false;
params.chomp = CLIP;
@@ -368,9 +365,10 @@ namespace YAML
// eat block indicator ('|' or '>')
Mark mark = INPUT.mark();
char indicator = INPUT.get();
params.fold = (indicator == Keys::FoldedScalar);
params.fold = (indicator == Keys::FoldedScalar ? FOLD_BLOCK : DONT_FOLD);
// eat chomping/indentation indicators
params.chomp = CLIP;
int n = Exp::Chomp.Match(INPUT);
for(int i=0;i<n;i++) {
char ch = INPUT.get();

View File

@@ -11,9 +11,8 @@ int main(int argc, char **argv)
std::istream& input = (argc > 1 ? fin : std::cin);
try {
YAML::Parser parser(input);
while(parser) {
YAML::Node doc;
parser.GetNextDocument(doc);
YAML::Node doc;
while(parser.GetNextDocument(doc)) {
YAML::Emitter emitter;
emitter << doc;
std::cout << emitter.c_str() << "\n";

View File

@@ -179,6 +179,10 @@
RelativePath=".\yaml-reader\parsertests.cpp"
>
</File>
<File
RelativePath=".\yaml-reader\spectests.cpp"
>
</File>
<File
RelativePath=".\yaml-reader\tests.cpp"
>
@@ -189,6 +193,18 @@
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\yaml-reader\emittertests.h"
>
</File>
<File
RelativePath=".\yaml-reader\parsertests.h"
>
</File>
<File
RelativePath=".\yaml-reader\spectests.h"
>
</File>
<File
RelativePath=".\yaml-reader\tests.h"
>

View File

@@ -447,6 +447,26 @@ namespace Test
desiredOutput = "- ~\n-\n null value: ~\n ~: null key";
}
void EscapedUnicode(YAML::Emitter& out, std::string& desiredOutput)
{
out << YAML::EscapeNonAscii << "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
desiredOutput = "\"$ \\xa2 \\u20ac \\U00024b62\"";
}
void Unicode(YAML::Emitter& out, std::string& desiredOutput)
{
out << "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
desiredOutput = "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
}
void DoubleQuotedUnicode(YAML::Emitter& out, std::string& desiredOutput)
{
out << YAML::DoubleQuoted << "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
desiredOutput = "\"\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2\"";
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// incorrect emitting
@@ -534,4 +554,97 @@ namespace Test
out << YAML::EndSeq;
}
}
namespace {
void RunEmitterTest(void (*test)(YAML::Emitter&, std::string&), const std::string& name, int& passed, int& total) {
YAML::Emitter out;
std::string desiredOutput;
test(out, desiredOutput);
std::string output = out.c_str();
if(output == desiredOutput) {
passed++;
} else {
std::cout << "Emitter test failed: " << name << "\n";
std::cout << "Output:\n";
std::cout << output << "<<<\n";
std::cout << "Desired output:\n";
std::cout << desiredOutput << "<<<\n";
}
total++;
}
void RunEmitterErrorTest(void (*test)(YAML::Emitter&, std::string&), const std::string& name, int& passed, int& total) {
YAML::Emitter out;
std::string desiredError;
test(out, desiredError);
std::string lastError = out.GetLastError();
if(!out.good() && lastError == desiredError) {
passed++;
} else {
std::cout << "Emitter test failed: " << name << "\n";
if(out.good())
std::cout << "No error detected\n";
else
std::cout << "Detected error: " << lastError << "\n";
std::cout << "Expected error: " << desiredError << "\n";
}
total++;
}
}
bool RunEmitterTests()
{
int passed = 0;
int total = 0;
RunEmitterTest(&Emitter::SimpleScalar, "simple scalar", passed, total);
RunEmitterTest(&Emitter::SimpleSeq, "simple seq", passed, total);
RunEmitterTest(&Emitter::SimpleFlowSeq, "simple flow seq", passed, total);
RunEmitterTest(&Emitter::EmptyFlowSeq, "empty flow seq", passed, total);
RunEmitterTest(&Emitter::NestedBlockSeq, "nested block seq", passed, total);
RunEmitterTest(&Emitter::NestedFlowSeq, "nested flow seq", passed, total);
RunEmitterTest(&Emitter::SimpleMap, "simple map", passed, total);
RunEmitterTest(&Emitter::SimpleFlowMap, "simple flow map", passed, total);
RunEmitterTest(&Emitter::MapAndList, "map and list", passed, total);
RunEmitterTest(&Emitter::ListAndMap, "list and map", passed, total);
RunEmitterTest(&Emitter::NestedBlockMap, "nested block map", passed, total);
RunEmitterTest(&Emitter::NestedFlowMap, "nested flow map", passed, total);
RunEmitterTest(&Emitter::MapListMix, "map list mix", passed, total);
RunEmitterTest(&Emitter::SimpleLongKey, "simple long key", passed, total);
RunEmitterTest(&Emitter::SingleLongKey, "single long key", passed, total);
RunEmitterTest(&Emitter::ComplexLongKey, "complex long key", passed, total);
RunEmitterTest(&Emitter::AutoLongKey, "auto long key", passed, total);
RunEmitterTest(&Emitter::ScalarFormat, "scalar format", passed, total);
RunEmitterTest(&Emitter::AutoLongKeyScalar, "auto long key scalar", passed, total);
RunEmitterTest(&Emitter::LongKeyFlowMap, "long key flow map", passed, total);
RunEmitterTest(&Emitter::BlockMapAsKey, "block map as key", passed, total);
RunEmitterTest(&Emitter::AliasAndAnchor, "alias and anchor", passed, total);
RunEmitterTest(&Emitter::AliasAndAnchorWithNull, "alias and anchor with null", passed, total);
RunEmitterTest(&Emitter::ComplexDoc, "complex doc", passed, total);
RunEmitterTest(&Emitter::STLContainers, "STL containers", passed, total);
RunEmitterTest(&Emitter::SimpleComment, "simple comment", passed, total);
RunEmitterTest(&Emitter::MultiLineComment, "multi-line comment", passed, total);
RunEmitterTest(&Emitter::ComplexComments, "complex comments", passed, total);
RunEmitterTest(&Emitter::Indentation, "indentation", passed, total);
RunEmitterTest(&Emitter::SimpleGlobalSettings, "simple global settings", passed, total);
RunEmitterTest(&Emitter::ComplexGlobalSettings, "complex global settings", passed, total);
RunEmitterTest(&Emitter::Null, "null", passed, total);
RunEmitterTest(&Emitter::EscapedUnicode, "escaped unicode", passed, total);
RunEmitterTest(&Emitter::Unicode, "unicode", passed, total);
RunEmitterTest(&Emitter::DoubleQuotedUnicode, "double quoted unicode", passed, total);
RunEmitterErrorTest(&Emitter::ExtraEndSeq, "extra EndSeq", passed, total);
RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed, total);
RunEmitterErrorTest(&Emitter::BadSingleQuoted, "bad single quoted string", passed, total);
RunEmitterErrorTest(&Emitter::InvalidAnchor, "invalid anchor", passed, total);
RunEmitterErrorTest(&Emitter::InvalidAlias, "invalid alias", passed, total);
RunEmitterErrorTest(&Emitter::MissingKey, "missing key", passed, total);
RunEmitterErrorTest(&Emitter::MissingValue, "missing value", passed, total);
RunEmitterErrorTest(&Emitter::UnexpectedKey, "unexpected key", passed, total);
RunEmitterErrorTest(&Emitter::UnexpectedValue, "unexpected value", passed, total);
std::cout << "Emitter tests: " << passed << "/" << total << " passed\n";
return passed == total;
}
}

View File

@@ -0,0 +1,11 @@
#pragma once
#ifndef EMITTERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define EMITTERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
namespace Test {
bool RunEmitterTests();
}
#endif // EMITTERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@@ -611,4 +611,278 @@ namespace Test
return true;
}
}
namespace {
void RunScalarParserTest(void (*test)(std::string&, std::string&), const std::string& name, int& passed, int& total) {
std::string error;
std::string inputScalar, desiredOutput;
std::string output;
bool ok = true;
try {
test(inputScalar, desiredOutput);
std::stringstream stream(inputScalar);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
doc >> output;
} catch(const YAML::Exception& e) {
ok = false;
error = e.msg;
}
if(ok && output == desiredOutput) {
passed++;
} else {
std::cout << "Parser test failed: " << name << "\n";
if(error != "")
std::cout << "Caught exception: " << error << "\n";
else {
std::cout << "Output:\n" << output << "<<<\n";
std::cout << "Desired output:\n" << desiredOutput << "<<<\n";
}
}
total++;
}
void RunParserTest(bool (*test)(), const std::string& name, int& passed, int& total) {
std::string error;
bool ok = true;
try {
ok = test();
} catch(const YAML::Exception& e) {
ok = false;
error = e.msg;
}
if(ok) {
passed++;
} else {
std::cout << "Parser test failed: " << name << "\n";
if(error != "")
std::cout << "Caught exception: " << error << "\n";
}
total++;
}
typedef void (*EncodingFn)(std::ostream&, int);
inline char Byte(int ch)
{
return static_cast<char>(static_cast<unsigned char>(static_cast<unsigned int>(ch)));
}
void EncodeToUtf8(std::ostream& stream, int ch)
{
if (ch <= 0x7F)
{
stream << Byte(ch);
}
else if (ch <= 0x7FF)
{
stream << Byte(0xC0 | (ch >> 6));
stream << Byte(0x80 | (ch & 0x3F));
}
else if (ch <= 0xFFFF)
{
stream << Byte(0xE0 | (ch >> 12));
stream << Byte(0x80 | ((ch >> 6) & 0x3F));
stream << Byte(0x80 | (ch & 0x3F));
}
else if (ch <= 0x1FFFFF)
{
stream << Byte(0xF0 | (ch >> 18));
stream << Byte(0x80 | ((ch >> 12) & 0x3F));
stream << Byte(0x80 | ((ch >> 6) & 0x3F));
stream << Byte(0x80 | (ch & 0x3F));
}
}
bool SplitUtf16HighChar(std::ostream& stream, EncodingFn encoding, int ch)
{
int biasedValue = ch - 0x10000;
if (biasedValue < 0)
{
return false;
}
int high = 0xD800 | (biasedValue >> 10);
int low = 0xDC00 | (biasedValue & 0x3FF);
encoding(stream, high);
encoding(stream, low);
return true;
}
void EncodeToUtf16LE(std::ostream& stream, int ch)
{
if (!SplitUtf16HighChar(stream, &EncodeToUtf16LE, ch))
{
stream << Byte(ch & 0xFF) << Byte(ch >> 8);
}
}
void EncodeToUtf16BE(std::ostream& stream, int ch)
{
if (!SplitUtf16HighChar(stream, &EncodeToUtf16BE, ch))
{
stream << Byte(ch >> 8) << Byte(ch & 0xFF);
}
}
void EncodeToUtf32LE(std::ostream& stream, int ch)
{
stream << Byte(ch & 0xFF) << Byte((ch >> 8) & 0xFF)
<< Byte((ch >> 16) & 0xFF) << Byte((ch >> 24) & 0xFF);
}
void EncodeToUtf32BE(std::ostream& stream, int ch)
{
stream << Byte((ch >> 24) & 0xFF) << Byte((ch >> 16) & 0xFF)
<< Byte((ch >> 8) & 0xFF) << Byte(ch & 0xFF);
}
class EncodingTester
{
public:
EncodingTester(EncodingFn encoding, bool declareEncoding)
{
if (declareEncoding)
{
encoding(m_yaml, 0xFEFF);
}
AddEntry(encoding, 0x0021, 0x007E); // Basic Latin
AddEntry(encoding, 0x00A1, 0x00FF); // Latin-1 Supplement
AddEntry(encoding, 0x0660, 0x06FF); // Arabic (largest contiguous block)
// CJK unified ideographs (multiple lines)
AddEntry(encoding, 0x4E00, 0x4EFF);
AddEntry(encoding, 0x4F00, 0x4FFF);
AddEntry(encoding, 0x5000, 0x51FF); // 512 character line
AddEntry(encoding, 0x5200, 0x54FF); // 768 character line
AddEntry(encoding, 0x5500, 0x58FF); // 1024 character line
AddEntry(encoding, 0x103A0, 0x103C3); // Old Persian
m_yaml.seekg(0, std::ios::beg);
}
std::istream& stream() {return m_yaml;}
const std::vector<std::string>& entries() {return m_entries;}
private:
std::stringstream m_yaml;
std::vector<std::string> m_entries;
void AddEntry(EncodingFn encoding, int startCh, int endCh)
{
encoding(m_yaml, '-');
encoding(m_yaml, ' ');
encoding(m_yaml, '|');
encoding(m_yaml, '\n');
encoding(m_yaml, ' ');
encoding(m_yaml, ' ');
std::stringstream entry;
for (int ch = startCh; ch <= endCh; ++ch)
{
encoding(m_yaml, ch);
EncodeToUtf8(entry, ch);
}
encoding(m_yaml, '\n');
m_entries.push_back(entry.str());
}
};
void RunEncodingTest(EncodingFn encoding, bool declareEncoding, const std::string& name, int& passed, int& total)
{
EncodingTester tester(encoding, declareEncoding);
std::string error;
bool ok = true;
try {
YAML::Parser parser(tester.stream());
YAML::Node doc;
parser.GetNextDocument(doc);
YAML::Iterator itNode = doc.begin();
std::vector<std::string>::const_iterator itEntry = tester.entries().begin();
for (; (itNode != doc.end()) && (itEntry != tester.entries().end()); ++itNode, ++itEntry)
{
std::string stScalarValue;
if (!itNode->GetScalar(stScalarValue) && (stScalarValue == *itEntry))
{
break;
}
}
if ((itNode != doc.end()) || (itEntry != tester.entries().end()))
{
ok = false;
}
} catch(const YAML::Exception& e) {
ok = false;
error = e.msg;
}
if(ok) {
passed++;
} else {
std::cout << "Parser test failed: " << name << "\n";
if(error != "")
std::cout << "Caught exception: " << error << "\n";
}
total++;
}
}
bool RunParserTests()
{
int passed = 0;
int total = 0;
RunScalarParserTest(&Parser::SimpleScalar, "simple scalar", passed, total);
RunScalarParserTest(&Parser::MultiLineScalar, "multi-line scalar", passed, total);
RunScalarParserTest(&Parser::LiteralScalar, "literal scalar", passed, total);
RunScalarParserTest(&Parser::FoldedScalar, "folded scalar", passed, total);
RunScalarParserTest(&Parser::ChompedFoldedScalar, "chomped folded scalar", passed, total);
RunScalarParserTest(&Parser::ChompedLiteralScalar, "chomped literal scalar", passed, total);
RunScalarParserTest(&Parser::FoldedScalarWithIndent, "folded scalar with indent", passed, total);
RunScalarParserTest(&Parser::ColonScalar, "colon scalar", passed, total);
RunScalarParserTest(&Parser::QuotedScalar, "quoted scalar", passed, total);
RunScalarParserTest(&Parser::CommaScalar, "comma scalar", passed, total);
RunScalarParserTest(&Parser::DashScalar, "dash scalar", passed, total);
RunScalarParserTest(&Parser::URLScalar, "url scalar", passed, total);
RunParserTest(&Parser::SimpleSeq, "simple seq", passed, total);
RunParserTest(&Parser::SimpleMap, "simple map", passed, total);
RunParserTest(&Parser::FlowSeq, "flow seq", passed, total);
RunParserTest(&Parser::FlowMap, "flow map", passed, total);
RunParserTest(&Parser::FlowMapWithOmittedKey, "flow map with omitted key", passed, total);
RunParserTest(&Parser::FlowMapWithOmittedValue, "flow map with omitted value", passed, total);
RunParserTest(&Parser::FlowMapWithSoloEntry, "flow map with solo entry", passed, total);
RunParserTest(&Parser::FlowMapEndingWithSoloEntry, "flow map ending with solo entry", passed, total);
RunParserTest(&Parser::QuotedSimpleKeys, "quoted simple keys", passed, total);
RunParserTest(&Parser::CompressedMapAndSeq, "compressed map and seq", passed, total);
RunParserTest(&Parser::NullBlockSeqEntry, "null block seq entry", passed, total);
RunParserTest(&Parser::NullBlockMapKey, "null block map key", passed, total);
RunParserTest(&Parser::NullBlockMapValue, "null block map value", passed, total);
RunParserTest(&Parser::SimpleAlias, "simple alias", passed, total);
RunParserTest(&Parser::AliasWithNull, "alias with null", passed, total);
RunParserTest(&Parser::AnchorInSimpleKey, "anchor in simple key", passed, total);
RunParserTest(&Parser::AliasAsSimpleKey, "alias as simple key", passed, total);
RunParserTest(&Parser::ExplicitDoc, "explicit doc", passed, total);
RunParserTest(&Parser::MultipleDocs, "multiple docs", passed, total);
RunParserTest(&Parser::ExplicitEndDoc, "explicit end doc", passed, total);
RunParserTest(&Parser::MultipleDocsWithSomeExplicitIndicators, "multiple docs with some explicit indicators", passed, total);
RunEncodingTest(&EncodeToUtf8, false, "UTF-8, no BOM", passed, total);
RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed, total);
RunEncodingTest(&EncodeToUtf16LE, false, "UTF-16LE, no BOM", passed, total);
RunEncodingTest(&EncodeToUtf16LE, true, "UTF-16LE with BOM", passed, total);
RunEncodingTest(&EncodeToUtf16BE, false, "UTF-16BE, no BOM", passed, total);
RunEncodingTest(&EncodeToUtf16BE, true, "UTF-16BE with BOM", passed, total);
RunEncodingTest(&EncodeToUtf32LE, false, "UTF-32LE, no BOM", passed, total);
RunEncodingTest(&EncodeToUtf32LE, true, "UTF-32LE with BOM", passed, total);
RunEncodingTest(&EncodeToUtf32BE, false, "UTF-32BE, no BOM", passed, total);
RunEncodingTest(&EncodeToUtf32BE, true, "UTF-32BE with BOM", passed, total);
std::cout << "Parser tests: " << passed << "/" << total << " passed\n";
return passed == total;
}
}

11
yaml-reader/parsertests.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#ifndef PARSERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define PARSERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
namespace Test {
bool RunParserTests();
}
#endif // PARSERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,6 @@
#include "tests.h"
#include "emittertests.h"
#include "parsertests.h"
#include "spectests.h"
#include "yaml.h"
#include <fstream>
@@ -14,376 +16,14 @@ namespace Test
if(!RunParserTests())
passed = false;
if(!RunSpecTests())
passed = false;
if(!RunEmitterTests())
passed = false;
if(!RunSpecTests())
passed = false;
if(passed)
std::cout << "All tests passed!\n";
}
////////////////////////////////////////////////////////////////////////////////////////
// Parser tests
namespace {
void RunScalarParserTest(void (*test)(std::string&, std::string&), const std::string& name, bool& passed) {
std::string error;
std::string inputScalar, desiredOutput;
std::string output;
bool ok = true;
try {
test(inputScalar, desiredOutput);
std::stringstream stream(inputScalar);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
doc >> output;
} catch(const YAML::Exception& e) {
ok = false;
error = e.msg;
}
if(ok && output == desiredOutput) {
std::cout << "Parser test passed: " << name << "\n";
} else {
passed = false;
std::cout << "Parser test failed: " << name << "\n";
if(error != "")
std::cout << "Caught exception: " << error << "\n";
else {
std::cout << "Output:\n" << output << "<<<\n";
std::cout << "Desired output:\n" << desiredOutput << "<<<\n";
}
}
}
void RunParserTest(bool (*test)(), 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 << "Parser test passed: " << name << "\n";
} else {
passed = false;
std::cout << "Parser test failed: " << name << "\n";
if(error != "")
std::cout << "Caught exception: " << error << "\n";
}
}
typedef void (*EncodingFn)(std::ostream&, int);
inline char Byte(int ch)
{
return static_cast<char>(static_cast<unsigned char>(static_cast<unsigned int>(ch)));
}
void EncodeToUtf8(std::ostream& stream, int ch)
{
if (ch <= 0x7F)
{
stream << Byte(ch);
}
else if (ch <= 0x7FF)
{
stream << Byte(0xC0 | (ch >> 6));
stream << Byte(0x80 | (ch & 0x3F));
}
else if (ch <= 0xFFFF)
{
stream << Byte(0xE0 | (ch >> 12));
stream << Byte(0x80 | ((ch >> 6) & 0x3F));
stream << Byte(0x80 | (ch & 0x3F));
}
else if (ch <= 0x1FFFFF)
{
stream << Byte(0xF0 | (ch >> 18));
stream << Byte(0x80 | ((ch >> 12) & 0x3F));
stream << Byte(0x80 | ((ch >> 6) & 0x3F));
stream << Byte(0x80 | (ch & 0x3F));
}
}
bool SplitUtf16HighChar(std::ostream& stream, EncodingFn encoding, int ch)
{
int biasedValue = ch - 0x10000;
if (biasedValue < 0)
{
return false;
}
int high = 0xD800 | (biasedValue >> 10);
int low = 0xDC00 | (biasedValue & 0x3FF);
encoding(stream, high);
encoding(stream, low);
return true;
}
void EncodeToUtf16LE(std::ostream& stream, int ch)
{
if (!SplitUtf16HighChar(stream, &EncodeToUtf16LE, ch))
{
stream << Byte(ch & 0xFF) << Byte(ch >> 8);
}
}
void EncodeToUtf16BE(std::ostream& stream, int ch)
{
if (!SplitUtf16HighChar(stream, &EncodeToUtf16BE, ch))
{
stream << Byte(ch >> 8) << Byte(ch & 0xFF);
}
}
void EncodeToUtf32LE(std::ostream& stream, int ch)
{
stream << Byte(ch & 0xFF) << Byte((ch >> 8) & 0xFF)
<< Byte((ch >> 16) & 0xFF) << Byte((ch >> 24) & 0xFF);
}
void EncodeToUtf32BE(std::ostream& stream, int ch)
{
stream << Byte((ch >> 24) & 0xFF) << Byte((ch >> 16) & 0xFF)
<< Byte((ch >> 8) & 0xFF) << Byte(ch & 0xFF);
}
class EncodingTester
{
public:
EncodingTester(EncodingFn encoding, bool declareEncoding)
{
if (declareEncoding)
{
encoding(m_yaml, 0xFEFF);
}
AddEntry(encoding, 0x0021, 0x007E); // Basic Latin
AddEntry(encoding, 0x00A1, 0x00FF); // Latin-1 Supplement
AddEntry(encoding, 0x0660, 0x06FF); // Arabic (largest contiguous block)
// CJK unified ideographs (multiple lines)
AddEntry(encoding, 0x4E00, 0x4EFF);
AddEntry(encoding, 0x4F00, 0x4FFF);
AddEntry(encoding, 0x5000, 0x51FF); // 512 character line
AddEntry(encoding, 0x5200, 0x54FF); // 768 character line
AddEntry(encoding, 0x5500, 0x58FF); // 1024 character line
AddEntry(encoding, 0x103A0, 0x103C3); // Old Persian
m_yaml.seekg(0, std::ios::beg);
}
std::istream& stream() {return m_yaml;}
const std::vector<std::string>& entries() {return m_entries;}
private:
std::stringstream m_yaml;
std::vector<std::string> m_entries;
void AddEntry(EncodingFn encoding, int startCh, int endCh)
{
encoding(m_yaml, '-');
encoding(m_yaml, ' ');
encoding(m_yaml, '|');
encoding(m_yaml, '\n');
encoding(m_yaml, ' ');
encoding(m_yaml, ' ');
std::stringstream entry;
for (int ch = startCh; ch <= endCh; ++ch)
{
encoding(m_yaml, ch);
EncodeToUtf8(entry, ch);
}
encoding(m_yaml, '\n');
m_entries.push_back(entry.str());
}
};
void RunEncodingTest(EncodingFn encoding, bool declareEncoding, const std::string& name, bool& passed)
{
EncodingTester tester(encoding, declareEncoding);
std::string error;
bool ok = true;
try {
YAML::Parser parser(tester.stream());
YAML::Node doc;
parser.GetNextDocument(doc);
YAML::Iterator itNode = doc.begin();
std::vector<std::string>::const_iterator itEntry = tester.entries().begin();
for (; (itNode != doc.end()) && (itEntry != tester.entries().end()); ++itNode, ++itEntry)
{
std::string stScalarValue;
if (!itNode->GetScalar(stScalarValue) && (stScalarValue == *itEntry))
{
break;
}
}
if ((itNode != doc.end()) || (itEntry != tester.entries().end()))
{
ok = false;
}
} catch(const YAML::Exception& e) {
ok = false;
error = e.msg;
}
if(ok) {
std::cout << "Parser test passed: " << name << "\n";
} else {
passed = false;
std::cout << "Parser test failed: " << name << "\n";
if(error != "")
std::cout << "Caught exception: " << error << "\n";
}
}
}
bool RunParserTests()
{
bool passed = true;
RunScalarParserTest(&Parser::SimpleScalar, "simple scalar", passed);
RunScalarParserTest(&Parser::MultiLineScalar, "multi-line scalar", passed);
RunScalarParserTest(&Parser::LiteralScalar, "literal scalar", passed);
RunScalarParserTest(&Parser::FoldedScalar, "folded scalar", passed);
RunScalarParserTest(&Parser::ChompedFoldedScalar, "chomped folded scalar", passed);
RunScalarParserTest(&Parser::ChompedLiteralScalar, "chomped literal scalar", passed);
RunScalarParserTest(&Parser::FoldedScalarWithIndent, "folded scalar with indent", passed);
RunScalarParserTest(&Parser::ColonScalar, "colon scalar", passed);
RunScalarParserTest(&Parser::QuotedScalar, "quoted scalar", passed);
RunScalarParserTest(&Parser::CommaScalar, "comma scalar", passed);
RunScalarParserTest(&Parser::DashScalar, "dash scalar", passed);
RunScalarParserTest(&Parser::URLScalar, "url scalar", passed);
RunParserTest(&Parser::SimpleSeq, "simple seq", passed);
RunParserTest(&Parser::SimpleMap, "simple map", passed);
RunParserTest(&Parser::FlowSeq, "flow seq", passed);
RunParserTest(&Parser::FlowMap, "flow map", passed);
RunParserTest(&Parser::FlowMapWithOmittedKey, "flow map with omitted key", passed);
RunParserTest(&Parser::FlowMapWithOmittedValue, "flow map with omitted value", passed);
RunParserTest(&Parser::FlowMapWithSoloEntry, "flow map with solo entry", passed);
RunParserTest(&Parser::FlowMapEndingWithSoloEntry, "flow map ending with solo entry", passed);
RunParserTest(&Parser::QuotedSimpleKeys, "quoted simple keys", passed);
RunParserTest(&Parser::CompressedMapAndSeq, "compressed map and seq", passed);
RunParserTest(&Parser::NullBlockSeqEntry, "null block seq entry", passed);
RunParserTest(&Parser::NullBlockMapKey, "null block map key", passed);
RunParserTest(&Parser::NullBlockMapValue, "null block map value", passed);
RunParserTest(&Parser::SimpleAlias, "simple alias", passed);
RunParserTest(&Parser::AliasWithNull, "alias with null", passed);
RunParserTest(&Parser::AnchorInSimpleKey, "anchor in simple key", passed);
RunParserTest(&Parser::AliasAsSimpleKey, "alias as simple key", passed);
RunParserTest(&Parser::ExplicitDoc, "explicit doc", passed);
RunParserTest(&Parser::MultipleDocs, "multiple docs", passed);
RunParserTest(&Parser::ExplicitEndDoc, "explicit end doc", passed);
RunParserTest(&Parser::MultipleDocsWithSomeExplicitIndicators, "multiple docs with some explicit indicators", passed);
RunEncodingTest(&EncodeToUtf8, false, "UTF-8, no BOM", passed);
RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed);
RunEncodingTest(&EncodeToUtf16LE, false, "UTF-16LE, no BOM", passed);
RunEncodingTest(&EncodeToUtf16LE, true, "UTF-16LE with BOM", passed);
RunEncodingTest(&EncodeToUtf16BE, false, "UTF-16BE, no BOM", passed);
RunEncodingTest(&EncodeToUtf16BE, true, "UTF-16BE with BOM", passed);
RunEncodingTest(&EncodeToUtf32LE, false, "UTF-32LE, no BOM", passed);
RunEncodingTest(&EncodeToUtf32LE, true, "UTF-32LE with BOM", passed);
RunEncodingTest(&EncodeToUtf32BE, false, "UTF-32BE, no BOM", passed);
RunEncodingTest(&EncodeToUtf32BE, true, "UTF-32BE with BOM", passed);
return passed;
}
////////////////////////////////////////////////////////////////////////////////////////
// Emitter tests
namespace {
void RunEmitterTest(void (*test)(YAML::Emitter&, std::string&), const std::string& name, bool& passed) {
YAML::Emitter out;
std::string desiredOutput;
test(out, desiredOutput);
std::string output = out.c_str();
if(output == desiredOutput) {
std::cout << "Emitter test passed: " << name << "\n";
} else {
passed = false;
std::cout << "Emitter test failed: " << name << "\n";
std::cout << "Output:\n";
std::cout << output << "<<<\n";
std::cout << "Desired output:\n";
std::cout << desiredOutput << "<<<\n";
}
}
void RunEmitterErrorTest(void (*test)(YAML::Emitter&, std::string&), const std::string& name, bool& passed) {
YAML::Emitter out;
std::string desiredError;
test(out, desiredError);
std::string lastError = out.GetLastError();
if(!out.good() && lastError == desiredError) {
std::cout << "Emitter test passed: " << name << "\n";
} else {
passed = false;
std::cout << "Emitter test failed: " << name << "\n";
if(out.good())
std::cout << "No error detected\n";
else
std::cout << "Detected error: " << lastError << "\n";
std::cout << "Expected error: " << desiredError << "\n";
}
}
}
bool RunEmitterTests()
{
bool passed = true;
RunEmitterTest(&Emitter::SimpleScalar, "simple scalar", passed);
RunEmitterTest(&Emitter::SimpleSeq, "simple seq", passed);
RunEmitterTest(&Emitter::SimpleFlowSeq, "simple flow seq", passed);
RunEmitterTest(&Emitter::EmptyFlowSeq, "empty flow seq", passed);
RunEmitterTest(&Emitter::NestedBlockSeq, "nested block seq", passed);
RunEmitterTest(&Emitter::NestedFlowSeq, "nested flow seq", passed);
RunEmitterTest(&Emitter::SimpleMap, "simple map", passed);
RunEmitterTest(&Emitter::SimpleFlowMap, "simple flow map", passed);
RunEmitterTest(&Emitter::MapAndList, "map and list", passed);
RunEmitterTest(&Emitter::ListAndMap, "list and map", passed);
RunEmitterTest(&Emitter::NestedBlockMap, "nested block map", passed);
RunEmitterTest(&Emitter::NestedFlowMap, "nested flow map", passed);
RunEmitterTest(&Emitter::MapListMix, "map list mix", passed);
RunEmitterTest(&Emitter::SimpleLongKey, "simple long key", passed);
RunEmitterTest(&Emitter::SingleLongKey, "single long key", passed);
RunEmitterTest(&Emitter::ComplexLongKey, "complex long key", passed);
RunEmitterTest(&Emitter::AutoLongKey, "auto long key", passed);
RunEmitterTest(&Emitter::ScalarFormat, "scalar format", passed);
RunEmitterTest(&Emitter::AutoLongKeyScalar, "auto long key scalar", passed);
RunEmitterTest(&Emitter::LongKeyFlowMap, "long key flow map", passed);
RunEmitterTest(&Emitter::BlockMapAsKey, "block map as key", passed);
RunEmitterTest(&Emitter::AliasAndAnchor, "alias and anchor", passed);
RunEmitterTest(&Emitter::AliasAndAnchorWithNull, "alias and anchor with null", passed);
RunEmitterTest(&Emitter::ComplexDoc, "complex doc", passed);
RunEmitterTest(&Emitter::STLContainers, "STL containers", passed);
RunEmitterTest(&Emitter::SimpleComment, "simple comment", passed);
RunEmitterTest(&Emitter::MultiLineComment, "multi-line comment", passed);
RunEmitterTest(&Emitter::ComplexComments, "complex comments", passed);
RunEmitterTest(&Emitter::Indentation, "indentation", passed);
RunEmitterTest(&Emitter::SimpleGlobalSettings, "simple global settings", passed);
RunEmitterTest(&Emitter::ComplexGlobalSettings, "complex global settings", passed);
RunEmitterTest(&Emitter::Null, "null", passed);
RunEmitterErrorTest(&Emitter::ExtraEndSeq, "extra EndSeq", passed);
RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed);
RunEmitterErrorTest(&Emitter::BadSingleQuoted, "bad single quoted string", passed);
RunEmitterErrorTest(&Emitter::InvalidAnchor, "invalid anchor", passed);
RunEmitterErrorTest(&Emitter::InvalidAlias, "invalid alias", passed);
RunEmitterErrorTest(&Emitter::MissingKey, "missing key", passed);
RunEmitterErrorTest(&Emitter::MissingValue, "missing value", passed);
RunEmitterErrorTest(&Emitter::UnexpectedKey, "unexpected key", passed);
RunEmitterErrorTest(&Emitter::UnexpectedValue, "unexpected value", passed);
return passed;
}
}

View File

@@ -5,13 +5,8 @@
#include <string>
namespace YAML { class Emitter; }
namespace Test {
void RunAll();
bool RunParserTests();
bool RunEmitterTests();
namespace Parser {
// scalar tests
@@ -51,53 +46,6 @@ namespace Test {
bool ExplicitEndDoc();
bool MultipleDocsWithSomeExplicitIndicators();
}
namespace Emitter {
// correct emitting
void SimpleScalar(YAML::Emitter& out, std::string& desiredOutput);
void SimpleSeq(YAML::Emitter& out, std::string& desiredOutput);
void SimpleFlowSeq(YAML::Emitter& ouptut, std::string& desiredOutput);
void EmptyFlowSeq(YAML::Emitter& out, std::string& desiredOutput);
void NestedBlockSeq(YAML::Emitter& out, std::string& desiredOutput);
void NestedFlowSeq(YAML::Emitter& out, std::string& desiredOutput);
void SimpleMap(YAML::Emitter& out, std::string& desiredOutput);
void SimpleFlowMap(YAML::Emitter& out, std::string& desiredOutput);
void MapAndList(YAML::Emitter& out, std::string& desiredOutput);
void ListAndMap(YAML::Emitter& out, std::string& desiredOutput);
void NestedBlockMap(YAML::Emitter& out, std::string& desiredOutput);
void NestedFlowMap(YAML::Emitter& out, std::string& desiredOutput);
void MapListMix(YAML::Emitter& out, std::string& desiredOutput);
void SimpleLongKey(YAML::Emitter& out, std::string& desiredOutput);
void SingleLongKey(YAML::Emitter& out, std::string& desiredOutput);
void ComplexLongKey(YAML::Emitter& out, std::string& desiredOutput);
void AutoLongKey(YAML::Emitter& out, std::string& desiredOutput);
void ScalarFormat(YAML::Emitter& out, std::string& desiredOutput);
void AutoLongKeyScalar(YAML::Emitter& out, std::string& desiredOutput);
void LongKeyFlowMap(YAML::Emitter& out, std::string& desiredOutput);
void BlockMapAsKey(YAML::Emitter& out, std::string& desiredOutput);
void AliasAndAnchor(YAML::Emitter& out, std::string& desiredOutput);
void AliasAndAnchorWithNull(YAML::Emitter& out, std::string& desiredOutput);
void ComplexDoc(YAML::Emitter& out, std::string& desiredOutput);
void STLContainers(YAML::Emitter& out, std::string& desiredOutput);
void SimpleComment(YAML::Emitter& out, std::string& desiredOutput);
void MultiLineComment(YAML::Emitter& out, std::string& desiredOutput);
void ComplexComments(YAML::Emitter& out, std::string& desiredOutput);
void Indentation(YAML::Emitter& out, std::string& desiredOutput);
void SimpleGlobalSettings(YAML::Emitter& out, std::string& desiredOutput);
void ComplexGlobalSettings(YAML::Emitter& out, std::string& desiredOutput);
void Null(YAML::Emitter& out, std::string& desiredOutput);
// incorrect emitting
void ExtraEndSeq(YAML::Emitter& out, std::string& desiredError);
void ExtraEndMap(YAML::Emitter& out, std::string& desiredError);
void BadSingleQuoted(YAML::Emitter& out, std::string& desiredError);
void InvalidAnchor(YAML::Emitter& out, std::string& desiredError);
void InvalidAlias(YAML::Emitter& out, std::string& desiredError);
void MissingKey(YAML::Emitter& out, std::string& desiredError);
void MissingValue(YAML::Emitter& out, std::string& desiredError);
void UnexpectedKey(YAML::Emitter& out, std::string& desiredError);
void UnexpectedValue(YAML::Emitter& out, std::string& desiredError);
}
}
#endif // TESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@@ -191,6 +191,10 @@
RelativePath=".\src\node.cpp"
>
</File>
<File
RelativePath=".\src\null.cpp"
>
</File>
<File
RelativePath=".\src\scalar.cpp"
>