remove Dumper.emit() and Loader.scan() (#239)

remove Dumper.emit() and Loader.scan()
merged-on-behalf-of: Basile-z <Basile-z@users.noreply.github.com>
This commit is contained in:
Cameron Ross 2019-03-18 03:40:47 -02:30 committed by The Dlang Bot
parent dcb4dcb93a
commit 8a31826124
4 changed files with 37 additions and 58 deletions

View file

@ -166,33 +166,6 @@ struct Dumper
~ name ~ " : " ~ e.msg, e.file, e.line); ~ name ~ " : " ~ e.msg, e.file, e.line);
} }
} }
package:
/*
* Emit specified events. Used for debugging/testing.
*
* Params: events = Events to emit.
*
* Throws: YAMLException if unable to emit.
*/
void emit(CharacterType = char, Range, T)(Range range, T events) @safe
if (isInputRange!T && is(ElementType!T == Event) &&
isOutputRange!(Range, char) || isOutputRange!(Range, wchar) || isOutputRange!(Range, dchar))
{
try
{
auto emitter = Emitter!(Range, CharacterType)(range, canonical, indent_, textWidth, lineBreak);
foreach(ref event; events)
{
emitter.emit(event);
}
}
catch(YAMLException e)
{
throw new YAMLException("Unable to emit YAML to stream "
~ name ~ " : " ~ e.msg, e.file, e.line);
}
}
} }
///Write to a file ///Write to a file
@safe unittest @safe unittest

View file

@ -246,24 +246,6 @@ struct Loader
} }
return currentNode; return currentNode;
} }
// Scan and return all tokens. Used for debugging.
const(Token)[] scan() @safe
{
try
{
const(Token)[] result;
foreach (token; scanner_)
{
result ~= token;
}
return result;
}
catch(YAMLException e)
{
throw new YAMLException("Unable to scan YAML from stream " ~
name_ ~ " : " ~ e.msg, e.file, e.line);
}
}
// Scan all tokens, throwing them away. Used for benchmarking. // Scan all tokens, throwing them away. Used for benchmarking.
void scanBench() @safe void scanBench() @safe

View file

@ -15,11 +15,21 @@ import std.file;
import std.range; import std.range;
import std.typecons; import std.typecons;
import dyaml.dumper; import dyaml.emitter;
import dyaml.event; import dyaml.event;
import dyaml.test.common; import dyaml.test.common;
import dyaml.token; import dyaml.token;
// Try to emit an event range.
void emitTestCommon(T)(ref Appender!string emitStream, T events, bool canonical = false) @safe
if (isInputRange!T && is(ElementType!T == Event))
{
auto emitter = Emitter!(typeof(emitStream), char)(emitStream, canonical, 2, 80, LineBreak.unix);
foreach(ref event; events)
{
emitter.emit(event);
}
}
/// Determine if events in events1 are equivalent to events in events2. /// Determine if events in events1 are equivalent to events in events2.
/// ///
@ -78,8 +88,8 @@ void testEmitterOnData(string dataFilename, string canonicalFilename) @safe
//Must exist due to Anchor, Tags reference counts. //Must exist due to Anchor, Tags reference counts.
auto loader = Loader.fromFile(dataFilename); auto loader = Loader.fromFile(dataFilename);
auto events = loader.parse(); auto events = loader.parse();
auto emitStream = new Appender!string; auto emitStream = Appender!string();
dumper().emit(emitStream, events); emitTestCommon(emitStream, events);
static if(verbose) static if(verbose)
{ {
@ -106,10 +116,8 @@ void testEmitterOnCanonical(string canonicalFilename) @safe
auto events = loader.parse(); auto events = loader.parse();
foreach(canonical; [false, true]) foreach(canonical; [false, true])
{ {
auto emitStream = new Appender!string; auto emitStream = Appender!string();
auto dumper = dumper(); emitTestCommon(emitStream, events, canonical);
dumper.canonical = canonical;
dumper.emit(emitStream, events);
static if(verbose) static if(verbose)
{ {
writeln("OUTPUT (canonical=", canonical, "):\n", writeln("OUTPUT (canonical=", canonical, "):\n",
@ -163,8 +171,8 @@ void testEmitterStyles(string dataFilename, string canonicalFilename) @safe
} }
styledEvents ~= event; styledEvents ~= event;
} }
auto emitStream = new Appender!string; auto emitStream = Appender!string();
dumper().emit(emitStream, styledEvents); emitTestCommon(emitStream, styledEvents);
static if(verbose) static if(verbose)
{ {
writeln("OUTPUT (", filename, ", ", to!string(flowStyle), ", ", writeln("OUTPUT (", filename, ", ", to!string(flowStyle), ", ",

View file

@ -14,8 +14,23 @@ import std.array;
import std.file; import std.file;
import dyaml.test.common; import dyaml.test.common;
import dyaml.reader;
import dyaml.scanner;
import dyaml.token; import dyaml.token;
// Read and scan a YAML doc, returning the tokens.
const(Token)[] scanTestCommon(string filename) @safe
{
ubyte[] yamlData;
() @trusted { yamlData = cast(ubyte[])std.file.read(filename); }();
auto scanner = Scanner(new Reader(yamlData));
const(Token)[] result;
foreach (token; scanner)
{
result ~= token;
}
return result;
}
/** /**
* Test tokens output by scanner. * Test tokens output by scanner.
@ -54,8 +69,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.fromFile(dataFilename); foreach(token; scanTestCommon(dataFilename))
foreach(token; loader.scan())
{ {
if(token.id != TokenID.streamStart && token.id != TokenID.streamEnd) if(token.id != TokenID.streamStart && token.id != TokenID.streamEnd)
{ {
@ -81,8 +95,10 @@ void testScanner(string dataFilename, string canonicalFilename) @safe
{ {
static if(verbose){writeln(tokens);} static if(verbose){writeln(tokens);}
} }
auto loader = Loader.fromFile(filename); foreach(ref token; scanTestCommon(filename))
foreach(ref token; loader.scan()){tokens ~= to!string(token.id);} {
tokens ~= to!string(token.id);
}
} }
} }