mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 04:41:16 +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 "tests.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
@@ -64,21 +65,23 @@ void operator >> (const YAML::Node& node, Level& level)
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream fin("test.yaml");
|
||||
YAML::Test::RunAll();
|
||||
|
||||
try {
|
||||
YAML::Parser parser(fin);
|
||||
if(!parser)
|
||||
return 0;
|
||||
//std::ifstream fin("test.yaml");
|
||||
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
std::cout << doc;
|
||||
} catch(YAML::Exception&) {
|
||||
std::cout << "Error parsing the yaml!\n";
|
||||
}
|
||||
//try {
|
||||
// YAML::Parser parser(fin);
|
||||
// if(!parser)
|
||||
// return 0;
|
||||
|
||||
// YAML::Node doc;
|
||||
// parser.GetNextDocument(doc);
|
||||
// std::cout << doc;
|
||||
//} catch(YAML::Exception&) {
|
||||
// std::cout << "Error parsing the yaml!\n";
|
||||
//}
|
||||
|
||||
getchar();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
10
parser.cpp
10
parser.cpp
@@ -8,8 +8,7 @@ namespace YAML
|
||||
{
|
||||
Parser::Parser(std::istream& in): m_pScanner(0)
|
||||
{
|
||||
m_pScanner = new Scanner(in);
|
||||
m_state.Reset();
|
||||
Load(in);
|
||||
}
|
||||
|
||||
Parser::~Parser()
|
||||
@@ -22,6 +21,13 @@ namespace YAML
|
||||
return m_pScanner->PeekNextToken() != 0;
|
||||
}
|
||||
|
||||
void Parser::Load(std::istream& in)
|
||||
{
|
||||
delete m_pScanner;
|
||||
m_pScanner = new Scanner(in);
|
||||
m_state.Reset();
|
||||
}
|
||||
|
||||
// GetNextDocument
|
||||
// . Reads the next document in the queue (of tokens).
|
||||
// . Throws (ScannerException|ParserException)s on errors.
|
||||
|
1
parser.h
1
parser.h
@@ -20,6 +20,7 @@ namespace YAML
|
||||
|
||||
operator bool() const;
|
||||
|
||||
void Load(std::istream& in);
|
||||
void GetNextDocument(Node& document);
|
||||
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"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\tests.cpp"
|
||||
>
|
||||
</File>
|
||||
<Filter
|
||||
Name="Scanner"
|
||||
>
|
||||
@@ -247,6 +251,10 @@
|
||||
RelativePath=".\exceptions.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\tests.h"
|
||||
>
|
||||
</File>
|
||||
<Filter
|
||||
Name="Scanner"
|
||||
>
|
||||
|
Reference in New Issue
Block a user