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.testtokens;
8 
9 
10 version(unittest)
11 {
12 
13 import std.array;
14 import std.file;
15 
16 import dyaml.testcommon;
17 import dyaml.token;
18 
19 
20 /**
21  * Test tokens output by scanner.
22  *
23  * Params:  verbose        = Print verbose output?
24  *          dataFilename   = File to scan.
25  *          tokensFilename = File containing expected tokens.
26  */
27 void testTokens(bool verbose, string dataFilename, string tokensFilename)
28 {
29     //representations of YAML tokens in tokens file.
30     auto replace = [TokenID.Directive          : "%"   ,
31                     TokenID.DocumentStart      : "---" ,
32                     TokenID.DocumentEnd        : "..." ,
33                     TokenID.Alias              : "*"   ,
34                     TokenID.Anchor             : "&"   ,
35                     TokenID.Tag                : "!"   ,
36                     TokenID.Scalar             : "_"   ,
37                     TokenID.BlockSequenceStart : "[["  ,
38                     TokenID.BlockMappingStart  : "{{"  ,
39                     TokenID.BlockEnd           : "]}"  ,
40                     TokenID.FlowSequenceStart  : "["   ,
41                     TokenID.FlowSequenceEnd    : "]"   ,
42                     TokenID.FlowMappingStart   : "{"   ,
43                     TokenID.FlowMappingEnd     : "}"   ,
44                     TokenID.BlockEntry         : ","   ,
45                     TokenID.FlowEntry          : ","   ,
46                     TokenID.Key                : "?"   ,
47                     TokenID.Value              : ":"   ];
48 
49     string[] tokens1;
50     string[] tokens2 = readText(tokensFilename).split();
51     scope(exit)
52     {
53         if(verbose){writeln("tokens1: ", tokens1, "\ntokens2: ", tokens2);}
54     }
55 
56     auto loader = Loader(dataFilename);
57     foreach(token; loader.scan())
58     {
59         if(token.id != TokenID.StreamStart && token.id != TokenID.StreamEnd)
60         {
61             tokens1 ~= replace[token.id];
62         }
63     }
64 
65     assert(tokens1 == tokens2);
66 }
67 
68 /**
69  * Test scanner by scanning a file, expecting no errors.
70  *
71  * Params:  verbose           = Print verbose output?
72  *          dataFilename      = File to scan.
73  *          canonicalFilename = Another file to scan, in canonical YAML format.
74  */
75 void testScanner(bool verbose, string dataFilename, string canonicalFilename)
76 {
77     foreach(filename; [dataFilename, canonicalFilename])
78     {
79         string[] tokens;
80         scope(exit)
81         {
82             if(verbose){writeln(tokens);}
83         }
84         auto loader = Loader(filename);
85         foreach(ref token; loader.scan()){tokens ~= to!string(token.id);}
86     }
87 }
88 
89 unittest
90 {
91     writeln("D:YAML tokens unittest");
92     run("testTokens",  &testTokens, ["data", "tokens"]);
93     run("testScanner", &testScanner, ["data", "canonical"]);
94 }
95 
96 } // version(unittest)