trial.interfaces 174/177(98%) 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
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
11215
1130
1140
1150
1160
1170
1182
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1361
1370
1381
1391
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
15464
1550
1560
1570
1580
1590
1600
1612
1620
1630
1640
165220
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
19511
19611
19711
19811
19911
20011
2010
20233
2030
2040
2050
2060
2070
20834
20934
21034
2110
2120
2130
214286
215286
2160
2170
2180
219438
220438
221438
222438
223438
2240
2250
2260
22763
2280
22963
23063
23171
23263
2330
23463
2350
2360
2370
2380
2390
2401
2410
2421
2431
2440
2452
2460
2470
2480
2490
2500
2510
252214
2530
254214
255214
256214
257214
258214
2590
260214
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
28952
29052
29152
29252
2930
2940
2950
29647
29747
29847
29947
3000
3010
3020
3033
3043
3053
3063
3073
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
329325
330325
331325
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
378286
3790
380286
381286
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4012
4020
4030
4040
4050
4061
4071
4080
4090
4100
4110
4126
4130
4140
4150
4160
4174
4182
4190
42016
4210
4226
4230
4240
4250
4260
4270
4280
4291
4301
4310
4322
4332
4342
4352
4362
4370
4380
4390
4400
4410
4421
4430
4441
4450
4461
4470
4481
4490
4501
4511
4520
4531
4541
4551
4560
4572
4582
4592
4602
4612
4622
4630
4640
4650
4660
4670
4681
4690
4701
4711
4720
4731
4740
4751
4761
4770
4781
4791
4801
4810
4822
4832
4842
4852
4862
4870
4882
4890
4900
4910
4920
4930
4941
4951
4961
4970
4981
4990
5001
5011
5020
5031
5041
5050
5060
5070
5082
5092
5102
5110
5122
5130
5141
5150
5160
5170
5180
5192
5202
5212
5222
5230
5241
5250
5260
5270
5280
5292
5302
5312
5322
5330
5341
5350
5360
5370
5380
5392
5402
5412
5422
5430
5441
5450
5460
5470
5481
5490
5501
5510
5522
5530
5540
5550
5560
5570
5581
5591
5600
5611
5620
5630
5641
5650
5660
5671
5681
5690
5701
5710
5722
5732
5742
5752
5760
5772
5787
5790
5800
5810
5820
5830
5841
5851
5860
5871
5880
5890
5900
5914
5920
5930
5940
5950
5964
5970
5980
5990
6001
6010
6021
6030
6041
6051
6061
6070
6081
6090
6102
6110
6120
6130
6140
6150
6160
6171
6181
6190
6201
6210
6221
6231
6240
6251
6260
6272
6280
6290
6300
6310
6320
6330
6340
6350
6366
6370
6380
6390
6400
6410
6420
6430
6440
6450
6460
6470
64812
6490
6500
6510
6520
6530
6540
6550
6560
6570
6580
6590
6600
6610
6620
6630
6640
6650
6660
6670
6680
6690
6700
6710
6720
6730
6741
6750
6760
6770
6780
6790
6800
6811
6821
6830
6840
/++ A module containing the interfaces used for extending the test 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.interfaces; import std.datetime; import std.algorithm; import std.array; import std.functional; import std.conv; /// Alias to a Test Case function type alias TestCaseDelegate = void delegate() @system; alias TestCaseFunction = void function() @system; /// A Listener for the main test events interface ILifecycleListener { /// This method is trigered when before the test start void begin(ulong testCount); /** This method is triggered when you can perform some updates. The frequency varries by the test executor that you choose */ void update(); /// This method is trigered when your tests are ended void end(SuiteResult[]); } /// A listener that provides test cases to be executed interface ITestDiscovery { /// Get the test cases from the compiled source code TestCase[] getTestCases(); } /// A listener that provides test cases contained in a certain file interface ITestDescribe { /// Get the test cases by parsing the source code TestCase[] discoverTestCases(string file); } /** A Listener that can run tests. During the test execution can be used only one instance of this listance. After all the tests were executed the result of all three methods are concatenated and passed to `ILifecycleListener.end(SuiteResult[])` */ interface ITestExecutor { /// Called before all tests were discovered and they are ready to be executed SuiteResult[] beginExecution(ref const(TestCase)[]); /// Run a particullary test case SuiteResult[] execute(ref const(TestCase)); /// Called when there is no more test to be executed SuiteResult[] endExecution(); } /// A Listener for the suite events interface ISuiteLifecycleListener { /// Called before a suite execution void begin(ref SuiteResult); /// Called after a suite execution void end(ref SuiteResult); } /// A Listener for handling attachments interface IAttachmentListener { /// Called when an attachment is ready void attach(ref const Attachment); } /// A Listener for the test case events interface ITestCaseLifecycleListener { /// Called before a test execution void begin(string suite, ref TestResult); // Called after a test execution void end(string suite, ref TestResult); } /// A Listener for the step events interface IStepLifecycleListener { /// Called before a step begins void begin(string suite, string test, ref StepResult); /// Called after a step ended void end(string suite, string test, ref StepResult); } /// A struct representing a label for test results struct Label { /// The label name string name; /// The label value string value; /// Convert the struct to a JSON string string toString() inout { return `{ "name": "` ~ name.escapeJson ~ `", "value": "` ~ value.escapeJson ~ `" }`; } } /// Label string representation should be in Json format unittest { Label("name", "value").toString.should.equal(`{ "name": "name", "value": "value" }`); } /// A struct representing an attachment for test steps struct Attachment { /// The attachment name string name; /// The absolute path to the attachment string file; /// The file mime path string mime; /// Add a file to the current test or step static void fromFile(const string name, const string path, const string mime) { import trial.runner; auto a = const Attachment(name, path, name); if(LifeCycleListeners.instance !is null) { LifeCycleListeners.instance.attach(a); } } } /// Represents a line of code in a certain file. struct SourceLocation { /// string fileName; /// size_t line; /// Converts the structure to a JSON string string toString() inout { return `{ "fileName": "` ~ fileName.escapeJson ~ `", "line": ` ~ line.to!string ~ ` }`; } } /// SourceLocation string representation should be a JSON string unittest { SourceLocation("file.d", 10).toString.should.equal(`{ "fileName": "file.d", "line": 10 }`); } private string escapeJson(string value) { return value.replace(`"`, `\"`).replace("\r", `\r`).replace("\n", `\n`).replace("\t", `\t`); } /// A test case that will be executed struct TestCase { /** The test case suite name. It can contain `.` which is treated as a separator for nested suites */ string suiteName; /// The test name string name; /** The function that must be executed to check if the test passes or not. In case of failure, an exception is thrown. */ TestCaseDelegate func; /** A list of labels that will be added to the final report */ Label[] labels; /// The test location SourceLocation location; /// this(const TestCase testCase) { suiteName = testCase.suiteName.dup; name = testCase.name.dup; func = testCase.func; location = testCase.location; labels.length = testCase.labels.length; foreach(key, val; testCase.labels) { labels[key] = val; } } /// this(T)(string suiteName, string name, T func, Label[] labels, SourceLocation location) { this(suiteName, name, func.toDelegate, labels); this.location = location; } /// this(string suiteName, string name, TestCaseFunction func, Label[] labels = []) { this(suiteName, name, func.toDelegate, labels); } /// this(string suiteName, string name, TestCaseDelegate func, Label[] labels = []) { this.suiteName = suiteName; this.name = name; this.func = func; this.labels = labels; } string toString() const { string jsonRepresentation = "{ "; jsonRepresentation ~= `"suiteName": "` ~ suiteName.escapeJson ~ `", `; jsonRepresentation ~= `"name": "` ~ name.escapeJson ~ `", `; jsonRepresentation ~= `"labels": [ ` ~ labels.map!(a => a.toString).join(", ") ~ ` ], `; jsonRepresentation ~= `"location": ` ~ location.toString; return jsonRepresentation ~ " }"; } } /// TestCase string representation should be a JSON string unittest { void MockTest() {} auto testCase = TestCase("some suite", "some name", &MockTest, [ Label("label1", "value1"), Label("label2", "value2") ]); testCase.location = SourceLocation("file.d", 42); testCase.toString.should.equal(`{ "suiteName": "some suite", "name": "some name", ` ~ `"labels": [ { "name": "label1", "value": "value1" }, { "name": "label2", "value": "value2" } ], ` ~ `"location": { "fileName": "file.d", "line": 42 } }`); } /// TestResult toTestResult(const TestCase testCase) { auto testResult = new TestResult(testCase.name.dup); testResult.begin = Clock.currTime; testResult.end = testResult.begin; testResult.labels = testCase.labels.dup; testResult.fileName = testCase.location.fileName; testResult.line = testCase.location.line; return testResult; } /// A suite result struct SuiteResult { /** The suite name. It can contain `.` which is treated as a separator for nested suites */ string name; /// when the suite started SysTime begin; /// when the suite ended SysTime end; /// the tests executed for the current suite TestResult[] tests; /// The list of attached files Attachment[] attachments; /// @disable this(); /// this(string name) { this.name = name; begin = SysTime.fromUnixTime(0); end = SysTime.fromUnixTime(0); } /// this(string name, SysTime begin, SysTime end) { this.name = name; this.begin = begin; this.end = end; } /// this(string name, SysTime begin, SysTime end, TestResult[] tests) { this.name = name; this.begin = begin; this.end = end; this.tests = tests; } } /// A step result class StepResult { /// The step name string name; /// When the step started SysTime begin; /// When the step ended SysTime end; /// The list of the child steps StepResult[] steps; /// The list of attached files Attachment[] attachments; this() { begin = SysTime.min; end = SysTime.min; } } /// A test result class TestResult : StepResult { /// The states that a test can have. enum Status { /// created, /// failure, /// skip, /// started, /// success, /// pending, /// unknown } /// The file that contains this test string fileName; /// The line where this test starts size_t line; /// Represents the test status Status status = Status.created; /** A list of labels that will be added to the final report */ Label[] labels; /** The reason why a test has failed. This value must be set only if the tests has the `failure` state */ Throwable throwable; /// Convenience constructor that sets the test name this(string name) { this.name = name; super(); } } version (unittest) { import std.stdio; import std.conv; import std.algorithm; import core.thread; 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); } /// Attribute that marks the test as flaky. Different reporters will interpret this information /// in different ways. struct Flaky { /// Returns the labels that set the test a flaky static Label[] labels() { return [Label("status_details", "flaky")]; } } /// Attribute that links an issue to a test. Some test reporters can display links, so the value can be also /// a link. struct Issue { private string name; /// Returns the labels that set the issue label Label[] labels() { return [ Label("issue", name) ]; } } /// Attribute that sets the feaure label struct Feature { private string name; /// Returns the labels that set the feature label Label[] labels() { return [ Label("feature", name) ]; } } /// Attribute that sets the story label struct Story { private string name; /// Returns the labels that set the feature label Label[] labels() { return [ Label("story", name) ]; } } /// Attach the readme file unittest { Attachment.fromFile("readme file", "README.md", "text/plain"); } /// An exception that should be thrown by the pending test cases class PendingTestException : Exception { /// this(string file = __FILE__, size_t line = __LINE__, Throwable next = null) { super("You cannot run pending tests", file, line, next); } }