blob: 758def8dd938d41af2952fcf096885ccbabd5883 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
// Copyright Ferdinand Majerech 2011-2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
module dyaml.test.inputoutput;
@safe unittest
{
import std.array : join, split;
import std.conv : to;
import std.exception : assertThrown;
import std.file : readText;
import std.system : endian, Endian;
import dyaml : Loader, Node, YAMLException;
import dyaml.test.common : run;
/**
Get an UTF-16 byte order mark.
Params: wrong = Get the incorrect BOM for this system.
Returns: UTF-16 byte order mark.
*/
static wchar bom16(bool wrong = false) pure @safe
{
wchar little = '\uFEFF';
wchar big = '\uFFFE';
if (!wrong)
{
return endian == Endian.littleEndian ? little : big;
}
return endian == Endian.littleEndian ? big : little;
}
/**
Get an UTF-32 byte order mark.
Params: wrong = Get the incorrect BOM for this system.
Returns: UTF-32 byte order mark.
*/
static dchar bom32(bool wrong = false) pure @safe
{
dchar little = '\uFEFF';
dchar big = '\uFFFE';
if (!wrong)
{
return endian == Endian.littleEndian ? little : big;
}
return endian == Endian.littleEndian ? big : little;
}
/**
Unicode input unittest. Tests various encodings.
Params: unicodeFilename = File name to read from.
*/
static void testUnicodeInput(string unicodeFilename) @safe
{
string data = readText(unicodeFilename);
string expected = data.split().join(" ");
Node output = Loader.fromString(data).load();
assert(output.as!string == expected);
foreach (buffer; [cast(ubyte[]) (bom16() ~ data.to!(wchar[])),
cast(ubyte[]) (bom32() ~ data.to!(dchar[]))])
{
output = Loader.fromBuffer(buffer).load();
assert(output.as!string == expected);
}
}
/**
Unicode input error unittest. Tests various encodings with incorrect BOMs.
Params: unicodeFilename = File name to read from.
*/
static void testUnicodeInputErrors(string unicodeFilename) @safe
{
string data = readText(unicodeFilename);
foreach (buffer; [cast(ubyte[]) (data.to!(wchar[])),
cast(ubyte[]) (data.to!(dchar[])),
cast(ubyte[]) (bom16(true) ~ data.to!(wchar[])),
cast(ubyte[]) (bom32(true) ~ data.to!(dchar[]))])
{
assertThrown(Loader.fromBuffer(buffer).load());
}
}
run(&testUnicodeInput, ["unicode"]);
run(&testUnicodeInputErrors, ["unicode"]);
}
|