dyaml/source/dyaml/test/inputoutput.d

94 lines
2.6 KiB
D
Raw Normal View History

2011-08-16 12:53:13 +00:00
2014-07-31 01:12:25 +00:00
// Copyright Ferdinand Majerech 2011-2014.
2011-08-16 12:53:13 +00:00
// 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;
2011-08-16 12:53:13 +00:00
version(unittest)
{
2011-08-16 12:53:13 +00:00
import std.array;
import std.file;
import std.system;
import dyaml.test.common;
2011-08-16 12:53:13 +00:00
2014-07-31 01:12:25 +00:00
/// Get an UTF-16 byte order mark.
///
2014-07-31 01:12:25 +00:00
/// Params: wrong = Get the incorrect BOM for this system.
///
2014-07-31 01:12:25 +00:00
/// Returns: UTF-16 byte order mark.
2018-03-23 21:35:16 +00:00
wchar bom16(bool wrong = false) pure @safe
2011-08-16 12:53:13 +00:00
{
2018-03-23 21:35:16 +00:00
wchar little = '\uFEFF';
wchar big = '\uFFFE';
if(!wrong){return endian == Endian.littleEndian ? little : big;}
return endian == Endian.littleEndian ? big : little;
2011-08-16 12:53:13 +00:00
}
2014-07-31 01:12:25 +00:00
/// Get an UTF-32 byte order mark.
///
2014-07-31 01:12:25 +00:00
/// Params: wrong = Get the incorrect BOM for this system.
///
2014-07-31 01:12:25 +00:00
/// Returns: UTF-32 byte order mark.
2018-03-23 21:35:16 +00:00
dchar bom32(bool wrong = false) pure @safe
2011-08-16 12:53:13 +00:00
{
2018-03-23 21:35:16 +00:00
dchar little = '\uFEFF';
dchar big = '\uFFFE';
if(!wrong){return endian == Endian.littleEndian ? little : big;}
return endian == Endian.littleEndian ? big : little;
2011-08-16 12:53:13 +00:00
}
2014-07-31 01:12:25 +00:00
/// Unicode input unittest. Tests various encodings.
///
/// Params: unicodeFilename = File name to read from.
void testUnicodeInput(string unicodeFilename) @safe
2011-08-16 12:53:13 +00:00
{
string data = readText(unicodeFilename);
string expected = data.split().join(" ");
Node output = Loader.fromString(data).load();
assert(output.as!string == expected);
2011-08-16 12:53:13 +00:00
foreach(buffer; [cast(ubyte[])(bom16() ~ data.to!(wchar[])),
cast(ubyte[])(bom32() ~ data.to!(dchar[]))])
2011-08-16 12:53:13 +00:00
{
output = Loader.fromBuffer(buffer).load();
assert(output.as!string == expected);
2011-08-16 12:53:13 +00:00
}
}
2014-07-31 01:12:25 +00:00
/// Unicode input error unittest. Tests various encodings with incorrect BOMs.
///
/// Params: unicodeFilename = File name to read from.
void testUnicodeInputErrors(string unicodeFilename) @safe
2011-08-16 12:53:13 +00:00
{
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[]))])
2011-08-16 12:53:13 +00:00
{
try { Loader.fromBuffer(buffer).load(); }
2011-08-16 12:53:13 +00:00
catch(YAMLException e)
{
printException(e);
2011-08-16 12:53:13 +00:00
continue;
}
assert(false, "Expected an exception");
}
}
2018-03-23 21:35:16 +00:00
@safe unittest
2011-08-16 12:53:13 +00:00
{
2018-04-30 22:11:36 +00:00
printProgress("D:YAML I/O unittest");
2011-08-16 12:53:13 +00:00
run("testUnicodeInput", &testUnicodeInput, ["unicode"]);
run("testUnicodeInputErrors", &testUnicodeInputErrors, ["unicode"]);
}
} // version(unittest)