trial.executor.single 71/71(100%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
3313
3413
350
360
370
380
393
401
411
420
430
442
450
460
470
480
490
5011
5111
5211
5311
540
550
560
570
580
5911
6011
6111
620
630
640
650
660
679
680
690
700
710
720
7312
740
751
760
770
7811
7911
8011
810
820
830
840
850
860
870
880
89229
90226
910
920
930
941
950
960
970
982
992
1000
1010
1020
1030
1040
1050
106234
107234
1080
109234
110234
1110
112234
1130
114234
115234
1160
117234
1180
119234
1200
121234
1220
123234
1240
1250
1260
1270
1280
1290
130234
131234
1320
133234
1340
13553
1360
13741
13841
13941
1400
1410
14253
14353
1440
1450
146234
147234
148234
149234
1500
151234
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1641
1651
1661
1670
1680
1691
1700
1710
1721
1731
1740
1750
1761
1771
1780
1792
1802
1810
1820
1830
1840
1850
1860
1871
1881
1891
1900
1910
1921
1930
1940
1951
1961
1970
1980
1991
2000
2011
2021
2031
2040
2052
2062
2070
/++ A module containing the single threaded runner Copyright: © 2017 Szabo Bogdan License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Szabo Bogdan +/ module trial.executor.single; public import trial.interfaces; import trial.runner; import std.datetime; import trial.step; import trial.stackresult; version (Have_fluent_asserts) { version = Have_fluent_asserts_core; } /** The default test executor runs test in sequential order in a single thread */ class DefaultExecutor : ITestExecutor, IStepLifecycleListener, IAttachmentListener { private { SuiteResult suiteResult; TestResult testResult; StepResult currentStep; StepResult[] stepStack; } this() { suiteResult = SuiteResult("unknown"); } /// Called when an attachment is ready void attach(ref const Attachment attachment) { if(currentStep is null) { suiteResult.attachments ~= Attachment(attachment.name, attachment.file, attachment.mime); return; } currentStep.attachments ~= Attachment(attachment.name, attachment.file, attachment.mime); } /// Add the step result and update the other listeners on every step void begin(string suite, string test, ref StepResult step) { currentStep.steps ~= step; stepStack ~= currentStep; currentStep = step; LifeCycleListeners.instance.update(); } /// Update the other listeners on every step void end(string suite, string test, ref StepResult step) { currentStep = stepStack[stepStack.length - 1]; stepStack = stepStack[0 .. $ - 1]; LifeCycleListeners.instance.update(); } /// It does nothing SuiteResult[] beginExecution(ref const(TestCase)[]) { return []; } /// Return the result for the last executed suite SuiteResult[] endExecution() { if (suiteResult.begin == SysTime.fromUnixTime(0)) { return []; } LifeCycleListeners.instance.update(); LifeCycleListeners.instance.end(suiteResult); return [ suiteResult ]; } protected { /// Run a test case void runTest(ref const(TestCase) testCase, TestResult testResult) { try { testCase.func(); testResult.status = TestResult.Status.success; } catch (PendingTestException) { testResult.status = TestResult.Status.pending; } catch (Throwable t) { testResult.status = TestResult.Status.failure; testResult.throwable = t.toTestException; } } /// Convert a test case to a test result void createTestResult(const(TestCase) testCase) { testResult = testCase.toTestResult; testResult.begin = Clock.currTime; testResult.status = TestResult.Status.started; currentStep = testResult; stepStack = []; Step.suite = testCase.suiteName; Step.test = testCase.name; LifeCycleListeners.instance.begin(testCase.suiteName, testResult); runTest(testCase, testResult); testResult.end = Clock.currTime; LifeCycleListeners.instance.end(testCase.suiteName, testResult); } } /// Execute a test case SuiteResult[] execute(ref const(TestCase) testCase) { SuiteResult[] result; LifeCycleListeners.instance.update(); if (suiteResult.name != testCase.suiteName) { if (suiteResult.begin != SysTime.fromUnixTime(0)) { suiteResult.end = Clock.currTime; LifeCycleListeners.instance.end(suiteResult); result = [suiteResult]; } suiteResult = SuiteResult(testCase.suiteName, Clock.currTime, Clock.currTime); LifeCycleListeners.instance.begin(suiteResult); } createTestResult(testCase); suiteResult.tests ~= testResult; currentStep = null; LifeCycleListeners.instance.update(); return result; } } version(unittest) { version(Have_fluent_asserts_core) { import fluent.asserts; } } /// Executing a test case that throws a PendingTestException should mark the test result /// as pending instead of a failure unittest { auto old = LifeCycleListeners.instance; LifeCycleListeners.instance = new LifeCycleListeners; LifeCycleListeners.instance.add(new DefaultExecutor); scope (exit) { LifeCycleListeners.instance = old; } void test() { throw new PendingTestException(); } auto testCase = const TestCase("Some.Suite", "test name", &test, []); auto result = [testCase].runTests; result.length.should.equal(1); result[0].tests[0].status.should.equal(TestResult.Status.pending); } /// Executing a test case should set the right begin and end times unittest { import core.thread; auto old = LifeCycleListeners.instance; LifeCycleListeners.instance = new LifeCycleListeners; LifeCycleListeners.instance.add(new DefaultExecutor); scope (exit) { LifeCycleListeners.instance = old; } void test() { Thread.sleep(1.msecs); } auto testCase = const TestCase("Some.Suite", "test name", &test, []); auto begin = Clock.currTime; auto result = [ testCase ].runTests; auto testResult = result[0].tests[0]; testResult.begin.should.be.greaterThan(begin); testResult.end.should.be.greaterThan(begin + 1.msecs); }