[pkg-d-commits] [ldc] 25/95: config unittest

Matthias Klumpp mak at moszumanska.debian.org
Thu Jul 13 20:53:57 UTC 2017


This is an automated email from the git hooks/post-receive script.

mak pushed a commit to annotated tag v1.3.0-beta1
in repository ldc.

commit 47054d29c610b702ff668cee8414cbb37405a16f
Author: Remi THEBAULT <remi.thebault at gmail.com>
Date:   Sat Feb 25 17:58:08 2017 +0100

    config unittest
---
 driver/config.d     | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 driver/configfile.d | 15 ++++++++++++
 2 files changed, 83 insertions(+)

diff --git a/driver/config.d b/driver/config.d
index 231cc6a..0fd4c77 100644
--- a/driver/config.d
+++ b/driver/config.d
@@ -498,3 +498,71 @@ class Parser
         assert(false);
     }
 }
+
+
+version(unittest)
+{
+    void writeToFile(const char *filepath, const char *text)
+    {
+        FILE *fp = fopen(filepath, "w");
+        assert(fp, "Cannot open test file for writing: "~fromStringz(filepath));
+
+        fputs(text, fp);
+        fclose(fp);
+    }
+}
+
+unittest
+{
+    enum confstr =
+`// This configuration file uses libconfig.
+// See http://www.hyperrealm.com/libconfig/ for syntax details.
+
+// The default group is required
+default:
+{
+    // 'switches' holds array of string that are appends to the command line
+    // arguments before they are parsed.
+    switches = [
+        "-I/opt/dev/ldc/runtime/druntime/src",
+        "-I/opt/dev/ldc/runtime/profile-rt/d",
+        "-I/opt/dev/ldc/runtime/phobos",
+        "-L-L/opt/dev/build/ldc/llvm-3.9.1-Debug/lib",
+        "-defaultlib=phobos2-ldc,druntime-ldc",
+        "-debuglib=phobos2-ldc-debug,druntime-ldc-debug"
+    ];
+
+    test_cat = "concatenated" " multiline"
+                " strings";
+};
+`;
+
+    enum filename = "ldc_config_test.conf";
+
+    writeToFile(filename, confstr);
+    scope(exit) remove(filename);
+
+    auto settings = parseConfigFile(filename);
+
+    assert(settings.length == 1);
+    assert(settings[0].name == "default");
+    assert(settings[0].type == Setting.Type.group);
+    auto grp = cast(GroupSetting)settings[0];
+    assert(grp.children.length == 2);
+
+    assert(grp.children[0].name == "switches");
+    assert(grp.children[0].type == Setting.Type.array);
+    auto arr = cast(ArraySetting)grp.children[0];
+    assert(arr.vals.length == 6);
+    assert(arr.vals[0] == "-I/opt/dev/ldc/runtime/druntime/src");
+    assert(arr.vals[1] == "-I/opt/dev/ldc/runtime/profile-rt/d");
+    assert(arr.vals[2] == "-I/opt/dev/ldc/runtime/phobos");
+    assert(arr.vals[3] == "-L-L/opt/dev/build/ldc/llvm-3.9.1-Debug/lib");
+    assert(arr.vals[4] == "-defaultlib=phobos2-ldc,druntime-ldc");
+    assert(arr.vals[5] == "-debuglib=phobos2-ldc-debug,druntime-ldc-debug");
+
+    assert(grp.children[1].name == "test_cat");
+    assert(grp.children[1].type == Setting.Type.scalar);
+    auto scalar = cast(ScalarSetting)grp.children[1];
+    assert(scalar.val == "concatenated multiline strings");
+}
diff --git a/driver/configfile.d b/driver/configfile.d
index 9a822b3..3c1cb70 100644
--- a/driver/configfile.d
+++ b/driver/configfile.d
@@ -73,6 +73,21 @@ string replace(string str, string pattern, string replacement)
     return res;
 }
 
+unittest
+{
+    enum pattern = "pattern";
+    enum test1 = "find the pattern in a sentence";
+    enum test2 = "find the pattern";
+    enum test3 = "pattern in a sentence";
+    enum test4 = "a pattern, yet other patterns";
+
+    assert(replace(test1, pattern, "word") == "find the word in a sentence");
+    assert(replace(test2, pattern, "word") == "find the word");
+    assert(replace(test3, pattern, "word") == "word in a sentence");
+    assert(replace(test4, pattern, "word") == "a word, yet other words");
+}
+
+
 struct ConfigFile
 {
 public:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-d/ldc.git



More information about the pkg-d-commits mailing list