Merge pull request #5 from dlang-tour/add-file-dumping-test

Add file dumping test
This commit is contained in:
Petar Kirov 2016-12-10 20:11:35 +02:00 committed by GitHub
commit 228d8f57cb
3 changed files with 103 additions and 50 deletions

14
.editorconfig Normal file
View file

@ -0,0 +1,14 @@
# EditorConfig file: http://EditorConfig.org
# top-most EditorConfig file
root = true
[*]
end_of_line = lf
insert_final_newline = true
[*.d]
charset = utf-8
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

View file

@ -6,7 +6,7 @@ This is a fork of the D:YAML library by kiith-sa (https://github.com/kiith-sa/D-
The intent of this fork is to provide a version suitable for use by dlang-tour, until
fixes are accepted upstream.
.. image:: https://travis-ci.org/dlang-tour/d-yaml-dlang-tour.svg?branch=master
.. image:: https://travis-ci.org/dlang-tour/dyaml-dlang-tour.svg?branch=master
.. image:: https://img.shields.io/dub/v/dyaml-dlang-tour.svg
:target: http://code.dlang.org/packages/dyaml-dlang-tour

View file

@ -1,74 +1,113 @@
module dyaml.stream;
enum BOM {
UTF8, /// UTF-8
UTF16LE, /// UTF-16 Little Endian
UTF16BE, /// UTF-16 Big Endian
UTF32LE, /// UTF-32 Little Endian
UTF32BE, /// UTF-32 Big Endian
enum BOM
{
UTF8, /// UTF-8
UTF16LE, /// UTF-16 Little Endian
UTF16BE, /// UTF-16 Big Endian
UTF32LE, /// UTF-32 Little Endian
UTF32BE, /// UTF-32 Big Endian
}
import std.system;
private enum int NBOMS = 5;
immutable Endian[NBOMS] BOMEndian =
[ std.system.endian,
Endian.littleEndian, Endian.bigEndian,
Endian.littleEndian, Endian.bigEndian
];
[
std.system.endian,
Endian.littleEndian, Endian.bigEndian,
Endian.littleEndian, Endian.bigEndian
];
immutable ubyte[][NBOMS] ByteOrderMarks =
[ [0xEF, 0xBB, 0xBF],
[0xFF, 0xFE],
[0xFE, 0xFF],
[0xFF, 0xFE, 0x00, 0x00],
[0x00, 0x00, 0xFE, 0xFF]
];
[
[0xEF, 0xBB, 0xBF],
[0xFF, 0xFE],
[0xFE, 0xFF],
[0xFF, 0xFE, 0x00, 0x00],
[0x00, 0x00, 0xFE, 0xFF]
];
interface YStream {
void writeExact(const void* buffer, size_t size);
size_t write(const(ubyte)[] buffer);
void flush();
@property bool writeable();
interface YStream
{
void writeExact(const void* buffer, size_t size);
size_t write(const(ubyte)[] buffer);
void flush();
@property bool writeable();
}
class YMemoryStream : YStream {
ubyte[] data;
class YMemoryStream : YStream
{
ubyte[] data;
void writeExact(const void* buffer, size_t size) {
data ~= cast(ubyte[])buffer[0 .. size];
}
void writeExact(const void* buffer, size_t size)
{
data ~= cast(ubyte[])buffer[0 .. size];
}
size_t write(const(ubyte)[] buffer) {
data ~= buffer;
return buffer.length;
}
size_t write(const(ubyte)[] buffer)
{
data ~= buffer;
return buffer.length;
}
void flush() {}
void flush() {}
@property bool writeable() { return true; }
@property bool writeable() { return true; }
}
class YFile : YStream {
static import std.stdio;
std.stdio.File file;
class YFile : YStream
{
static import std.stdio;
std.stdio.File file;
this(string fn) {
this.file = std.stdio.File(fn, "w");
}
this(string fn)
{
this.file = std.stdio.File(fn, "w");
}
void writeExact(const void* buffer, size_t size) {
this.file.rawWrite(cast(const) buffer[0 .. size]);
}
void writeExact(const void* buffer, size_t size)
{
this.file.rawWrite(cast(const) buffer[0 .. size]);
}
size_t write(const(ubyte)[] buffer) {
this.file.rawWrite(buffer);
return buffer.length;
}
size_t write(const(ubyte)[] buffer)
{
this.file.rawWrite(buffer);
return buffer.length;
}
void flush() {
this.file.flush();
}
void flush()
{
this.file.flush();
}
@property bool writeable() { return true; }
@property bool writeable() { return true; }
}
unittest
{
import dyaml.dumper, dyaml.loader, dyaml.node;
import std.file : readText, remove;
char[] test = ("Hello World : [Hello, World]\n" ~
"Answer: 42").dup;
//Read the input.
Node expected = Loader.fromString(test).load();
assert(expected["Hello World"][0] == "Hello");
assert(expected["Hello World"][1] == "World");
assert(expected["Answer"].as!int == 42);
//Dump the loaded document to output.yaml.
Dumper("output.yaml").dump(expected);
// Load the file and verify that it was saved correctly.
Node actual = Loader("output.yaml").load();
assert(actual["Hello World"][0] == "Hello");
assert(actual["Hello World"][1] == "World");
assert(actual["Answer"].as!int == 42);
assert(actual == expected);
// Clean up.
remove("output.yaml");
}