Skip to content

Commit

Permalink
overhaul test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
Herringway committed Jul 27, 2024
1 parent 988132a commit 872f6a9
Show file tree
Hide file tree
Showing 677 changed files with 7,194 additions and 4,649 deletions.
10 changes: 2 additions & 8 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@ dyaml_src = [
'source/dyaml/serializer.d',
'source/dyaml/style.d',
'source/dyaml/tagdirective.d',
'source/dyaml/test/common.d',
'source/dyaml/test/compare.d',
'source/dyaml/test/constructor.d',
'source/dyaml/test/emitter.d',
'source/dyaml/test/errors.d',
'source/dyaml/test/inputoutput.d',
'source/dyaml/test/reader.d',
'source/dyaml/test/representer.d',
'source/dyaml/test/resolver.d',
'source/dyaml/test/tokens.d',
'source/dyaml/test/suite.d',
'source/dyaml/test/suitehelpers.d',
'source/dyaml/token.d'
]
install_subdir('source/dyaml', install_dir: 'include/d/yaml/')
Expand Down
82 changes: 0 additions & 82 deletions source/dyaml/composer.d
Original file line number Diff line number Diff line change
Expand Up @@ -387,85 +387,3 @@ struct Composer
return node;
}
}

// Provide good error message on multiple keys (which JSON supports)
@safe unittest
{
import dyaml.loader : Loader;

const str = `{
"comment": "This is a common technique",
"name": "foobar",
"comment": "To write down comments pre-JSON5"
}`;

const exc = collectException!LoaderException(Loader.fromString(str).load());
assert(exc);
assert(exc.message() ==
"Unable to load <unknown>: Key 'comment' appears multiple times in mapping\n" ~
"<unknown>:4,5\ndefined here: <unknown>:2,5");
}

// Provide good error message on duplicate anchors
@safe unittest
{
import dyaml.loader : Loader;

const str = `{
a: &anchor b,
b: &anchor c,
}`;

const exc = collectException!LoaderException(Loader.fromString(str).load());
assert(exc);
assert(exc.message() ==
"Unable to load <unknown>: Found duplicate anchor: anchor\n" ~
"<unknown>:3,8\ndefined here: <unknown>:2,8");
}

// Provide good error message on missing alias
@safe unittest
{
import dyaml.loader : Loader;

const str = `{
a: *anchor,
}`;

const exc = collectException!LoaderException(Loader.fromString(str).load());
assert(exc);
assert(exc.message() ==
"Unable to load <unknown>: Found undefined alias: anchor\n" ~
"<unknown>:2,8");
}

// Provide good error message on recursive alias
@safe unittest
{
import dyaml.loader : Loader;

const str = `a: &anchor {
b: *anchor
}`;

const exc = collectException!LoaderException(Loader.fromString(str).load());
assert(exc);
assert(exc.message() ==
"Unable to load <unknown>: Found recursive alias: anchor\n" ~
"<unknown>:2,8\ndefined here: <unknown>:1,4");
}

// Provide good error message on failed merges
@safe unittest
{
import dyaml.loader : Loader;

const str = `a: &anchor 3
b: { <<: *anchor }`;

const exc = collectException!LoaderException(Loader.fromString(str).load());
assert(exc);
assert(exc.message() ==
"Unable to load <unknown>: While constructing a mapping, expected a mapping or a list of mappings for merging, but found: integer\n" ~
"<unknown>:2,19\nmapping started here: <unknown>:2,4");
}
3 changes: 1 addition & 2 deletions source/dyaml/event.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
module dyaml.event;

import std.algorithm;
import std.array;
import std.conv;

Expand Down Expand Up @@ -43,8 +44,6 @@ enum EventID : ubyte
*/
struct Event
{
@disable int opCmp(ref Event);

///Value of the event, if any.
string value;
///Start position of the event in file/stream.
Expand Down
43 changes: 43 additions & 0 deletions source/dyaml/loader.d
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,46 @@ EOS";
assert(mark.column == 1);
}
}

@safe unittest
{
assertThrown(Loader.fromString("Invalid character: \xFF").load());
}

@safe unittest
{
assertThrown(Loader.fromFile("test/data/odd-utf16.stream-error").load());
}

// UTF-16 and 32 test
@safe unittest
{
import std.conv : to;
import std.range : only;
enum string yaml = `ABCDØ`;
enum bom = '\uFEFF';
foreach (doc; only(
cast(ubyte[])(bom~yaml.to!(wchar[])),
cast(ubyte[])(bom~yaml.to!(dchar[])),
))
{
assert(Loader.fromBuffer(doc).load().as!string == yaml);
}
}
// Invalid unicode test
@safe unittest
{
import std.conv : to;
import std.range : only;
enum string yaml = `ABCDØ`;
enum badBOM = '\uFFFE';
foreach (doc; only(
cast(ubyte[])yaml.to!(wchar[]),
cast(ubyte[])yaml.to!(dchar[]),
cast(ubyte[])(badBOM~yaml.to!(wchar[])),
cast(ubyte[])(badBOM~yaml.to!(dchar[])),
))
{
assertThrown(Loader.fromBuffer(doc).load());
}
}
86 changes: 0 additions & 86 deletions source/dyaml/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -953,89 +953,3 @@ final class Parser
return scalarEvent(mark, mark, null, null, true, "");
}
}

