1 
2 //          Copyright Ferdinand Majerech 2011-2014.
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.testinputoutput;
8 
9 
10 version(unittest)
11 {
12 
13 import std.array;
14 import std.file;
15 import std.system;
16 
17 import dyaml.testcommon;
18 import dyaml.stream;
19 
20 
21 alias std.system.endian endian;
22 
23 /// Get an UTF-16 byte order mark.
24 /// 
25 /// Params:  wrong = Get the incorrect BOM for this system.
26 /// 
27 /// Returns: UTF-16 byte order mark.
28 wchar bom16(bool wrong = false) pure
29 {
30     wchar little  = *(cast(wchar*)ByteOrderMarks[BOM.UTF16LE]);
31     wchar big     = *(cast(wchar*)ByteOrderMarks[BOM.UTF16BE]);
32     if(!wrong){return endian == Endian.littleEndian ? little : big;}
33     return endian == Endian.littleEndian ? big : little;
34 }
35 
36 /// Get an UTF-32 byte order mark.
37 /// 
38 /// Params:  wrong = Get the incorrect BOM for this system.
39 /// 
40 /// Returns: UTF-32 byte order mark.
41 dchar bom32(bool wrong = false) pure
42 {
43     dchar little = *(cast(dchar*)ByteOrderMarks[BOM.UTF32LE]);
44     dchar big    = *(cast(dchar*)ByteOrderMarks[BOM.UTF32BE]);
45     if(!wrong){return endian == Endian.littleEndian ? little : big;}
46     return endian == Endian.littleEndian ? big : little;
47 }
48 
49 /// Unicode input unittest. Tests various encodings.
50 /// 
51 /// Params:  verbose         = Print verbose output?
52 ///          unicodeFilename = File name to read from.
53 void testUnicodeInput(bool verbose, string unicodeFilename)
54 {
55     string data     = readText(unicodeFilename);
56     string expected = data.split().join(" ");
57 
58     Node output = Loader(cast(void[])data.to!(char[])).load();
59     assert(output.as!string == expected);
60 
61     foreach(buffer; [cast(void[])(bom16() ~ data.to!(wchar[])), 
62                      cast(void[])(bom32() ~ data.to!(dchar[]))])
63     {
64         output = Loader(buffer).load();
65         assert(output.as!string == expected);
66     }
67 }
68 
69 /// Unicode input error unittest. Tests various encodings with incorrect BOMs.
70 /// 
71 /// Params:  verbose         = Print verbose output?
72 ///          unicodeFilename = File name to read from.
73 void testUnicodeInputErrors(bool verbose, string unicodeFilename)
74 {
75     string data = readText(unicodeFilename);
76     foreach(buffer; [cast(void[])(data.to!(wchar[])),
77                      cast(void[])(data.to!(dchar[])),
78                      cast(void[])(bom16(true) ~ data.to!(wchar[])),
79                      cast(void[])(bom32(true) ~ data.to!(dchar[]))])
80     {
81         try { Loader(buffer).load(); }
82         catch(YAMLException e)
83         {
84             if(verbose) { writeln(typeid(e).toString(), "\n", e); }
85             continue;
86         }
87         assert(false, "Expected an exception");
88     }
89 }
90 
91 
92 unittest
93 {
94     writeln("D:YAML I/O unittest");
95     run("testUnicodeInput", &testUnicodeInput, ["unicode"]);
96     run("testUnicodeInputErrors", &testUnicodeInputErrors, ["unicode"]);
97 }
98 
99 } // version(unittest)