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 in size_t line = __LINE__ ) const { 66 import tests.utils; 67 import std.path: buildPath; 68 return [buildPath(testPath, arg)].shouldExecuteOk(WorkDir(testPath), file, line); 69 } 70 71 auto shouldFail(in string arg, in string file = __FILE__, in size_t line = __LINE__) const { 72 import tests.utils; 73 import std.path: buildPath; 74 return [buildPath(testPath, arg)].shouldFailToExecute(testPath, file, line); 75 } 76 77 void copyProject(in string projectName, in string testSubPath = ".") const { 78 import std.path; 79 const fromPath = buildPath(origPath, "tests", "projects", projectName); 80 const toPath = buildPath(testPath, testSubPath); 81 copyProjectFiles(fromPath, toPath); 82 } 83 84 85 private: 86 87 auto runImpl(string[] args, string project = "") const { 88 import std.file: thisExePath; 89 import std.path: buildPath, dirName, absolutePath; 90 if(project == "") project = testPath; 91 return testRun(["reggae", "-C", testPath] ~ args ~ project); 92 } 93 } 94 95 96 void shouldContain(string[] haystack, in string needle, 97 string file = __FILE__, size_t line = __LINE__) { 98 import std.algorithm; 99 import std.array; 100 if(!haystack.canFind!(a => a.canFind(needle))) 101 throw new UnitTestException(["Could not find " ~ needle ~ " in:"] ~ haystack, file, line); 102 } 103 104 void shouldContain(in string haystack, in string needle, 105 in string file = __FILE__, in size_t line = __LINE__) { 106 import std.algorithm; 107 import std.array; 108 if(!haystack.canFind(needle)) 109 throw new UnitTestException(["Could not find " ~ needle ~ " in:"] ~ haystack, file, line); 110 } 111 112 113 void shouldNotContain(string[] haystack, in string needle, 114 string file = __FILE__, size_t line = __LINE__) { 115 import std.algorithm; 116 import std.array; 117 if(haystack.canFind!(a => a.canFind(needle))) 118 throw new UnitTestException(["Should not have found " ~ needle ~ " in:"] ~ haystack, file, line); 119 } 120 121 void shouldNotContain(in string haystack, in string needle, 122 in string file = __FILE__, in size_t line = __LINE__) { 123 import std.algorithm; 124 import std.array; 125 if(haystack.canFind(needle)) 126 throw new UnitTestException(["Should not have found " ~ needle ~ " in:"] ~ haystack, file, line); 127 }