// Provide good error message for bad block mapping
@safe unittest
{
import dyaml.loader : Loader;

const str = `[`;

const exc = collectException!LoaderException(Loader.fromString(str).load());
assert(exc);
assert(exc.message() ==
"Unable to load <unknown>: While parsing a flow node, expected node content, but found: streamEnd\n" ~
"<unknown>:1,2\nnode started here: <unknown>:1,2");
}

// Provide good error message for bad block mapping
@safe unittest
{
import dyaml.loader : Loader;

const str = `&anchor !foo!bar value`;

const exc = collectException!LoaderException(Loader.fromString(str).load());
assert(exc);
assert(exc.message() ==
"Unable to load <unknown>: While parsing a node, found undefined tag handle: !foo!\n" ~
"<unknown>:1,9\nnode started here: <unknown>:1,1");
}

// Provide good error message for bad block mapping
@safe unittest
{
import dyaml.loader : Loader;

const str = `- a
,`;

const exc = collectException!LoaderException(Loader.fromString(str).load());
assert(exc);
assert(exc.message() ==
"Unable to load <unknown>: While parsing a block sequence, expected block end, but found: flowEntry\n" ~
"<unknown>:2,1\nsequence started here: <unknown>:1,1");
}

// Provide good error message for bad block mapping
@safe unittest
{
import dyaml.loader : Loader;

const str = `a: b
,`;

const exc = collectException!LoaderException(Loader.fromString(str).load());
assert(exc);
assert(exc.message() ==
"Unable to load <unknown>: While parsing a block mapping, expected block end, but found: flowEntry\n" ~
"<unknown>:2,1\nmapping started here: <unknown>:1,1");
}

// Provide good error message for bad flow sequence
@safe unittest
{
import dyaml.loader : Loader;

const str = `[a,b,c`;

const exc = collectException!LoaderException(Loader.fromString(str).load());
assert(exc);
assert(exc.message() ==
"Unable to load <unknown>: While parsing a flow sequence, expected ',' or ']', but got: streamEnd\n" ~
"<unknown>:1,7\nsequence started here: <unknown>:1,1");
}

// Provide good error message for bad flow mapping
@safe unittest
{
import dyaml.loader : Loader;

const str = `{a,b,c`;

const exc = collectException!LoaderException(Loader.fromString(str).load());
assert(exc);
assert(exc.message() ==
"Unable to load <unknown>: While parsing a flow mapping, expected ',' or '}', but got: streamEnd\n" ~
"<unknown>:1,7\nmapping started here: <unknown>:1,1");
}
2 changes: 1 addition & 1 deletion source/dyaml/reader.d
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ private:
// this first.
// $(D char[] utf8) input converted to UTF-8. May be a slice of input.
// $(D size_t characterCount) Number of characters (code points) in input.
auto toUTF8(ubyte[] input, const UTFEncoding encoding) @safe pure nothrow
public auto toUTF8(ubyte[] input, const UTFEncoding encoding) @safe pure nothrow
{
// Documented in function ddoc.
struct Result
Expand Down
39 changes: 0 additions & 39 deletions source/dyaml/resolver.d
Original file line number Diff line number Diff line change
Expand Up @@ -209,45 +209,6 @@ struct Resolver
assert(false, "Cannot resolve an invalid node");
}
}
@safe unittest
{
auto resolver = Resolver.withDefaultResolvers;

bool tagMatch(string tag, string[] values) @safe
{
const string expected = tag;
foreach(value; values)
{
const string resolved = resolver.resolve(NodeID.scalar, null, value, true);
if(expected != resolved)
{
return false;
}
}
return true;
}

assert(tagMatch("tag:yaml.org,2002:bool",
["yes", "NO", "True", "on"]));
assert(tagMatch("tag:yaml.org,2002:float",
["6.8523015e+5", "685.230_15e+03", "685_230.15",
"190:20:30.15", "-.inf", ".NaN"]));
assert(tagMatch("tag:yaml.org,2002:int",
["685230", "+685_230", "02472256", "0x_0A_74_AE",
"0b1010_0111_0100_1010_1110", "190:20:30"]));
assert(tagMatch("tag:yaml.org,2002:merge", ["<<"]));
assert(tagMatch("tag:yaml.org,2002:null", ["~", "null", ""]));
assert(tagMatch("tag:yaml.org,2002:str",
["abcd", "9a8b", "9.1adsf"]));
assert(tagMatch("tag:yaml.org,2002:timestamp",
["2001-12-15T02:59:43.1Z",
"2001-12-14t21:59:43.10-05:00",
"2001-12-14 21:59:43.10 -5",
"2001-12-15 2:59:43.10",
"2002-12-14"]));
assert(tagMatch("tag:yaml.org,2002:value", ["="]));
assert(tagMatch("tag:yaml.org,2002:yaml", ["!", "&", "*"]));
}

///Returns: Default scalar tag.
@property string defaultScalarTag() const pure @safe nothrow {return defaultScalarTag_;}
Expand Down
Loading

0 comments on commit 872f6a9

Please sign in to comment.