Commit 302b51c5 authored by Kirill Smelkov's avatar Kirill Smelkov

golang_str: tests: Make test_strings_methods more robust with upcoming unicode=ustr

Previously test_strings_methods was testing a method via comparing bstr
and ustr results of .method() with similar result of unicode.method().
This works reasonably ok. However under gpython, when unicode will be
replaced with ustr, it will no longer compare results of bstr/ustr
methods with something good and external - indeed in that case bstr/ustr
.method() will be compared to result of ustr.method() which opens the
door for bugs to stay unnoticed.

-> Adjust the test to explicitly provide expected result for all entries
in the test vector. We make sure those results are good and match std
python because we also assert that unicode.method() matches it.
parent bf16f685
...@@ -1530,7 +1530,10 @@ def test_strings_methods(): ...@@ -1530,7 +1530,10 @@ def test_strings_methods():
# argv and kw being various combinations of unicode,bstr,ustr, bytes/bytearray. # argv and kw being various combinations of unicode,bstr,ustr, bytes/bytearray.
def checkop(s, meth, *argv, **kw): def checkop(s, meth, *argv, **kw):
assert type(s) is str assert type(s) is str
ok = kw.pop('ok', None) ok = kw.pop('ok')
if six.PY2:
ok = deepReplaceStr(ok, xunicode)
optional = kw.pop('optional', False)
bs = b(s) bs = b(s)
us = u(s) us = u(s)
# verify {str,bstr,ustr}.meth with str arguments # verify {str,bstr,ustr}.meth with str arguments
...@@ -1545,13 +1548,11 @@ def test_strings_methods(): ...@@ -1545,13 +1548,11 @@ def test_strings_methods():
r = xcall(s, meth, *argv_unicode, **kw_unicode) r = xcall(s, meth, *argv_unicode, **kw_unicode)
# we provide fallback implementations on e.g. py2 # we provide fallback implementations on e.g. py2
if ok is not None: if isinstance(r, NotImplementedError):
if six.PY2: if not optional:
ok = xunicode(ok)
if isinstance(r, NotImplementedError):
r = ok r = ok
else: else:
assert r == ok assert r == ok
assert type(s) is unicode assert type(s) is unicode
br = xcall(bs, meth, *argv, **kw) br = xcall(bs, meth, *argv, **kw)
...@@ -1662,90 +1663,91 @@ def test_strings_methods(): ...@@ -1662,90 +1663,91 @@ def test_strings_methods():
_ = Verifier _ = Verifier
_("миру мир").__contains__("ру") _("миру мир").__contains__("ру", ok=True)
_("миру мир").__contains__("α") _("миру мир").__contains__("α", ok=False)
_("мир").capitalize() _("мир").capitalize( ok="Мир")
_("МиР").casefold() _("МиР").casefold( ok="мир", optional=True) # py3.3
_("мир").center(10) _("мир").center(10, ok=" мир ")
_("мир").center(10, "ж") _("мир").center(10, "ж", ok="жжжмиржжжж")
# count, endswith - tested in test_strings_index # count, endswith - tested in test_strings_index
_("миру\tмир").expandtabs() _("миру\tмир").expandtabs( ok="миру мир")
_("миру\tмир").expandtabs(4) _("миру\tмир").expandtabs(2, ok="миру мир")
# find, index - tested in test_strings_index # find, index - tested in test_strings_index
_("мир").isalnum() _("мир").isalnum( ok=True)
_("мир!").isalnum() _("мир!").isalnum( ok=False)
_("мир").isalpha() _("мир").isalpha( ok=True)
_("мир!").isalpha() _("мир!").isalpha( ok=False)
_("мир").isascii() _("мир").isascii( ok=False, optional=True) # py3.7
_("hello").isascii() _("hello").isascii( ok=True, optional=True) # py3.7
_("hellЫ").isascii() _("hellЫ").isascii( ok=False, optional=True) # py3.7
_("123 мир").isdecimal() _("123 мир").isdecimal( ok=False)
_("123 q").isdecimal() _("123 q").isdecimal( ok=False)
_("123").isdecimal() _("123").isdecimal( ok=True)
_("мир").isdigit() _("мир").isdigit( ok=False)
_("123 мир").isdigit() _("123 мир").isdigit( ok=False)
_("123 q").isdigit() _("123 q").isdigit( ok=False)
_("123").isdigit() _("123").isdigit( ok=True)
_("٤").isdigit() # arabic 4 _("٤").isdigit( ok=True) # arabic 4
_("мир").isidentifier() _("мир").isidentifier( ok=True, optional=True) # py3.0
_("мир$").isidentifier() _("мир$").isidentifier( ok=False, optional=True) # py3.0
_("мир").islower() _("мир").islower( ok=True)
_("Мир").islower() _("Мир").islower( ok=False)
_("мир").isnumeric() _("мир").isnumeric( ok=False)
_("123").isnumeric() _("123").isnumeric( ok=True)
_("0x123").isnumeric() _("0x123").isnumeric( ok=False)
_("мир").isprintable() _("мир").isprintable( ok=True, optional=True) # py3.0
_("\u2009").isspace() # thin space _("\u2009").isspace( ok=x32(True,False)) # thin space
_(" ").isspace() _(" ").isspace( ok=True)
_("мир").isspace() _("мир").isspace( ok=False)
_("мир").istitle() _("мир").istitle( ok=False)
_("Мир").istitle() _("Мир").istitle( ok=True)
_(" мир ").join(["да", "май", "труд"]) _("МИр").istitle( ok=False)
_("мир").ljust(10) _(" мир ").join(["да", "май", "труд"], ok="да мир май мир труд")
_("мир").ljust(10, 'ж') _("мир").ljust(10, ok="мир ")
_("МиР").lower() _("мир").ljust(10, 'ж', ok="миржжжжжжж")
_("\u2009 мир").lstrip() _("МиР").lower( ok="мир")
_("\u2009 мир\u2009 ").lstrip() _("\u2009 мир").lstrip( ok=x32("мир", "\u2009 мир"))
_("мммир").lstrip('ми') _("\u2009 мир\u2009 ").lstrip( ok=x32("мир\u2009 ", "\u2009 мир\u2009 "))
_("миру мир").partition('ру') _("мммир").lstrip('ми', ok="р")
_("миру мир").partition('ж') _("миру мир").partition('ру', ok=("ми", "ру", " мир"))
_("миру мир").removeprefix("мир") _("миру мир").partition('ж', ok=("миру мир", "", ""))
_("миру мир").removesuffix("мир") _("миру мир").removeprefix("мир", ok="у мир", optional=True) # py3.9
_("миру мир").replace("ир", "ж") _("миру мир").removesuffix("мир", ok="миру ", optional=True) # py3.9
_("миру мир").replace("ир", "ж", 1) _("миру мир").replace("ир", "ж", ok="мжу мж")
_("миру мир").replace("ир", "ж", 1, ok="мжу мир")
# rfind, rindex - tested in test_strings_index # rfind, rindex - tested in test_strings_index
_("мир").rjust(10) _("мир").rjust(10, ok=" мир")
_("мир").rjust(10, 'ж') _("мир").rjust(10, 'ж', ok="жжжжжжжмир")
_("миру мир").rpartition('ру') _("миру мир").rpartition('ру', ok=("ми", "ру", " мир"))
_("миру мир").rpartition('ж') _("миру мир").rpartition('ж', ok=("", "", "миру мир"))
_("мир").rsplit() _("мир").rsplit( ok=["мир"])
_("привет мир").rsplit() _("привет мир").rsplit( ok=["привет", "мир"])
_("привет\u2009мир").rsplit() _("привет\u2009мир").rsplit( ok=x32(["привет", "мир"], ["привет\u2009мир"]))
_("привет мир").rsplit("и") _("привет мир").rsplit("и", ok=["пр", "вет м", "р"])
_("привет мир").rsplit("и", 1) _("привет мир").rsplit("и", 1, ok=["привет м", "р"])
_("мир \u2009").rstrip() _("мир \u2009").rstrip( ok=x32("мир", "мир \u2009"))
_(" мир \u2009").rstrip() _(" мир \u2009").rstrip( ok=x32(" мир", " мир \u2009"))
_("мируу").rstrip('ру') _("мируу").rstrip('ру', ok="ми")
_("мир").split() _("мир").split( ok=["мир"])
_("привет мир").split() _("привет мир").split( ok=["привет", "мир"])
_("привет\u2009мир").split() _("привет\u2009мир").split( ok=x32(['привет', 'мир'], ["привет\u2009мир"]))
_("привет мир").split("и") _("привет мир").split("и", ok=["пр", "вет м", "р"])
_("привет мир").split("и", 1) _("привет мир").split("и", 1, ok=["пр", "вет мир"])
_("мир").splitlines() _("мир").splitlines( ok=["мир"])
_("миру\nмир").splitlines() _("миру\nмир").splitlines( ok=["миру", "мир"])
_("миру\nмир").splitlines(True) _("миру\nмир").splitlines(True, ok=["миру\n", "мир"])
_("миру\nмир\n").splitlines(True) _("миру\nмир\n").splitlines(True, ok=["миру\n", "мир\n"])
_("мир\nтруд\nмай\n").splitlines() _("мир\nтруд\nмай\n").splitlines( ok=["мир", "труд", "май"])
_("мир\nтруд\nмай\n").splitlines(True) _("мир\nтруд\nмай\n").splitlines(True, ok=["мир\n", "труд\n", "май\n"])
# startswith - tested in test_strings_index # startswith - tested in test_strings_index
_("\u2009 мир \u2009").strip() _("\u2009 мир \u2009").strip( ok=x32("мир", "\u2009 мир \u2009"))
_("миру мир").strip('мир') _("миру мир").strip('мир', ok="у ")
_("МиР").swapcase() _("МиР").swapcase( ok="мИр")
_("МиР").title() _("МиР").title( ok="Мир")
_("мир").translate({ord(u'м'):ord(u'и'), ord(u'и'):'я', ord(u'р'):None}) _("мир").translate({ord(u'м'):ord(u'и'), ord(u'и'):'я', ord(u'р'):None}, ok="ия")
_("МиР").upper() _("МиР").upper( ok="МИР")
_("мир").zfill(10) _("мир").zfill(10, ok="0000000мир")
_("123").zfill(10) _("123").zfill(10, ok="0000000123")
# verify bstr.translate in bytes mode # verify bstr.translate in bytes mode
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment