tests.trial.interfaces 108/108(100%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
202
210
220
230
240
251
261
270
280
290
300
316
320
330
340
350
364
372
380
3916
400
416
420
430
440
450
460
471
481
490
502
512
522
532
542
550
560
570
580
590
601
610
621
630
641
650
661
670
681
691
700
711
721
731
740
752
762
772
782
792
802
810
820
830
840
850
861
870
881
891
900
911
920
931
941
950
961
971
981
990
1002
1012
1022
1032
1042
1050
1062
1070
1080
1090
1100
1110
1121
1131
1141
1150
1161
1170
1181
1191
1200
1211
1221
1230
1240
1250
1262
1272
1282
1290
1302
1310
1321
1330
1340
1350
1360
1372
1382
1392
1402
1410
1421
1430
1440
1450
1460
1472
1482
1492
1502
1510
1521
1530
1540
1550
1560
1572
1582
1592
1602
1610
1621
1630
1640
1650
1661
1670
1681
1690
1702
1710
1720
1730
1740
1750
1761
1771
1780
1791
1800
1810
1821
1830
1840
1851
1861
1870
1881
1890
1902
1912
1922
1932
1940
1952
1967
1970
1980
1990
2000
2010
2021
2031
2040
2051
2060
2070
2080
2094
2100
2110
2120
2130
2144
2150
2160
2170
2181
2190
2201
2210
2221
2231
2241
2250
2261
2270
2282
2290
2300
2310
2320
2330
2340
2351
2361
2370
2381
2390
2401
2411
2420
2431
2440
2452
2460
module tests.trial.interfaces; import trial.interfaces; import std.stdio; import std.conv; import std.algorithm; import core.thread; import std.datetime; import trial.step; import trial.runner; import fluent.asserts; import trial.executor.single; __gshared bool executed; void mock() @system { executed = true; } void failureMock() @system { executed = true; assert(false); } void stepFunction(int i) { Step("Step " ~ i.to!string); } void stepMock() @system { auto a = Step("some step"); executed = true; for (int i; i < 3; i++) { stepFunction(i); } } /// Convert a test case to test result unittest { auto testCase = TestCase("Suite name", "test name", &stepMock, [ Label("label", "value") ]); auto testResult = testCase.toTestResult; testResult.name.should.equal("test name"); testResult.labels.should.equal([ Label("label", "value") ]); testResult.begin.should.be.greaterThan(Clock.currTime - 1.seconds); testResult.end.should.be.greaterThan(Clock.currTime - 1.seconds); testResult.status.should.equal(TestResult.Status.created); } @("A suite runner should run a success test case and add it to the result") unittest { TestCase[] tests = [TestCase("Suite name1", "someTestCase", &mock)]; executed = false; auto old = LifeCycleListeners.instance; scope (exit) LifeCycleListeners.instance = old; LifeCycleListeners.instance = new LifeCycleListeners; LifeCycleListeners.instance.add(new DefaultExecutor); auto begin = Clock.currTime - 1.msecs; auto result = tests.runTests; auto end = Clock.currTime + 1.msecs; result.length.should.equal(1); result[0].tests.length.should.equal(1); result[0].tests[0].begin.should.be.between(begin, end); result[0].tests[0].end.should.be.between(begin, end); result[0].tests[0].status.should.be.equal(TestResult.Status.success); executed.should.equal(true); } @("A suite runner should run a failing test case and add it to the result") unittest { TestCase[] tests = [TestCase("Suite name2", "someTestCase", &failureMock)]; executed = false; auto old = LifeCycleListeners.instance; scope (exit) LifeCycleListeners.instance = old; LifeCycleListeners.instance = new LifeCycleListeners; LifeCycleListeners.instance.add(new DefaultExecutor); auto begin = Clock.currTime - 1.msecs; auto result = tests.runTests; auto end = Clock.currTime + 1.msecs; result.length.should.equal(1); result[0].tests.length.should.equal(1); result[0].tests[0].begin.should.be.between(begin, end); result[0].tests[0].end.should.be.between(begin, end); result[0].tests[0].status.should.be.equal(TestResult.Status.failure); executed.should.equal(true); } @("A suite runner should call the suite lifecycle listener methods") unittest { auto old = LifeCycleListeners.instance; LifeCycleListeners.instance = new LifeCycleListeners; LifeCycleListeners.instance.add(new DefaultExecutor); scope (exit) LifeCycleListeners.instance = old; auto beginTime = Clock.currTime - 1.msecs; TestCase[] tests = [TestCase("Suite name", "someTestCase", &mock)]; string[] order = []; class TestSuiteListener : ISuiteLifecycleListener, ITestCaseLifecycleListener { void begin(ref SuiteResult suite) { suite.name.should.equal("Suite name"); suite.begin.should.be.greaterThan(beginTime); suite.end.should.be.greaterThan(beginTime); suite.tests.length.should.equal(0); order ~= "beginSuite"; } void end(ref SuiteResult suite) { suite.name.should.equal("Suite name"); suite.begin.should.be.greaterThan(beginTime); suite.end.should.be.greaterThan(beginTime); suite.tests[0].status.should.equal(TestResult.Status.success); order ~= "endSuite"; } void begin(string suite, ref TestResult test) { test.name.should.equal("someTestCase"); test.begin.should.be.greaterThan(beginTime); test.end.should.be.greaterThan(beginTime); test.status.should.equal(TestResult.Status.started); order ~= "beginTest"; } void end(string suite, ref TestResult test) { test.name.should.equal("someTestCase"); test.begin.should.be.greaterThan(beginTime); test.end.should.be.greaterThan(beginTime); test.status.should.equal(TestResult.Status.success); order ~= "endTest"; } } LifeCycleListeners.instance.add(new TestSuiteListener); tests.runTests; order.should.equal(["beginSuite", "beginTest", "endTest", "endSuite"]); } @("A test runner should add the steps to the report") unittest { auto beginTime = Clock.currTime - 1.msecs; auto const test = TestCase("Suite name", "someTestCase", &stepMock); auto old = LifeCycleListeners.instance; scope (exit) { LifeCycleListeners.instance = old; } LifeCycleListeners.instance = new LifeCycleListeners; LifeCycleListeners.instance.add(new DefaultExecutor); auto result = [ test ].runTests; result[0].tests[0].steps.length.should.equal(1); result[0].tests[0].steps[0].name.should.equal("some step"); result[0].tests[0].steps[0].begin.should.be.greaterThan(beginTime); result[0].tests[0].steps[0].end.should.be.greaterThan(beginTime); result[0].tests[0].steps[0].steps.length.should.equal(3); result[0].tests[0].steps[0].steps.each!(step => step.name.should.startWith("Step ")); } @("A test runner should call the test listeners in the right order") unittest { auto const test = TestCase("Suite name", "someTestCase", &stepMock); string[] order = []; class StepListener : IStepLifecycleListener { void begin(string suite, string test, ref StepResult step) { order ~= "begin " ~ step.name; } void end(string suite, string test, ref StepResult step) { order ~= "end " ~ step.name; } } auto old = LifeCycleListeners.instance; scope (exit) LifeCycleListeners.instance = old; LifeCycleListeners.instance = new LifeCycleListeners; LifeCycleListeners.instance.add(new DefaultExecutor); LifeCycleListeners.instance.add(new StepListener); auto result = [test].runTests; order.should.equal(["begin some step", "begin Step 0", "end Step 0", "begin Step 1", "end Step 1", "begin Step 2", "end Step 2", "end some step"]); } @("A suite runner should set the data to an empty suite runner") unittest { TestCase[] tests; auto old = LifeCycleListeners.instance; scope (exit) LifeCycleListeners.instance = old; LifeCycleListeners.instance = new LifeCycleListeners; LifeCycleListeners.instance.add(new DefaultExecutor); auto result = tests.runTests(); result.length.should.equal(0); }