mirror of
https://github.com/loot/yaml-cpp.git
synced 2026-07-27 14:13:42 -07:00
Switched from loading test files to testing specific parsing constructs. The tests don't fully cover the span (eventually I'll add more, maybe), but there's a bunch there.
More to the point, the yaml-reader program doesn't do any file IO, so it doesn't require a specific working directory.
This commit is contained in:
+8
-26
@@ -167,10 +167,18 @@
|
|||||||
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
|
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
|
||||||
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
||||||
>
|
>
|
||||||
|
<File
|
||||||
|
RelativePath=".\yaml-reader\emittertests.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\yaml-reader\main.cpp"
|
RelativePath=".\yaml-reader\main.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\yaml-reader\parsertests.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\yaml-reader\tests.cpp"
|
RelativePath=".\yaml-reader\tests.cpp"
|
||||||
>
|
>
|
||||||
@@ -186,32 +194,6 @@
|
|||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
</Filter>
|
</Filter>
|
||||||
<Filter
|
|
||||||
Name="Resource Files"
|
|
||||||
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
|
|
||||||
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
|
|
||||||
>
|
|
||||||
<File
|
|
||||||
RelativePath=".\yaml-reader\tests\directives.yaml"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath=".\yaml-reader\tests\mixed.yaml"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath=".\yaml-reader\tests\scalars.yaml"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath=".\yaml-reader\tests\simple.yaml"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath=".\yaml-reader\tests\test.yaml"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
</Filter>
|
|
||||||
</Files>
|
</Files>
|
||||||
<Globals>
|
<Globals>
|
||||||
</Globals>
|
</Globals>
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
+1
-28
@@ -1,38 +1,11 @@
|
|||||||
#include "yaml.h"
|
|
||||||
#include "tests.h"
|
#include "tests.h"
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
void run()
|
|
||||||
{
|
|
||||||
std::ifstream fin("tests/test.yaml");
|
|
||||||
YAML::Parser parser(fin);
|
|
||||||
|
|
||||||
while(parser)
|
|
||||||
{
|
|
||||||
YAML::Node doc;
|
|
||||||
parser.GetNextDocument(doc);
|
|
||||||
std::cout << doc;
|
|
||||||
}
|
|
||||||
|
|
||||||
// try some output
|
|
||||||
YAML::Emitter out;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
bool verbose = false;
|
|
||||||
for(int i=1;i<argc;i++) {
|
|
||||||
if(std::strcmp(argv[i], "-v") == 0)
|
|
||||||
verbose = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
|
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
|
||||||
#endif // WINDOWS
|
#endif // WINDOWS
|
||||||
Test::RunAll(verbose);
|
Test::RunAll();
|
||||||
run();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,222 @@
|
|||||||
|
#include "tests.h"
|
||||||
|
#include "yaml.h"
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace Test
|
||||||
|
{
|
||||||
|
namespace Parser {
|
||||||
|
void SimpleScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar = "Hello, World!";
|
||||||
|
desiredOutput = "Hello, World!";
|
||||||
|
}
|
||||||
|
|
||||||
|
void MultiLineScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar =
|
||||||
|
"normal scalar, but\n"
|
||||||
|
"over several lines";
|
||||||
|
desiredOutput = "normal scalar, but over several lines";
|
||||||
|
}
|
||||||
|
|
||||||
|
void LiteralScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar =
|
||||||
|
"|\n"
|
||||||
|
" literal scalar - so we can draw ASCII:\n"
|
||||||
|
" \n"
|
||||||
|
" - -\n"
|
||||||
|
" | - |\n"
|
||||||
|
" -----\n";
|
||||||
|
desiredOutput =
|
||||||
|
"literal scalar - so we can draw ASCII:\n"
|
||||||
|
"\n"
|
||||||
|
" - -\n"
|
||||||
|
" | - |\n"
|
||||||
|
" -----\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void FoldedScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar =
|
||||||
|
">\n"
|
||||||
|
" and a folded scalar... so we\n"
|
||||||
|
" can just keep writing various\n"
|
||||||
|
" things. And if we want to keep indentation:\n"
|
||||||
|
" \n"
|
||||||
|
" we just indent a little\n"
|
||||||
|
" see, this stays indented";
|
||||||
|
desiredOutput =
|
||||||
|
"and a folded scalar... so we"
|
||||||
|
" can just keep writing various"
|
||||||
|
" things. And if we want to keep indentation:\n"
|
||||||
|
"\n"
|
||||||
|
" we just indent a little\n"
|
||||||
|
" see, this stays indented";
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChompedFoldedScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar =
|
||||||
|
">-\n"
|
||||||
|
" Here's a folded scalar\n"
|
||||||
|
" that gets chomped.";
|
||||||
|
desiredOutput =
|
||||||
|
"Here's a folded scalar"
|
||||||
|
" that gets chomped.";
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChompedLiteralScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar =
|
||||||
|
"|-\n"
|
||||||
|
" Here's a literal scalar\n"
|
||||||
|
" that gets chomped.";
|
||||||
|
desiredOutput =
|
||||||
|
"Here's a literal scalar\n"
|
||||||
|
"that gets chomped.";
|
||||||
|
}
|
||||||
|
|
||||||
|
void FoldedScalarWithIndent(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar =
|
||||||
|
">2\n"
|
||||||
|
" Here's a folded scalar\n"
|
||||||
|
" that starts with some indentation.";
|
||||||
|
desiredOutput =
|
||||||
|
" Here's a folded scalar\n"
|
||||||
|
"that starts with some indentation.";
|
||||||
|
}
|
||||||
|
|
||||||
|
void ColonScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar = "::vector";
|
||||||
|
desiredOutput = "::vector";
|
||||||
|
}
|
||||||
|
|
||||||
|
void QuotedScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar = "\": - ()\"";
|
||||||
|
desiredOutput = ": - ()";
|
||||||
|
}
|
||||||
|
|
||||||
|
void CommaScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar = "Up, up, and away!";
|
||||||
|
desiredOutput = "Up, up, and away!";
|
||||||
|
}
|
||||||
|
|
||||||
|
void DashScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar = "-123";
|
||||||
|
desiredOutput = "-123";
|
||||||
|
}
|
||||||
|
|
||||||
|
void URLScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||||
|
{
|
||||||
|
inputScalar = "http://example.com/foo#bar";
|
||||||
|
desiredOutput = "http://example.com/foo#bar";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SimpleSeq()
|
||||||
|
{
|
||||||
|
std::string input =
|
||||||
|
"- eggs\n"
|
||||||
|
"- bread\n"
|
||||||
|
"- milk";
|
||||||
|
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
std::string output;
|
||||||
|
doc[0] >> output;
|
||||||
|
if(output != "eggs")
|
||||||
|
return false;
|
||||||
|
doc[1] >> output;
|
||||||
|
if(output != "bread")
|
||||||
|
return false;
|
||||||
|
doc[2] >> output;
|
||||||
|
if(output != "milk")
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SimpleMap()
|
||||||
|
{
|
||||||
|
std::string input =
|
||||||
|
"name: Prince Fielder\n"
|
||||||
|
"position: 1B\n"
|
||||||
|
"bats: L";
|
||||||
|
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
std::string output;
|
||||||
|
doc["name"] >> output;
|
||||||
|
if(output != "Prince Fielder")
|
||||||
|
return false;
|
||||||
|
doc["position"] >> output;
|
||||||
|
if(output != "1B")
|
||||||
|
return false;
|
||||||
|
doc["bats"] >> output;
|
||||||
|
if(output != "L")
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FlowSeq()
|
||||||
|
{
|
||||||
|
std::string input = "[ 2 , 3, 5 , 7, 11]";
|
||||||
|
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
int output;
|
||||||
|
doc[0] >> output;
|
||||||
|
if(output != 2)
|
||||||
|
return false;
|
||||||
|
doc[1] >> output;
|
||||||
|
if(output != 3)
|
||||||
|
return false;
|
||||||
|
doc[2] >> output;
|
||||||
|
if(output != 5)
|
||||||
|
return false;
|
||||||
|
doc[3] >> output;
|
||||||
|
if(output != 7)
|
||||||
|
return false;
|
||||||
|
doc[4] >> output;
|
||||||
|
if(output != 11)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FlowMap()
|
||||||
|
{
|
||||||
|
std::string input = "{hr: 65, avg: 0.278}";
|
||||||
|
|
||||||
|
std::stringstream stream(input);
|
||||||
|
YAML::Parser parser(stream);
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
std::string output;
|
||||||
|
doc["hr"] >> output;
|
||||||
|
if(output != "65")
|
||||||
|
return false;
|
||||||
|
doc["avg"] >> output;
|
||||||
|
if(output != "0.278")
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+119
-619
File diff suppressed because it is too large
Load Diff
+24
-2
@@ -3,11 +3,33 @@
|
|||||||
namespace YAML { class Emitter; }
|
namespace YAML { class Emitter; }
|
||||||
|
|
||||||
namespace Test {
|
namespace Test {
|
||||||
void RunAll(bool verbose);
|
void RunAll();
|
||||||
bool Inout(const std::string& file, bool verbose);
|
|
||||||
|
|
||||||
|
bool RunParserTests();
|
||||||
bool RunEmitterTests();
|
bool RunEmitterTests();
|
||||||
|
|
||||||
|
namespace Parser {
|
||||||
|
// scalar tests
|
||||||
|
void SimpleScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
void MultiLineScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
void LiteralScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
void FoldedScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
void ChompedFoldedScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
void ChompedLiteralScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
void FoldedScalarWithIndent(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
void ColonScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
void QuotedScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
void CommaScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
void DashScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
void URLScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||||
|
|
||||||
|
// misc tests
|
||||||
|
bool SimpleSeq();
|
||||||
|
bool SimpleMap();
|
||||||
|
bool FlowSeq();
|
||||||
|
bool FlowMap();
|
||||||
|
}
|
||||||
|
|
||||||
namespace Emitter {
|
namespace Emitter {
|
||||||
// correct emitting
|
// correct emitting
|
||||||
void SimpleScalar(YAML::Emitter& out, std::string& desiredOutput);
|
void SimpleScalar(YAML::Emitter& out, std::string& desiredOutput);
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
%YAML 1.2
|
|
||||||
%TAG ! !howdy
|
|
||||||
---
|
|
||||||
- basic node
|
|
||||||
- ! yeah baby
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
- the main thing is a sequence
|
|
||||||
- here's a key: value
|
|
||||||
and another: value
|
|
||||||
- let's inline: [1, 2, 3]
|
|
||||||
and an inline map: {key: value, 243: 101}
|
|
||||||
- and multiple indents:
|
|
||||||
- here's
|
|
||||||
- a
|
|
||||||
- list
|
|
||||||
and another:
|
|
||||||
- list
|
|
||||||
- of
|
|
||||||
- things
|
|
||||||
- maybe now:
|
|
||||||
let's: get
|
|
||||||
pretty:
|
|
||||||
deep: here
|
|
||||||
in:
|
|
||||||
the: nesting
|
|
||||||
just: to
|
|
||||||
confuse:
|
|
||||||
the: heck
|
|
||||||
out:
|
|
||||||
- of
|
|
||||||
- the: parser
|
|
||||||
if:
|
|
||||||
- we
|
|
||||||
- can
|
|
||||||
- do: that
|
|
||||||
what: do
|
|
||||||
you: think?
|
|
||||||
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
- normal scalar, but
|
|
||||||
over several lines
|
|
||||||
- |
|
|
||||||
literal scalar - so we can draw ASCII:
|
|
||||||
|
|
||||||
- -
|
|
||||||
| - |
|
|
||||||
------
|
|
||||||
- >
|
|
||||||
and a folded scalar... so we
|
|
||||||
can just keep writing various
|
|
||||||
things. And if we want to keep indentation:
|
|
||||||
|
|
||||||
we just indent a little
|
|
||||||
see, this stays indented
|
|
||||||
- >-
|
|
||||||
Here's a folded scalar
|
|
||||||
that gets chomped.
|
|
||||||
- |-
|
|
||||||
And here's a literal scalar
|
|
||||||
that gets chomped.
|
|
||||||
- >2
|
|
||||||
Here's a folded scalar
|
|
||||||
that starts with some indentation.
|
|
||||||
- ::vector
|
|
||||||
- ": - ()"
|
|
||||||
- Up, up, and away!
|
|
||||||
- -123
|
|
||||||
- http://example.com/foo#bar
|
|
||||||
# Inside flow collection:
|
|
||||||
- [ ::vector,
|
|
||||||
": - ()",
|
|
||||||
"Up, up and away!",
|
|
||||||
-123,
|
|
||||||
http://example.com/foo#bar ]
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
---
|
|
||||||
just a scalar
|
|
||||||
---
|
|
||||||
and another scalar
|
|
||||||
---
|
|
||||||
now an end document
|
|
||||||
...
|
|
||||||
---
|
|
||||||
and now two
|
|
||||||
...
|
|
||||||
...
|
|
||||||
---
|
|
||||||
and that's it
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
- true
|
|
||||||
- false
|
|
||||||
- y
|
|
||||||
- n
|
|
||||||
Reference in New Issue
Block a user