Skip to content

Commit

Permalink
tests: avoid using staticmethod in pytest
Browse files Browse the repository at this point in the history
It cannot work reliably, see
pytest-dev/pytest#12065.
  • Loading branch information
nijel committed Apr 16, 2024
1 parent 587ac7b commit a98dcd2
Show file tree
Hide file tree
Showing 28 changed files with 128 additions and 256 deletions.
3 changes: 1 addition & 2 deletions tests/translate/convert/test_dtd2po.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ def test_preserving_spaces(self):
print(repr(unit.source))
assert unit.source == " - with a newline and more text"

@staticmethod
def test_escaping_newline_tabs():
def test_escaping_newline_tabs(self):
"""Test that we handle all kinds of newline permutations."""
dtdsource = '<!ENTITY noupdatesfound.intro "A hard coded newline.\\nAnd tab\\t and a \\r carriage return.">\n'
converter = dtd2po.dtd2po()
Expand Down
15 changes: 5 additions & 10 deletions tests/translate/convert/test_po2oo.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def test_convertoo(self):
newoo = self.convertoo(posource, ootemplate, language="zu")
assert newoo.decode("utf-8") == ootemplate + ooexpected

@staticmethod
def test_pofilter():
def test_pofilter(self):
"""Tests integration with pofilter."""
# Some bad po with a few errors:
posource = b'#: sourcefile.bla#ID_NUMBER.txet.gnirts\nmsgid "<tag cow=\\"3\\">Mistake."\nmsgstr " <etiket koei=\\"3\\">(fout) "'
Expand Down Expand Up @@ -114,8 +113,7 @@ def test_roundtrip_spaces(self):
self.check_roundtrip(" ")
self.check_roundtrip("\u00a0")

@staticmethod
def test_default_timestamp():
def test_default_timestamp(self):
"""Test to ensure that we revert to the default timestamp."""
oointro, oooutro = (
r"svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Text ",
Expand All @@ -133,8 +131,7 @@ def test_default_timestamp():
== oointro + "2002-02-02 02:02:02" + oooutro
)

@staticmethod
def test_escape_conversion():
def test_escape_conversion(self):
"""Test to ensure that we convert escapes correctly."""
oosource = (
r"svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Column1\tColumn2\r\n 2002-02-02 02:02:02"
Expand All @@ -149,8 +146,7 @@ def test_escape_conversion():
)
assert b"\tKolom1\\tKolom2\\r\\n\t" in outputfile.getvalue()

@staticmethod
def test_helpcontent_escapes():
def test_helpcontent_escapes(self):
"""Test to ensure that we convert helpcontent escapes correctly."""
# Note how this test specifically uses incorrect spacing in the
# translation. The extra space before 'hid' and an extra space before
Expand Down Expand Up @@ -178,8 +174,7 @@ def test_helpcontent_escapes():
in outputfile.getvalue()
)

