Skip to content

Commit

Permalink
convert tests to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
entorb committed May 18, 2024
1 parent dc2a860 commit a1d994b
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 139 deletions.
184 changes: 91 additions & 93 deletions scripts/check_chapters_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Tests for check_chapters.py .""" # noqa: INP001
# ruff: noqa: S101 RUF001 RUF003 D103
# ruff: noqa: INP001 D103 RUF001 S101
"""Tests for check_chapters.py."""

from collections.abc import Callable

import pytest
from check_chapters import (
fix_common_typos,
fix_ellipsis,
Expand All @@ -20,22 +21,9 @@
from check_chapters_settings import settings


def test_it(fct: Callable, pairs: list[tuple[str, str]]) -> None:
for text, expected_output in pairs:
# test of isolated function
assert fct(text) == expected_output, f"'{fct(text)}' != '{expected_output}'"
# test in complete fix_line context
assert (
fix_line(text) == expected_output
), f"'{fix_line(text)}' != '{expected_output}'"


for lang in ["EN", "DE"]:
@pytest.mark.parametrize("lang", ["EN", "DE"])
def test_fix_common_typos(lang: str) -> None:
settings["lang"] = lang

#
# fix_common_typos
#
pairs = [
("Test Mungo's King's Cross", "Test Mungo’s King’s Cross"),
("Test", "Test"),
Expand All @@ -56,23 +44,25 @@ def test_it(fct: Callable, pairs: list[tuple[str, str]]) -> None:
("Fritz'scher Gesetz", "Fritz’scher Gesetz"),
]
)
test_it(fix_common_typos, pairs)
checkit(fix_common_typos, pairs)


#
# fix_ellipsis
#
@pytest.mark.parametrize("lang", ["EN", "DE"])
def test_fix_ellipsis(lang: str) -> None:
settings["lang"] = lang
pairs = [
("foo…bar", "foo…bar"),
("foo … bar", "foo…bar"),
("foo… bar", "foo…bar"),
("foo …bar", "foo…bar"),
("foo, …", "foo, …"),
]
test_it(fix_ellipsis, pairs)
checkit(fix_ellipsis, pairs)


#
# fix_emph
#
@pytest.mark.parametrize("lang", ["EN", "DE"])
def test_fix_emph(lang: str) -> None:
settings["lang"] = lang
pairs = [
(r"That’s not \emph{true!}", r"That’s not \emph{true}!"),
(r"she got \emph{magic,} can you", r"she got \emph{magic}, can you"),
Expand All @@ -81,82 +71,75 @@ def test_it(fct: Callable, pairs: list[tuple[str, str]]) -> None:
if lang == "EN":
pairs.extend(
[
(
r"briefly. \emph{Hopeless.} Both",
r"briefly. \emph{Hopeless.} Both", # . unchanged
),
(r"briefly. \emph{Hopeless.} Both", r"briefly. \emph{Hopeless.} Both"),
("asdf", "asdf"),
]
)
elif lang == "DE":
pairs.extend(
[
(
r"briefly. \emph{Hopeless.} Both",
r"briefly. \emph{Hopeless}. Both", # . now out
),
(r"briefly. \emph{Hopeless.} Both", r"briefly. \emph{Hopeless}. Both"),
("asdf", "asdf"),
]
)
test_it(fix_emph, pairs)
checkit(fix_emph, pairs)

#
# fix_hyphens
#

@pytest.mark.parametrize("lang", ["EN", "DE"])
def test_fix_hyphens(lang: str) -> None:
settings["lang"] = lang
pairs = [
("2-3-4", "2–3–4"),
(" —,", "—,"),
(" —.", "—."),
(" —!", "—!"),
(" —?", "—?"),
# start of line
("— asdf", "—asdf"),
("- asdf", "—asdf"),
("-asdf", "—asdf"),
]
if lang == "DE":
pairs.extend(
[
# end of line
("Text —", "Text—"),
# start of quote
("Text—„", "Text— „"),
("Text —„", "Text— „"),
("Text „ —Quote", "Text „—Quote"),
("Text „ — Quote", "Text „—Quote"),
("Text—„— Quote", "Text— „—Quote"),
# end of quote
("Text -“asdf", "Text—“ asdf"),
("Text —“", "Text—“"),
]
)
test_it(fix_hyphens, pairs)
checkit(fix_hyphens, pairs)

#
# fix_latex
#

@pytest.mark.parametrize("lang", ["EN", "DE"])
def test_fix_latex(lang: str) -> None:
settings["lang"] = lang
pairs = [
("begin at new line\\begin{em}", "begin at new line\n\\begin{em}"),
("end at new line\\end{em}", "end at new line\n\\end{em}"),
("new line after \\\\ asdf", "new line after \\\\\nasdf"),
("no new line after \\\\", "no new line after \\\\"),
]
test_it(fix_latex, pairs)
checkit(fix_latex, pairs)

#
# fix_linebreaks_speach
#
if lang == "DE":
pairs = [
(" „Hello", "\n„Hello"),
(" „hello", " „hello"),
("„hello", "„hello"),
]
test_it(fix_linebreaks_speach, pairs)

#
# fix_MrMrs
#

@pytest.mark.parametrize("lang", ["DE"])
def test_fix_linebreaks_speach(lang: str) -> None:
settings["lang"] = lang
pairs = [
(" „Hello", "\n„Hello"),
(" „hello", " „hello"),
("„hello", "„hello"),
]
checkit(fix_linebreaks_speach, pairs)


@pytest.mark.parametrize("lang", ["EN", "DE"])
def test_fix_MrMrs(lang: str) -> None: # noqa: N802
settings["lang"] = lang
pairs = [
("Mr. H. Potter", "Mr~H.~Potter"),
("it’s Doctor now, not Miss.", "it’s Doctor now, not Miss."),
Expand All @@ -173,52 +156,67 @@ def test_it(fct: Callable, pairs: list[tuple[str, str]]) -> None:
("Mr. and Mrs. Davis", "Mr~and Mrs~Davis"),
]
)
test_it(fix_MrMrs, pairs)
checkit(fix_MrMrs, pairs)

#
# fix_numbers
#
if lang == "DE":
pairs = [
("Es ist 12:23 Uhr.", "Es ist 12:23~Uhr."),
("asdf", "asdf"),
]
test_it(fix_numbers, pairs)

#
# fix_punctuation
#

@pytest.mark.parametrize("lang", ["DE"])
def test_fix_numbers(lang: str) -> None:
settings["lang"] = lang
pairs = [
("Es ist 12:23 Uhr.", "Es ist 12:23~Uhr."),
("asdf", "asdf"),
]
checkit(fix_numbers, pairs)


@pytest.mark.parametrize("lang", ["EN", "DE"])
def test_fix_punctuation(lang: str) -> None:
settings["lang"] = lang
pairs = [
("!!", "!"),
("??", "?"),
("! !", "!"),
("..", "."),
(",,", ","),
]
test_it(fix_punctuation, pairs)
checkit(fix_punctuation, pairs)


#
# fix_spaces
#
@pytest.mark.parametrize("lang", ["EN", "DE"])
def test_fix_spaces(lang: str) -> None:
settings["lang"] = lang
pairs = [
("Hallo  Harry", "Hallo Harry"),
("Hallo Harry", "Hallo Harry"),
("tabs\tto\t\tspace", "tabs to space"),
("trailing spaces ", "trailing spaces"),
(" ", ""),
("multiple spaces", "multiple spaces"),
]
test_it(fix_spaces, pairs)
checkit(fix_spaces, pairs)

#
# fix_spell
#
if lang == "DE":
pairs = [
(r"‚Lumos‘", r"\spell{Lumos}"),
(r"„Lumos“", r"\spell{Lumos}"),
(r"„\emph{Lumos}“", r"\spell{Lumos}"),
(r"\emph{„Lumos“}", r"\spell{Lumos}"),
(r"\emph{Lumos!}", r"\spell{Lumos}"),
(r"„\spell{Lumos}“", r"\spell{Lumos}"),
]
test_it(fix_spell, pairs)

@pytest.mark.parametrize("lang", ["DE"])
def test_fix_spell(lang: str) -> None:
settings["lang"] = lang
pairs = [
(r"‚Lumos‘", r"\spell{Lumos}"),
(r"„Lumos“", r"\spell{Lumos}"),
(r"„\emph{Lumos}“", r"\spell{Lumos}"),
(r"\emph{„Lumos“}", r"\spell{Lumos}"),
(r"\emph{Lumos!}", r"\spell{Lumos}"),
(r"„\spell{Lumos}“", r"\spell{Lumos}"),
]
checkit(fix_spell, pairs)


def checkit(fct: Callable, pairs: list[tuple[str, str]]) -> None:
for text, expected_output in pairs:
# test of isolated function
assert (
fct(text) == expected_output #
), f"'{fct(text)}' != '{expected_output}'"

# test in complete fix_line context
assert (
fix_line(text) == expected_output
), f"'{fix_line(text)}' != '{expected_output}'"
48 changes: 29 additions & 19 deletions scripts/ebook/step_4_test.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
"""Unit Tests.""" # noqa: INP001
# ruff: noqa: S101
# ruff: noqa: INP001 S101
# cspell:disable

"""Unit Tests."""

import pytest
from step_4 import convert_parsel

assert convert_parsel("foo") == "foo"
# s
assert convert_parsel("house") == "housse"
assert convert_parsel("Special") == "Sspecial"
# ss and ß
assert convert_parsel("Professor") == "Professsor"
assert convert_parsel("muß") == "musss"
# z
assert convert_parsel("zero") == "zzero"
assert convert_parsel("Zero") == "Zzero"
# zz
assert convert_parsel("puzzled") == "puzzzled"
# x -> xs
assert convert_parsel("Bellatrix") == "Bellatrixs"

# combined
assert convert_parsel("expression") == "exspresssion"
assert convert_parsel("Salazar") == "Ssalazzar"
@pytest.mark.parametrize(
("text", "expected"),
[
("foo", "foo"),
# s
("house", "housse"),
("Special", "Sspecial"),
# ss and ß
("Professor", "Professsor"),
("muß", "musss"),
# z
("zero", "zzero"),
("Zero", "Zzero"),
# zz
("puzzled", "puzzzled"),
# x -> xs
("Bellatrix", "Bellatrixs"),
# combined
("expression", "exspresssion"),
("Salazar", "Ssalazzar"),
],
)
def test_convert_parsel(text: str, expected: str) -> None: # noqa: D103
assert convert_parsel(text) == expected
Loading

0 comments on commit a1d994b

Please sign in to comment.