1 module tests.it.runtime;
2 
3 public import tests.it;
4 public import tests.utils;
5 import reggae.reggae;
6 
7 // calls reggae.run, which is basically main, but with a
8 // fake file
9 auto testRun(string[] args) {
10     auto output = FakeFile();
11     run(output, args);
12     return output;
13 }
14 
15 struct ReggaeSandbox {
16     Sandbox sandbox;
17     alias sandbox this;
18 
19     static ReggaeSandbox opCall() {
20         ReggaeSandbox ret;
21         ret.sandbox = Sandbox();
22         return ret;
23     }
24 
25     static ReggaeSandbox opCall(in string projectName) {
26         auto ret = ReggaeSandbox();
27         ret.copyProject(projectName);
28         return ret;
29     }
30 
31     auto runReggae(string[] args...) const {
32         return runImpl(args);
33     }
34 
35     auto runReggae(string[] args, string project) const {
36         return runImpl(args, project);
37     }
38 
39     void writeHelloWorldApp() const {
40         import std.stdio;
41         import std.path;
42         import std.file;
43         import std.array;
44 
45         mkdir(buildPath(testPath, "src"));
46         sandbox.writeFile(buildPath("src", "hello.d"), q{
47                 import std.stdio;
48                 void main() {
49                     writeln("Hello world!");
50                 }
51         }.split("\n").array);
52     }
53 
54     auto shouldSucceed(in string arg,
55                        in string file = __FILE__,
56                        ulong line = __LINE__ ) const {
57         import tests.utils;
58         return [buildPath(testPath, arg)].shouldExecuteOk(testPath, file, line);
59     }
60 
61     auto shouldFail(in string arg, in string file = __FILE__, ulong line = __LINE__) const {
62         import tests.utils;
63         return [buildPath(testPath, arg)].shouldFailToExecute(testPath, file, line);
64     }
65 
66     void copyProject(in string projectName) const {
67         import std.path;
68         const projPath = buildPath(origPath, "tests", "projects", projectName);
69         copyProjectFiles(projPath, testPath);
70     }
71 
72 
73 private:
74 
75     auto runImpl(string[] args, string project = "") const {
76         import std.file: thisExePath;
77         import std.path: buildPath, dirName, absolutePath;
78         if(project == "") project = testPath;
79         //return testRun([buildPath(thisExePath.absolutePath.dirName, "reggae"), "-C", testPath] ~ args ~ project);
80         return testRun(["reggae", "-C", testPath] ~ args ~ project);
81     }
82 }
83 
84 
85 void shouldContain(string[] haystack, in string needle,
86                    string file = __FILE__, size_t line = __LINE__) {
87     import std.algorithm;
88     import std.array;
89     if(!haystack.canFind!(a => a.canFind(needle)))
90         throw new UnitTestException(["Could not find " ~ needle ~ " in:"] ~ haystack);
91 }
92 
93 void shouldContain(in string haystack, in string needle,
94                    in string file = __FILE__, in size_t line = __LINE__) {
95     import std.algorithm;
96     import std.array;
97     if(!haystack.canFind(needle))
98         throw new UnitTestException(["Could not find " ~ needle ~ " in:"] ~ haystack);
99 }
100 
101 
102 void shouldNotContain(string[] haystack, in string needle,
103                       string file = __FILE__, size_t line = __LINE__) {
104     import std.algorithm;
105     import std.array;
106     if(haystack.canFind!(a => a.canFind(needle)))
107         throw new UnitTestException(["Should not have found " ~ needle ~ " in:"] ~ haystack);
108 }
109 
110 void shouldNotContain(in string haystack, in string needle,
111                       in string file = __FILE__, in size_t line = __LINE__) {
112     import std.algorithm;
113     import std.array;
114     if(haystack.canFind(needle))
115         throw new UnitTestException(["Should not have found " ~ needle ~ " in:"] ~ haystack);
116 }