Browse Source

Replaced Tinkerbell library by UTest for testing

main
Cédric Belin 1 month ago
parent
commit
277c812b7f
13 changed files with 97 additions and 144 deletions
  1. +2
    -3
      share/test.hxml
  2. +0
    -18
      test/AssertionTools.hx
  3. +0
    -30
      test/RunTests.hx
  4. +35
    -0
      test/TestAll.hx
  5. +4
    -8
      test/lcov/BranchCoverageTest.hx
  6. +4
    -8
      test/lcov/BranchDataTest.hx
  7. +4
    -8
      test/lcov/FunctionCoverageTest.hx
  8. +6
    -11
      test/lcov/FunctionDataTest.hx
  9. +4
    -8
      test/lcov/LineCoverageTest.hx
  10. +4
    -8
      test/lcov/LineDataTest.hx
  11. +4
    -8
      test/lcov/RecordTest.hx
  12. +28
    -34
      test/lcov/ReportTest.hx
  13. +2
    -0
      test/lcov/import.hx

+ 2
- 3
share/test.hxml View File

@@ -1,7 +1,6 @@
--class-path src
--class-path test
--library mcover
--library tink_testrunner
--library tink_unittest
--library utest
--macro mcover.MCover.coverage([''], ['src'])
--main RunTests
--main TestAll

+ 0
- 18
test/AssertionTools.hx View File

@@ -1,18 +0,0 @@
import haxe.Exception;
import tink.unit.AssertionBuffer;

/** Provides helper methods for assertions. **/
class AssertionTools {

/** Expects the function `func` to throw an error. **/
public static function throws(asserts: AssertionBuffer, func: () -> Void, ?exceptionClass: Class<Exception>): Void {
try {
func();
asserts.fail("Exception not thrown");
}

catch (e) {
asserts.assert(Std.isOfType(e, exceptionClass != null ? exceptionClass : Exception));
}
}
}

+ 0
- 30
test/RunTests.hx View File

@@ -1,30 +0,0 @@
import lcov.*;
import mcover.coverage.MCoverage;
import mcover.coverage.client.LcovPrintClient;
import tink.testrunner.Runner;
import tink.unit.TestBatch;

/** Runs the test suites. **/
class RunTests {

/** Application entry point. **/
static function main(): Void {
final tests = TestBatch.make([
new BranchCoverageTest(),
new BranchDataTest(),
new FunctionCoverageTest(),
new FunctionDataTest(),
new LineCoverageTest(),
new LineDataTest(),
new RecordTest(),
new ReportTest()
]);

Runner.run(tests).handle(result -> {
final logger = MCoverage.getLogger();
logger.addClient(new LcovPrintClient("lcov", "var/lcov.info"));
logger.report();
Runner.exit(result);
});
}
}

+ 35
- 0
test/TestAll.hx View File

@@ -0,0 +1,35 @@
import lcov.*;
import mcover.coverage.MCoverage;
import mcover.coverage.client.LcovPrintClient;
import utest.Runner;
import utest.ui.Report;

/** Runs the test suite. **/
class TestAll {

/** The test cases. **/
static final tests = [
new BranchCoverageTest(),
new BranchDataTest(),
new FunctionCoverageTest(),
new FunctionDataTest(),
new LineCoverageTest(),
new LineDataTest(),
new RecordTest(),
new ReportTest()
];

/** Application entry point. **/
static function main(): Void {
final runner = new Runner();
runner.onComplete.add(_ -> {
final logger = MCoverage.getLogger();
logger.addClient(new LcovPrintClient("lcov", "var/lcov.info"));
logger.report();
});

Report.create(runner);
for (test in tests) runner.addCase(test);
runner.run();
}
}

+ 4
- 8
test/lcov/BranchCoverageTest.hx View File

@@ -1,17 +1,13 @@
package lcov;

