コード例 #1
0
 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"
     )
コード例 #2
0
 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"
     )
コード例 #3
0
 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"
     )
コード例 #4
0
 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",
     )
コード例 #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)
コード例 #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'
コード例 #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'
コード例 #8
0
 def test_mul(self):
     assert LazyString(str.upper, "abc") * 2 == "ABCABC"
コード例 #9
0
 def test_mod(self):
     assert LazyString(str.upper, "hello %f") % 1.0 == "HELLO 1.000000"
コード例 #10
0
 def test_swapcase(self):
     assert LazyString(str.upper, "hello world").swapcase() == "hello world"
     assert LazyString(str.lower, "HELLO WORLD").swapcase() == "HELLO WORLD"
コード例 #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'
コード例 #12
0
 def test_capitalize(self):
     assert LazyString(str.lower, "HELLO WORLD").capitalize() == "Hello world"
コード例 #13
0
 def test_rmul(self):
     assert 2 * LazyString(str.upper, "abc") == "ABCABC"
コード例 #14
0
 def test_getitem(self):
     assert LazyString(str.upper, "hello world")[1] == "E"
コード例 #15
0
 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")
コード例 #16
0
 def test_contains(self):
     assert "world" not in LazyString(str.upper, "hello world")
     assert "WORLD" in LazyString(str.upper, "hello world")
コード例 #17
0
 def test_add(self):
     assert LazyString(str.upper, "hello") + " world" == "HELLO world"
     assert "hello" + LazyString(str.upper, " world") == "hello WORLD"
コード例 #18
0
 def test_zfill(self):
     assert LazyString(str.upper, "hello world").zfill(20) == "000000000HELLO WORLD"
コード例 #19
0
 def test_upper(self):
     assert LazyString(str.lower, "HELLO WORLD").upper() == "HELLO WORLD"
コード例 #20
0
 def test_ne(self):
     assert not LazyString(str.upper, "hello world") != "HELLO WORLD"
     assert LazyString(str.upper, "hello world") != "hello world"
コード例 #21
0
 def test_rmod(self):
     assert "hello %s" % LazyString(str.upper, "world") == "hello WORLD"
コード例 #22
0
 def test_gt(self):
     assert not LazyString(str.upper, "b") > "a"
     assert LazyString(str.upper, "b") > "A"
コード例 #23
0
 def test_str(self):
     assert str(LazyString(str.upper, "hello world")) == "HELLO WORLD"
コード例 #24
0
 def test_hash(self):
     assert hash(LazyString(str.upper, "hello world")) != hash("hello world")
     assert hash(LazyString(str.upper, "hello world")) == hash("HELLO WORLD")
コード例 #25
0
 def test_casefold(self):
     assert LazyString(str.upper, "ß").casefold() == "ss"
コード例 #26
0
 def test_strip(self):
     assert LazyString(str.upper, "  hello world  ").strip() == "HELLO WORLD"
コード例 #27
0
 def test_len(self):
     assert len(LazyString(lambda s: s * 2, "abc")) == 6
コード例 #28
0
 def test_lt(self):
     assert LazyString(str.upper, "b") < "a"
     assert not LazyString(str.upper, "b") < "A"
コード例 #29
0
 def test_iter(self):
     assert list(LazyString(str.upper, "hello")) == ["H", "E", "L", "L", "O"]
コード例 #30
0
 def test_title(self):
     assert LazyString(str.upper, "hello world").title() == "Hello World"