trial.interfaces 169/175(96%) 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
54228
55226
560
570
582
590
602
612
622
632
642
650
662
670
680
690
700
711
722
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
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
15234
1530
1540
1550
1560
1576
1580
1590
1600
1610
1621
1630
1641
1650
1660
1670
1680
1690
1702
1710
1720
1730
1740
1751
1760
1772
1782
1790
1800
1810
1820
1831
1840
1852
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2051
2061
2070
2081
2090
2101
2111
2120
2130
2141
2150
2160
2170
2185
2195
2205
2215
2220
2235
2240
2250
2260
2270
2280
2290
2302
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
24570
2460
2470
2480
2490
2500
2510
2522
2530
2540
2550
256790
2570
2580
2590
2600
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
28611
28711
28811
28911
29011
29111
2920
29333
2940
2950
2960
2970
2980
29935
30035
30135
3020
3030
3040
305317
306317
3070
3080
3090
310495
311495
312495
313495
314495
3150
3160
3170
31869
3190
32069
32169
32277
32369
3240
32569
3260
3270
3280
3290
3300
3311
3320
3331
3341
3350
3362
3370
3380
3390
3400
3410
3420
343235
3440
345235
346235
347235
348235
349235
3500
351235
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
3780
3790
38056
38156
38256
38356
3840
3850
3860
3870
3880
3890
3900
3910
3920
39353
39453
39553
39653
3970
3980
3990
4001
4011
4021
4031
4041
4050
4060
4070
4081
4091
4101
4111
4121
4131
4140
4150
4160
4170
41845
41945
42045
42145
422270
42346
4240
42545
4260
4270
4280
4290
4301
4310
4320
4330
4340
4350
4362
4370
4380
4390
4400
4410
4420
4430
4440
4450
4460
4470
4480
4490
4500
4510
4520
4530
4540
4550
4560
4570
4580
459352
460352
461352
4620
4630
4640
465232
4660
467232
468232
469232
470236
471235
4720
473232
4740
4750
4760
4770
4785
4790
4800
4810
4820
4830
4841
4851
4861
4871
4881
4891
4900
4912
4920
4930
4940
4950
4960
4970
4980
4990
5000
5010
5020
5030
5040
5050
5060
5070
5080
5090
5100
5110
5120
5130
5140
5150
5160
5170
5180
5190
5200
5210
5220
5230
5240
5250
5260
5270
5280
5290
5300
5310
5320
5330
5340
5350
5360
5370
5380
539311
5400
541311
542311
5430
5440
5450
5460
547227
5480
549227
550227
551227
552230
553227
5540
555227
5560
5570
5580
5590
5600
5610
5620
5630
5640
5656
5660
5670
5680
5690
5700
5710
5720
5730
5740
5750
5760
57712
5780
5790
5800
5810
5820
5830
5840
5850
5860
5870
5880
5890
5900
5910
5920
5930
5940
5950
5960
5970
5980
5990
6000
6010
6020
6031
6040
6052
6060
6070
6080
6090
6100
6110
6121
6131
6140
6150
6160
6170
6180
6190
6200
6210
6220
6230
6240
6250
6260
6270
6280
6290
6300
6310
6320
6330
6340
6350
6360
6370
6380
6390
6400
6410
6421
6430
6440
6450
6460
6471
6480
6490
6500
6510
6520
6534
6540
6550
6560
6570
6580
65932
66080
6610
6620
6630
66432
66589
6660
6670
6680
66932
67085
6710
6720
6730
67432
67595
6760
6770
6780
67932
68019
6810
6820
6830
6840
68532
68667
6870
6880
6890
69032
69169
6920
6930
6940
6950
6960
6979
6980
6990
7000
7010
7021912658
7030
7040
7050
7060
70727
7080
7090
7100
7110
71227
7130
7140
7150
7160
717234
7180
7190
7200
7210
722233
7230
7240
7250
7260
727245
7281583
7290
7300
7310
7320
733245
7341583
7350
7360
7370
7380
73921
74067
7410
7420
7430
7440
74521
74667
7470
7480
7490
7500
751240
752240
753240
7540
7550
7560
7570
75815
75915
7600
7610
7620
7630
76415
7650
7660
/++ 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; import std.file; import std.path; import std.uuid; import std.exception; import std.json; import std.algorithm; version (Have_fluent_asserts) { version = Have_fluent_asserts_core; } version(unittest) { version(Have_fluent_asserts_core) { import fluent.asserts; } } /// 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[]); } /// Convert a Throwable to a json string string toJsonString(Throwable throwable) { if(throwable is null) { return "{}"; } string fields; fields ~= `"file":"` ~ throwable.file.escapeJson ~ `",`; fields ~= `"line":"` ~ throwable.line.to!string.escapeJson ~ `",`; fields ~= `"msg":"` ~ throwable.msg.escapeJson ~ `",`; fields ~= `"info":"` ~ throwable.info.to!string.escapeJson ~ `",`; fields ~= `"raw":"` ~ throwable.toString.escapeJson ~ `"`; return "{" ~ fields ~ "}"; } /// convert a Throwable to json unittest { auto exception = new Exception("some message", __FILE__, 58); exception.toJsonString.should.equal(`{"file":"lifecycle/trial/interfaces.d","line":"58","msg":"some message","info":"null","raw":"object.Exception@lifecycle/trial/interfaces.d(58): some message"}`); } /// 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 ~ `" }`; } /// static Label[] fromJsonArray(string value) { return parseJSON(value).array.map!(a => Label(a["name"].str, a["value"].str)).array; } /// static Label fromJson(string value) { auto parsedValue = parseJSON(value); return Label(parsedValue["name"].str, parsedValue["value"].str); } } /// Label string representation should be in Json format unittest { Label("name", "value").toString.should.equal(`{ "name": "name", "value": "value" }`); } /// create a label from a json object unittest { auto label = Label.fromJson(`{ "name": "name", "value": "value" }`); label.name.should.equal("name"); label.value.should.equal("value"); } /// create a label list from a json array unittest { auto labels = Label.fromJsonArray(`[{ "name": "name1", "value": "value1" }, { "name": "name2", "value": "value2" }]`); labels.should.equal([ Label("name1", "value1"), Label("name2", "value2") ]); } /// 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; /// The attachement destination. All the attached files will be copied in this folder if /// it is not allready inside static string destination; /// Add a file to the current test or step static Attachment fromFile(const string name, const string path, const string mime) { auto fileDestination = buildPath(destination, randomUUID.toString ~ "." ~ path.baseName); copy(path, fileDestination); auto a = const Attachment(name, fileDestination, mime); if(LifeCycleListeners.instance !is null) { LifeCycleListeners.instance.attach(a); } return a; } string toString() inout { string fields; fields ~= `"name":"` ~ name ~ `",`; fields ~= `"file":"` ~ file ~ `",`; fields ~= `"mime":"` ~ mime ~ `"`; return "{" ~ fields ~ "}"; } } /// Convert an attachement to Json string unittest { Attachment("dub", "dub.json", "text/json").toString.should.equal( `{"name":"dub","file":"dub.json","mime":"text/json"}` ); } /// 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) { this.name = name; this.begin = begin; } /// 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; } /// this(string name, SysTime begin, SysTime end, TestResult[] tests, Attachment[] attachments) { this.name = name; this.begin = begin; this.end = end; this.tests = tests; this.attachments = attachments; } /// Convert the struct to a json string string toString() { string fields; fields ~= `"name":"` ~ name.escapeJson ~ `",`; fields ~= `"begin":"` ~ begin.toISOExtString ~ `",`; fields ~= `"end":"` ~ end.toISOExtString ~ `",`; fields ~= `"tests":[` ~ tests.map!(a => a.toString).join(",") ~ `],`; fields ~= `"attachments":[` ~ attachments.map!(a => a.toString).join(",") ~ `]`; return "{" ~ fields ~ "}"; } } unittest { auto result = SuiteResult("suite name", SysTime.fromISOExtString("2000-01-01T00:00:00Z"), SysTime.fromISOExtString("2000-01-01T01:00:00Z"), [ new TestResult("test name") ], [ Attachment() ]); result.toString.should.equal( `{"name":"suite name","begin":"2000-01-01T00:00:00Z","end":"2000-01-01T01:00:00Z","tests":[{"name":"test name","begin":"-29227-04-19T21:11:54.5224192Z","end":"-29227-04-19T21:11:54.5224192Z","steps":[],"attachments":[],"fileName":"","line":"0","status":"created","labels":[],"throwable":{}}],"attachments":[{"name":"","file":"","mime":""}]}` ); } /// 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; } protected string fields() { string result; result ~= `"name":"` ~ name.escapeJson ~ `",`; result ~= `"begin":"` ~ begin.toISOExtString ~ `",`; result ~= `"end":"` ~ end.toISOExtString ~ `",`; result ~= `"steps":[` ~ steps.map!(a => a.toString).join(",") ~ `],`; result ~= `"attachments":[` ~ attachments.map!(a => a.toString).join(",") ~ `]`; return result; } /// Convert the result to a json string override string toString() { return "{" ~ fields ~ "}"; } } /// Convert a step result to a json unittest { auto step = new StepResult(); step.name = "step name"; step.begin = SysTime.fromISOExtString("2000-01-01T00:00:00Z"); step.end = SysTime.fromISOExtString("2000-01-01T01:00:00Z"); step.steps = [ new StepResult() ]; step.attachments = [ Attachment() ]; step.toString.should.equal(`{"name":"step name","begin":"2000-01-01T00:00:00Z","end":"2000-01-01T01:00:00Z","steps":` ~ `[{"name":"","begin":"-29227-04-19T21:11:54.5224192Z","end":"-29227-04-19T21:11:54.5224192Z","steps":[],"attachments":`~ `[]}],"attachments":[{"name":"","file":"","mime":""}]}`); } /// 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(); } /// Convert the result to a json string override string toString() { string result = fields ~ ","; result ~= `"fileName":"` ~ fileName.escapeJson ~ `",`; result ~= `"line":"` ~ line.to!string ~ `",`; result ~= `"status":"` ~ status.to!string ~ `",`; result ~= `"labels":[` ~ labels.map!(a => a.toString).join(",") ~ `],`; result ~= `"throwable":` ~ throwable.toJsonString; return "{" ~ result ~ "}"; } } /// 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 { auto attachment = Attachment.fromFile("readme file", "README.md", "text/plain"); attachment.file.exists.should.equal(true); } /// 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); } } /// The lifecycle listeners collections. You must use this instance in order /// to extend the runner. You can have as many listeners as you want. The only restriction /// is for ITestExecutor, which has no sense to have more than one instance for a run class LifeCycleListeners { /// The global instange. static LifeCycleListeners instance; private { ISuiteLifecycleListener[] suiteListeners; ITestCaseLifecycleListener[] testListeners; IStepLifecycleListener[] stepListeners; ILifecycleListener[] lifecycleListeners; ITestDiscovery[] testDiscoveryListeners; IAttachmentListener[] attachmentListeners; ITestExecutor executor; string currentTest; bool started; } @property { /// Return an unique name for the current running test. If there is no test running it /// will return an empty string string runningTest() const nothrow { return currentTest; } /// True if the tests are being executed bool isRunning() { return started; } } /// TestCase[] getTestCases() { return testDiscoveryListeners.map!(a => a.getTestCases).join; } /// Add a listener to the collection void add(T)(T listener) { static if(!is(CommonType!(ISuiteLifecycleListener, T) == void)) { suiteListeners ~= cast(ISuiteLifecycleListener) listener; suiteListeners = suiteListeners.filter!(a => a !is null).array; } static if(!is(CommonType!(ITestCaseLifecycleListener, T) == void)) { testListeners ~= cast(ITestCaseLifecycleListener) listener; testListeners = testListeners.filter!(a => a !is null).array; } static if(!is(CommonType!(IStepLifecycleListener, T) == void)) { stepListeners ~= cast(IStepLifecycleListener) listener; stepListeners = stepListeners.filter!(a => a !is null).array; } static if(!is(CommonType!(ILifecycleListener, T) == void)) { lifecycleListeners ~= cast(ILifecycleListener) listener; lifecycleListeners = lifecycleListeners.filter!(a => a !is null).array; } static if(!is(CommonType!(ITestExecutor, T) == void)) { if(cast(ITestExecutor) listener !is null) { executor = cast(ITestExecutor) listener; } } static if(!is(CommonType!(ITestDiscovery, T) == void)) { testDiscoveryListeners ~= cast(ITestDiscovery) listener; testDiscoveryListeners = testDiscoveryListeners.filter!(a => a !is null).array; } static if(!is(CommonType!(IAttachmentListener, T) == void)) { attachmentListeners ~= cast(IAttachmentListener) listener; attachmentListeners = attachmentListeners.filter!(a => a !is null).array; } } /// Send the attachment to all listeners void attach(ref const Attachment attachment) { attachmentListeners.each!(a => a.attach(attachment)); } /// Send the update event to all listeners void update() { lifecycleListeners.each!"a.update"; } /// Send the begin run event to all listeners void begin(ulong testCount) { lifecycleListeners.each!(a => a.begin(testCount)); } /// Send the end runer event to all listeners void end(SuiteResult[] result) { lifecycleListeners.each!(a => a.end(result)); } /// Send the begin suite event to all listeners void begin(ref SuiteResult suite) { suiteListeners.each!(a => a.begin(suite)); } /// Send the end suite event to all listeners void end(ref SuiteResult suite) { suiteListeners.each!(a => a.end(suite)); } /// Send the begin test event to all listeners void begin(string suite, ref TestResult test) { currentTest = suite ~ "." ~ test.name; testListeners.each!(a => a.begin(suite, test)); } /// Send the end test event to all listeners void end(string suite, ref TestResult test) { currentTest = ""; testListeners.each!(a => a.end(suite, test)); } /// Send the begin step event to all listeners void begin(string suite, string test, ref StepResult step) { currentTest = suite ~ "." ~ test ~ "." ~ step.name; stepListeners.each!(a => a.begin(suite, test, step)); } /// Send the end step event to all listeners void end(string suite, string test, ref StepResult step) { currentTest = ""; stepListeners.each!(a => a.end(suite, test, step)); } /// Send the execute test to the executor listener SuiteResult[] execute(ref const(TestCase) func) { started = true; scope(exit) started = false; return executor.execute(func); } /// Send the begin execution with the test case list to the executor listener SuiteResult[] beginExecution(ref const(TestCase)[] tests) { enforce(executor !is null, "The test executor was not set."); return executor.beginExecution(tests); } /// Send the end execution the executor listener SuiteResult[] endExecution() { return executor.endExecution(); } }