def test_replace(self):
     assert (
         LazyString(str.upper, "hello world").replace("world", "jim")
         == "HELLO WORLD"
     )
     assert (
         LazyString(str.upper, "hello world").replace("WORLD", "Jim") == "HELLO Jim"
     )
 def test_encode(self):
     assert (
         LazyString(str.upper, "hεllo world").encode("utf8") == b"H\xce\x95LLO WORLD"
     )
     assert (
         LazyString(str.upper, "hεllo world").encode("iso-8859-7")
         == b"H\xc5LLO WORLD"
     )
 def test_translate(self):
     assert (
         LazyString(str.upper, "hello world").translate({ord("w"): ord("b")})
         == "HELLO WORLD"
     )
     assert (
         LazyString(str.upper, "hello world").translate({ord("W"): ord("B")})
         == "HELLO BORLD"
     )
 def test_rpartition(self):
     assert LazyString(str.upper, "hello world").rpartition("l") == (
         "",
         "",
         "HELLO WORLD",
     )
     assert LazyString(str.upper, "hello world").rpartition("L") == (
         "HELLO WOR",
         "L",
         "D",
     )
Beispiel #5
0
def lazy_translate(_string, _context=None, _escape=True, **params):
    """Lazily translate the given source string to the current language.

    Delays the evaluation of translating the given string until necessary.
    This is useful in cases where the call to translate() happens
    before any translations have been retrieved, e.g. in the definition
    of a Python class.

    See translate() for more details.

    :param unicode _string: the source string to get the translation for
    :param unicode _context: an optional context that gives more information
        about the source string
    :param bool _escape: if True, the returned string will be HTML-escaped,
        otherwise it won't
    :return: an object that when evaluated as a string will return
        the final translation in the current language
    :rtype: LazyString
    """
    return LazyString(translate,
                      _string,
                      _context=_context,
                      escape=_escape,
                      fallback_value=_string,
                      **params)
Beispiel #6
0
 def test_no_memoization(self):
     """Make sure the string produces a value every time it evaluates,
     i.e. it does not memoize the result."""
     mapping = {}
     string = LazyString(
         lambda x: '{}={}'.format(x, mapping.get(x, '<unknown>')), 'foo')
     assert '{}'.format(string) == 'foo=<unknown>'
     mapping.update({'foo': 33, 'bar': 44})
     assert '{}'.format(string) == 'foo=33'
Beispiel #7
0
 def test_str_functionality(self):
     string = LazyString(lambda x: x * 2, 'foo')
     assert string == 'foofoo'
     assert len(string) == len('foofoo')
     assert string[1] == 'o'
     assert '.'.join([x for x in string]) == 'f.o.o.f.o.o'
     assert 'foo' in string
     assert 'goo' not in string
     assert string + 'z' == 'foofooz'
     assert 'z' + string == 'zfoofoo'
     assert string * 2 == 'foofoofoofoo'
     assert 2 * string == 'foofoofoofoo'
     assert string < 'goofoo'
     assert string > 'eoofoo'
     assert string <= 'goofoo'
     assert string >= 'eoofoo'
     assert {string: 'FOO'}.get('foofoo') == 'FOO'
 def test_mul(self):
     assert LazyString(str.upper, "abc") * 2 == "ABCABC"
 def test_mod(self):
     assert LazyString(str.upper, "hello %f") % 1.0 == "HELLO 1.000000"
 def test_swapcase(self):
     assert LazyString(str.upper, "hello world").swapcase() == "hello world"
     assert LazyString(str.lower, "HELLO WORLD").swapcase() == "HELLO WORLD"
Beispiel #11
0
 def test_laziness(self):
     """Make sure the string produces the proper value when evaluates."""
     mapping = {}
     string = LazyString(lambda x: '{}={}'.format(x, mapping.get(x)), 'foo')
     mapping.update({'foo': 33, 'bar': 44})
     assert '{}'.format(string) == 'foo=33'
 def test_capitalize(self):
     assert LazyString(str.lower, "HELLO WORLD").capitalize() == "Hello world"
 def test_rmul(self):
     assert 2 * LazyString(str.upper, "abc") == "ABCABC"
 def test_getitem(self):
     assert LazyString(str.upper, "hello world")[1] == "E"
 def test_eq(self):
     assert LazyString(str.upper, "hello world") == "HELLO WORLD"
     assert "HELLO WORLD" == LazyString(str.upper, "hello world")
     assert LazyString(str.upper, "hello world") != "hello world"
     assert "hello world" != LazyString(str.upper, "hello world")
 def test_contains(self):
     assert "world" not in LazyString(str.upper, "hello world")
     assert "WORLD" in LazyString(str.upper, "hello world")
 def test_add(self):
     assert LazyString(str.upper, "hello") + " world" == "HELLO world"
     assert "hello" + LazyString(str.upper, " world") == "hello WORLD"
 def test_zfill(self):
     assert LazyString(str.upper, "hello world").zfill(20) == "000000000HELLO WORLD"
 def test_upper(self):
     assert LazyString(str.lower, "HELLO WORLD").upper() == "HELLO WORLD"
 def test_ne(self):
     assert not LazyString(str.upper, "hello world") != "HELLO WORLD"
     assert LazyString(str.upper, "hello world") != "hello world"
 def test_rmod(self):
     assert "hello %s" % LazyString(str.upper, "world") == "hello WORLD"
 def test_gt(self):
     assert not LazyString(str.upper, "b") > "a"
     assert LazyString(str.upper, "b") > "A"
 def test_str(self):
     assert str(LazyString(str.upper, "hello world")) == "HELLO WORLD"
 def test_hash(self):
     assert hash(LazyString(str.upper, "hello world")) != hash("hello world")
     assert hash(LazyString(str.upper, "hello world")) == hash("HELLO WORLD")
 def test_casefold(self):
     assert LazyString(str.upper, "ß").casefold() == "ss"
 def test_strip(self):
     assert LazyString(str.upper, "  hello world  ").strip() == "HELLO WORLD"
 def test_len(self):
     assert len(LazyString(lambda s: s * 2, "abc")) == 6
 def test_lt(self):
     assert LazyString(str.upper, "b") < "a"
     assert not LazyString(str.upper, "b") < "A"
 def test_iter(self):
     assert list(LazyString(str.upper, "hello")) == ["H", "E", "L", "L", "O"]
 def test_title(self):
     assert LazyString(str.upper, "hello world").title() == "Hello World"