Skip to content

Commit

Permalink
🔧 Display Percentage fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
WiredNerd committed Jan 6, 2024
1 parent b7972fd commit ef85b1e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/poodle/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .mutate import create_mutants_for_all_mutators, initialize_mutators
from .report import generate_reporters
from .run import clean_run_each_source_folder, get_runner, run_mutant_trails
from .util import calc_timeout, create_temp_zips, create_unified_diff, pprint_str
from .util import calc_timeout, create_temp_zips, create_unified_diff, display_percent, pprint_str

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -50,7 +50,8 @@ def main_process(config: PoodleConfig) -> None:
delete_folder(config.work_folder)

if config.fail_under and results.summary.success_rate < config.fail_under / 100:
msg = f"Mutation score {results.summary.coverage_display} is below goal of {config.fail_under:.2f}%"
display_fail_under = display_percent(config.fail_under / 100)
msg = f"Mutation score {results.summary.coverage_display} is below goal of {display_fail_under}"
raise PoodleTestingFailedError(msg)


Expand Down
4 changes: 3 additions & 1 deletion src/poodle/data_types/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any

from poodle import util

if TYPE_CHECKING:
from typing_extensions import Self

Expand Down Expand Up @@ -165,7 +167,7 @@ def success_rate(self) -> float:
@property
def coverage_display(self) -> str:
"""Return a formatted string for the coverage percentage."""
return f"{self.success_rate * 100:.2f}%"
return util.display_percent(self.success_rate)

def __iadd__(self, result: MutantTrialResult) -> Self:
"""Update Testing Summary with data from MutantTrialResult."""
Expand Down
5 changes: 5 additions & 0 deletions src/poodle/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,8 @@ def to_json(obj: PoodleSerialize, indent: int | str | None = None) -> str:
def from_json(data: str, datatype: type[PoodleSerialize]) -> PoodleSerialize:
"""Convert json string to dataclass."""
return datatype(**json.loads(data, object_hook=datatype.from_dict))


def display_percent(value: float) -> str:
"""Convert float to string with percent sign."""
return f"{value * 1000 // 1 / 10:.3g}%"
4 changes: 2 additions & 2 deletions tests/data_types/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def test_success_rate_zero(self):

def test_coverage_display(self):
summary = TestingSummary(trials=9, found=6)
assert summary.coverage_display == "66.67%"
assert summary.coverage_display == "66.6%"

def test_iadd(self):
summary = TestingSummary(trials=10)
Expand Down Expand Up @@ -438,7 +438,7 @@ def summary_dict(self):
"timeout": 6,
"errors": 5,
"success_rate": 0.8,
"coverage_display": "80.00%",
"coverage_display": "80%",
}

def test_serialize(self):
Expand Down
10 changes: 2 additions & 8 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,7 @@ def test_main_process_report(
@pytest.mark.usefixtures("_setup_main_process")
def test_main_process_fail_under_pass(
self,
poodle_work_class: mock.MagicMock,
run_mutant_trails: mock.MagicMock,
generate_reporters: mock.MagicMock,
create_unified_diff: mock.MagicMock,
):
config = PoodleConfigStub(fail_under=80.0)

Expand All @@ -269,18 +266,15 @@ def test_main_process_fail_under_pass(
@pytest.mark.usefixtures("_setup_main_process")
def test_main_process_fail_under_fail(
self,
poodle_work_class: mock.MagicMock,
run_mutant_trails: mock.MagicMock,
generate_reporters: mock.MagicMock,
create_unified_diff: mock.MagicMock,
):
config = PoodleConfigStub(fail_under=80.0)

results = run_mutant_trails.return_value
results.summary.success_rate = 0.7999
results.summary.coverage_display = "79.99%"
results.summary.coverage_display = "79.9%"

with pytest.raises(PoodleTestingFailedError, match=r"^Mutation score 79.99% is below goal of 80.00%$"):
with pytest.raises(PoodleTestingFailedError, match=r"^Mutation score 79.9% is below goal of 80%$"):
core.main_process(config)


Expand Down
18 changes: 18 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,21 @@ def test_create_unified_diff_no_file(self):
source_file=None,
)
assert util.create_unified_diff(mutant) is None


class TestDisplayPercent:
@pytest.mark.parametrize(
("value", "expected"),
[
(0.0014, "0.1%"),
(0.0016, "0.1%"),
(0.1239, "12.3%"),
(0.12, "12%"),
(0.1, "10%"),
(0.99, "99%"),
(0.999, "99.9%"),
(0.9999, "99.9%"),
],
)
def test_display_percent(self, value, expected):
assert util.display_percent(value) == expected

0 comments on commit ef85b1e

Please sign in to comment.