diff --git a/bowtie/_cli.py b/bowtie/_cli.py index 04c921db8..d7af5f7ee 100644 --- a/bowtie/_cli.py +++ b/bowtie/_cli.py @@ -35,6 +35,7 @@ NoSuchImage, StartupFailed, ) +from bowtie._utils import pluralize from bowtie.exceptions import ( _ProtocolError, # type: ignore[reportPrivateUsage] ) @@ -207,14 +208,13 @@ def _failure_table( summary: _report._Summary, # type: ignore[reportPrivateUsage] results: list[tuple[tuple[str, str], _report.Count]], ): - test = "tests" if summary.total_tests != 1 else "test" table = Table( "Implementation", "Skips", "Errors", "Failures", title="Bowtie", - caption=f"{summary.total_tests} {test} ran\n", + caption=f"{pluralize(summary.total_tests, 'test')} ran\n", ) for (implementation, language), counts in results: table.add_row( @@ -252,12 +252,11 @@ def _validation_results_table( summary: _report._Summary, # type: ignore[reportPrivateUsage] results: Iterable[tuple[Any, Iterable[tuple[Any, dict[str, str]]]]], ): - test = "tests" if summary.total_tests != 1 else "test" table = Table( Column(header="Schema", vertical="middle"), "", title="Bowtie", - caption=f"{summary.total_tests} {test} ran", + caption=f"{pluralize(summary.total_tests, 'test')} ran", ) for schema, case_results in results: diff --git a/bowtie/_utils.py b/bowtie/_utils.py new file mode 100644 index 000000000..94578175e --- /dev/null +++ b/bowtie/_utils.py @@ -0,0 +1,22 @@ +""" +Small, generic helpers shared across Bowtie's modules. +""" +from __future__ import annotations + + +def pluralize(count: int, noun: str, plural: str | None = None) -> str: + """ + Combine a count with the singular or plural form of a noun. + + >>> pluralize(1, "test") + '1 test' + >>> pluralize(0, "test") + '0 tests' + >>> pluralize(2, "test") + '2 tests' + >>> pluralize(2, "story", plural="stories") + '2 stories' + """ + plural = plural or f"{noun}s" + word = noun if count == 1 else plural + return f"{count} {word}" diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 000000000..a0b57c338 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,25 @@ +from bowtie._utils import pluralize + + +def test_pluralize_singular(): + assert pluralize(1, "test") == "1 test" + + +def test_pluralize_plural(): + assert pluralize(2, "test") == "2 tests" + + +def test_pluralize_zero(): + assert pluralize(0, "test") == "0 tests" + + +def test_pluralize_negative(): + assert pluralize(-1, "test") == "-1 tests" + + +def test_pluralize_custom_plural(): + assert pluralize(2, "story", plural="stories") == "2 stories" + + +def test_pluralize_custom_plural_singular_unaffected(): + assert pluralize(1, "story", plural="stories") == "1 story"