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