tests.cpp 2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#include "yaml.h"
#include "tests.h"
#include "parser.h"
#include <fstream>
#include <sstream>
#include <vector>
#include <iostream>

namespace Test
{
	// runs all the tests on all data we have
12
	void RunAll(bool verbose)
13
14
	{
		std::vector <std::string> files;
15
16
17
18
		files.push_back("tests/simple.yaml");
		files.push_back("tests/mixed.yaml");
		files.push_back("tests/scalars.yaml");
		files.push_back("tests/directives.yaml");
19
20
21

		bool passed = true;
		for(unsigned i=0;i<files.size();i++) {
22
23
			if(!Inout(files[i], verbose)) {
				std::cout << "Inout test failed on " << files[i] << "\n";
24
				passed = false;
25
26
			} else
				std::cout << "Inout test passed: " << files[i] << "\n";
27
28
29
30
31
32
33
34
		}

		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
35
	bool Inout(const std::string& file, bool verbose)
36
	{
37
		std::ifstream fin(file.c_str());
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

		try {
			// read and output
			YAML::Parser parser(fin);
			if(!parser)
				return false;

			YAML::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(YAML::ParserException& e) {
74
75
76
77
78
79
80
81
82
			std::cout << file << " (line " << e.line + 1 << ", col " << e.column + 1 << "): " << e.msg << std::endl;
			
			if(verbose) {
				std::cout << "Token queue:\n";
				std::ifstream f(file.c_str());
				YAML::Parser p(f);
				p.PrintTokens(std::cout);
			}
			
83
84
85
86
87
88
			return false;
		}

		return true;
	}
}