Merge branch 'refactoring_loadSubstitutionFile' into 'master'
Refactoring load substitution file Made loadSubstitutionFile testable and added tests. See merge request !1
This commit is contained in:
commit
d149500489
2 changed files with 59 additions and 9 deletions
|
@ -1,5 +1,6 @@
|
||||||
import std.array : array, replace;
|
import std.array : array, replace;
|
||||||
import std.datetime : Clock;
|
import std.datetime : Clock;
|
||||||
|
import std.file : exists, isFile;
|
||||||
import std.format : format;
|
import std.format : format;
|
||||||
import std.getopt : defaultGetoptPrinter, getopt;
|
import std.getopt : defaultGetoptPrinter, getopt;
|
||||||
import std.json : JSONValue;
|
import std.json : JSONValue;
|
||||||
|
@ -36,7 +37,10 @@ void main(string[] args)
|
||||||
"type_dm" : "any",
|
"type_dm" : "any",
|
||||||
"itdLPxx_bcl" : "true"]);
|
"itdLPxx_bcl" : "true"]);
|
||||||
|
|
||||||
loadSubstitutionFile(substitutionFileName);
|
if (substitutionFileName.exists && substitutionFileName.isFile)
|
||||||
|
{
|
||||||
|
loadSubstitutionFile(substitutionFileName);
|
||||||
|
}
|
||||||
|
|
||||||
auto currentTime = Clock.currTime;
|
auto currentTime = Clock.currTime;
|
||||||
JSONValue j = ["time" : "%02s:%02s".format(currentTime.hour, currentTime.minute)];
|
JSONValue j = ["time" : "%02s:%02s".format(currentTime.hour, currentTime.minute)];
|
||||||
|
|
|
@ -1,22 +1,68 @@
|
||||||
module substitution;
|
module substitution;
|
||||||
|
|
||||||
|
import std.file : slurp;
|
||||||
|
import std.meta : AliasSeq;
|
||||||
|
import std.traits : Parameters;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
void loadSubstitutionFile(string fileName)
|
void loadSubstitutionFile(alias slurpFun = slurp)(string fileName)
|
||||||
|
if (is(Parameters!(slurpFun!(string, string)) == AliasSeq!(string, const char[])))
|
||||||
{
|
{
|
||||||
import std.file : slurp, exists, isFile;
|
|
||||||
import std.algorithm.iteration : each;
|
import std.algorithm.iteration : each;
|
||||||
|
|
||||||
if (fileName.exists && fileName.isFile)
|
map = (string[string]).init;
|
||||||
|
slurpFun!(string, string)(fileName, `"%s" = "%s"`).each!(pair => map[pair[0]] = pair[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@safe unittest
|
||||||
|
{
|
||||||
|
import std.typecons : Tuple, tuple;
|
||||||
|
|
||||||
|
static Tuple!(string, string)[] mockSlurpEmpty(Type1, Type2)(string filename, in char[] format)
|
||||||
{
|
{
|
||||||
auto data = slurp!(string, string)(fileName, `"%s" = "%s"`);
|
return [];
|
||||||
map = (string[string]).init;
|
|
||||||
data.each!(pair => map[pair[0]] = pair[1]);
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
loadSubstitutionFile!mockSlurpEmpty("");
|
||||||
|
assert(map.length == 0);
|
||||||
|
|
||||||
|
static Tuple!(string, string)[] mockSlurpEmptyEntry(Type1, Type2)(string filename,
|
||||||
|
in char[] format)
|
||||||
{
|
{
|
||||||
map = (string[string]).init;
|
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
|
||||||
|
|
Loading…
Reference in a new issue