Compare commits

..

1 commit

Author SHA1 Message Date
faaea1172b use unit-threaded for unittests 2017-08-04 02:21:59 +02:00
5 changed files with 181 additions and 132 deletions

5
.gitignore vendored
View file

@ -1,4 +1 @@
bayernfahrplan* /bayernfahrplan
.dub/*
replacement.txt
dub.selections.json

View file

@ -1,19 +0,0 @@
image: base/devel
before_script:
- pacman -Sy
- pacman --noconfirm -S ldc dub libevent
stages:
- build
- test
build:
stage: build
script:
- dub build --compiler=ldc2
test:
stage: test
script:
- dub test --compiler=ldc2

View file

@ -5,10 +5,24 @@
"Oliver Rümpelein" "Oliver Rümpelein"
], ],
"dependencies": { "dependencies": {
"requests": "0.5.3", "requests": "~>0.5.0",
"kxml": "~>1.0.1" "kxml": "~>1.0.1"
}, },
"description": "A minimal D application.", "description": "A minimal D application.",
"copyright": "Copyright © 2017, Johannes Loher", "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"
}
}
]
} }

View file

@ -1,12 +1,17 @@
module fahrplanparser; module fahrplanparser;
import kxml.xml : readDocument, XmlNode;
import std.algorithm : map; import std.algorithm : map;
import std.array : empty, front; import std.array : empty, front;
import std.conv : to; import std.conv : to;
import std.datetime : dur, TimeOfDay, DateTimeException; import std.datetime : dur, TimeOfDay, DateTimeException;
import std.string : format; import std.string : format;
import kxml.xml : readDocument, XmlNode; version (unittest)
{
import unit_threaded;
}
import substitution; import substitution;
@ -50,22 +55,74 @@ auto parsedFahrplan(in string data)
{ {
import std.array : array; import std.array : array;
auto xml = ""; "".parsedFahrplan.array.shouldEqual([]);
assert(xml.parsedFahrplan.array == []);
xml = "<efa><dps></dps></efa>"; "<efa><dps></dps></efa>".parsedFahrplan.array.shouldEqual([]);
assert(xml.parsedFahrplan.array == []);
xml = "<efa><dps><dp><realtime>1</realtime><st><t>1224</t><rt>1242</rt></st><m><nu>6</nu><des>Wernerwerkstraße</des></m></dp></dps></efa>"; q"[
assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße", <efa>
<dps>
<dp>
<realtime>1</realtime>
<st>
<t>1224</t>
<rt>1242</rt>
</st>
<m>
<nu>6</nu>
<des>Wernerwerkstraße</des>
</m>
</dp>
</dps>
</efa>
]".parsedFahrplan.array.shouldEqual([["direction" : "Wernerwerkstraße",
"line" : "6", "departure" : "12:24", "delay" : "18"]]); "line" : "6", "departure" : "12:24", "delay" : "18"]]);
xml = "<efa><dps><dp><realtime>0</realtime><st><t>1224</t></st><m><nu>6</nu><des>Wernerwerkstraße</des></m></dp></dps></efa>"; q"[
assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße", <efa>
<dps>
<dp>
<realtime>0</realtime>
<st>
<t>1224</t>
</st>
<m>
<nu>6</nu>
<des>Wernerwerkstraße</des>
</m>
</dp>
</dps>
</efa>
]".parsedFahrplan.array.shouldEqual([["direction" : "Wernerwerkstraße",
"line" : "6", "departure" : "12:24", "delay" : "0"]]); "line" : "6", "departure" : "12:24", "delay" : "0"]]);
xml = "<efa><dps><dp><realtime>0</realtime><st><t>1224</t></st><m><nu>6</nu><des>Wernerwerkstraße</des></m></dp><dp><realtime>1</realtime><st><t>1353</t><rt>1356</rt></st><m><nu>11</nu><des>Burgweinting</des></m></dp></dps></efa>"; q"[
assert(xml.parsedFahrplan.array == [["direction" : "Wernerwerkstraße", "line" : "6", <efa>
<dps>
<dp>
<realtime>0</realtime>
<st>
<t>1224</t>
</st>
<m>
<nu>6</nu>
<des>Wernerwerkstraße</des>
</m>
</dp>
<dp>
<realtime>1</realtime>
<st>
<t>1353</t>
<rt>1356</rt>
</st>
<m>
<nu>11</nu>
<des>Burgweinting</des>
</m>
</dp>
</dps>
</efa>
]".parsedFahrplan.array.shouldEqual([["direction" : "Wernerwerkstraße", "line" : "6",
"departure" : "12:24", "delay" : "0"], ["direction" : "Burgweinting", "departure" : "12:24", "delay" : "0"], ["direction" : "Burgweinting",
"line" : "11", "departure" : "13:53", "delay" : "3"]]); "line" : "11", "departure" : "13:53", "delay" : "3"]]);
} }
@ -104,41 +161,47 @@ body
@system unittest @system unittest
{ {
import std.exception : assertThrown; "<dp><st><t>0000</t></st></dp>".readDocument.parseXPath("/dp")
.front.departureTime.shouldEqual(TimeOfDay(0, 0));
auto xml = "<dp><st><t>0000</t></st></dp>".readDocument.parseXPath("/dp").front; "<dp><st><t>0013</t></st></dp>".readDocument.parseXPath("/dp")
assert(xml.departureTime == TimeOfDay(0, 0)); .front.departureTime.shouldEqual(TimeOfDay(0, 13));
xml = "<dp><st><t>0013</t></st></dp>".readDocument.parseXPath("/dp").front; "<dp><st><t>1100</t></st></dp>".readDocument.parseXPath("/dp")
assert(xml.departureTime == TimeOfDay(0, 13)); .front.departureTime.shouldEqual(TimeOfDay(11, 00));
xml = "<dp><st><t>1100</t></st></dp>".readDocument.parseXPath("/dp").front; "<dp><st><t>1242</t></st></dp>".readDocument.parseXPath("/dp")
assert(xml.departureTime == TimeOfDay(11, 00)); .front.departureTime.shouldEqual(TimeOfDay(12, 42));
xml = "<dp><st><t>1242</t></st></dp>".readDocument.parseXPath("/dp").front; "<dp><st><t>2359</t></st></dp>".readDocument.parseXPath("/dp")
assert(xml.departureTime == TimeOfDay(12, 42)); .front.departureTime.shouldEqual(TimeOfDay(23, 59));
xml = "<dp><st><t>2359</t></st></dp>".readDocument.parseXPath("/dp").front; "<dp><st><t>2400</t></st></dp>".readDocument.parseXPath("/dp")
assert(xml.departureTime == TimeOfDay(23, 59)); .front.departureTime.shouldThrow!DateTimeException;
assertThrown!DateTimeException("<dp><st><t>2400</t></st></dp>".readDocument.parseXPath("/dp") "<dp><st><t>0061</t></st></dp>".readDocument.parseXPath("/dp")
.front.departureTime); .front.departureTime.shouldThrow!DateTimeException;
assertThrown!DateTimeException("<dp><st><t>0061</t></st></dp>".readDocument.parseXPath("/dp")
.front.departureTime); "<dp><st><t>2567</t></st></dp>".readDocument.parseXPath("/dp")
assertThrown!DateTimeException("<dp><st><t>2567</t></st></dp>".readDocument.parseXPath("/dp") .front.departureTime.shouldThrow!DateTimeException;
.front.departureTime);
assertThrown!DateTimeException("<dp><st><t></t></st></dp>".readDocument.parseXPath("/dp") "<dp><st><t></t></st></dp>".readDocument.parseXPath("/dp")
.front.departureTime); .front.departureTime.shouldThrow!DateTimeException;
assertThrown!DateTimeException("<dp><st><t>0</t></st></dp>".readDocument.parseXPath("/dp")
.front.departureTime); "<dp><st><t>0</t></st></dp>".readDocument.parseXPath("/dp")
assertThrown!DateTimeException("<dp><st><t>00</t></st></dp>".readDocument.parseXPath("/dp") .front.departureTime.shouldThrow!DateTimeException;
.front.departureTime);
assertThrown!DateTimeException("<dp><st><t>000000</t></st></dp>".readDocument.parseXPath("/dp") "<dp><st><t>00</t></st></dp>".readDocument.parseXPath("/dp")
.front.departureTime); .front.departureTime.shouldThrow!DateTimeException;
assertThrown!DateTimeException("<dp><st><t>00:00</t></st></dp>".readDocument.parseXPath("/dp")
.front.departureTime); "<dp><st><t>000000</t></st></dp>".readDocument.parseXPath("/dp")
assertThrown!DateTimeException("<dp><st><t>abcd</t></st></dp>".readDocument.parseXPath("/dp") .front.departureTime.shouldThrow!DateTimeException;
.front.departureTime);
"<dp><st><t>00:00</t></st></dp>".readDocument.parseXPath("/dp")
.front.departureTime.shouldThrow!DateTimeException;
"<dp><st><t>abcd</t></st></dp>".readDocument.parseXPath("/dp")
.front.departureTime.shouldThrow!DateTimeException;
} }
auto delay(XmlNode dp) auto delay(XmlNode dp)
@ -173,70 +236,59 @@ body
@system unittest @system unittest
{ {
import std.exception : assertThrown;
import core.exception : AssertError; import core.exception : AssertError;
auto xml = "<dp><realtime>0</realtime></dp>".readDocument.parseXPath("/dp").front; "<dp><realtime>0</realtime></dp>".readDocument.parseXPath("/dp")
assert(xml.delay == dur!"minutes"(0)); .front.delay.shouldEqual(dur!"minutes"(0));
xml = "<dp><realtime></realtime></dp>".readDocument.parseXPath("/dp").front; "<dp><realtime></realtime></dp>".readDocument.parseXPath("/dp")
assertThrown!(UnexpectedValueException!string)(xml.delay); .front.delay.shouldThrow!(UnexpectedValueException!string);
xml = "<dp><realtime>2</realtime></dp>".readDocument.parseXPath("/dp").front; "<dp><realtime>2</realtime></dp>".readDocument.parseXPath("/dp")
assertThrown!(UnexpectedValueException!string)(xml.delay); .front.delay.shouldThrow!(UnexpectedValueException!string);
xml = "<dp><realtime>a</realtime></dp>".readDocument.parseXPath("/dp").front; "<dp><realtime>a</realtime></dp>".readDocument.parseXPath("/dp")
assertThrown!(UnexpectedValueException!string)(xml.delay); .front.delay.shouldThrow!(UnexpectedValueException!string);
xml = "<dp><realtime>1</realtime></dp>".readDocument.parseXPath("/dp").front; "<dp><realtime>1</realtime></dp>".readDocument.parseXPath("/dp")
assert(xml.delay == dur!"seconds"(0)); .front.delay.shouldEqual(dur!"seconds"(0));
xml = "<dp><realtime>1</realtime><st><t></t></st></dp>".readDocument.parseXPath("/dp").front; "<dp><realtime>1</realtime><st><t></t></st></dp>".readDocument.parseXPath("/dp")
assertThrown!DateTimeException(xml.delay); .front.delay.shouldThrow!(DateTimeException);
xml = "<dp><realtime>1</realtime><st><rt></rt></st></dp>".readDocument.parseXPath("/dp").front; "<dp><realtime>1</realtime><st><rt></rt></st></dp>".readDocument.parseXPath("/dp")
assert(xml.delay == dur!"seconds"(0)); .front.delay.shouldEqual(dur!"seconds"(0));
xml = "<dp><st><rt></rt><t></t></st></dp>".readDocument.parseXPath("/dp").front; "<dp><st><rt></rt><t></t></st></dp>".readDocument.parseXPath("/dp")
assertThrown!AssertError(xml.delay); .front.delay.shouldThrow!(AssertError);
xml = "<dp><realtime>1</realtime><st><rt></rt><t></t></st></dp>".readDocument.parseXPath("/dp") "<dp><realtime>1</realtime><st><rt></rt><t></t></st></dp>".readDocument.parseXPath("/dp")
.front; .front.delay.shouldThrow!(DateTimeException);
assertThrown!DateTimeException(xml.delay);
xml = "<dp><realtime>1</realtime><st><rt>0000</rt><t></t></st></dp>".readDocument.parseXPath("/dp") "<dp><realtime>1</realtime><st><rt>0000</rt><t></t></st></dp>".readDocument.parseXPath("/dp")
.front; .front.delay.shouldThrow!(DateTimeException);
assertThrown!DateTimeException(xml.delay);
xml = "<dp><realtime>1</realtime><st><rt></rt><t>0000</t></st></dp>".readDocument.parseXPath("/dp") "<dp><realtime>1</realtime><st><rt></rt><t>0000</t></st></dp>".readDocument.parseXPath("/dp")
.front; .front.delay.shouldThrow!(DateTimeException);
assertThrown!DateTimeException(xml.delay);
xml = "<dp><realtime>1</realtime><st><rt>0000</rt><t>0000</t></st></dp>" "<dp><realtime>1</realtime><st><rt>0000</rt><t>0000</t></st></dp>".readDocument.parseXPath("/dp")
.readDocument.parseXPath("/dp").front; .front.delay.shouldEqual(dur!"minutes"(0));
assert(xml.delay == dur!"minutes"(0));
xml = "<dp><realtime>1</realtime><st><rt>0001</rt><t>0000</t></st></dp>" "<dp><realtime>1</realtime><st><rt>0001</rt><t>0000</t></st></dp>".readDocument.parseXPath("/dp")
.readDocument.parseXPath("/dp").front; .front.delay.shouldEqual(dur!"minutes"(1));
assert(xml.delay == dur!"minutes"(1));
xml = "<dp><realtime>1</realtime><st><rt>1753</rt><t>1751</t></st></dp>" "<dp><realtime>1</realtime><st><rt>1753</rt><t>1751</t></st></dp>".readDocument.parseXPath("/dp")
.readDocument.parseXPath("/dp").front; .front.delay.shouldEqual(dur!"minutes"(2));
assert(xml.delay == dur!"minutes"(2));
xml = "<dp><realtime>1</realtime><st><rt>1010</rt><t>1000</t></st></dp>" "<dp><realtime>1</realtime><st><rt>1010</rt><t>1000</t></st></dp>".readDocument.parseXPath("/dp")
.readDocument.parseXPath("/dp").front; .front.delay.shouldEqual(dur!"minutes"(10));
assert(xml.delay == dur!"minutes"(10));
xml = "<dp><realtime>1</realtime><st><rt>1301</rt><t>1242</t></st></dp>" "<dp><realtime>1</realtime><st><rt>1301</rt><t>1242</t></st></dp>".readDocument.parseXPath("/dp")
.readDocument.parseXPath("/dp").front; .front.delay.shouldEqual(dur!"minutes"(19));
assert(xml.delay == dur!"minutes"(19));
xml = "<dp><realtime>1</realtime><st><rt>0000</rt><t>1242</t></st></dp>" "<dp><realtime>1</realtime><st><rt>0000</rt><t>1242</t></st></dp>".readDocument.parseXPath("/dp")
.readDocument.parseXPath("/dp").front; .front.delay.shouldEqual(dur!"minutes"(678));
assert(xml.delay == dur!"minutes"(678));
xml = "<dp><realtime>1</realtime><st><rt>0000</rt><t>2359</t></st></dp>" "<dp><realtime>1</realtime><st><rt>0000</rt><t>2359</t></st></dp>".readDocument.parseXPath("/dp")
.readDocument.parseXPath("/dp").front; .front.delay.shouldEqual(dur!"minutes"(1));
assert(xml.delay == dur!"minutes"(1));
} }

View file

@ -4,6 +4,11 @@ import std.file : slurp;
import std.meta : AliasSeq; import std.meta : AliasSeq;
import std.traits : Parameters; import std.traits : Parameters;
version (unittest)
{
import unit_threaded;
}
public: public:
/*********************************** /***********************************
@ -30,7 +35,7 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
} }
loadSubstitutionFile!mockSlurpEmpty(""); loadSubstitutionFile!mockSlurpEmpty("");
assert(map.length == 0); map.length.shouldEqual(0);
static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename, static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename,
in char[] format) in char[] format)
@ -39,9 +44,9 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
} }
loadSubstitutionFile!mockSlurpEmptyEntry(""); loadSubstitutionFile!mockSlurpEmptyEntry("");
assert("" in map); "".shouldBeIn(map);
assert(map.length == 1); map.length.shouldEqual(1);
assert(map[""] == ""); map[""].shouldEqual("");
static Tuple!(string, string)[] mockSlurpSingleEntry(Type1, Type2)(string filename, static Tuple!(string, string)[] mockSlurpSingleEntry(Type1, Type2)(string filename,
in char[] format) in char[] format)
@ -50,9 +55,9 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
} }
loadSubstitutionFile!mockSlurpSingleEntry(""); loadSubstitutionFile!mockSlurpSingleEntry("");
assert("foo" in map); "foo".shouldBeIn(map);
assert(map.length == 1); map.length.shouldEqual(1);
assert(map["foo"] == "bar"); map["foo"].shouldEqual("bar");
static Tuple!(string, string)[] mockSlurpMultipleEntries(Type1, Type2)( static Tuple!(string, string)[] mockSlurpMultipleEntries(Type1, Type2)(
string filename, in char[] format) string filename, in char[] format)
@ -61,13 +66,13 @@ void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
} }
loadSubstitutionFile!mockSlurpMultipleEntries(""); loadSubstitutionFile!mockSlurpMultipleEntries("");
assert("" in map); "".shouldBeIn(map);
assert("0" in map); "0".shouldBeIn(map);
assert("Text in" in map); "Text in".shouldBeIn(map);
assert(map.length == 3); map.length.shouldEqual(3);
assert(map[""] == ""); map[""].shouldEqual("");
assert(map["0"] == "1"); map["0"].shouldEqual("1");
assert(map["Text in"] == "wird durch diesen ersetzt"); map["Text in"].shouldEqual("wird durch diesen ersetzt");
} }
/*********************************** /***********************************
@ -84,20 +89,20 @@ auto substitute(string s) @safe nothrow
@safe unittest @safe unittest
{ {
map[""] = ""; map[""] = "";
assert(substitute("") == ""); "".substitute.shouldEqual("");
map["a"] = "b"; map["a"] = "b";
assert(substitute("a") == "b"); "a".substitute.shouldEqual("b");
map["Regensburg Danziger Freiheit"] = "Danziger Freiheit"; 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"; 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: private: