Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Count timeouts as escapes #1134

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/Mutant/Calculator.php
Expand Up @@ -61,18 +61,25 @@ final class Calculator
*/
private $coveredMutationScoreIndicator;

/**
* @var bool
*/
private $treatTimeoutsAsEscapes = false;

public function __construct(
int $killedCount,
int $errorCount,
int $timedOutCount,
int $notTestedCount,
int $totalCount
int $totalCount,
bool $treatTimeoutsAsEscapes = false
) {
$this->killedCount = $killedCount;
$this->errorCount = $errorCount;
$this->timedOutCount = $timedOutCount;
$this->notTestedCount = $notTestedCount;
$this->totalCount = $totalCount;
$this->treatTimeoutsAsEscapes = $treatTimeoutsAsEscapes;
}

public static function fromMetrics(MetricsCalculator $calculator): self
Expand All @@ -82,7 +89,8 @@ public static function fromMetrics(MetricsCalculator $calculator): self
$calculator->getErrorCount(),
$calculator->getTimedOutCount(),
$calculator->getNotTestedCount(),
$calculator->getTotalMutantsCount()
$calculator->getTotalMutantsCount(),
$calculator->getTreatTimeoutsAsEscapes()
);
}

Expand All @@ -96,7 +104,11 @@ public function getMutationScoreIndicator(): float
}

$score = 0.;
$coveredTotal = $this->killedCount + $this->timedOutCount + $this->errorCount;
$coveredTotal = $this->killedCount + $this->errorCount;

if (!$this->treatTimeoutsAsEscapes) {
$coveredTotal += $this->timedOutCount;
}
$totalCount = $this->totalCount;

