1 module example.tests.pass_tests; 2 3 import unit_threaded.all; 4 import core.thread; 5 6 7 class IntEqualTest: TestCase { 8 override void test() { 9 checkNotEqual(1, 5); 10 checkNotEqual(5, 1); 11 checkEqual(3, 3); 12 checkEqual(2, 2); 13 } 14 } 15 16 class DoubleEqualTest: TestCase { 17 override void test() { 18 checkNotEqual(1.0, 2.0); 19 checkEqual(2.0, 2.0); 20 checkEqual(2.0, 2.0); 21 } 22 } 23 24 void testEqual() { 25 checkEqual(1, 1); 26 checkEqual(1.0, 1.0); 27 checkEqual("foo", "foo"); 28 } 29 30 void testNotEqual() { 31 checkNotEqual(3, 4); 32 checkNotEqual(5.0, 6.0); 33 checkNotEqual("foo", "bar"); 34 } 35 36 37 private class MyException: Exception { 38 this() { 39 super("MyException"); 40 } 41 } 42 43 void testThrown() { 44 checkThrown!MyException(throwFunc()); 45 } 46 47 void testNotThrown() { 48 checkNotThrown(nothrowFunc()); 49 } 50 51 private void throwFunc() { 52 throw new MyException; 53 } 54 55 private void nothrowFunc() nothrow { 56 {} 57 } 58 59 //the tests below should take only 1 second in total if using parallelism 60 //(given enough cores) 61 void testLongRunning1() { 62 Thread.sleep( dur!"seconds"(1)); 63 } 64 65 void testLongRunning2() { 66 Thread.sleep( dur!"seconds"(1)); 67 } 68 69 void testLongRunning3() { 70 Thread.sleep( dur!"seconds"(1)); 71 } 72 73 void testLongRunning4() { 74 Thread.sleep( dur!"seconds"(1)); 75 } 76 77 class TestIo: TestCase { 78 override void test() { 79 writelnUt("Class writelnUt should only print with '-d' option"); 80 } 81 } 82 83 void testNoIo1() { 84 import std.stdio; 85 writeln("This should not be seen except for -d option"); 86 writeln("Or this"); 87 stderr.writeln("Stderr shouldn't be seen either"); 88 writelnUt("But this should show up when using -d option"); 89 } 90 91 92 void testNoIo2() { 93 import std.stdio; 94 writeln("This should not be seen except for -d option"); 95 writeln("Or this"); 96 stderr.writeln("Stderr shouldn't be seen either"); 97 } 98 99 void testNoIo3() { 100 import std.stdio; 101 writeln("This should not be seen except for -d option"); 102 writeln("Or this"); 103 stderr.writeln("Stderr shouldn't be seen either"); 104 } 105 106 107 @UnitTest 108 void funcAttributes() { 109 //tests that using the @UnitTest UDA adds this function 110 //to the list of tests despite its name 111 checkEqual(1, 1); 112 } 113 114 //won't be registered, impossible to instantiate 115 class BaseAttr: TestCase { 116 override void test() { 117 doTest(); 118 } 119 120 abstract void doTest(); 121 } 122 123 //will be registered since actually has 'test' method 124 class Attr: BaseAttr{ 125 override void doTest() { 126 checkEqual(2, 2); 127 } 128 } 129 130 //won't be tested due to attribute 131 @DontTest 132 void testThatWontRun() { 133 checkEqual(1, 2); //doesn't matter, won't run anyway 134 } 135 136 @DontTest 137 class TestThatWontRun: TestCase { 138 override void test() { 139 checkNotNull(null); //doesn't matter, won't run anyway 140 } 141 } 142 143 @HiddenTest 144 class HiddenTest: TestCase { 145 override void test() { 146 checkNotNull(null); //hidden by default, fails if explicitly run 147 } 148 } 149 150 @HiddenTest 151 void testHidden() { 152 checkNotNull(null); //hidden by default, fails if explicitly run 153 } 154 155 private void testPrivate() { 156 //private function, won't get run 157 checkNotNull(null); //won't run, can't fail 158 } 159 160 private class PrivateTest: TestCase { 161 override void test() { 162 checkNotNull(null); //won't run, can't fail 163 } 164 } 165 166 unittest { 167 assert(true); //unit test block that always passes 168 }