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