Skip to content

Commit

Permalink
added support for attributable coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe committed Jan 7, 2025
1 parent bc47985 commit 2e79507
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .haxerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "4.3.4",
"version": "4.3.6",
"resolveLibs": "scoped"
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## dev branch / next version (1.x.x)

## version 1.3.0 (2025-01-07)

- added support for attributable coverage

## version 1.2.0 (2024-06-02)

- added `-D instrument-quiet` conditional to skip printing dots during instrumentation
Expand Down
1 change: 1 addition & 0 deletions display.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
-cp tests

-lib utest
-lib safety

# --macro instrument.Instrumentation.init(["demo"], ["haxe"])
-D profiler-console-detail-reporter
Expand Down
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"profiler"
],
"description": "a profiling and coverage library for Haxe",
"version": "1.2.0",
"releasenote": "added support for null coalesence and safe navigation operators, reworked branch coverage, also bugfixes - see CHANGELOG",
"version": "1.3.0",
"releasenote": "added support for attributable coverage - see CHANGELOG",
"contributors": [
"AlexHaxe"
],
Expand Down
19 changes: 10 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "haxe-instrument",
"version": "1.2.0",
"version": "1.3.0",
"description": "a profiling and coverage library for Haxe",
"author": {
"name": "Alexander Blum",
"email": "[email protected]"
},
"devDependencies": {
"lix": "^15.12.0"
"lix": "^15.12.4"
},
"license": "MIT"
}
40 changes: 37 additions & 3 deletions src/instrument/coverage/Coverage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,50 @@ import instrument.coverage.reporter.ICoverageReporter;
class Coverage {
public static var RESOURCE_NAME:String = "coverageTypeInfo";

static var context:Null<CoverageContext>;

static function loadContext():CoverageContext {
if (context == null) {
context = CoverageContext.contextFromJson();
}
return context.sure();
}

/**
* reports coverage data using reporters provided by caller
*
* @param reporters each reporter can access all recorded coverage data
*/
public static function endCustomCoverage(reporters:Array<ICoverageReporter>) {
var context:CoverageContext = CoverageContext.contextFromJson();
context.calcStatistic();
final ctxt = loadContext();
ctxt.calcStatistic(CoverageContext.covered);
for (report in reporters) {
report.generateReport(ctxt);
}
}

/**
* resets attributable coverage data.
*
* use before running a new testcase to zero out coverage counters
*
*/
public static function resetAttributableCoverage() {
CoverageContext.coveredAttributable.clear();
}

/**
* reports attributable coverage data using reporters provided by caller
*
* use in conjunction with `resetAttributableCoverage` to attribute coverage per individual testcase
*
* @param reporters each reporter can access all recorded coverage data
*/
public static function reportAttributableCoverage(reporters:Array<ICoverageReporter>) {
final ctxt = loadContext();
ctxt.calcStatistic(CoverageContext.coveredAttributable);
for (report in reporters) {
report.generateReport(context);
report.generateReport(ctxt);
}
}

Expand Down
29 changes: 20 additions & 9 deletions src/instrument/coverage/CoverageContext.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CoverageContext {
static var id:Int = 0;

public static var covered:Null<Map<Int, Int>> = null;
public static var coveredAttributable:Null<Map<Int, Int>> = null;

public var types:Array<TypeInfo>;
public var files:Array<FileInfo>;
Expand Down Expand Up @@ -114,6 +115,9 @@ class CoverageContext {
if (covered == null) {
covered = new Map<Int, Int>();
}
if (coveredAttributable == null) {
coveredAttributable = new Map<Int, Int>();
}
lock.sure().acquire();
#if debug_log_expression
Sys.println(findLogId(logId));
Expand All @@ -123,6 +127,11 @@ class CoverageContext {
} else {
covered.sure().set(logId, 1);
}
if (coveredAttributable.sure().exists(logId)) {
coveredAttributable.sure().set(logId, coveredAttributable.sure().get(logId).sure() + 1);
} else {
coveredAttributable.sure().set(logId, 1);
}
lock.sure().release();
}

Expand Down Expand Up @@ -159,15 +168,17 @@ class CoverageContext {
}
#end

public function calcStatistic() {
if (covered == null) {
return;
}
function getCoverage(id:Int):Int {
if (CoverageContext.covered.sure().exists(id)) {
return CoverageContext.covered.sure().get(id).sure();
public function calcStatistic(coveredData:Null<Map<Int, Int>>) {
var coverageCallback = (id:Int) -> 0;

if (coveredData != null) {
final coveredIds:Map<Int, Int> = coveredData.sure();
coverageCallback = function getCoverage(id:Int):Int {
if (coveredIds.exists(id)) {
return coveredIds.get(id).sure();
}
return 0;
}
return 0;
}

filesCovered = 0;
Expand All @@ -183,7 +194,7 @@ class CoverageContext {
linesCovered = 0;

for (type in types) {
type.calcStatistic(getCoverage);
type.calcStatistic(coverageCallback);
if (type.isCovered()) {
typesCovered++;
}
Expand Down
2 changes: 1 addition & 1 deletion srcDemo/demo/NullSafety.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class NullSafety {
@:nullSafety(StrictThreaded)
static function func2(o:{field:Null<String>}) {
if (o.field != null) {
trace(o.field.length);
trace(o.field?.length);
}
}
}

0 comments on commit 2e79507

Please sign in to comment.