1 module tests.ut.dub.json;
2 
3 
4 import unit_threaded;
5 import reggae;
6 import reggae.dub.json;
7 import std.string;
8 
9 @("foobar.json gets parsed correctly")
10 unittest {
11     import std.format: format;
12     const path = "/path/to";
13     const info = getDubInfo(import("foobar.json").format(path, path, path, path, path, path, path, path, path, path, path));
14     info.shouldEqual(
15         DubInfo(
16             [
17                 DubPackage(
18                     "foo", //name
19                     "/path/to/", //path
20                     "/path/to/source/app.d", // mainSourceFile
21                     "foo", // targetFileName
22                     [], // dflags
23                     [], // lflags
24                     ["/path/to/source/", "/path/to/bar/source/"], // importPaths
25                     [], // stringImportPaths
26                     ["/path/to/source/app.d"], // sourceFiles
27                     TargetType.executable,
28                     ["lefoo", "Have_foo", "Have_bar"], // versions
29                     ["bar"], // dependencies
30                     [], // libs
31                     true, // active
32                     [], // preBuildCommands
33                     [], //postBuildCommands
34                 ),
35                 DubPackage(
36                     "bar", //name
37                     "/path/to/bar/", //path
38                     "", // mainSourceFile
39                     "bar", // targetFileName
40                     [], // dflags
41                     [], // lflags
42                     ["/path/to/bar/source/"], // importPaths
43                     [], // stringImportPaths
44                     ["/path/to/bar/source/bar.d"], // sourceFiles
45                     TargetType.staticLibrary,
46                     ["lefoo", "Have_bar"], // versions
47                     [], // dependencies
48                     [], // libs
49                     true, // active
50                     [], // preBuildCommands
51                     [], //postBuildCommands
52                 ),
53             ]
54         )
55     );
56 }
57 
58 @("DubInfo.targetName")
59 unittest {
60     import std.format: format;
61     const path = "/path/to";
62     auto info = getDubInfo(import("foobar.json").format(path, path, path, path, path, path, path, path, path, path, path));
63 
64     info.targetName.shouldEqual(TargetName("foo"));
65 
66     info.packages[0].targetType = TargetType.dynamicLibrary;
67     info.targetName.shouldEqual(TargetName("libfoo.so"));
68 
69     info.packages[0].targetType = TargetType.library;
70     info.targetName.shouldEqual(TargetName("libfoo.a"));
71 }
72 
73 @("PACKAGE_DIR")
74 unittest {
75     const jsonString = q{
76         {
77             "packages": [
78                 {
79                     "name": "lepackage",
80                     "path": "/dub/packages/lepackage",
81                     "files": [
82                         {"role": "source", "path": "$PACKAGE_DIR/foo.o"},
83                         {"role": "source", "path": "src/file.d"}
84                     ]
85                 },
86                 {
87                     "name": "dep",
88                     "path": "/dub/packages/dep",
89                     "files": [
90                         {"role": "source", "path": "$PACKAGE_DIR/bar.o"},
91                         {"role": "source", "path": "src/dep.d"}
92                     ]
93                 }
94             ],
95             "targets": [
96                 {
97                     "rootPackage": "lepackage",
98                     "buildSettings": {
99                         "targetName": "lepackage",
100                         "targetPath": "/dub/packages/lepackage",
101                         "dflags": [],
102                         "lflags": ["-L$PACKAGE_DIR"],
103                         "importPaths": ["$PACKAGE_DIR/imports"],
104                         "stringImportPaths": ["$PACKAGE_DIR/stringImports"],
105                         "sourceFiles": [
106                             "$PACKAGE_DIR/foo.o",
107                             "src/file.d"
108                         ]
109                     }
110                 },
111                 {
112                     "rootPackage": "dep",
113                     "buildSettings": {
114                         "targetName": "dep",
115                         "targetPath": "/dub/packages/dep",
116                         "dflags": [],
117                         "lflags": [],
118                         "importPaths": [],
119                         "stringImportPaths": [],
120                         "sourceFiles": [
121                             "$PACKAGE_DIR/bar.o",
122                             "src/dep.d"
123                         ]
124                     }
125                 }
126             ]
127         }
128     };
129 
130     getDubInfo(jsonString).shouldEqual(
131         DubInfo(
132             [
133                 DubPackage(
134                     "lepackage",
135                     "/dub/packages/lepackage",
136                     "", // mainSourceFile
137                     "lepackage", // targetFileName
138                     [], // dflags
139                     ["-L/dub/packages/lepackage"],
140                     ["/dub/packages/lepackage/imports"],
141                     ["/dub/packages/lepackage/stringImports"],
142                     ["/dub/packages/lepackage/foo.o", "src/file.d"],
143                     TargetType.autodetect, // targetType
144                     [], // versions
145                     [], // dependencies
146                     [], // libs
147                     true, // active
148                 ),
149                 DubPackage(
150                     "dep",
151                     "/dub/packages/dep",
152                     "", // mainSourceFile
153                     "dep", //targetFileName
154                     [], // dflags
155                     [], // lflags
156                     [], // importPaths
157                     [], // stringImportPaths
158                     ["/dub/packages/dep/bar.o", "src/dep.d"],
159                     TargetType.autodetect, // targetType
160                     [], // versions
161                     [], // dependencies
162                     [], // libs
163                     true, // active
164                 ),
165             ]
166     ));
167 }
168 
169 @("remove object file duplicates")
170 unittest {
171    const info = DubInfo(
172        [
173            DubPackage(
174                "foo", //name
175                "/path/to/", //path
176                "/path/to/source/app.d", // mainSourceFile
177                "foo", // targetFileName
178                [], // dflags
179                [], // lflags
180                ["/path/to/source/", "/path/to/bar/source/"], // importPaths
181                [], // stringImportPaths
182                ["/path/to/source/app.d", "baz.o"], // sourceFiles
183                TargetType.executable,
184                ["lefoo", "Have_foo", "Have_bar"], // versions
185                ["bar"], // dependencies
186                [], // libs
187                true, // active
188                [], // preBuildCommands
189                [], //postBuildCommands
190            ),
191            DubPackage(
192                "bar", //name
193                "/path/to/bar/", //path
194                "", // mainSourceFile
195                "bar", // targetFileName
196                [], // dflags
197                [], // lflags
198                ["/path/to/bar/source/"], // importPaths
199                [], // stringImportPaths
200                ["/path/to/bar/source/bar.d", "baz.o"], // sourceFiles
201                TargetType.staticLibrary,
202                ["lefoo", "Have_bar"], // versions
203                [], // dependencies
204                [], // libs
205                true, // active
206                [], // preBuildCommands
207                [], //postBuildCommands
208            ),
209        ]
210    );
211 
212    info.cleanObjectSourceFiles.shouldEqual(DubInfo(
213        [
214            DubPackage(
215                "foo", //name
216                "/path/to/", //path
217                "/path/to/source/app.d", // mainSourceFile
218                "foo", // targetFileName
219                [], // dflags
220                [], // lflags
221                ["/path/to/source/", "/path/to/bar/source/"], // importPaths
222                [], // stringImportPaths
223                ["/path/to/source/app.d"], // sourceFiles
224                TargetType.executable,
225                ["lefoo", "Have_foo", "Have_bar"], // versions
226                ["bar"], // dependencies
227                [], // libs
228                true, // active
229                [], // preBuildCommands
230                [], //postBuildCommands
231            ),
232            DubPackage(
233                "bar", //name
234                "/path/to/bar/", //path
235                "", // mainSourceFile
236                "bar", // targetFileName
237                [], // dflags
238                [], // lflags
239                ["/path/to/bar/source/"], // importPaths
240                [], // stringImportPaths
241                ["/path/to/bar/source/bar.d", "baz.o"], // sourceFiles
242                TargetType.staticLibrary,
243                ["lefoo", "Have_bar"], // versions
244                [], // dependencies
245                [], // libs
246                true, // active
247                [], // preBuildCommands
248                [], //postBuildCommands
249            ),
250        ]
251    ));
252 }
253 
254 @("cleanObjectSourceFiles with no duplicates")
255 unittest {
256    const info = DubInfo(
257        [
258            DubPackage(
259                "foo", //name
260                "/path/to/", //path
261                "/path/to/source/app.d", // mainSourceFile
262                "foo", // targetFileName
263                [], // dflags
264                [], // lflags
265                ["/path/to/source/", "/path/to/bar/source/"], // importPaths
266                [], // stringImportPaths
267                ["/path/to/source/app.d", "baz.o"], // sourceFiles
268                TargetType.executable,
269                ["lefoo", "Have_foo", "Have_bar"], // versions
270                ["bar"], // dependencies
271                [], // libs
272                true, // active
273                [], // preBuildCommands
274                [], //postBuildCommands
275            ),
276            DubPackage(
277                "bar", //name
278                "/path/to/bar/", //path
279                "", // mainSourceFile
280                "bar", // targetFileName
281                [], // dflags
282                [], // lflags
283                ["/path/to/bar/source/"], // importPaths
284                [], // stringImportPaths
285                ["/path/to/bar/source/bar.d", "quux.o"], // sourceFiles
286                TargetType.staticLibrary,
287                ["lefoo", "Have_bar"], // versions
288                [], // dependencies
289                [], // libs
290                true, // active
291                [], // preBuildCommands
292                [], //postBuildCommands
293            ),
294        ]
295    );
296 
297    info.cleanObjectSourceFiles.shouldEqual(DubInfo(
298        [
299            DubPackage(
300                "foo", //name
301                "/path/to/", //path
302                "/path/to/source/app.d", // mainSourceFile
303                "foo", // targetFileName
304                [], // dflags
305                [], // lflags
306                ["/path/to/source/", "/path/to/bar/source/"], // importPaths
307                [], // stringImportPaths
308                ["/path/to/source/app.d", "baz.o"], // sourceFiles
309                TargetType.executable,
310                ["lefoo", "Have_foo", "Have_bar"], // versions
311                ["bar"], // dependencies
312                [], // libs
313                true, // active
314                [], // preBuildCommands
315                [], //postBuildCommands
316            ),
317            DubPackage(
318                "bar", //name
319                "/path/to/bar/", //path
320                "", // mainSourceFile
321                "bar", // targetFileName
322                [], // dflags
323                [], // lflags
324                ["/path/to/bar/source/"], // importPaths
325                [], // stringImportPaths
326                ["/path/to/bar/source/bar.d", "quux.o"], // sourceFiles
327                TargetType.staticLibrary,
328                ["lefoo", "Have_bar"], // versions
329                [], // dependencies
330                [], // libs
331                true, // active
332                [], // preBuildCommands
333                [], //postBuildCommands
334            ),
335        ]
336    ));
337 }