Merge pull request #106 from Herringway/test-suite-easier-verbose

convert verbose test variable to debug spec for easier access
This commit is contained in:
Sebastian Wilzbach 2018-04-17 01:19:16 +02:00 committed by GitHub
commit 4cad68f924
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 68 additions and 88 deletions

View file

@ -24,6 +24,8 @@ import std.typecons;
package: package:
debug(verbose) enum verbose = true;
else enum verbose = false;
/** /**
* Run an unittest. * Run an unittest.
* *
@ -37,7 +39,6 @@ void run(D)(string testName, D testFunction,
{ {
immutable string dataDir = __FILE_FULL_PATH__.dirName ~ "/../../../test/data"; immutable string dataDir = __FILE_FULL_PATH__.dirName ~ "/../../../test/data";
auto testFilenames = findTestFilenames(dataDir); auto testFilenames = findTestFilenames(dataDir);
bool verbose = false;
Result[] results; Result[] results;
if(unittestExt.length > 0) if(unittestExt.length > 0)
@ -55,24 +56,23 @@ void run(D)(string testName, D testFunction,
if(extensions.canFind(ext)){continue outer;} if(extensions.canFind(ext)){continue outer;}
} }
results ~= execute(testName, testFunction, filenames, verbose); results ~= execute(testName, testFunction, filenames);
} }
} }
else else
{ {
results ~= execute(testName, testFunction, cast(string[])[], verbose); results ~= execute(testName, testFunction, cast(string[])[]);
} }
display(results, verbose); display(results);
} }
/** /**
* Prints an exception if verbosity is turned on. * Prints an exception if verbosity is turned on.
* Params: e = Exception to print. * Params: e = Exception to print.
* verbose = Whether verbose mode is enabled.
*/ */
void printException(YAMLException e, bool verbose) @trusted void printException(YAMLException e) @trusted
{ {
if(verbose) { writeln(typeid(e).toString(), "\n", e); } static if(verbose) { writeln(typeid(e).toString(), "\n", e); }
} }
private: private:
@ -136,14 +136,13 @@ body
* Params: testName = Name of the unittest. * Params: testName = Name of the unittest.
* testFunction = Unittest function. * testFunction = Unittest function.
* filenames = Names of input files to test with. * filenames = Names of input files to test with.
* verbose = Print verbose output?
* *
* Returns: Information about the results of the unittest. * Returns: Information about the results of the unittest.
*/ */
Result execute(D)(const string testName, D testFunction, Result execute(D)(const string testName, D testFunction,
string[] filenames, const bool verbose) @trusted string[] filenames) @trusted
{ {
if(verbose) static if(verbose)
{ {
writeln("==========================================================================="); writeln("===========================================================================");
writeln(testName ~ "(" ~ filenames.join(", ") ~ ")..."); writeln(testName ~ "(" ~ filenames.join(", ") ~ ")...");
@ -154,11 +153,11 @@ Result execute(D)(const string testName, D testFunction,
try try
{ {
//Convert filenames to parameters tuple and call the test function. //Convert filenames to parameters tuple and call the test function.
alias F = Parameters!D[1..$]; alias F = Parameters!D[0..$];
F parameters; F parameters;
stringsToTuple!(F.length - 1, F)(parameters, filenames); stringsToTuple!(F.length - 1, F)(parameters, filenames);
testFunction(verbose, parameters); testFunction(parameters);
if(!verbose){write(".");} static if(!verbose){write(".");}
} }
catch(Throwable e) catch(Throwable e)
{ {
@ -176,23 +175,22 @@ Result execute(D)(const string testName, D testFunction,
* Display unittest results. * Display unittest results.
* *
* Params: results = Unittest results. * Params: results = Unittest results.
* verbose = Print verbose output?
*/ */
void display(Result[] results, const bool verbose) @safe void display(Result[] results) @safe
{ {
if(results.length > 0 && !verbose){write("\n");} if(results.length > 0 && !verbose){write("\n");}
size_t failures = 0; size_t failures = 0;
size_t errors = 0; size_t errors = 0;
if(verbose) static if(verbose)
{ {
writeln("==========================================================================="); writeln("===========================================================================");
} }
//Results of each test. //Results of each test.
foreach(result; results) foreach(result; results)
{ {
if(verbose) static if(verbose)
{ {
writeln(result.name, "(" ~ result.filenames.join(", ") ~ "): ", writeln(result.name, "(" ~ result.filenames.join(", ") ~ "): ",
to!string(result.kind)); to!string(result.kind));

View file

@ -16,10 +16,9 @@ import dyaml.token;
/// Test parser by comparing output from parsing two equivalent YAML files. /// Test parser by comparing output from parsing two equivalent YAML files.
/// ///
/// Params: verbose = Print verbose output? /// Params: dataFilename = YAML file to parse.
/// dataFilename = YAML file to parse.
/// canonicalFilename = Another file to parse, in canonical YAML format. /// canonicalFilename = Another file to parse, in canonical YAML format.
void testParser(bool verbose, string dataFilename, string canonicalFilename) @safe void testParser(string dataFilename, string canonicalFilename) @safe
{ {
auto dataEvents = Loader(dataFilename).parse(); auto dataEvents = Loader(dataFilename).parse();
auto canonicalEvents = Loader(canonicalFilename).parse(); auto canonicalEvents = Loader(canonicalFilename).parse();
@ -35,10 +34,9 @@ void testParser(bool verbose, string dataFilename, string canonicalFilename) @sa
/// Test loader by comparing output from loading two equivalent YAML files. /// Test loader by comparing output from loading two equivalent YAML files.
/// ///
/// Params: verbose = Print verbose output? /// Params: dataFilename = YAML file to load.
/// dataFilename = YAML file to load.
/// canonicalFilename = Another file to load, in canonical YAML format. /// canonicalFilename = Another file to load, in canonical YAML format.
void testLoader(bool verbose, string dataFilename, string canonicalFilename) @safe void testLoader(string dataFilename, string canonicalFilename) @safe
{ {
auto data = Loader(dataFilename).loadAll(); auto data = Loader(dataFilename).loadAll();
auto canonical = Loader(canonicalFilename).loadAll(); auto canonical = Loader(canonicalFilename).loadAll();
@ -48,7 +46,7 @@ void testLoader(bool verbose, string dataFilename, string canonicalFilename) @sa
{ {
if(data[n] != canonical[n]) if(data[n] != canonical[n])
{ {
if(verbose) static if(verbose)
{ {
writeln("Normal value:"); writeln("Normal value:");
writeln(data[n].debugString); writeln(data[n].debugString);

View file

@ -393,12 +393,11 @@ Node representStruct(ref Node node, Representer representer) @safe
/** /**
* Constructor unittest. * Constructor unittest.
* *
* Params: verbose = Print verbose output? * Params: dataFilename = File name to read from.
* dataFilename = File name to read from.
* codeDummy = Dummy .code filename, used to determine that * codeDummy = Dummy .code filename, used to determine that
* .data file with the same name should be used in this test. * .data file with the same name should be used in this test.
*/ */
void testConstructor(bool verbose, string dataFilename, string codeDummy) @safe void testConstructor(string dataFilename, string codeDummy) @safe
{ {
string base = dataFilename.baseName.stripExtension; string base = dataFilename.baseName.stripExtension;
enforce((base in expected) !is null, enforce((base in expected) !is null,
@ -420,7 +419,7 @@ void testConstructor(bool verbose, string dataFilename, string codeDummy) @safe
{ {
if(!node.equals!(No.useTag)(exp[i])) if(!node.equals!(No.useTag)(exp[i]))
{ {
if(verbose) static if(verbose)
{ {
writeln("Expected value:"); writeln("Expected value:");
writeln(exp[i].debugString); writeln(exp[i].debugString);

View file

@ -75,11 +75,10 @@ bool compareEvents(Event[] events1, Event[] events2) @system
/// the emitted result and comparing events from parsing the emitted result with /// the emitted result and comparing events from parsing the emitted result with
/// originally parsed events. /// originally parsed events.
/// ///
/// Params: verbose = Print verbose output? /// Params: dataFilename = YAML file to parse.
/// dataFilename = YAML file to parse.
/// canonicalFilename = Canonical YAML file used as dummy to determine /// canonicalFilename = Canonical YAML file used as dummy to determine
/// which data files to load. /// which data files to load.
void testEmitterOnData(bool verbose, 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(dataFilename);
@ -87,7 +86,7 @@ void testEmitterOnData(bool verbose, string dataFilename, string canonicalFilena
auto emitStream = new YMemoryStream; auto emitStream = new YMemoryStream;
Dumper(emitStream).emit(events); Dumper(emitStream).emit(events);
if(verbose) static if(verbose)
{ {
writeln(dataFilename); writeln(dataFilename);
writeln("ORIGINAL:\n", readText(dataFilename)); writeln("ORIGINAL:\n", readText(dataFilename));
@ -106,9 +105,8 @@ void testEmitterOnData(bool verbose, string dataFilename, string canonicalFilena
/// them both in canonical and normal format, parsing the emitted results and /// them both in canonical and normal format, parsing the emitted results and
/// comparing events from parsing the emitted result with originally parsed events. /// comparing events from parsing the emitted result with originally parsed events.
/// ///
/// Params: verbose = Print verbose output? /// Params: canonicalFilename = Canonical YAML file to parse.
/// canonicalFilename = Canonical YAML file to parse. void testEmitterOnCanonical(string canonicalFilename) @system
void testEmitterOnCanonical(bool verbose, 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(canonicalFilename);
@ -119,7 +117,7 @@ void testEmitterOnCanonical(bool verbose, string canonicalFilename) @system
auto dumper = Dumper(emitStream); auto dumper = Dumper(emitStream);
dumper.canonical = canonical; dumper.canonical = canonical;
dumper.emit(events); dumper.emit(events);
if(verbose) static if(verbose)
{ {
writeln("OUTPUT (canonical=", canonical, "):\n", writeln("OUTPUT (canonical=", canonical, "):\n",
cast(string)emitStream.data); cast(string)emitStream.data);
@ -137,11 +135,10 @@ void testEmitterOnCanonical(bool verbose, string canonicalFilename) @system
/// possible scalar and collection styles, parsing the emitted results and /// possible scalar and collection styles, parsing the emitted results and
/// comparing events from parsing the emitted result with originally parsed events. /// comparing events from parsing the emitted result with originally parsed events.
/// ///
/// Params: verbose = Print verbose output? /// Params: dataFilename = YAML file to parse.
/// dataFilename = YAML file to parse.
/// canonicalFilename = Canonical YAML file used as dummy to determine /// canonicalFilename = Canonical YAML file used as dummy to determine
/// which data files to load. /// which data files to load.
void testEmitterStyles(bool verbose, string dataFilename, string canonicalFilename) @system void testEmitterStyles(string dataFilename, string canonicalFilename) @system
{ {
foreach(filename; [dataFilename, canonicalFilename]) foreach(filename; [dataFilename, canonicalFilename])
{ {
@ -177,7 +174,7 @@ void testEmitterStyles(bool verbose, string dataFilename, string canonicalFilena
} }
auto emitStream = new YMemoryStream; auto emitStream = new YMemoryStream;
Dumper(emitStream).emit(styledEvents); Dumper(emitStream).emit(styledEvents);
if(verbose) static if(verbose)
{ {
writeln("OUTPUT (", filename, ", ", to!string(flowStyle), ", ", writeln("OUTPUT (", filename, ", ", to!string(flowStyle), ", ",
to!string(style), ")"); to!string(style), ")");

View file

@ -17,9 +17,8 @@ import dyaml.test.common;
/// Loader error unittest from file stream. /// Loader error unittest from file stream.
/// ///
/// Params: verbose = Print verbose output? /// Params: errorFilename = File name to read from.
/// errorFilename = File name to read from. void testLoaderError(string errorFilename) @safe
void testLoaderError(bool verbose, string errorFilename) @safe
{ {
auto buffer = std.file.read(errorFilename); auto buffer = std.file.read(errorFilename);
@ -27,7 +26,7 @@ void testLoaderError(bool verbose, string errorFilename) @safe
try { nodes = Loader(buffer).loadAll(); } try { nodes = Loader(buffer).loadAll(); }
catch(YAMLException e) catch(YAMLException e)
{ {
printException(e, verbose); printException(e);
return; return;
} }
assert(false, "Expected an exception"); assert(false, "Expected an exception");
@ -35,9 +34,8 @@ void testLoaderError(bool verbose, string errorFilename) @safe
/// Loader error unittest from string. /// Loader error unittest from string.
/// ///
/// Params: verbose = Print verbose output? /// Params: errorFilename = File name to read from.
/// errorFilename = File name to read from. void testLoaderErrorString(string errorFilename) @safe
void testLoaderErrorString(bool verbose, string errorFilename) @safe
{ {
// Load file to a buffer, then pass that to the YAML loader. // Load file to a buffer, then pass that to the YAML loader.
auto buffer = std.file.read(errorFilename); auto buffer = std.file.read(errorFilename);
@ -48,7 +46,7 @@ void testLoaderErrorString(bool verbose, string errorFilename) @safe
} }
catch(YAMLException e) catch(YAMLException e)
{ {
printException(e, verbose); printException(e);
return; return;
} }
assert(false, "Expected an exception"); assert(false, "Expected an exception");
@ -56,30 +54,28 @@ void testLoaderErrorString(bool verbose, string errorFilename) @safe
/// Loader error unittest from filename. /// Loader error unittest from filename.
/// ///
/// Params: verbose = Print verbose output? /// Params: errorFilename = File name to read from.
/// errorFilename = File name to read from. void testLoaderErrorFilename(string errorFilename) @safe
void testLoaderErrorFilename(bool verbose, string errorFilename) @safe
{ {
try { auto nodes = Loader(errorFilename).loadAll(); } try { auto nodes = Loader(errorFilename).loadAll(); }
catch(YAMLException e) catch(YAMLException e)
{ {
printException(e, verbose); printException(e);
return; return;
} }
assert(false, "testLoaderErrorSingle(" ~ verbose.to!string ~ assert(false, "testLoaderErrorSingle(" ~ ", " ~ errorFilename ~
", " ~ errorFilename ~ ") Expected an exception"); ") Expected an exception");
} }
/// Loader error unittest loading a single document from a file. /// Loader error unittest loading a single document from a file.
/// ///
/// Params: verbose = Print verbose output? /// Params: errorFilename = File name to read from.
/// errorFilename = File name to read from. void testLoaderErrorSingle(string errorFilename) @safe
void testLoaderErrorSingle(bool verbose, string errorFilename) @safe
{ {
try { auto nodes = Loader(errorFilename).load(); } try { auto nodes = Loader(errorFilename).load(); }
catch(YAMLException e) catch(YAMLException e)
{ {
printException(e, verbose); printException(e);
return; return;
} }
assert(false, "Expected an exception"); assert(false, "Expected an exception");

View file

@ -48,9 +48,8 @@ dchar bom32(bool wrong = false) pure @safe
/// Unicode input unittest. Tests various encodings. /// Unicode input unittest. Tests various encodings.
/// ///
/// Params: verbose = Print verbose output? /// Params: unicodeFilename = File name to read from.
/// unicodeFilename = File name to read from. void testUnicodeInput(string unicodeFilename) @safe
void testUnicodeInput(bool verbose, string unicodeFilename) @safe
{ {
string data = readText(unicodeFilename); string data = readText(unicodeFilename);
string expected = data.split().join(" "); string expected = data.split().join(" ");
@ -68,9 +67,8 @@ void testUnicodeInput(bool verbose, string unicodeFilename) @safe
/// Unicode input error unittest. Tests various encodings with incorrect BOMs. /// Unicode input error unittest. Tests various encodings with incorrect BOMs.
/// ///
/// Params: verbose = Print verbose output? /// Params: unicodeFilename = File name to read from.
/// unicodeFilename = File name to read from. void testUnicodeInputErrors(string unicodeFilename) @safe
void testUnicodeInputErrors(bool verbose, string unicodeFilename) @safe
{ {
string data = readText(unicodeFilename); string data = readText(unicodeFilename);
foreach(buffer; [cast(void[])(data.to!(wchar[])), foreach(buffer; [cast(void[])(data.to!(wchar[])),
@ -81,7 +79,7 @@ void testUnicodeInputErrors(bool verbose, string unicodeFilename) @safe
try { Loader(buffer).load(); } try { Loader(buffer).load(); }
catch(YAMLException e) catch(YAMLException e)
{ {
printException(e, verbose); printException(e);
continue; continue;
} }
assert(false, "Expected an exception"); assert(false, "Expected an exception");

View file

@ -16,9 +16,8 @@ import dyaml.reader;
// Try reading entire file through Reader, expecting an error (the file is invalid). // Try reading entire file through Reader, expecting an error (the file is invalid).
// //
// Params: verbose = Print verbose output? // Params: data = Stream to read.
// data = Stream to read. void runReader(ubyte[] fileData) @safe
void runReader(const bool verbose, ubyte[] fileData) @safe
{ {
try try
{ {
@ -27,7 +26,7 @@ void runReader(const bool verbose, ubyte[] fileData) @safe
} }
catch(ReaderException e) catch(ReaderException e)
{ {
printException(e, verbose); printException(e);
return; return;
} }
assert(false, "Expected an exception"); assert(false, "Expected an exception");
@ -36,11 +35,10 @@ void runReader(const bool verbose, ubyte[] fileData) @safe
/// Stream error unittest. Tries to read invalid input files, expecting errors. /// Stream error unittest. Tries to read invalid input files, expecting errors.
/// ///
/// Params: verbose = Print verbose output? /// Params: errorFilename = File name to read from.
/// errorFilename = File name to read from. void testStreamError(string errorFilename) @safe
void testStreamError(bool verbose, string errorFilename) @safe
{ {
runReader(verbose, readData(errorFilename)); runReader(readData(errorFilename));
} }
// TODO: remove when a @safe ubyte[] file read can be done. // TODO: remove when a @safe ubyte[] file read can be done.

View file

@ -20,11 +20,10 @@ import dyaml.test.constructor;
/// Representer unittest. /// Representer unittest.
/// ///
/// Params: verbose = Print verbose output? /// Params: codeFilename = File name to determine test case from.
/// codeFilename = File name to determine test case from.
/// Nothing is read from this file, it only exists /// Nothing is read from this file, it only exists
/// to specify that we need a matching unittest. /// to specify that we need a matching unittest.
void testRepresenterTypes(bool verbose, string codeFilename) @safe void testRepresenterTypes(string codeFilename) @safe
{ {
string baseName = codeFilename.baseName.stripExtension; string baseName = codeFilename.baseName.stripExtension;
enforce((baseName in dyaml.test.constructor.expected) !is null, enforce((baseName in dyaml.test.constructor.expected) !is null,
@ -38,7 +37,7 @@ void testRepresenterTypes(bool verbose, string codeFilename) @safe
scope(failure) scope(failure)
{ {
if(verbose) static if(verbose)
{ {
writeln("Expected nodes:"); writeln("Expected nodes:");
foreach(ref n; expectedNodes){writeln(n.debugString, "\n---\n");} foreach(ref n; expectedNodes){writeln(n.debugString, "\n---\n");}

View file

@ -19,11 +19,10 @@ import dyaml.test.common;
/** /**
* Implicit tag resolution unittest. * Implicit tag resolution unittest.
* *
* Params: verbose = Print verbose output? * Params: dataFilename = File with unittest data.
* dataFilename = File with unittest data.
* detectFilename = Dummy filename used to specify which data filenames to use. * detectFilename = Dummy filename used to specify which data filenames to use.
*/ */
void testImplicitResolver(bool verbose, string dataFilename, string detectFilename) @safe void testImplicitResolver(string dataFilename, string detectFilename) @safe
{ {
string correctTag; string correctTag;
Node node; Node node;

View file

@ -20,11 +20,10 @@ import dyaml.token;
/** /**
* Test tokens output by scanner. * Test tokens output by scanner.
* *
* Params: verbose = Print verbose output? * Params: dataFilename = File to scan.
* dataFilename = File to scan.
* tokensFilename = File containing expected tokens. * tokensFilename = File containing expected tokens.
*/ */
void testTokens(bool verbose, string dataFilename, string tokensFilename) @safe void testTokens(string dataFilename, string tokensFilename) @safe
{ {
//representations of YAML tokens in tokens file. //representations of YAML tokens in tokens file.
auto replace = [TokenID.Directive : "%" , auto replace = [TokenID.Directive : "%" ,
@ -50,7 +49,7 @@ void testTokens(bool verbose, string dataFilename, string tokensFilename) @safe
string[] tokens2 = readText(tokensFilename).split(); string[] tokens2 = readText(tokensFilename).split();
scope(exit) scope(exit)
{ {
if(verbose){writeln("tokens1: ", tokens1, "\ntokens2: ", tokens2);} static if(verbose){writeln("tokens1: ", tokens1, "\ntokens2: ", tokens2);}
} }
auto loader = Loader(dataFilename); auto loader = Loader(dataFilename);
@ -68,18 +67,17 @@ void testTokens(bool verbose, string dataFilename, string tokensFilename) @safe
/** /**
* Test scanner by scanning a file, expecting no errors. * Test scanner by scanning a file, expecting no errors.
* *
* Params: verbose = Print verbose output? * Params: dataFilename = File to scan.
* dataFilename = File to scan.
* canonicalFilename = Another file to scan, in canonical YAML format. * canonicalFilename = Another file to scan, in canonical YAML format.
*/ */
void testScanner(bool verbose, string dataFilename, string canonicalFilename) @safe void testScanner(string dataFilename, string canonicalFilename) @safe
{ {
foreach(filename; [dataFilename, canonicalFilename]) foreach(filename; [dataFilename, canonicalFilename])
{ {
string[] tokens; string[] tokens;
scope(exit) scope(exit)
{ {
if(verbose){writeln(tokens);} static if(verbose){writeln(tokens);}
} }
auto loader = Loader(filename); auto loader = Loader(filename);
foreach(ref token; loader.scan()){tokens ~= to!string(token.id);} foreach(ref token; loader.scan()){tokens ~= to!string(token.id);}