mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Wrote some tests, but they don't work because it doesn't output maps in a canonical form.
This commit is contained in:
27
main.cpp
27
main.cpp
@@ -1,4 +1,5 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include "tests.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@@ -64,21 +65,23 @@ void operator >> (const YAML::Node& node, Level& level)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::ifstream fin("test.yaml");
|
YAML::Test::RunAll();
|
||||||
|
|
||||||
try {
|
//std::ifstream fin("test.yaml");
|
||||||
YAML::Parser parser(fin);
|
|
||||||
if(!parser)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
YAML::Node doc;
|
//try {
|
||||||
parser.GetNextDocument(doc);
|
// YAML::Parser parser(fin);
|
||||||
std::cout << doc;
|
// if(!parser)
|
||||||
} catch(YAML::Exception&) {
|
// return 0;
|
||||||
std::cout << "Error parsing the yaml!\n";
|
|
||||||
}
|
// YAML::Node doc;
|
||||||
|
// parser.GetNextDocument(doc);
|
||||||
|
// std::cout << doc;
|
||||||
|
//} catch(YAML::Exception&) {
|
||||||
|
// std::cout << "Error parsing the yaml!\n";
|
||||||
|
//}
|
||||||
|
|
||||||
getchar();
|
getchar();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
10
parser.cpp
10
parser.cpp
@@ -8,8 +8,7 @@ namespace YAML
|
|||||||
{
|
{
|
||||||
Parser::Parser(std::istream& in): m_pScanner(0)
|
Parser::Parser(std::istream& in): m_pScanner(0)
|
||||||
{
|
{
|
||||||
m_pScanner = new Scanner(in);
|
Load(in);
|
||||||
m_state.Reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Parser::~Parser()
|
Parser::~Parser()
|
||||||
@@ -22,6 +21,13 @@ namespace YAML
|
|||||||
return m_pScanner->PeekNextToken() != 0;
|
return m_pScanner->PeekNextToken() != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Parser::Load(std::istream& in)
|
||||||
|
{
|
||||||
|
delete m_pScanner;
|
||||||
|
m_pScanner = new Scanner(in);
|
||||||
|
m_state.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
// GetNextDocument
|
// GetNextDocument
|
||||||
// . Reads the next document in the queue (of tokens).
|
// . Reads the next document in the queue (of tokens).
|
||||||
// . Throws (ScannerException|ParserException)s on errors.
|
// . Throws (ScannerException|ParserException)s on errors.
|
||||||
|
1
parser.h
1
parser.h
@@ -20,6 +20,7 @@ namespace YAML
|
|||||||
|
|
||||||
operator bool() const;
|
operator bool() const;
|
||||||
|
|
||||||
|
void Load(std::istream& in);
|
||||||
void GetNextDocument(Node& document);
|
void GetNextDocument(Node& document);
|
||||||
void PrintTokens(std::ostream& out);
|
void PrintTokens(std::ostream& out);
|
||||||
|
|
||||||
|
80
tests.cpp
Normal file
80
tests.cpp
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
#include "tests.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace YAML
|
||||||
|
{
|
||||||
|
namespace Test
|
||||||
|
{
|
||||||
|
// runs all the tests on all data we have
|
||||||
|
void RunAll()
|
||||||
|
{
|
||||||
|
std::vector <std::string> files;
|
||||||
|
files.push_back("tests/simple.yaml");
|
||||||
|
files.push_back("tests/mixed.yaml");
|
||||||
|
|
||||||
|
bool passed = true;
|
||||||
|
for(unsigned i=0;i<files.size();i++) {
|
||||||
|
if(!YAML::Test::Inout(files[i])) {
|
||||||
|
std::cout << "Inout test failed on " << files[i] << std::endl;
|
||||||
|
passed = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(passed)
|
||||||
|
std::cout << "All tests passed!\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// loads the given YAML file, outputs it, and then loads the outputted file,
|
||||||
|
// outputs again, and makes sure that the two outputs are the same
|
||||||
|
bool Inout(const std::string& file)
|
||||||
|
{
|
||||||
|
std::ifstream fin(file.c_str());
|
||||||
|
|
||||||
|
try {
|
||||||
|
// read and output
|
||||||
|
Parser parser(fin);
|
||||||
|
if(!parser)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
std::stringstream out;
|
||||||
|
out << doc;
|
||||||
|
// and save
|
||||||
|
std::string firstTry = out.str();
|
||||||
|
|
||||||
|
// and now again
|
||||||
|
parser.Load(out);
|
||||||
|
if(!parser)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
std::stringstream out2;
|
||||||
|
out2 << doc;
|
||||||
|
// and save
|
||||||
|
std::string secondTry = out2.str();
|
||||||
|
|
||||||
|
// now compare
|
||||||
|
if(firstTry == secondTry)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
std::ofstream fout("out.yaml");
|
||||||
|
fout << "---\n";
|
||||||
|
fout << firstTry << std::endl;
|
||||||
|
fout << "---\n";
|
||||||
|
fout << secondTry << std::endl;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
} catch(Exception&) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
tests.h
Normal file
10
tests.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace YAML
|
||||||
|
{
|
||||||
|
namespace Test {
|
||||||
|
void RunAll();
|
||||||
|
|
||||||
|
bool Inout(const std::string& file);
|
||||||
|
}
|
||||||
|
}
|
32
tests/mixed.yaml
Normal file
32
tests/mixed.yaml
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
- 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?
|
||||||
|
|
13
tests/simple.yaml
Normal file
13
tests/simple.yaml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
---
|
||||||
|
just a scalar
|
||||||
|
---
|
||||||
|
and another scalar
|
||||||
|
---
|
||||||
|
now an end document
|
||||||
|
...
|
||||||
|
---
|
||||||
|
and now two
|
||||||
|
...
|
||||||
|
...
|
||||||
|
---
|
||||||
|
and that's it
|
@@ -165,6 +165,10 @@
|
|||||||
RelativePath=".\main.cpp"
|
RelativePath=".\main.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\tests.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<Filter
|
<Filter
|
||||||
Name="Scanner"
|
Name="Scanner"
|
||||||
>
|
>
|
||||||
@@ -247,6 +251,10 @@
|
|||||||
RelativePath=".\exceptions.h"
|
RelativePath=".\exceptions.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\tests.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<Filter
|
<Filter
|
||||||
Name="Scanner"
|
Name="Scanner"
|
||||||
>
|
>
|
||||||
|
Reference in New Issue
Block a user