if ($totalCount !== 0) {
Expand Down Expand Up @@ -137,7 +149,11 @@ public function getCoveredCodeMutationScoreIndicator(): float

$score = 0.;
$testedTotal = $this->totalCount - $this->notTestedCount;
$coveredTotal = $this->killedCount + $this->timedOutCount + $this->errorCount;
$coveredTotal = $this->killedCount + $this->errorCount;

if (!$this->treatTimeoutsAsEscapes) {
$coveredTotal += $this->timedOutCount;
}
sanmai marked this conversation as resolved.
Show resolved Hide resolved

if ($testedTotal !== 0) {
$score = 100 * $coveredTotal / $testedTotal;
Expand Down
16 changes: 15 additions & 1 deletion src/Mutant/MetricsCalculator.php
Expand Up @@ -85,14 +85,20 @@ class MetricsCalculator
*/
private $calculator;

public function __construct()
/**
* @var bool
*/
private $treatTimeoutsAsEscapes = false;

public function __construct(bool $treatTimeoutsAsEscapes = false)
{
$this->killedExecutionResults = new SortableMutantExecutionResults();
$this->errorExecutionResults = new SortableMutantExecutionResults();
$this->escapedExecutionResults = new SortableMutantExecutionResults();
$this->timedOutExecutionResults = new SortableMutantExecutionResults();
$this->notCoveredExecutionResults = new SortableMutantExecutionResults();
$this->allExecutionResults = new SortableMutantExecutionResults();
$this->treatTimeoutsAsEscapes = $treatTimeoutsAsEscapes;
}

public function collect(MutantExecutionResult ...$executionResults): void
Expand Down Expand Up @@ -243,6 +249,14 @@ public function getCoveredCodeMutationScoreIndicator(): float
return $this->getCalculator()->getCoveredCodeMutationScoreIndicator();
}

/**
* Are mutation timeouts treated as escapes?
*/
public function getTreatTimeoutsAsEscapes(): bool
{
return $this->treatTimeoutsAsEscapes;
}

private function getCalculator(): Calculator
{
return $this->calculator ?? Calculator::fromMetrics($this);
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/Logger/CreateMetricsCalculator.php
Expand Up @@ -46,9 +46,9 @@

trait CreateMetricsCalculator
{
private function createCompleteMetricsCalculator(): MetricsCalculator
private function createCompleteMetricsCalculator(bool $timeoutAsEscape = false): MetricsCalculator
{
$calculator = new MetricsCalculator();
$calculator = new MetricsCalculator($timeoutAsEscape);

$calculator->collect(
$this->createMutantExecutionResult(
Expand Down
118 changes: 118 additions & 0 deletions tests/phpunit/Mutant/CalculatorTest.php
Expand Up @@ -83,6 +83,44 @@ public function test_it_can_calculate_the_scores(
$this->assertSame($expectedCoveredMsi, $calculator->getCoveredCodeMutationScoreIndicator());
}

/**
* @dataProvider metricsWithTimeoutProvider
*/
public function test_it_can_calculate_the_scores_while_counting_timeouts_as_escapes(
int $killedCount,
int $errorCount,
int $escapedCount,
int $timedOutCount,
int $notTestedCount,
float $expectedMsi,
float $expectedCoverageRate,
float $expectedCoveredMsi
): void {
$calculator = new Calculator(
$killedCount,
$errorCount,
$timedOutCount,
$notTestedCount,
array_sum([
$killedCount,
$errorCount,
$escapedCount,
$timedOutCount,
$notTestedCount,
]),
true
);

$this->assertSame($expectedMsi, $calculator->getMutationScoreIndicator());
$this->assertSame($expectedCoverageRate, $calculator->getCoverageRate());
$this->assertSame($expectedCoveredMsi, $calculator->getCoveredCodeMutationScoreIndicator());

// The calls are idempotent
$this->assertSame($expectedMsi, $calculator->getMutationScoreIndicator());
$this->assertSame($expectedCoverageRate, $calculator->getCoverageRate());
$this->assertSame($expectedCoveredMsi, $calculator->getCoveredCodeMutationScoreIndicator());
}

/**
* @dataProvider metricsCalculatorProvider
*/
Expand All @@ -99,6 +137,22 @@ public function test_it_can_be_created_from_a_metrics_calculator(
$this->assertSame($expectedCoveredMsi, $calculator->getCoveredCodeMutationScoreIndicator());
}

/**
* @dataProvider metricsCalculatorWithTimeoutProvider
*/
public function test_it_can_be_created_from_a_metrics_calculator_while_counting_timeouts_as_escapes(
MetricsCalculator $metricsCalculator,
float $expectedMsi,
float $expectedCoverageRate,
float $expectedCoveredMsi
): void {
$calculator = Calculator::fromMetrics($metricsCalculator);

$this->assertSame($expectedMsi, $calculator->getMutationScoreIndicator());
$this->assertSame($expectedCoverageRate, $calculator->getCoverageRate());
$this->assertSame($expectedCoveredMsi, $calculator->getCoveredCodeMutationScoreIndicator());
}

public function metricsProvider(): Generator
{
yield 'empty' => [
Expand Down Expand Up @@ -146,6 +200,53 @@ public function metricsProvider(): Generator
];
}

public function metricsWithTimeoutProvider(): Generator
{
yield 'empty' => [
0,
0,
0,
0,
0,
0.,
0.,
0.,
];

yield 'int scores' => [
1,
0,
9,
0,
0,
10.,
100.0,
10.0,
];

yield 'nominal' => [
7,
2,
2,
2,
1,
64.285714285714292,
92.85714285714286,
69.230769230769226,
];

yield 'nominal no non-tested' => [
7,
2,
2,
2,
0,
69.230769230769226,
100,
69.230769230769226,
];
}

public function metricsCalculatorProvider(): Generator
{
yield 'empty' => [
Expand All @@ -162,4 +263,21 @@ public function metricsCalculatorProvider(): Generator
75.0,
];
}

public function metricsCalculatorWithTimeoutProvider(): Generator
{
yield 'empty' => [
new MetricsCalculator(true),
0.,
0.,
0.,
];

yield 'nominal' => [
$this->createCompleteMetricsCalculator(true),
40.0,
80.0,
50.0,
];
}
}