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 void runReggae(string[] args...) const { 32 runImpl(args); 33 } 34 35 void runReggae(string[] args, string project) const { 36 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 void 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 void runImpl(string[] args, string project = "") const { 76 if(project == "") project = testPath; 77 testRun(["reggae", "-C", testPath] ~ args ~ project); 78 } 79 } 80 81 82 void shouldContain(string[] haystack, in string needle, 83 string file = __FILE__, size_t line = __LINE__) { 84 import std.algorithm; 85 import std.array; 86 if(!haystack.canFind!(a => a.canFind(needle))) 87 throw new UnitTestException(["Could not find " ~ needle ~ " in:"] ~ haystack); 88 89 }