tests.cpp 1.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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");
18
			files.push_back("tests/scalars.yaml");
19
			files.push_back("tests/directives.yaml");
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

			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;

68
				std::ofstream fout("tests/out.yaml");
69
70
71
72
				fout << "---\n";
				fout << firstTry << std::endl;
				fout << "---\n";
				fout << secondTry << std::endl;
73
74
			} catch(ParserException& e) {
				std::cout << file << " (line " << e.line + 1 << ", col " << e.column + 1 << "): " << e.msg << std::endl;
75
76
77
78
79
80
81
				return false;
			}

			return true;
		}
	}
}