made slurp a template parameter of loadSubstitutionFile and created tests for loadSubstitutionFile

This commit is contained in:
Johannes Loher 2017-04-10 22:08:46 +02:00
parent 99d36745eb
commit 86d8860767
2 changed files with 53 additions and 4 deletions

View file

@ -36,7 +36,7 @@ void main(string[] args)
"mode" : "direct", "mode" : "direct",
"type_dm" : "any", "type_dm" : "any",
"itdLPxx_bcl" : "true"]); "itdLPxx_bcl" : "true"]);
if (substitutionFileName.exists && substitutionFileName.isFile) if (substitutionFileName.exists && substitutionFileName.isFile)
{ {
loadSubstitutionFile(substitutionFileName); loadSubstitutionFile(substitutionFileName);

View file

@ -1,16 +1,65 @@
module substitution; module substitution;
import std.file: slurp;
public: public:
void loadSubstitutionFile(string fileName) void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
{ {
import std.file : slurp;
import std.algorithm.iteration : each; import std.algorithm.iteration : each;
auto data = slurp!(string, string)(fileName, `"%s" = "%s"`); auto data = slurpFun!(string, string)(fileName, `"%s" = "%s"`);
map = (string[string]).init; map = (string[string]).init;
data.each!(pair => map[pair[0]] = pair[1]); data.each!(pair => map[pair[0]] = pair[1]);
} }
@safe unittest
{
import std.algorithm: canFind;
import std.typecons: Tuple, tuple;
static Tuple!(string, string)[] mockSlurpEmpty(Type1, Type2)(string filename, in char[] format)
{
return [];
}
loadSubstitutionFile!mockSlurpEmpty("");
assert(map.length == 0);
static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename, in char[] format)
{
return [tuple("", "")];
}
loadSubstitutionFile!mockSlurpEmptyEntry("");
assert("" in map);
assert(map.length == 1);
assert(map[""] == "");
static Tuple!(string, string)[] mockSlurpSingleEntry(Type1, Type2)(string filename, in char[] format)
{
return [tuple("foo", "bar")];
}
loadSubstitutionFile!mockSlurpSingleEntry("");
assert("foo" in map);
assert(map.length == 1);
assert(map["foo"] == "bar");
static Tuple!(string, string)[] mockSlurpMultipleEntries(Type1, Type2)(string filename, in char[] format)
{
return [tuple("", ""), tuple("0", "1"), tuple("Text in", "wird durch diesen ersetzt")];
}
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");
}
auto substitute(string s) @safe nothrow auto substitute(string s) @safe nothrow
{ {
return s in map ? map[s] : s; return s in map ? map[s] : s;