mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 20:51:16 +00:00
Moved the testing source to the yaml-reader folder.
This commit is contained in:
93
src/main.cpp
93
src/main.cpp
@@ -1,93 +0,0 @@
|
||||
#include "parser.h"
|
||||
#include "tests.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef _DEBUG
|
||||
#pragma comment(lib, "yamlcppd.lib")
|
||||
#else
|
||||
#pragma comment(lib, "yamlcpp.lib")
|
||||
#endif
|
||||
|
||||
struct Vec3 {
|
||||
float x, y, z;
|
||||
|
||||
friend std::ostream& operator << (std::ostream& out, const Vec3& v) {
|
||||
out << v.x << " " << v.y << " " << v.z;
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
void operator >> (const YAML::Node& node, Vec3& v)
|
||||
{
|
||||
node[0] >> v.x;
|
||||
node[1] >> v.y;
|
||||
node[2] >> v.z;
|
||||
}
|
||||
|
||||
struct Room {
|
||||
std::string name;
|
||||
Vec3 pos, size;
|
||||
float height;
|
||||
|
||||
friend std::ostream& operator << (std::ostream& out, const Room& room) {
|
||||
out << "Name: " << room.name << std::endl;
|
||||
out << "Pos: " << room.pos << std::endl;
|
||||
out << "Size: " << room.size << std::endl;
|
||||
out << "Height: " << room.height << std::endl;
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
void operator >> (const YAML::Node& node, Room& room)
|
||||
{
|
||||
node["name"] >> room.name;
|
||||
node["pos"] >> room.pos;
|
||||
node["size"] >> room.size;
|
||||
node["height"] >> room.height;
|
||||
}
|
||||
|
||||
struct Level {
|
||||
std::vector <Room> rooms;
|
||||
|
||||
friend std::ostream& operator << (std::ostream& out, const Level& level) {
|
||||
for(unsigned i=0;i<level.rooms.size();i++) {
|
||||
out << level.rooms[i];
|
||||
out << "---------------------------------------\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
void operator >> (const YAML::Node& node, Level& level)
|
||||
{
|
||||
const YAML::Node& rooms = node["rooms"];
|
||||
for(unsigned i=0;i<rooms.size();i++) {
|
||||
Room room;
|
||||
rooms[i] >> room;
|
||||
level.rooms.push_back(room);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
YAML::Test::RunAll();
|
||||
|
||||
//std::ifstream fin("test.yaml");
|
||||
|
||||
//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;
|
||||
}
|
@@ -1,81 +0,0 @@
|
||||
#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");
|
||||
files.push_back("tests/scalars.yaml");
|
||||
files.push_back("tests/directives.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("tests/out.yaml");
|
||||
fout << "---\n";
|
||||
fout << firstTry << std::endl;
|
||||
fout << "---\n";
|
||||
fout << secondTry << std::endl;
|
||||
} catch(ParserException& e) {
|
||||
std::cout << file << " (line " << e.line + 1 << ", col " << e.column + 1 << "): " << e.msg << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
10
src/tests.h
10
src/tests.h
@@ -1,10 +0,0 @@
|
||||
#include <string>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
namespace Test {
|
||||
void RunAll();
|
||||
|
||||
bool Inout(const std::string& file);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user