From faaea1172b589bafbd8a7d4b38260d4e27ace739 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Fri, 4 Aug 2017 02:21:59 +0200 Subject: [PATCH] use unit-threaded for unittests --- dub.json | 16 ++- source/fahrplanparser.d | 226 ++++++++++++++++++++++++---------------- source/substitution.d | 45 ++++---- 3 files changed, 179 insertions(+), 108 deletions(-) diff --git a/dub.json b/dub.json index 63601d1..fcd5a94 100644 --- a/dub.json +++ b/dub.json @@ -10,5 +10,19 @@ }, "description": "A minimal D application.", "copyright": "Copyright © 2017, Johannes Loher", - "license": "MIT" + "license": "MIT", + "targetType": "executable", + "configurations": [ + { "name": "executable" }, + { + "name": "unittest", + "targetType": "executable", + "preBuildCommands": ["dub run unit-threaded -c gen_ut_main -- -f gen/ut.d"], + "mainSourceFile": "gen/ut.d", + "excludedSourceFiles": ["source/app.d"], + "dependencies": { + "unit-threaded": "~>0.7.28" + } + } + ] } diff --git a/source/fahrplanparser.d b/source/fahrplanparser.d index 952c487..64ca475 100644 --- a/source/fahrplanparser.d +++ b/source/fahrplanparser.d @@ -1,12 +1,17 @@ module fahrplanparser; +import kxml.xml : readDocument, XmlNode; + import std.algorithm : map; import std.array : empty, front; import std.conv : to; import std.datetime : dur, TimeOfDay, DateTimeException; import std.string : format; -import kxml.xml : readDocument, XmlNode; +version (unittest) +{ + import unit_threaded; +} import substitution; @@ -50,22 +55,74 @@ auto parsedFahrplan(in string data) { import std.array : array; - auto xml = ""; - assert(xml.parsedFahrplan.array == []); + "".parsedFahrplan.array.shouldEqual([]); - xml = ""; - assert(xml.parsedFahrplan.array == []); + "".parsedFahrplan.array.shouldEqual([]); - xml = "1122412426Wernerwerkstraße"; - assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße", + q"[ + + + + 1 + + 1224 + 1242 + + + 6 + Wernerwerkstraße + + + + + ]".parsedFahrplan.array.shouldEqual([["direction" : "Wernerwerkstraße", "line" : "6", "departure" : "12:24", "delay" : "18"]]); - xml = "012246Wernerwerkstraße"; - assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße", + q"[ + + + + 0 + + 1224 + + + 6 + Wernerwerkstraße + + + + + ]".parsedFahrplan.array.shouldEqual([["direction" : "Wernerwerkstraße", "line" : "6", "departure" : "12:24", "delay" : "0"]]); - xml = "012246Wernerwerkstraße11353135611Burgweinting"; - assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße", "line" : "6", + q"[ + + + + 0 + + 1224 + + + 6 + Wernerwerkstraße + + + + 1 + + 1353 + 1356 + + + 11 + Burgweinting + + + + + ]".parsedFahrplan.array.shouldEqual([["direction" : "Wernerwerkstraße", "line" : "6", "departure" : "12:24", "delay" : "0"], ["direction" : "Burgweinting", "line" : "11", "departure" : "13:53", "delay" : "3"]]); } @@ -104,41 +161,47 @@ body @system unittest { - import std.exception : assertThrown; + "0000".readDocument.parseXPath("/dp") + .front.departureTime.shouldEqual(TimeOfDay(0, 0)); - auto xml = "0000".readDocument.parseXPath("/dp").front; - assert(xml.departureTime == TimeOfDay(0, 0)); + "0013".readDocument.parseXPath("/dp") + .front.departureTime.shouldEqual(TimeOfDay(0, 13)); - xml = "0013".readDocument.parseXPath("/dp").front; - assert(xml.departureTime == TimeOfDay(0, 13)); + "1100".readDocument.parseXPath("/dp") + .front.departureTime.shouldEqual(TimeOfDay(11, 00)); - xml = "1100".readDocument.parseXPath("/dp").front; - assert(xml.departureTime == TimeOfDay(11, 00)); + "1242".readDocument.parseXPath("/dp") + .front.departureTime.shouldEqual(TimeOfDay(12, 42)); - xml = "1242".readDocument.parseXPath("/dp").front; - assert(xml.departureTime == TimeOfDay(12, 42)); + "2359".readDocument.parseXPath("/dp") + .front.departureTime.shouldEqual(TimeOfDay(23, 59)); - xml = "2359".readDocument.parseXPath("/dp").front; - assert(xml.departureTime == TimeOfDay(23, 59)); + "2400".readDocument.parseXPath("/dp") + .front.departureTime.shouldThrow!DateTimeException; - assertThrown!DateTimeException("2400".readDocument.parseXPath("/dp") - .front.departureTime); - assertThrown!DateTimeException("0061".readDocument.parseXPath("/dp") - .front.departureTime); - assertThrown!DateTimeException("2567".readDocument.parseXPath("/dp") - .front.departureTime); - assertThrown!DateTimeException("".readDocument.parseXPath("/dp") - .front.departureTime); - assertThrown!DateTimeException("0".readDocument.parseXPath("/dp") - .front.departureTime); - assertThrown!DateTimeException("00".readDocument.parseXPath("/dp") - .front.departureTime); - assertThrown!DateTimeException("000000".readDocument.parseXPath("/dp") - .front.departureTime); - assertThrown!DateTimeException("00:00".readDocument.parseXPath("/dp") - .front.departureTime); - assertThrown!DateTimeException("abcd".readDocument.parseXPath("/dp") - .front.departureTime); + "0061".readDocument.parseXPath("/dp") + .front.departureTime.shouldThrow!DateTimeException; + + "2567".readDocument.parseXPath("/dp") + .front.departureTime.shouldThrow!DateTimeException; + + "".readDocument.parseXPath("/dp") + .front.departureTime.shouldThrow!DateTimeException; + + "0".readDocument.parseXPath("/dp") + .front.departureTime.shouldThrow!DateTimeException; + + "00".readDocument.parseXPath("/dp") + .front.departureTime.shouldThrow!DateTimeException; + + "000000".readDocument.parseXPath("/dp") + .front.departureTime.shouldThrow!DateTimeException; + + "00:00".readDocument.parseXPath("/dp") + .front.departureTime.shouldThrow!DateTimeException; + + "abcd".readDocument.parseXPath("/dp") + .front.departureTime.shouldThrow!DateTimeException; } auto delay(XmlNode dp) @@ -173,70 +236,59 @@ body @system unittest { - import std.exception : assertThrown; import core.exception : AssertError; - auto xml = "0".readDocument.parseXPath("/dp").front; - assert(xml.delay == dur!"minutes"(0)); + "0".readDocument.parseXPath("/dp") + .front.delay.shouldEqual(dur!"minutes"(0)); - xml = "".readDocument.parseXPath("/dp").front; - assertThrown!(UnexpectedValueException!string)(xml.delay); + "".readDocument.parseXPath("/dp") + .front.delay.shouldThrow!(UnexpectedValueException!string); - xml = "2".readDocument.parseXPath("/dp").front; - assertThrown!(UnexpectedValueException!string)(xml.delay); + "2".readDocument.parseXPath("/dp") + .front.delay.shouldThrow!(UnexpectedValueException!string); - xml = "a".readDocument.parseXPath("/dp").front; - assertThrown!(UnexpectedValueException!string)(xml.delay); + "a".readDocument.parseXPath("/dp") + .front.delay.shouldThrow!(UnexpectedValueException!string); - xml = "1".readDocument.parseXPath("/dp").front; - assert(xml.delay == dur!"seconds"(0)); + "1".readDocument.parseXPath("/dp") + .front.delay.shouldEqual(dur!"seconds"(0)); - xml = "1".readDocument.parseXPath("/dp").front; - assertThrown!DateTimeException(xml.delay); + "1".readDocument.parseXPath("/dp") + .front.delay.shouldThrow!(DateTimeException); - xml = "1".readDocument.parseXPath("/dp").front; - assert(xml.delay == dur!"seconds"(0)); + "1".readDocument.parseXPath("/dp") + .front.delay.shouldEqual(dur!"seconds"(0)); - xml = "".readDocument.parseXPath("/dp").front; - assertThrown!AssertError(xml.delay); + "".readDocument.parseXPath("/dp") + .front.delay.shouldThrow!(AssertError); - xml = "1".readDocument.parseXPath("/dp") - .front; - assertThrown!DateTimeException(xml.delay); + "1".readDocument.parseXPath("/dp") + .front.delay.shouldThrow!(DateTimeException); - xml = "10000".readDocument.parseXPath("/dp") - .front; - assertThrown!DateTimeException(xml.delay); + "10000".readDocument.parseXPath("/dp") + .front.delay.shouldThrow!(DateTimeException); - xml = "10000".readDocument.parseXPath("/dp") - .front; - assertThrown!DateTimeException(xml.delay); + "10000".readDocument.parseXPath("/dp") + .front.delay.shouldThrow!(DateTimeException); - xml = "100000000" - .readDocument.parseXPath("/dp").front; - assert(xml.delay == dur!"minutes"(0)); + "100000000".readDocument.parseXPath("/dp") + .front.delay.shouldEqual(dur!"minutes"(0)); - xml = "100010000" - .readDocument.parseXPath("/dp").front; - assert(xml.delay == dur!"minutes"(1)); + "100010000".readDocument.parseXPath("/dp") + .front.delay.shouldEqual(dur!"minutes"(1)); - xml = "117531751" - .readDocument.parseXPath("/dp").front; - assert(xml.delay == dur!"minutes"(2)); + "117531751".readDocument.parseXPath("/dp") + .front.delay.shouldEqual(dur!"minutes"(2)); - xml = "110101000" - .readDocument.parseXPath("/dp").front; - assert(xml.delay == dur!"minutes"(10)); + "110101000".readDocument.parseXPath("/dp") + .front.delay.shouldEqual(dur!"minutes"(10)); - xml = "113011242" - .readDocument.parseXPath("/dp").front; - assert(xml.delay == dur!"minutes"(19)); + "113011242".readDocument.parseXPath("/dp") + .front.delay.shouldEqual(dur!"minutes"(19)); - xml = "100001242" - .readDocument.parseXPath("/dp").front; - assert(xml.delay == dur!"minutes"(678)); + "100001242".readDocument.parseXPath("/dp") + .front.delay.shouldEqual(dur!"minutes"(678)); - xml = "100002359" - .readDocument.parseXPath("/dp").front; - assert(xml.delay == dur!"minutes"(1)); + "100002359".readDocument.parseXPath("/dp") + .front.delay.shouldEqual(dur!"minutes"(1)); } diff --git a/source/substitution.d b/source/substitution.d index 07c239f..9575426 100644 --- a/source/substitution.d +++ b/source/substitution.d @@ -4,6 +4,11 @@ import std.file : slurp; import std.meta : AliasSeq; import std.traits : Parameters; +version (unittest) +{ + import unit_threaded; +} + public: /*********************************** @@ -30,7 +35,7 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) } loadSubstitutionFile!mockSlurpEmpty(""); - assert(map.length == 0); + map.length.shouldEqual(0); static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename, in char[] format) @@ -39,9 +44,9 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) } loadSubstitutionFile!mockSlurpEmptyEntry(""); - assert("" in map); - assert(map.length == 1); - assert(map[""] == ""); + "".shouldBeIn(map); + map.length.shouldEqual(1); + map[""].shouldEqual(""); static Tuple!(string, string)[] mockSlurpSingleEntry(Type1, Type2)(string filename, in char[] format) @@ -50,9 +55,9 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) } loadSubstitutionFile!mockSlurpSingleEntry(""); - assert("foo" in map); - assert(map.length == 1); - assert(map["foo"] == "bar"); + "foo".shouldBeIn(map); + map.length.shouldEqual(1); + map["foo"].shouldEqual("bar"); static Tuple!(string, string)[] mockSlurpMultipleEntries(Type1, Type2)( string filename, in char[] format) @@ -61,13 +66,13 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) } loadSubstitutionFile!mockSlurpMultipleEntries(""); - assert("" in map); - assert("0" in map); - assert("Text in" in map); - assert(map.length == 3); - assert(map[""] == ""); - assert(map["0"] == "1"); - assert(map["Text in"] == "wird durch diesen ersetzt"); + "".shouldBeIn(map); + "0".shouldBeIn(map); + "Text in".shouldBeIn(map); + map.length.shouldEqual(3); + map[""].shouldEqual(""); + map["0"].shouldEqual("1"); + map["Text in"].shouldEqual("wird durch diesen ersetzt"); } /*********************************** @@ -84,20 +89,20 @@ auto substitute(string s) @safe nothrow @safe unittest { map[""] = ""; - assert(substitute("") == ""); + "".substitute.shouldEqual(""); map["a"] = "b"; - assert(substitute("a") == "b"); + "a".substitute.shouldEqual("b"); map["Regensburg Danziger Freiheit"] = "Danziger Freiheit"; - assert(substitute("Regensburg Danziger Freiheit") == "Danziger Freiheit"); + "Regensburg Danziger Freiheit".substitute.shouldEqual("Danziger Freiheit"); map["Regensburg Danziger Freiheit"] = "Anderer Test"; - assert(substitute("Regensburg Danziger Freiheit") == "Anderer Test"); + "Regensburg Danziger Freiheit".substitute.shouldEqual("Anderer Test"); - assert(substitute("z") == "z"); + "z".substitute.shouldEqual("z"); - assert(substitute("Regensburg Hauptbahnhof") == "Regensburg Hauptbahnhof"); + "Regensburg Hauptbahnhof".substitute.shouldEqual("Regensburg Hauptbahnhof"); } private: