diff --git a/source/app.d b/source/app.d index 604f62d..db49108 100644 --- a/source/app.d +++ b/source/app.d @@ -36,7 +36,7 @@ void main(string[] args) "mode" : "direct", "type_dm" : "any", "itdLPxx_bcl" : "true"]); - + if (substitutionFileName.exists && substitutionFileName.isFile) { loadSubstitutionFile(substitutionFileName); diff --git a/source/substitution.d b/source/substitution.d index 9275705..deb90b6 100644 --- a/source/substitution.d +++ b/source/substitution.d @@ -1,16 +1,65 @@ module substitution; +import std.file: slurp; + public: -void loadSubstitutionFile(string fileName) +void loadSubstitutionFile(alias slurpFun = slurp)(string fileName) { - import std.file : slurp; 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; 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 { return s in map ? map[s] : s;