/** Tests the features of the `BranchCoverage` class. **/
@:asserts class BranchCoverageTest {

/** Creates a new test suite. **/
public function new() {}
class BranchCoverageTest extends Test {

/** Tests the `toString()` method. **/
public function testToString() {
function testToString(): Void {
// It should return a format like "BRF:<found>\nBRH:<hit>".
final data = new BranchData(127, 3, 2, 1);
asserts.assert(new BranchCoverage().toString() == "BRF:0\nBRH:0");
asserts.assert(new BranchCoverage(23, 11, [data]).toString() == '$data\nBRF:23\nBRH:11');
return asserts.done();
Assert.equals("BRF:0\nBRH:0", new BranchCoverage().toString());
Assert.equals('$data\nBRF:23\nBRH:11', new BranchCoverage(23, 11, [data]).toString());
}
}

+ 4
- 8
test/lcov/BranchDataTest.hx View File

@@ -1,16 +1,12 @@
package lcov;

/** Tests the features of the `BranchData` class. **/
@:asserts class BranchDataTest {

/** Creates a new test suite. **/
public function new() {}
class BranchDataTest extends Test {

/** Tests the `toString()` method. **/
public function testToString() {
function testToString(): Void {
// It should return a format like "BRDA:<lineNumber>,<blockNumber>,<branchNumber>,<taken>".
asserts.assert(new BranchData(0, 0, 0).toString() == "BRDA:0,0,0,-");
asserts.assert(new BranchData(127, 3, 2, 1).toString() == "BRDA:127,3,2,1");
return asserts.done();
Assert.equals("BRDA:0,0,0,-", new BranchData(0, 0, 0).toString());
Assert.equals("BRDA:127,3,2,1", new BranchData(127, 3, 2, 1).toString());
}
}

+ 4
- 8
test/lcov/FunctionCoverageTest.hx View File

@@ -1,17 +1,13 @@
package lcov;

/** Tests the features of the `FunctionCoverage` class. **/
@:asserts class FunctionCoverageTest {

/** Creates a new test suite. **/
public function new() {}
class FunctionCoverageTest extends Test {

/** Tests the `toString()` method. **/
public function testToString() {
function testToString(): Void {
// It should return a format like "FNF:<found>\nFNH:<hit>".
final coverage = new FunctionCoverage(23, 11, [new FunctionData("main", 127, 3)]);
asserts.assert(new FunctionCoverage().toString() == "FNF:0\nFNH:0");
asserts.assert(coverage.toString() == "FN:127,main\nFNDA:3,main\nFNF:23\nFNH:11");
return asserts.done();
Assert.equals("FNF:0\nFNH:0", new FunctionCoverage().toString());
Assert.equals("FN:127,main\nFNDA:3,main\nFNF:23\nFNH:11", coverage.toString());
}
}

+ 6
- 11
test/lcov/FunctionDataTest.hx View File

@@ -1,21 +1,16 @@
package lcov;

/** Tests the features of the `FunctionData` class. **/
@:asserts class FunctionDataTest {

/** Creates a new test suite. **/
public function new() {}
class FunctionDataTest extends Test {

/** Tests the `toString()` method. **/
public function testToString() {
function testToString(): Void {
// It should return a format like "FN:<lineNumber>,<functionName>" when used as definition.
asserts.assert(new FunctionData("", 0).toString(true) == "FN:0,");
asserts.assert(new FunctionData("main", 127, 3).toString(true) == "FN:127,main");
Assert.equals("FN:0,", new FunctionData("", 0).toString(true));
Assert.equals("FN:127,main", new FunctionData("main", 127, 3).toString(true));

// It should return a format like "FNDA:<executionCount>,<functionName>" when used as data.
asserts.assert(new FunctionData("", 0).toString(false) == "FNDA:0,");
asserts.assert(new FunctionData("main", 127, 3).toString(false) == "FNDA:3,main");

return asserts.done();
Assert.equals("FNDA:0,", new FunctionData("", 0).toString(false));
Assert.equals("FNDA:3,main", new FunctionData("main", 127, 3).toString(false));
}
}

+ 4
- 8
test/lcov/LineCoverageTest.hx View File

@@ -1,17 +1,13 @@
package lcov;

/** Tests the features of the `LineCoverage` class. **/
@:asserts class LineCoverageTest {

/** Creates a new test suite. **/
public function new() {}
class LineCoverageTest extends Test {

/** Tests the `toString()` method. **/
public function testToString() {
function testToString(): Void {
// It should return a format like "LF:<found>\nLH:<hit>".
final data = new LineData(127, 3);
asserts.assert(new LineCoverage().toString() == "LF:0\nLH:0");
asserts.assert(new LineCoverage(23, 11, [data]).toString() == '$data\nLF:23\nLH:11');
return asserts.done();
Assert.equals("LF:0\nLH:0", new LineCoverage().toString());
Assert.equals('$data\nLF:23\nLH:11', new LineCoverage(23, 11, [data]).toString());
}
}

+ 4
- 8
test/lcov/LineDataTest.hx View File

@@ -1,16 +1,12 @@
package lcov;

/** Tests the features of the `LineData` class. **/
@:asserts class LineDataTest {

/** Creates a new test suite. **/
public function new() {}
class LineDataTest extends Test {

/** Tests the `toString()` method. **/
public function testToString() {
function testToString(): Void {
// It should return a format like "DA:<lineNumber>,<executionCount>[,<checksum>]".
asserts.assert(new LineData(0).toString() == "DA:0,0");
asserts.assert(new LineData(127, 3, "ed076287532e86365e841e92bfc50d8c").toString() == "DA:127,3,ed076287532e86365e841e92bfc50d8c");
return asserts.done();
Assert.equals("DA:0,0", new LineData(0).toString());
Assert.equals("DA:127,3,ed076287532e86365e841e92bfc50d8c", new LineData(127, 3, "ed076287532e86365e841e92bfc50d8c").toString());
}
}

+ 4
- 8
test/lcov/RecordTest.hx View File

@@ -1,13 +1,10 @@
package lcov;

/** Tests the features of the `Record` class. **/
@:asserts class RecordTest {

/** Creates a new test suite. **/
public function new() {}
class RecordTest extends Test {

/** Tests the `toString()` method. **/
public function testToString() {
function testToString(): Void {
// It should return a format like "SF:<sourceFile>\nend_of_record".
final record = new Record("/home/cedx/lcov.hx", {
branches: new BranchCoverage(),
@@ -15,8 +12,7 @@ package lcov;
lines: new LineCoverage()
});

asserts.assert(new Record("").toString() == "SF:\nend_of_record");
asserts.assert(record.toString() == 'SF:/home/cedx/lcov.hx\n${record.functions}\n${record.branches}\n${record.lines}\nend_of_record');
return asserts.done();
Assert.equals("SF:\nend_of_record", new Record("").toString());
Assert.equals('SF:/home/cedx/lcov.hx\n${record.functions}\n${record.branches}\n${record.lines}\nend_of_record', record.toString());
}
}

+ 28
- 34
test/lcov/ReportTest.hx View File

@@ -1,69 +1,63 @@
package lcov;

import sys.io.File;
using AssertionTools;

/** Tests the features of the `Report` class. **/
@:asserts class ReportTest {

/** Creates a new test suite. **/
public function new() {}
class ReportTest extends Test {

/** Tests the `fromCoverage()` method. **/
public function testFromCoverage() {
function testFromCoverage() {
final report = Report.fromCoverage(File.getContent("test/fixtures/lcov.info"));

// It should have a test name.
asserts.assert(report.testName == "Example");
Assert.equals("Example", report.testName);

// It should contain three records.
asserts.assert(report.records.length == 3);
asserts.assert(Std.isOfType(report.records[0], Record));
asserts.assert(report.records[0].sourceFile == "/home/cedx/lcov.hx/fixture.hx");
asserts.assert(report.records[1].sourceFile == "/home/cedx/lcov.hx/func1.hx");
asserts.assert(report.records[2].sourceFile == "/home/cedx/lcov.hx/func2.hx");
Assert.equals(3, report.records.length);
Assert.is(report.records[0], Record);
Assert.equals("/home/cedx/lcov.hx/fixture.hx", report.records[0].sourceFile);
Assert.equals("/home/cedx/lcov.hx/func1.hx", report.records[1].sourceFile);
Assert.equals("/home/cedx/lcov.hx/func2.hx", report.records[2].sourceFile);

// It should have detailed branch coverage.
final branches = report.records[1].branches;
asserts.assert(branches.found == 4);
asserts.assert(branches.hit == 4);
Assert.equals(4, branches.found);
Assert.equals(4, branches.hit);

asserts.assert(branches.data.length == 4);
asserts.assert(Std.isOfType(branches.data[0], BranchData));
asserts.assert(branches.data[0].lineNumber == 8);
Assert.equals(4, branches.data.length);
Assert.is(branches.data[0], BranchData);
Assert.equals(8, branches.data[0].lineNumber);

// It should have detailed function coverage.
final functions = report.records[1].functions;
asserts.assert(functions.found == 1);
asserts.assert(functions.hit == 1);
Assert.equals(1, functions.found);
Assert.equals(1, functions.hit);

asserts.assert(functions.data.length == 1);
asserts.assert(Std.isOfType(functions.data[0], FunctionData));
asserts.assert(functions.data[0].functionName == "func1");
Assert.equals(1, functions.data.length);
Assert.is(functions.data[0], FunctionData);
Assert.equals("func1", functions.data[0].functionName);

// It should have detailed line coverage.
final lines = report.records[1].lines;
asserts.assert(lines.found == 9);
asserts.assert(lines.hit == 9);
Assert.equals(9, lines.found);
Assert.equals(9, lines.hit);

asserts.assert(lines.data.length == 9);
asserts.assert(Std.isOfType(lines.data[0], LineData));
asserts.assert(lines.data[0].checksum == "5kX7OTfHFcjnS98fjeVqNA");
Assert.equals(9, lines.data.length);
Assert.is(lines.data[0], LineData);
Assert.equals("5kX7OTfHFcjnS98fjeVqNA", lines.data[0].checksum);

// It should throw an error if the input is invalid.
asserts.throws(() -> Report.fromCoverage("ZZ"), LcovException);
Assert.raises(() -> Report.fromCoverage("ZZ"), LcovException);

// It should throw an error if the report is empty.
asserts.throws(() -> Report.fromCoverage("TN:Example"), LcovException);
return asserts.done();
Assert.raises(() -> Report.fromCoverage("TN:Example"), LcovException);
}

/** Tests the `toString()` method. **/
public function testToString() {
function testToString(): Void {
// It should return a format like "TN:<testName>".
final record = new Record("");
asserts.assert(new Report().toString().length == 0);
asserts.assert(new Report("LcovTest", [record]).toString() == 'TN:LcovTest\n$record');
return asserts.done();
Assert.equals(0, new Report().toString().length);
Assert.equals('TN:LcovTest\n$record', new Report("LcovTest", [record]).toString());
}
}

+ 2
- 0
test/lcov/import.hx View File

@@ -0,0 +1,2 @@
import utest.Assert;
import utest.Test;

Loading…
Cancel
Save