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 void testLink() {
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     newOptions.cppCompiler.shouldEqual("g++");
82 }
83 
84 private string toVersion1(in string jsonString, in string dependencies = `[]`) {
85     return `{"version": 1, "defaultOptions": {"cCompiler": "huh"}, "dependencies": ` ~ dependencies ~ `, "build": ` ~ jsonString ~ `}`;
86 }
87 
88 @("jsonToOptions version1")
89 unittest {
90     import reggae.config: gDefaultOptions;
91     import std.json;
92 
93     auto oldOptions = gDefaultOptions.dup;
94     oldOptions.args = ["reggae", "-b", "ninja", "/path/to/my/project"];
95     immutable jsonString = linkJsonString.toVersion1;
96     auto newOptions = jsonToOptions(oldOptions, parseJSON(jsonString));
97     newOptions.cCompiler.shouldEqual("huh");
98     newOptions.cppCompiler.shouldEqual("g++");
99     newOptions.oldNinja.shouldBeFalse;
100 }
101 
102 @("jsonToOptions with dependencies")
103 unittest {
104     import std.json;
105     import std.file;
106     import std.path;
107 
108     Options defaultOptions;
109     defaultOptions.args = ["reggae", "-b", "ninja", "/path/to/my/project"];
110     immutable jsonString = linkJsonString.toVersion1(`["/path/to/foo.py", "/other/path/bar.py"]`);
111     auto options = jsonToOptions(defaultOptions, parseJSON(jsonString));
112     options.reggaeFileDependencies.shouldEqual(
113         [thisExePath,
114          buildPath("/path/to/my/project", "reggaefile.d"),
115          "/path/to/foo.py",
116          "/other/path/bar.py"]);
117 }
118 
119 
120 string targetConcatFixedJsonStr() @safe pure nothrow {
121     return `
122       [{"type": "fixed",
123           "command": {"type": "link", "flags": "-L-M"},
124           "outputs": ["myapp"],
125           "dependencies": {
126               "type": "dynamic",
127               "func": "targetConcat",
128               "dependencies": [
129                   {
130                       "type": "fixed",
131                       "targets":
132                       [{"type": "fixed",
133                         "command": {"type": "shell",
134                                     "cmd": "dmd -I$project/src -c $in -of$out"},
135                         "outputs": ["main.o"],
136                         "dependencies": {"type": "fixed",
137                                          "targets": [
138                                              {"type": "fixed",
139                                               "command": {}, "outputs": ["src/main.d"],
140                                               "dependencies": {
141                                                   "type": "fixed",
142                                                   "targets": []},
143                                               "implicits": {
144                                                   "type": "fixed",
145                                                   "targets": []}}]},
146                         "implicits": {
147                             "type": "fixed",
148                             "targets": []}},
149                        {"type": "fixed",
150                         "command": {"type": "shell", "cmd":
151                                     "dmd -c $in -of$out"},
152                         "outputs": ["maths.o"],
153                         "dependencies": {
154                             "type": "fixed",
155                             "targets": [
156                                 {"type": "fixed",
157                                  "command": {}, "outputs": ["src/maths.d"],
158                                  "dependencies": {
159                                      "type": "fixed",
160                                      "targets": []},
161                                  "implicits": {
162                                      "type": "fixed",
163                                      "targets": []}}]},
164                         "implicits": {
165                             "type": "fixed",
166                             "targets": []}}]}]},
167                   "implicits": {
168                       "type": "fixed",
169                       "targets": []}}]
170 `;
171 }
172 
173 void testJsonTargetConcatFixed() {
174     auto mainObj = Target("main.o", "dmd -I$project/src -c $in -of$out", Target("src/main.d"));
175     auto mathsObj = Target("maths.o", "dmd -c $in -of$out", Target("src/maths.d"));
176     auto app = link(ExeName("myapp"), [mainObj, mathsObj], Flags("-L-M"));
177     jsonToBuild("", targetConcatFixedJsonStr).shouldEqual(Build(app));
178 }