1 
2 //          Copyright Ferdinand Majerech 2011.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 module dyaml.testcompare;
8 
9 
10 version(unittest)
11 {
12 
13 import dyaml.testcommon;
14 import dyaml.token;
15 
16 
17 /// Test parser by comparing output from parsing two equivalent YAML files.
18 /// 
19 /// Params:  verbose           = Print verbose output?
20 ///          dataFilename      = YAML file to parse.
21 ///          canonicalFilename = Another file to parse, in canonical YAML format.
22 void testParser(bool verbose, string dataFilename, string canonicalFilename)
23 {
24     auto dataEvents = Loader(dataFilename).parse();
25     auto canonicalEvents = Loader(canonicalFilename).parse();
26 
27     assert(dataEvents.length == canonicalEvents.length);
28 
29     foreach(e; 0 .. dataEvents.length)
30     {
31         assert(dataEvents[e].id == canonicalEvents[e].id);
32     }
33 }
34 
35 
36 /// Test loader by comparing output from loading two equivalent YAML files.
37 /// 
38 /// Params:  verbose           = Print verbose output?
39 ///          dataFilename      = YAML file to load.
40 ///          canonicalFilename = Another file to load, in canonical YAML format.
41 void testLoader(bool verbose, string dataFilename, string canonicalFilename)
42 {
43     auto data = Loader(dataFilename).loadAll();
44     auto canonical = Loader(canonicalFilename).loadAll();
45 
46     assert(data.length == canonical.length, "Unequal node count");
47     foreach(n; 0 .. data.length)
48     {
49         if(data[n] != canonical[n])
50         {
51             if(verbose)
52             {
53                 writeln("Normal value:");
54                 writeln(data[n].debugString);
55                 writeln("\n");
56                 writeln("Canonical value:");
57                 writeln(canonical[n].debugString);
58             }
59             assert(false, "testLoader(" ~ dataFilename ~ ", " ~ canonicalFilename ~ ") failed");
60         }
61     }
62 }
63 
64 
65 unittest
66 {
67     writeln("D:YAML comparison unittest");
68     run("testParser", &testParser, ["data", "canonical"]);
69     run("testLoader", &testLoader, ["data", "canonical"], ["test_loader_skip"]);
70 }
71 
72 } // version(unittest)