@staticmethod
def test_helpcontent_escapes2():
def test_helpcontent_escapes2(self):
"""Test to ensure that we convert helpcontent escapes correctly."""
oosource = (
r"helpcontent2 source\text\scalc\05\empty_cells.xhp 0 help par_id2629474 0 en-US A1: <empty> 2002-02-02 02:02:02"
Expand Down
9 changes: 3 additions & 6 deletions tests/translate/convert/test_po2php.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def merge2php(phpsource, posource):
print(outputphp)
return outputphp

@staticmethod
def test_convertphp():
def test_convertphp(self):
"""Test convertphp helper."""
posource = """#: $lang['name']
msgid "value"
Expand All @@ -47,8 +46,7 @@ def test_convertphp():
assert po2php.convertphp(inputfile, outputfile, templatefile) == 1
assert outputfile.getvalue() == phpexpected

@staticmethod
def test_convertphp_notemplate():
def test_convertphp_notemplate(self):
"""Test convertphp helper without template."""
posource = """#: $lang['name']
msgid "value"
Expand All @@ -59,8 +57,7 @@ def test_convertphp_notemplate():
with raises(ValueError):
po2php.convertphp(inputfile, outputfile, None)

@staticmethod
def test_convertphp_empty_template():
def test_convertphp_empty_template(self):
"""Test convertphp helper with empty translation."""
posource = """#: $lang['name']
msgid "value"
Expand Down
3 changes: 1 addition & 2 deletions tests/translate/lang/test_identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ def test_identify_store(self):
unit.target = TEXT_LIST[i]
assert self.langident.identify_target_lang(langlist) == "de"

@staticmethod
def test_bad_init_data():
def test_bad_init_data(self):
"""Test __init__ with bad conf files and data dirs."""
with raises(ValueError):
LanguageIdentifier(model_dir="missing")
Expand Down
24 changes: 8 additions & 16 deletions tests/translate/misc/test_multistring.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@


class TestMultistring:
@staticmethod
def test_constructor():
def test_constructor(self):
t = multistring.multistring
s1 = t("test")
assert type(s1) is t
Expand All @@ -19,8 +18,7 @@ def test_constructor():
with pytest.raises(ValueError):
t([])

@staticmethod
def test_repr():
def test_repr(self):
t = multistring.multistring
s1 = t("test")
assert repr(s1) == "multistring(['test'])"
Expand All @@ -30,8 +28,7 @@ def test_repr():
assert repr(s2) == "multistring(['test', 'mé'])"
assert eval("multistring.%s" % repr(s2)) == s2

@staticmethod
def test_replace():
def test_replace(self):
t = multistring.multistring
s1 = t(["abcdef", "def"])

Expand Down Expand Up @@ -62,8 +59,7 @@ def test_replace():
result = s2.replace("e", "g", 1)
assert result == t(["abcdgef", "dgef"])

@staticmethod
def test_comparison():
def test_comparison(self):
t = multistring.multistring
assert t("test") == "test"
assert t("test").__cmp__("test") == 0
Expand All @@ -72,14 +68,12 @@ def test_comparison():
assert t("téßt") > "test"
assert t("téßt").__cmp__("test") > 0

@staticmethod
def test_coercion():
def test_coercion(self):
t = multistring.multistring
assert str(t("test")) == "test"
assert str(t("téßt")) == "téßt"

@staticmethod
def test_unicode_coercion():
def test_unicode_coercion(self):
t = multistring.multistring
assert str(t("test")) == "test"
assert str(t("test")) == "test"
Expand All @@ -88,14 +82,12 @@ def test_unicode_coercion():
assert str(t(["téßt", "blāh"])) == "téßt"
assert str(t(["téßt"])) == "téßt"

@staticmethod
def test_list_coercion():
def test_list_coercion(self):
t = multistring.multistring
assert str([t("test")]) == "[multistring(['test'])]"
assert str([t("tést")]) == "[multistring(['tést'])]"

@staticmethod
def test_multistring_hash():
def test_multistring_hash(self):
t = multistring.multistring
foo = t(["foo", "bar"])
foodict = {foo: "baz"}
Expand Down
3 changes: 1 addition & 2 deletions tests/translate/misc/test_optrecurse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def test_splitext(self):
print(fullpath)
assert self.parser.splitext(fullpath) == (root, extension)

@staticmethod
def test_outputfile_receives_bytes(capsys):
def test_outputfile_receives_bytes(self, capsys):
parser = optrecurse.RecursiveOptionParser({"txt": ("po", None)})

temp_file = NamedTemporaryFile(delete=False)
Expand Down
30 changes: 10 additions & 20 deletions tests/translate/misc/test_quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,58 +108,50 @@ def test_stripcomment():


class TestEncoding:
@staticmethod
def test_javapropertiesencode():
def test_javapropertiesencode(self):
assert quote.javapropertiesencode("abc") == "abc"
assert quote.javapropertiesencode("abcḓ") == r"abc\u1E13"
assert quote.javapropertiesencode("abc\n") == "abc\\n"

@staticmethod
def test_java_utf8_properties_encode():
def test_java_utf8_properties_encode(self):
assert quote.java_utf8_properties_encode("abc") == "abc"
assert quote.java_utf8_properties_encode("abcḓ") == "abcḓ"
assert quote.java_utf8_properties_encode("abc\n") == "abc\\n"

@staticmethod
def test_escapespace():
def test_escapespace(self):
assert quote.escapespace(" ") == "\\u0020"
assert quote.escapespace("\t") == "\\u0009"

@staticmethod
def test_mozillaescapemarginspaces():
def test_mozillaescapemarginspaces(self):
assert quote.mozillaescapemarginspaces(" ") == r"\u0020"
assert quote.mozillaescapemarginspaces("A") == "A"
assert quote.mozillaescapemarginspaces(" abc ") == r"\u0020abc\u0020"
assert quote.mozillaescapemarginspaces(" abc ") == r"\u0020 abc\u0020"

@staticmethod
def test_mozilla_control_escapes():
def test_mozilla_control_escapes(self):
r"""Test that we do \uNNNN escapes for certain control characters instead of converting to UTF-8 characters."""
prefix, suffix = "bling", "blang"
for control in ("\u0005", "\u0006", "\u0007", "\u0011"):
string = prefix + control + suffix
assert quote.escapecontrols(string) != string

@staticmethod
def test_propertiesdecode():
def test_propertiesdecode(self):
assert quote.propertiesdecode("abc") == "abc"
assert quote.propertiesdecode("abc\\u1e13") == "abcḓ"
assert quote.propertiesdecode("abc\\u1E13") == "abcḓ"
assert quote.propertiesdecode("abc\N{LEFT CURLY BRACKET}") == "abc{"
assert quote.propertiesdecode("abc\\") == "abc\\"
assert quote.propertiesdecode("abc\\") == "abc\\"

@staticmethod
def test_controlchars():
def test_controlchars(self):
assert quote.javapropertiesencode(quote.propertiesdecode("\u0001")) == r"\u0001"
assert quote.javapropertiesencode(quote.propertiesdecode("\\u01")) == r"\u0001"
assert quote.javapropertiesencode("\\") == "\\\\"
assert quote.javapropertiesencode("\x01") == "\\u0001"
assert quote.propertiesdecode("\x01") == "\x01"
assert quote.propertiesdecode("\\u0001") == "\x01"

@staticmethod
def test_properties_decode_slashu():
def test_properties_decode_slashu(self):
# The real input strings don't have double backslashes, but we have to
# double them here because Python immediately decode them, even for raw
# strings.
Expand All @@ -180,8 +172,7 @@ def test_htmlencoding(self):
raw_encoded = [("€", "&euro;"), ("©", "&copy;"), ('"', "&quot;")]
self._html_encoding_helper(raw_encoded)

@staticmethod
def test_htmlencoding_existing_entities():
def test_htmlencoding_existing_entities(self):
"""Test that we don't mess existing entities."""
assert quote.htmlentityencode("&amp;") == "&amp;"

Expand All @@ -192,8 +183,7 @@ def test_htmlencoding_passthrough(self):
] # Raw text should have nothing done to it.
self._html_encoding_helper(raw_encoded)

