make Loader interface a bit more consistent (#124)
make Loader interface a bit more consistent merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
This commit is contained in:
parent
a20d7143e7
commit
a0ac38fbd7
|
@ -78,7 +78,7 @@ void main()
|
||||||
constructor.addConstructorScalar("!color", &constructColorScalar);
|
constructor.addConstructorScalar("!color", &constructColorScalar);
|
||||||
constructor.addConstructorMapping("!color-mapping", &constructColorMapping);
|
constructor.addConstructorMapping("!color-mapping", &constructColorMapping);
|
||||||
|
|
||||||
auto loader = Loader("input.yaml");
|
auto loader = Loader.fromFile("input.yaml");
|
||||||
loader.constructor = constructor;
|
loader.constructor = constructor;
|
||||||
|
|
||||||
auto root = loader.load();
|
auto root = loader.load();
|
||||||
|
|
|
@ -4,7 +4,7 @@ import dyaml;
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
//Read the input.
|
//Read the input.
|
||||||
Node root = Loader("input.yaml").load();
|
Node root = Loader.fromFile("input.yaml").load();
|
||||||
|
|
||||||
//Display the data read.
|
//Display the data read.
|
||||||
foreach(string word; root["Hello World"])
|
foreach(string word; root["Hello World"])
|
||||||
|
|
|
@ -83,7 +83,7 @@ void main()
|
||||||
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
||||||
"0123456789abcdefABCDEF");
|
"0123456789abcdefABCDEF");
|
||||||
|
|
||||||
auto loader = Loader("input.yaml");
|
auto loader = Loader.fromFile("input.yaml");
|
||||||
loader.constructor = constructor;
|
loader.constructor = constructor;
|
||||||
loader.resolver = resolver;
|
loader.resolver = resolver;
|
||||||
|
|
||||||
|
|
|
@ -104,7 +104,7 @@ void main(string[] args) //@safe
|
||||||
else { fileWorkingCopy[] = fileInMemory[]; }
|
else { fileWorkingCopy[] = fileInMemory[]; }
|
||||||
void[] fileToLoad = reload ? fileInMemory : fileWorkingCopy;
|
void[] fileToLoad = reload ? fileInMemory : fileWorkingCopy;
|
||||||
|
|
||||||
auto loader = Loader(fileToLoad);
|
auto loader = Loader.fromBuffer(fileToLoad);
|
||||||
if(scanOnly)
|
if(scanOnly)
|
||||||
{
|
{
|
||||||
loader.scanBench();
|
loader.scanBench();
|
||||||
|
|
|
@ -264,7 +264,7 @@ Node generateNode(const string type, bool root = false)
|
||||||
|
|
||||||
Node[] generate(const string configFileName)
|
Node[] generate(const string configFileName)
|
||||||
{
|
{
|
||||||
config = Loader(configFileName).load();
|
config = Loader.fromFile(configFileName).load();
|
||||||
|
|
||||||
minNodesDocument = config["min-nodes-per-document"].as!long;
|
minNodesDocument = config["min-nodes-per-document"].as!long;
|
||||||
|
|
||||||
|
|
|
@ -90,7 +90,7 @@ void main(string[] args)
|
||||||
writeln("------------------------------------------------------------");
|
writeln("------------------------------------------------------------");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto loader = Loader(file);
|
auto loader = Loader.fromFile(file);
|
||||||
|
|
||||||
size_t idx = 0;
|
size_t idx = 0;
|
||||||
foreach(ref document; loader)
|
foreach(ref document; loader)
|
||||||
|
|
|
@ -159,7 +159,7 @@ final class Constructor
|
||||||
return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2]));
|
return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2]));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto loader = Loader.fromString("!mystruct 12:34:56".dup);
|
auto loader = Loader.fromString("!mystruct 12:34:56");
|
||||||
auto constructor = new Constructor;
|
auto constructor = new Constructor;
|
||||||
constructor.addConstructorScalar("!mystruct", &constructMyStructScalar);
|
constructor.addConstructorScalar("!mystruct", &constructMyStructScalar);
|
||||||
loader.constructor = constructor;
|
loader.constructor = constructor;
|
||||||
|
@ -200,7 +200,7 @@ final class Constructor
|
||||||
//!mystruct [x, y, z]
|
//!mystruct [x, y, z]
|
||||||
return MyStruct(node[0].as!int, node[1].as!int, node[2].as!int);
|
return MyStruct(node[0].as!int, node[1].as!int, node[2].as!int);
|
||||||
}
|
}
|
||||||
auto loader = Loader.fromString("!mystruct [1,2,3]".dup);
|
auto loader = Loader.fromString("!mystruct [1,2,3]");
|
||||||
auto constructor = new Constructor;
|
auto constructor = new Constructor;
|
||||||
constructor.addConstructorSequence("!mystruct", &constructMyStructSequence);
|
constructor.addConstructorSequence("!mystruct", &constructMyStructSequence);
|
||||||
loader.constructor = constructor;
|
loader.constructor = constructor;
|
||||||
|
@ -241,7 +241,7 @@ final class Constructor
|
||||||
//!mystruct {"x": x, "y": y, "z": z}
|
//!mystruct {"x": x, "y": y, "z": z}
|
||||||
return MyStruct(node["x"].as!int, node["y"].as!int, node["z"].as!int);
|
return MyStruct(node["x"].as!int, node["y"].as!int, node["z"].as!int);
|
||||||
}
|
}
|
||||||
auto loader = Loader.fromString(`!mystruct {"x": 11, "y": 22, "z": 33}`.dup);
|
auto loader = Loader.fromString(`!mystruct {"x": 11, "y": 22, "z": 33}`);
|
||||||
auto constructor = new Constructor;
|
auto constructor = new Constructor;
|
||||||
constructor.addConstructorMapping("!mystruct", &constructMyStructMapping);
|
constructor.addConstructorMapping("!mystruct", &constructMyStructMapping);
|
||||||
loader.constructor = constructor;
|
loader.constructor = constructor;
|
||||||
|
@ -836,8 +836,8 @@ MyStruct constructMyStructMapping(ref Node node) @safe
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
char[] data = "!mystruct 1:2:3".dup;
|
string data = "!mystruct 1:2:3";
|
||||||
auto loader = Loader(data);
|
auto loader = Loader.fromString(data);
|
||||||
auto constructor = new Constructor;
|
auto constructor = new Constructor;
|
||||||
constructor.addConstructorScalar("!mystruct", &constructMyStructScalar);
|
constructor.addConstructorScalar("!mystruct", &constructMyStructScalar);
|
||||||
loader.constructor = constructor;
|
loader.constructor = constructor;
|
||||||
|
@ -848,8 +848,8 @@ MyStruct constructMyStructMapping(ref Node node) @safe
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
char[] data = "!mystruct [1, 2, 3]".dup;
|
string data = "!mystruct [1, 2, 3]";
|
||||||
auto loader = Loader(data);
|
auto loader = Loader.fromString(data);
|
||||||
auto constructor = new Constructor;
|
auto constructor = new Constructor;
|
||||||
constructor.addConstructorSequence("!mystruct", &constructMyStructSequence);
|
constructor.addConstructorSequence("!mystruct", &constructMyStructSequence);
|
||||||
loader.constructor = constructor;
|
loader.constructor = constructor;
|
||||||
|
@ -860,8 +860,8 @@ MyStruct constructMyStructMapping(ref Node node) @safe
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
char[] data = "!mystruct {x: 1, y: 2, z: 3}".dup;
|
string data = "!mystruct {x: 1, y: 2, z: 3}";
|
||||||
auto loader = Loader(data);
|
auto loader = Loader.fromString(data);
|
||||||
auto constructor = new Constructor;
|
auto constructor = new Constructor;
|
||||||
constructor.addConstructorMapping("!mystruct", &constructMyStructMapping);
|
constructor.addConstructorMapping("!mystruct", &constructMyStructMapping);
|
||||||
loader.constructor = constructor;
|
loader.constructor = constructor;
|
||||||
|
|
|
@ -30,7 +30,7 @@ ScalarStyle scalarStyleHack(ref const(Node) node) @safe nothrow
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
import dyaml;
|
import dyaml;
|
||||||
Node node = Loader.fromString(`"42"`.dup).load(); // loaded from a file
|
Node node = Loader.fromString(`"42"`).load(); // loaded from a file
|
||||||
if(node.isScalar)
|
if(node.isScalar)
|
||||||
{
|
{
|
||||||
assert(node.scalarStyleHack() == ScalarStyle.DoubleQuoted);
|
assert(node.scalarStyleHack() == ScalarStyle.DoubleQuoted);
|
||||||
|
|
|
@ -58,25 +58,25 @@ struct Loader
|
||||||
*
|
*
|
||||||
* Throws: YAMLException if the file could not be opened or read.
|
* Throws: YAMLException if the file could not be opened or read.
|
||||||
*/
|
*/
|
||||||
this(string filename) @trusted
|
static Loader fromFile(string filename) @trusted
|
||||||
{
|
{
|
||||||
name_ = filename;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
this(std.file.read(filename));
|
auto loader = Loader(std.file.read(filename));
|
||||||
|
loader.name_ = filename;
|
||||||
|
return loader;
|
||||||
}
|
}
|
||||||
catch(FileException e)
|
catch(FileException e)
|
||||||
{
|
{
|
||||||
throw new YAMLException("Unable to open file %s for YAML loading: %s"
|
throw new YAMLException("Unable to open file %s for YAML loading: %s"
|
||||||
.format(filename, e.msg));
|
.format(filename, e.msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Construct a Loader to load YAML from a string (char []).
|
/** Construct a Loader to load YAML from a string.
|
||||||
*
|
*
|
||||||
* Params: data = String to load YAML from. $(B will) be overwritten during
|
* Params: data = String to load YAML from. The char[] version $(B will)
|
||||||
* parsing as D:YAML reuses memory. Use data.dup if you don't
|
* overwrite its input during parsing as D:YAML reuses memory.
|
||||||
* want to modify the original string.
|
|
||||||
*
|
*
|
||||||
* Returns: Loader loading YAML from given string.
|
* Returns: Loader loading YAML from given string.
|
||||||
*
|
*
|
||||||
|
@ -88,11 +88,21 @@ struct Loader
|
||||||
{
|
{
|
||||||
return Loader(cast(ubyte[])data);
|
return Loader(cast(ubyte[])data);
|
||||||
}
|
}
|
||||||
///
|
/// Ditto
|
||||||
|
static Loader fromString(string data) @safe
|
||||||
|
{
|
||||||
|
return fromString(data.dup);
|
||||||
|
}
|
||||||
|
/// Load a char[].
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
assert(Loader.fromString("42".dup).load().as!int == 42);
|
assert(Loader.fromString("42".dup).load().as!int == 42);
|
||||||
}
|
}
|
||||||
|
/// Load a string.
|
||||||
|
@safe unittest
|
||||||
|
{
|
||||||
|
assert(Loader.fromString("42").load().as!int == 42);
|
||||||
|
}
|
||||||
|
|
||||||
/** Construct a Loader to load YAML from a buffer.
|
/** Construct a Loader to load YAML from a buffer.
|
||||||
*
|
*
|
||||||
|
@ -111,11 +121,26 @@ struct Loader
|
||||||
*
|
*
|
||||||
* Throws: YAMLException if yamlData contains data illegal in YAML.
|
* Throws: YAMLException if yamlData contains data illegal in YAML.
|
||||||
*/
|
*/
|
||||||
this(void[] yamlData) @trusted
|
static Loader fromBuffer(ubyte[] yamlData) @safe
|
||||||
|
{
|
||||||
|
return Loader(yamlData);
|
||||||
|
}
|
||||||
|
/// Ditto
|
||||||
|
static Loader fromBuffer(void[] yamlData) @system
|
||||||
|
{
|
||||||
|
return Loader(yamlData);
|
||||||
|
}
|
||||||
|
/// Ditto
|
||||||
|
private this(void[] yamlData) @system
|
||||||
|
{
|
||||||
|
this(cast(ubyte[])yamlData);
|
||||||
|
}
|
||||||
|
/// Ditto
|
||||||
|
private this(ubyte[] yamlData) @safe
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
reader_ = new Reader(cast(ubyte[])yamlData);
|
reader_ = new Reader(yamlData);
|
||||||
scanner_ = new Scanner(reader_);
|
scanner_ = new Scanner(reader_);
|
||||||
parser_ = new Parser(scanner_);
|
parser_ = new Parser(scanner_);
|
||||||
}
|
}
|
||||||
|
@ -304,7 +329,7 @@ struct Loader
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
write("example.yaml", "Hello world!");
|
write("example.yaml", "Hello world!");
|
||||||
auto rootNode = Loader("example.yaml").load();
|
auto rootNode = Loader.fromFile("example.yaml").load();
|
||||||
assert(rootNode == "Hello world!");
|
assert(rootNode == "Hello world!");
|
||||||
}
|
}
|
||||||
/// Load all YAML documents from a file:
|
/// Load all YAML documents from a file:
|
||||||
|
@ -319,7 +344,7 @@ struct Loader
|
||||||
"Hello world 2!\n"~
|
"Hello world 2!\n"~
|
||||||
"...\n"
|
"...\n"
|
||||||
);
|
);
|
||||||
auto nodes = Loader("example.yaml").loadAll();
|
auto nodes = Loader.fromFile("example.yaml").loadAll();
|
||||||
assert(nodes.length == 2);
|
assert(nodes.length == 2);
|
||||||
}
|
}
|
||||||
/// Iterate over YAML documents in a file, lazily loading them:
|
/// Iterate over YAML documents in a file, lazily loading them:
|
||||||
|
@ -334,7 +359,7 @@ struct Loader
|
||||||
"Hello world 2!\n"~
|
"Hello world 2!\n"~
|
||||||
"...\n"
|
"...\n"
|
||||||
);
|
);
|
||||||
auto loader = Loader("example.yaml");
|
auto loader = Loader.fromFile("example.yaml");
|
||||||
|
|
||||||
foreach(ref node; loader)
|
foreach(ref node; loader)
|
||||||
{
|
{
|
||||||
|
@ -344,9 +369,9 @@ struct Loader
|
||||||
/// Load YAML from a string:
|
/// Load YAML from a string:
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
char[] yaml_input = ("red: '#ff0000'\n" ~
|
string yaml_input = ("red: '#ff0000'\n" ~
|
||||||
"green: '#00ff00'\n" ~
|
"green: '#00ff00'\n" ~
|
||||||
"blue: '#0000ff'").dup;
|
"blue: '#0000ff'");
|
||||||
|
|
||||||
auto colors = Loader.fromString(yaml_input).load();
|
auto colors = Loader.fromString(yaml_input).load();
|
||||||
|
|
||||||
|
@ -372,8 +397,8 @@ struct Loader
|
||||||
);
|
);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
void[] buffer = read("example.yaml");
|
string buffer = readText("example.yaml");
|
||||||
auto yamlNode = Loader(buffer);
|
auto yamlNode = Loader.fromString(buffer);
|
||||||
|
|
||||||
// Read data from yamlNode here...
|
// Read data from yamlNode here...
|
||||||
}
|
}
|
||||||
|
@ -397,7 +422,7 @@ struct Loader
|
||||||
|
|
||||||
// Add constructor functions / resolver expressions here...
|
// Add constructor functions / resolver expressions here...
|
||||||
|
|
||||||
auto loader = Loader("example.yaml");
|
auto loader = Loader.fromFile("example.yaml");
|
||||||
loader.constructor = constructor;
|
loader.constructor = constructor;
|
||||||
loader.resolver = resolver;
|
loader.resolver = resolver;
|
||||||
auto rootNode = loader.load();
|
auto rootNode = loader.load();
|
||||||
|
|
|
@ -106,7 +106,7 @@ final class Resolver
|
||||||
|
|
||||||
write("example.yaml", "A");
|
write("example.yaml", "A");
|
||||||
|
|
||||||
auto loader = Loader("example.yaml");
|
auto loader = Loader.fromFile("example.yaml");
|
||||||
auto resolver = new Resolver();
|
auto resolver = new Resolver();
|
||||||
resolver.addImplicitResolver("!tag", regex("A.*"), "A");
|
resolver.addImplicitResolver("!tag", regex("A.*"), "A");
|
||||||
loader.resolver = resolver;
|
loader.resolver = resolver;
|
||||||
|
|
|
@ -96,8 +96,8 @@ class YFile : YStream
|
||||||
import dyaml.dumper, dyaml.loader, dyaml.node;
|
import dyaml.dumper, dyaml.loader, dyaml.node;
|
||||||
import std.file : readText, remove;
|
import std.file : readText, remove;
|
||||||
|
|
||||||
char[] test = ("Hello World : [Hello, World]\n" ~
|
string test = "Hello World : [Hello, World]\n" ~
|
||||||
"Answer: 42").dup;
|
"Answer: 42";
|
||||||
//Read the input.
|
//Read the input.
|
||||||
Node expected = Loader.fromString(test).load();
|
Node expected = Loader.fromString(test).load();
|
||||||
assert(expected["Hello World"][0] == "Hello");
|
assert(expected["Hello World"][0] == "Hello");
|
||||||
|
@ -108,7 +108,7 @@ class YFile : YStream
|
||||||
Dumper("output.yaml").dump(expected);
|
Dumper("output.yaml").dump(expected);
|
||||||
|
|
||||||
// Load the file and verify that it was saved correctly.
|
// Load the file and verify that it was saved correctly.
|
||||||
Node actual = Loader("output.yaml").load();
|
Node actual = Loader.fromFile("output.yaml").load();
|
||||||
assert(actual["Hello World"][0] == "Hello");
|
assert(actual["Hello World"][0] == "Hello");
|
||||||
assert(actual["Hello World"][1] == "World");
|
assert(actual["Hello World"][1] == "World");
|
||||||
assert(actual["Answer"].as!int == 42);
|
assert(actual["Answer"].as!int == 42);
|
||||||
|
@ -128,7 +128,7 @@ class YFile : YStream
|
||||||
|
|
||||||
auto dumper = Dumper(fn);
|
auto dumper = Dumper(fn);
|
||||||
dumper.YAMLVersion = null; // supress directive
|
dumper.YAMLVersion = null; // supress directive
|
||||||
dumper.dump(Loader.fromString("Hello world".dup).load);
|
dumper.dump(Loader.fromString("Hello world").load);
|
||||||
|
|
||||||
assert (fn.read()[0..3] == "Hel");
|
assert (fn.read()[0..3] == "Hel");
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,8 +20,8 @@ import dyaml.token;
|
||||||
/// canonicalFilename = Another file to parse, in canonical YAML format.
|
/// canonicalFilename = Another file to parse, in canonical YAML format.
|
||||||
void testParser(string dataFilename, string canonicalFilename) @safe
|
void testParser(string dataFilename, string canonicalFilename) @safe
|
||||||
{
|
{
|
||||||
auto dataEvents = Loader(dataFilename).parse();
|
auto dataEvents = Loader.fromFile(dataFilename).parse();
|
||||||
auto canonicalEvents = Loader(canonicalFilename).parse();
|
auto canonicalEvents = Loader.fromFile(canonicalFilename).parse();
|
||||||
|
|
||||||
assert(dataEvents.length == canonicalEvents.length);
|
assert(dataEvents.length == canonicalEvents.length);
|
||||||
|
|
||||||
|
@ -38,8 +38,8 @@ void testParser(string dataFilename, string canonicalFilename) @safe
|
||||||
/// canonicalFilename = Another file to load, in canonical YAML format.
|
/// canonicalFilename = Another file to load, in canonical YAML format.
|
||||||
void testLoader(string dataFilename, string canonicalFilename) @safe
|
void testLoader(string dataFilename, string canonicalFilename) @safe
|
||||||
{
|
{
|
||||||
auto data = Loader(dataFilename).loadAll();
|
auto data = Loader.fromFile(dataFilename).loadAll();
|
||||||
auto canonical = Loader(canonicalFilename).loadAll();
|
auto canonical = Loader.fromFile(canonicalFilename).loadAll();
|
||||||
|
|
||||||
assert(data.length == canonical.length, "Unequal node count");
|
assert(data.length == canonical.length, "Unequal node count");
|
||||||
foreach(n; 0 .. data.length)
|
foreach(n; 0 .. data.length)
|
||||||
|
|
|
@ -407,7 +407,7 @@ void testConstructor(string dataFilename, string codeDummy) @safe
|
||||||
constructor.addConstructorMapping("!tag1", &constructClass);
|
constructor.addConstructorMapping("!tag1", &constructClass);
|
||||||
constructor.addConstructorScalar("!tag2", &constructStruct);
|
constructor.addConstructorScalar("!tag2", &constructStruct);
|
||||||
|
|
||||||
auto loader = Loader(dataFilename);
|
auto loader = Loader.fromFile(dataFilename);
|
||||||
loader.constructor = constructor;
|
loader.constructor = constructor;
|
||||||
loader.resolver = new Resolver;
|
loader.resolver = new Resolver;
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ bool compareEvents(Event[] events1, Event[] events2) @system
|
||||||
void testEmitterOnData(string dataFilename, string canonicalFilename) @system
|
void testEmitterOnData(string dataFilename, string canonicalFilename) @system
|
||||||
{
|
{
|
||||||
//Must exist due to Anchor, Tags reference counts.
|
//Must exist due to Anchor, Tags reference counts.
|
||||||
auto loader = Loader(dataFilename);
|
auto loader = Loader.fromFile(dataFilename);
|
||||||
auto events = cast(Event[])loader.parse();
|
auto events = cast(Event[])loader.parse();
|
||||||
auto emitStream = new YMemoryStream;
|
auto emitStream = new YMemoryStream;
|
||||||
Dumper(emitStream).emit(events);
|
Dumper(emitStream).emit(events);
|
||||||
|
@ -93,7 +93,7 @@ void testEmitterOnData(string dataFilename, string canonicalFilename) @system
|
||||||
writeln("OUTPUT:\n", cast(string)emitStream.data);
|
writeln("OUTPUT:\n", cast(string)emitStream.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto loader2 = Loader(emitStream.data.dup);
|
auto loader2 = Loader.fromBuffer(emitStream.data);
|
||||||
loader2.name = "TEST";
|
loader2.name = "TEST";
|
||||||
loader2.constructor = new Constructor;
|
loader2.constructor = new Constructor;
|
||||||
loader2.resolver = new Resolver;
|
loader2.resolver = new Resolver;
|
||||||
|
@ -109,7 +109,7 @@ void testEmitterOnData(string dataFilename, string canonicalFilename) @system
|
||||||
void testEmitterOnCanonical(string canonicalFilename) @system
|
void testEmitterOnCanonical(string canonicalFilename) @system
|
||||||
{
|
{
|
||||||
//Must exist due to Anchor, Tags reference counts.
|
//Must exist due to Anchor, Tags reference counts.
|
||||||
auto loader = Loader(canonicalFilename);
|
auto loader = Loader.fromFile(canonicalFilename);
|
||||||
auto events = cast(Event[])loader.parse();
|
auto events = cast(Event[])loader.parse();
|
||||||
foreach(canonical; [false, true])
|
foreach(canonical; [false, true])
|
||||||
{
|
{
|
||||||
|
@ -122,7 +122,7 @@ void testEmitterOnCanonical(string canonicalFilename) @system
|
||||||
writeln("OUTPUT (canonical=", canonical, "):\n",
|
writeln("OUTPUT (canonical=", canonical, "):\n",
|
||||||
cast(string)emitStream.data);
|
cast(string)emitStream.data);
|
||||||
}
|
}
|
||||||
auto loader2 = Loader(emitStream.data.dup);
|
auto loader2 = Loader.fromBuffer(emitStream.data);
|
||||||
loader2.name = "TEST";
|
loader2.name = "TEST";
|
||||||
loader2.constructor = new Constructor;
|
loader2.constructor = new Constructor;
|
||||||
loader2.resolver = new Resolver;
|
loader2.resolver = new Resolver;
|
||||||
|
@ -143,7 +143,7 @@ void testEmitterStyles(string dataFilename, string canonicalFilename) @system
|
||||||
foreach(filename; [dataFilename, canonicalFilename])
|
foreach(filename; [dataFilename, canonicalFilename])
|
||||||
{
|
{
|
||||||
//must exist due to Anchor, Tags reference counts
|
//must exist due to Anchor, Tags reference counts
|
||||||
auto loader = Loader(canonicalFilename);
|
auto loader = Loader.fromFile(canonicalFilename);
|
||||||
auto events = cast(Event[])loader.parse();
|
auto events = cast(Event[])loader.parse();
|
||||||
foreach(flowStyle; [CollectionStyle.Block, CollectionStyle.Flow])
|
foreach(flowStyle; [CollectionStyle.Block, CollectionStyle.Flow])
|
||||||
{
|
{
|
||||||
|
@ -180,7 +180,7 @@ void testEmitterStyles(string dataFilename, string canonicalFilename) @system
|
||||||
to!string(style), ")");
|
to!string(style), ")");
|
||||||
writeln(emitStream.data);
|
writeln(emitStream.data);
|
||||||
}
|
}
|
||||||
auto loader2 = Loader(emitStream.data.dup);
|
auto loader2 = Loader.fromBuffer(emitStream.data);
|
||||||
loader2.name = "TEST";
|
loader2.name = "TEST";
|
||||||
loader2.constructor = new Constructor;
|
loader2.constructor = new Constructor;
|
||||||
loader2.resolver = new Resolver;
|
loader2.resolver = new Resolver;
|
||||||
|
|
|
@ -20,10 +20,8 @@ import dyaml.test.common;
|
||||||
/// Params: errorFilename = File name to read from.
|
/// Params: errorFilename = File name to read from.
|
||||||
void testLoaderError(string errorFilename) @safe
|
void testLoaderError(string errorFilename) @safe
|
||||||
{
|
{
|
||||||
auto buffer = std.file.read(errorFilename);
|
|
||||||
|
|
||||||
Node[] nodes;
|
Node[] nodes;
|
||||||
try { nodes = Loader(buffer).loadAll(); }
|
try { nodes = Loader.fromFile(errorFilename).loadAll(); }
|
||||||
catch(YAMLException e)
|
catch(YAMLException e)
|
||||||
{
|
{
|
||||||
printException(e);
|
printException(e);
|
||||||
|
@ -37,12 +35,9 @@ void testLoaderError(string errorFilename) @safe
|
||||||
/// Params: errorFilename = File name to read from.
|
/// Params: errorFilename = File name to read from.
|
||||||
void testLoaderErrorString(string errorFilename) @safe
|
void testLoaderErrorString(string errorFilename) @safe
|
||||||
{
|
{
|
||||||
// Load file to a buffer, then pass that to the YAML loader.
|
|
||||||
auto buffer = std.file.read(errorFilename);
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto nodes = Loader(buffer).loadAll();
|
auto nodes = Loader.fromFile(errorFilename).loadAll();
|
||||||
}
|
}
|
||||||
catch(YAMLException e)
|
catch(YAMLException e)
|
||||||
{
|
{
|
||||||
|
@ -57,7 +52,7 @@ void testLoaderErrorString(string errorFilename) @safe
|
||||||
/// Params: errorFilename = File name to read from.
|
/// Params: errorFilename = File name to read from.
|
||||||
void testLoaderErrorFilename(string errorFilename) @safe
|
void testLoaderErrorFilename(string errorFilename) @safe
|
||||||
{
|
{
|
||||||
try { auto nodes = Loader(errorFilename).loadAll(); }
|
try { auto nodes = Loader.fromFile(errorFilename).loadAll(); }
|
||||||
catch(YAMLException e)
|
catch(YAMLException e)
|
||||||
{
|
{
|
||||||
printException(e);
|
printException(e);
|
||||||
|
@ -72,7 +67,7 @@ void testLoaderErrorFilename(string errorFilename) @safe
|
||||||
/// Params: errorFilename = File name to read from.
|
/// Params: errorFilename = File name to read from.
|
||||||
void testLoaderErrorSingle(string errorFilename) @safe
|
void testLoaderErrorSingle(string errorFilename) @safe
|
||||||
{
|
{
|
||||||
try { auto nodes = Loader(errorFilename).load(); }
|
try { auto nodes = Loader.fromFile(errorFilename).load(); }
|
||||||
catch(YAMLException e)
|
catch(YAMLException e)
|
||||||
{
|
{
|
||||||
printException(e);
|
printException(e);
|
||||||
|
|
|
@ -54,13 +54,13 @@ void testUnicodeInput(string unicodeFilename) @safe
|
||||||
string data = readText(unicodeFilename);
|
string data = readText(unicodeFilename);
|
||||||
string expected = data.split().join(" ");
|
string expected = data.split().join(" ");
|
||||||
|
|
||||||
Node output = Loader(cast(void[])data.to!(char[])).load();
|
Node output = Loader.fromBuffer(cast(ubyte[])data.to!(char[])).load();
|
||||||
assert(output.as!string == expected);
|
assert(output.as!string == expected);
|
||||||
|
|
||||||
foreach(buffer; [cast(void[])(bom16() ~ data.to!(wchar[])),
|
foreach(buffer; [cast(ubyte[])(bom16() ~ data.to!(wchar[])),
|
||||||
cast(void[])(bom32() ~ data.to!(dchar[]))])
|
cast(ubyte[])(bom32() ~ data.to!(dchar[]))])
|
||||||
{
|
{
|
||||||
output = Loader(buffer).load();
|
output = Loader.fromBuffer(buffer).load();
|
||||||
assert(output.as!string == expected);
|
assert(output.as!string == expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,12 +71,12 @@ void testUnicodeInput(string unicodeFilename) @safe
|
||||||
void testUnicodeInputErrors(string unicodeFilename) @safe
|
void testUnicodeInputErrors(string unicodeFilename) @safe
|
||||||
{
|
{
|
||||||
string data = readText(unicodeFilename);
|
string data = readText(unicodeFilename);
|
||||||
foreach(buffer; [cast(void[])(data.to!(wchar[])),
|
foreach(buffer; [cast(ubyte[])(data.to!(wchar[])),
|
||||||
cast(void[])(data.to!(dchar[])),
|
cast(ubyte[])(data.to!(dchar[])),
|
||||||
cast(void[])(bom16(true) ~ data.to!(wchar[])),
|
cast(ubyte[])(bom16(true) ~ data.to!(wchar[])),
|
||||||
cast(void[])(bom32(true) ~ data.to!(dchar[]))])
|
cast(ubyte[])(bom32(true) ~ data.to!(dchar[]))])
|
||||||
{
|
{
|
||||||
try { Loader(buffer).load(); }
|
try { Loader.fromBuffer(buffer).load(); }
|
||||||
catch(YAMLException e)
|
catch(YAMLException e)
|
||||||
{
|
{
|
||||||
printException(e);
|
printException(e);
|
||||||
|
|
|
@ -65,7 +65,7 @@ void testRepresenterTypes(string codeFilename) @safe
|
||||||
constructor.addConstructorMapping("!tag1", &constructClass);
|
constructor.addConstructorMapping("!tag1", &constructClass);
|
||||||
constructor.addConstructorScalar("!tag2", &constructStruct);
|
constructor.addConstructorScalar("!tag2", &constructStruct);
|
||||||
|
|
||||||
auto loader = Loader(emitStream.data.dup);
|
auto loader = Loader.fromBuffer(emitStream.data);
|
||||||
loader.name = "TEST";
|
loader.name = "TEST";
|
||||||
loader.constructor = constructor;
|
loader.constructor = constructor;
|
||||||
readNodes = loader.loadAll();
|
readNodes = loader.loadAll();
|
||||||
|
|
|
@ -37,7 +37,7 @@ void testImplicitResolver(string dataFilename, string detectFilename) @safe
|
||||||
}
|
}
|
||||||
|
|
||||||
correctTag = readText(detectFilename).strip();
|
correctTag = readText(detectFilename).strip();
|
||||||
node = Loader(dataFilename).load();
|
node = Loader.fromFile(dataFilename).load();
|
||||||
assert(node.isSequence);
|
assert(node.isSequence);
|
||||||
foreach(ref Node scalar; node)
|
foreach(ref Node scalar; node)
|
||||||
{
|
{
|
||||||
|
|
|
@ -52,7 +52,7 @@ void testTokens(string dataFilename, string tokensFilename) @safe
|
||||||
static if(verbose){writeln("tokens1: ", tokens1, "\ntokens2: ", tokens2);}
|
static if(verbose){writeln("tokens1: ", tokens1, "\ntokens2: ", tokens2);}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto loader = Loader(dataFilename);
|
auto loader = Loader.fromFile(dataFilename);
|
||||||
foreach(token; loader.scan())
|
foreach(token; loader.scan())
|
||||||
{
|
{
|
||||||
if(token.id != TokenID.StreamStart && token.id != TokenID.StreamEnd)
|
if(token.id != TokenID.StreamStart && token.id != TokenID.StreamEnd)
|
||||||
|
@ -79,7 +79,7 @@ void testScanner(string dataFilename, string canonicalFilename) @safe
|
||||||
{
|
{
|
||||||
static if(verbose){writeln(tokens);}
|
static if(verbose){writeln(tokens);}
|
||||||
}
|
}
|
||||||
auto loader = Loader(filename);
|
auto loader = Loader.fromFile(filename);
|
||||||
foreach(ref token; loader.scan()){tokens ~= to!string(token.id);}
|
foreach(ref token; loader.scan()){tokens ~= to!string(token.id);}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue