From 86d88607670e373088eb9df06861a7f9db008a75 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Mon, 10 Apr 2017 22:08:46 +0200 Subject: [PATCH] made slurp a template parameter of loadSubstitutionFile and created tests for loadSubstitutionFile --- source/app.d | 2 +- source/substitution.d | 55 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-) 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;