| 1 |
from electrum import i18n |
| 2 |
from electrum.i18n import _ensure_translation_keeps_format_string_syntax_similar |
| 3 |
|
| 4 |
from . import ElectrumTestCase |
| 5 |
|
| 6 |
|
| 7 |
syntax_check_decorator = _ensure_translation_keeps_format_string_syntax_similar |
| 8 |
|
| 9 |
|
| 10 |
class TestSyntaxChecks(ElectrumTestCase): |
| 11 |
|
| 12 |
|
| 13 |
def test_no_format(self): |
| 14 |
src, dst = ("hello there", "HELLO THEEEEERE") |
| 15 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 16 |
|
| 17 |
def test_malformed_src_string_raises(self): |
| 18 |
src, dst = ("hel{lo there", "HELLO THE{}RE") |
| 19 |
with self.assertRaises(ValueError): |
| 20 |
syntax_check_decorator(lambda x: dst)(src) |
| 21 |
|
| 22 |
def test_malformed_dst_string_gets_rejected(self): |
| 23 |
src, dst = ("hel{}lo there", "HELLO THE{RE") |
| 24 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 25 |
src, dst = ("hello there", "HELLO THE{RE") |
| 26 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 27 |
src, dst = ("hello there", "HELLO THE{{}RE") |
| 28 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 29 |
|
| 30 |
def test_simple_substitution(self): |
| 31 |
src, dst = ("hel{}lo there", "HELLO THE{}RE") |
| 32 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 33 |
src, dst = ("hel{}lo {} there {}", "HELLO {} THE{}RE {}") |
| 34 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 35 |
|
| 36 |
def test_positional_substitution(self): |
| 37 |
src, dst = ("hel{0}lo there", "HELLO THE{0}RE") |
| 38 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 39 |
src, dst = ("hel{0}lo there {1}", "HELLO THE{0}RE {1}") |
| 40 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 41 |
src, dst = ("hel{0}lo {2} there {1}", "HELLO THE{0}RE {2} {1}") |
| 42 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 43 |
|
| 44 |
def test_keyword_substitution(self): |
| 45 |
src, dst = ("hello there {title}. {name}. welc", "HELLO THERE {title}. {name}. WELC") |
| 46 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 47 |
|
| 48 |
def test_mixed_sub(self): |
| 49 |
src, dst = ("{1} aaa {qq} {0} bbb", "{1} AAA {qq} {0} BBB") |
| 50 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 51 |
src, dst = ("{1} aaa {pp} {qq} {0} bbb", "{1} AAA {pp} {qq} {0} BBB") |
| 52 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 53 |
|
| 54 |
def test_allow_reordering_replacement_fields(self): |
| 55 |
src, dst = ("time left: {0} minutes, {1} seconds", "TIME LEFT: {1} SECONDS, {0} MINUTES") |
| 56 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 57 |
src, dst = ("{1} aaa {pp} {qq} {0} bbb", "{qq} AAA {0} {1} {pp} BBB") |
| 58 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 59 |
|
| 60 |
def test_replacement_field_name_cannot_change(self): |
| 61 |
|
| 62 |
src, dst = ("hel{}lo there", "HELLO THE{RE}") |
| 63 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 64 |
src, dst = ("hel{}lo there", "HELLO THE{0}") |
| 65 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 66 |
src, dst = ("hel{0}lo there", "HELLO THE{}") |
| 67 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 68 |
src, dst = ("hel{0}lo there", "HELLO THE{RE}") |
| 69 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 70 |
src, dst = ("hel{RE}lo there", "HELLO THE{}") |
| 71 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 72 |
src, dst = ("hel{RE}lo there", "HELLO THE{0}") |
| 73 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 74 |
|
| 75 |
src, dst = ("hello there {} {} {} {p} {q}", "HELLO THERE {} {q} {q} {p} {q}") |
| 76 |
self.assertEqual(dst, syntax_check_decorator(lambda x: dst)(src)) |
| 77 |
|
| 78 |
def test_replacement_field_count_cannot_change(self): |
| 79 |
|
| 80 |
src, dst = ("hello there", "HELLO THERE {}") |
| 81 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 82 |
src, dst = ("hello there", "HELLO {} {} THERE") |
| 83 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 84 |
src, dst = ("hello {} there", "HELLO THERE {} {}") |
| 85 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 86 |
src, dst = ("hello there {}", "HELLO THERE") |
| 87 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 88 |
src, dst = ("hello there {p} {q} {r}", "HELLO THERE {p} {q}") |
| 89 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 90 |
src, dst = ("hello there {p} {q} {r}", "HELLO THERE {p} {q} {r} {s}") |
| 91 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |
| 92 |
src, dst = ("hello there {p} {0}", "HELLO THERE {p}") |
| 93 |
self.assertEqual(src, syntax_check_decorator(lambda x: dst)(src)) |