1 module tests.ut.json_build.rules;
2 
3 
4 import reggae;
5 import reggae.json_build;
6 import unit_threaded;
7 
8 
9 string linkJsonString() @safe pure nothrow {
10     return `
11         [{"type": "fixed",
12           "command": {"type": "link", "flags": ["-L-M"]},
13           "outputs": ["myapp"],
14           "dependencies": {
15               "type": "fixed",
16               "targets":
17               [{"type": "fixed",
18                 "command": {"type": "shell", "cmd":
19                             "dmd -I$project/src -c $in -of$out"},
20                 "outputs": ["main.o"],
21                 "dependencies": {"type": "fixed",
22                                  "targets": [
23                                      {"type": "fixed",
24                                       "command": {}, "outputs": ["src/main.d"],
25                                       "dependencies": {
26                                           "type": "fixed",
27                                           "targets": []},
28                                       "implicits": {
29                                           "type": "fixed",
30                                           "targets": []}}]},
31                 "implicits": {
32                     "type": "fixed",
33                     "targets": []}},
34                {"type": "fixed",
35                 "command": {"type": "shell", "cmd":
36                             "dmd -c $in -of$out"},
37                 "outputs": ["maths.o"],
38                 "dependencies": {
39                     "type": "fixed",
40                     "targets": [
41                         {"type": "fixed",
42                          "command": {}, "outputs": ["src/maths.d"],
43                          "dependencies": {
44                              "type": "fixed",
45                              "targets": []},
46                          "implicits": {
47                              "type": "fixed",
48                              "targets": []}}]},
49                 "implicits": {
50                     "type": "fixed",
51                     "targets": []}}]},
52           "implicits": {
53               "type": "fixed",
54               "targets": []}},
55         {"type": "defaultOptions",
56          "cCompiler": "weirdcc",
57          "oldNinja": true
58         }]
59 `;
60 }
61 
62 
63 @("Link") unittest {
64     auto mainObj = Target("main.o", "dmd -I$project/src -c $in -of$out", Target("src/main.d"));
65     auto mathsObj = Target("maths.o", "dmd -c $in -of$out", Target("src/maths.d"));
66     auto app = link(ExeName("myapp"), [mainObj, mathsObj], Flags("-L-M"));
67 
68     jsonToBuild("", linkJsonString).shouldEqual(Build(app));
69 }
70 
71 
72 @("jsonToOptions version0")
73 unittest {
74     import reggae.config: gDefaultOptions;
75     import std.json;
76 
77     auto oldOptions = gDefaultOptions.dup;
78     oldOptions.args = ["reggae", "-b", "ninja", "/path/to/my/project"];
79     auto newOptions = jsonToOptions(oldOptions, parseJSON(linkJsonString));
80     newOptions.cCompiler.shouldEqual("weirdcc");
81     version(Windows)
82         enum expectedCxx = "cl.exe";
83     else
84         enum expectedCxx = "g++";
85     newOptions.cppCompiler.shouldEqual(expectedCxx);
86 }
87 
88 private string toVersion1(in string jsonString, in string dependencies = `[]`) {
89     return `{"version": 1, "defaultOptions": {"cCompiler": "huh"}, "dependencies": ` ~ dependencies ~ `, "build": ` ~ jsonString ~ `}`;
90 }
91 
92 @("jsonToOptions version1")
93 unittest {
94     import reggae.config: gDefaultOptions;
95     import std.json;
96 
97     auto oldOptions = gDefaultOptions.dup;
98     oldOptions.args = ["reggae", "-b", "ninja", "/path/to/my/project"];
99     immutable jsonString = linkJsonString.toVersion1;
100     auto newOptions = jsonToOptions(oldOptions, parseJSON(jsonString));
101     newOptions.cCompiler.shouldEqual("huh");
102     version(Windows)
103         enum expectedCxx = "cl.exe";
104     else
105         enum expectedCxx = "g++";
106     newOptions.cppCompiler.shouldEqual(expectedCxx);
107     newOptions.oldNinja.shouldBeFalse;
108 }
109 
110 @("jsonToOptions with dependencies")
111 unittest {
112     import std.json;
113     import std.file;
114     import reggae.path: buildPath;
115 
116     version(Windows)
117         enum projectPath = "C:/path/to/my/project";
118     else
119         enum projectPath = "/path/to/my/project";
120 
121     Options defaultOptions;
122     defaultOptions.args = ["reggae", "-b", "ninja", projectPath];
123     immutable jsonString = linkJsonString.toVersion1(`["/path/to/foo.py", "/other/path/bar.py"]`);
124     auto options = jsonToOptions(defaultOptions, parseJSON(jsonString));
125     options.reggaeFileDependencies.shouldEqual(
126         [thisExePath,
127          buildPath(projectPath, "reggaefile.d"),
128          "/path/to/foo.py",
129          "/other/path/bar.py"]);
130 }
131 
132 
133 string targetConcatFixedJsonStr() @safe pure nothrow {
134     return `
135       [{"type": "fixed",
136           "command": {"type": "link", "flags": ["-L-M"]},
137           "outputs": ["myapp"],
138           "dependencies": {
139               "type": "dynamic",
140               "func": "targetConcat",
141               "dependencies": [
142                   {
143                       "type": "fixed",
144                       "targets":
145                       [{"type": "fixed",
146                         "command": {"type": "shell",
147                                     "cmd": "dmd -I$project/src -c $in -of$out"},
148                         "outputs": ["main.o"],
149                         "dependencies": {"type": "fixed",
150                                          "targets": [
151                                              {"type": "fixed",
152                                               "command": {}, "outputs": ["src/main.d"],
153                                               "dependencies": {
154                                                   "type": "fixed",
155                                                   "targets": []},
156                                               "implicits": {
157                                                   "type": "fixed",
158                                                   "targets": []}}]},
159                         "implicits": {
160                             "type": "fixed",
161                             "targets": []}},
162                        {"type": "fixed",
163                         "command": {"type": "shell", "cmd":
164                                     "dmd -c $in -of$out"},
165                         "outputs": ["maths.o"],
166                         "dependencies": {
167                             "type": "fixed",
168                             "targets": [
169                                 {"type": "fixed",
170                                  "command": {}, "outputs": ["src/maths.d"],
171                                  "dependencies": {
172                                      "type": "fixed",
173                                      "targets": []},
174                                  "implicits": {
175                                      "type": "fixed",
176                                      "targets": []}}]},
177                         "implicits": {
178                             "type": "fixed",
179                             "targets": []}}]}]},
180                   "implicits": {
181                       "type": "fixed",
182                       "targets": []}}]
183 `;
184 }
185 
186 @("JSON target concat fixed") unittest {
187     auto mainObj = Target("main.o", "dmd -I$project/src -c $in -of$out", Target("src/main.d"));
188     auto mathsObj = Target("maths.o", "dmd -c $in -of$out", Target("src/maths.d"));
189     auto app = link(ExeName("myapp"), [mainObj, mathsObj], Flags("-L-M"));
190     jsonToBuild("", targetConcatFixedJsonStr).shouldEqual(Build(app));
191 }