@staticmethod
def test_htmlencoding_nonentities():
def test_htmlencoding_nonentities(self):
"""Tests to give us full coverage."""
for encoded, real in [
("Some &; text", "Some &; text"),
Expand Down
9 changes: 3 additions & 6 deletions tests/translate/search/test_lshtein.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
class TestLevenshtein:
"""Test whether Levenshtein distance calculations are correct."""

@staticmethod
def test_basic_distance():
def test_basic_distance(self):
"""Tests distance correctness with a few basic values."""
assert lshtein.distance("word", "word") == 0
assert lshtein.distance("word", "") == 4
Expand All @@ -14,17 +13,15 @@ def test_basic_distance():
assert lshtein.distance("words", "word") == 1
assert lshtein.distance("word", "woord") == 1

@staticmethod
def test_basic_similarity():
def test_basic_similarity(self):
"""Tests similarity correctness with a few basic values."""
levenshtein = lshtein.LevenshteinComparer()
assert levenshtein.similarity("word", "word") == 100
assert levenshtein.similarity("word", "words") == 80
assert levenshtein.similarity("word", "wood") == 75
assert levenshtein.similarity("aaa", "bbb", 0) == 0

@staticmethod
def test_long_similarity():
def test_long_similarity(self):
"""Tests that very long strings are handled well."""
# A sentence with 240 characters:
sentence = "A long, dreary sentence about a cow that never new his mother. Actually it didn't known its father either. One day he decided that enough is enough, and that he would stop making long, dreary sentences just for the sake of making sentences."
Expand Down
3 changes: 1 addition & 2 deletions tests/translate/search/test_terminology.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
class TestTerminology:
"""Test terminology matching."""

@staticmethod
def test_basic():
def test_basic(self):
"""Tests basic functionality."""
termmatcher = terminology.TerminologyComparer()
assert termmatcher.similarity("Open the file", "file") > 75
3 changes: 1 addition & 2 deletions tests/translate/storage/placeables/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ def test_isleaf(self):
for child in self.elem.sub:
assert child.isleaf()

@staticmethod
def test_prune():
def test_prune(self):
elem = StringElem("foo")
child = StringElem("bar")
elem.sub.append(child)
Expand Down
3 changes: 1 addition & 2 deletions tests/translate/storage/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def cleardir(dirname):
os.rmdir(dirname)
assert not os.path.exists(dirname)

@staticmethod
def test_getclass():
def test_getclass(self):
assert classname("file.po") == "pofile"
assert classname("file.pot") == "pofile"
assert classname("file.dtd.po") == "pofile"
Expand Down
3 changes: 1 addition & 2 deletions tests/translate/storage/test_fluent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3442,8 +3442,7 @@ def test_parse_errors(self):
r"message = { -ref",
)

@staticmethod
def test_unit_ids():
def test_unit_ids(self):
"""Test that setting valid ids is ok, and invalid ids are blocked."""
# Test valid ids.
for fluent_type, unit_id in [
Expand Down

0 comments on commit a98dcd2

Please sign in to comment.