Exemple #1
0
def test_variable_formatting():
    value = 12.35
    width = 10
    precision=2
    # Needs the trailing }f on 2.7
    assert str(f('result: {value:{width}.{precision}f}')) == 'result:      12.35'
    assert str(f('result: {value:{width!r}.{precision}f}')) == 'result:      12.35'
    assert str(f('result: {value:{width:0}.{precision:1}f}')) == 'result:      12.35'
Exemple #2
0
def test_constant():
    assert str(f('{{{10}')) == '{10'
    assert str(f('}}{10}')) == '}10'
    assert str(f('}}{{{10}')) == '}{10'
    assert str(f('}}a{{{10}')) == '}a{10'
    assert str(f('{10}{{')) == '10{'
    assert str(f('{10}}}')) == '10}'
    assert str(f('{10}}}{{')) == '10}{'
    assert str(f('{10}}}a{{'))+ '}' == '10}a{}'
Exemple #3
0
def test_lookups():
    a = 0
    d = {"#":"hash",0:'integer',"a":"string","'":"squote","foo":"bar"}
    assert str(f('{"#"}')) == '#'
    assert str(f('{d["#"]}')) == 'hash'
    assert str(f('{d[0]}')) == 'integer'
    assert str(f('{d["a"]}')) == 'string'
    assert str(f('''{d["'"]}''')) == 'squote'
    assert str(f('{d["foo"]}')) == 'bar'
    # This one does not pass because it's not really 3.6
    with pytest.raises(AssertionError):
        assert str(f('{d[a]}')) == 'integer'
    assert str(f('{d[a]}')) == 'string'
Exemple #4
0
def test_complex_eval():
    assert str(f('{3,}')) == '(3,)'
    assert "module 'string' from" in str(f('{string}'))
    assert str(f('g:{a_global}')) == 'g:really a local'
    assert str(f('g:{a_global!r}')) == "g:'really a local'"
    assert str(f('{[x for x in range(5)]}')) == '[0, 1, 2, 3, 4]'
    assert str(f('{3.14:!<10.10}')) == '3.14!!!!!!'
Exemple #5
0
def examples_from_python():
    class Extra(object):
        def __init__(self):
            self._waiters = [1, 2]

        def test(self, extra):
            extra1 = '{},waiters:{}'.format(extra, len(self._waiters))
            extra2 = str(f('{extra},waiters:{len(self._waiters)}'))
            assert extra1 == extra2

    e = Extra()
    e.test("x")

    lineno = 123
    m1 = " [line {0:2d}]".format(lineno)
    m2 = str(f(" [line {lineno:2d}]"))
    assert m1 == m2

    c_basename = "some_class_name"
    m1 = methoddef_name = "{}_METHODDEF".format(c_basename.upper())
    m2 = methoddef_name = str(f("{c_basename.upper()}_METHODDEF"))
    assert m1 == m2

    class Sys(object):
        def __init__(self, *args):
            self.argv = list(args)

    xsys = Sys("someprogram.py", "arg0")
    valid_opts = ["option"]

    print "Usage: {0} [{1}]".format(xsys.argv[0],
                                    '|'.join('--' + opt for opt in valid_opts))
    print f(
        "Usage: {xsys.argv[0]} [{'|'.join('--'+opt for opt in valid_opts)}]")
    printf(
        "Usage: {xsys.argv[0]} [{'|'.join('--'+opt for opt in valid_opts)}]")
    printf("Usage: {xsys.argv[0]} [{opts}]",
           opts='|'.join('--' + opt for opt in valid_opts))
    for i in range(10):
        s = str(f("{i*10}"))
Exemple #6
0
def test_concat():
    x = 'def'
    assert 'abc' +  str(f('## {x}ghi')) == 'abc## defghi'
Exemple #7
0
def test_braces():
    assert str(f('{"{{}}"}')) == '{{}}'
Exemple #8
0
def test_simple():
    y="class"
    assert str(f('{y}')) == format(y)
    assert str(f('{y}')) == 'class'
    assert str(f('{{')) == '{'
    assert str(f('a{{')) == 'a{'
    assert str(f('{{b')) == '{b'
    assert str(f('a{{b')) == 'a{b'
    assert str(f('}}')) == '}'
    assert str(f('a}}')) == 'a}'
    assert str(f('}}b')) == '}b'
    assert str(f('a}}b')) == 'a}b'
    assert str(f('{{}}')) == '{}'
    assert str(f('a{{}}')) == 'a{}'
    assert str(f('{{b}}')) == '{b}'
    assert str(f('{{}}c')) == '{}c'
    assert str(f('a{{b}}')) == 'a{b}'
    assert str(f('a{{}}c')) == 'a{}c'
    assert str(f('{{b}}c')) == '{b}c'
    assert str(f('a{{b}}c')) == 'a{b}c'
Exemple #9
0
 def test(self, extra):
     extra1 = '{},waiters:{}'.format(extra, len(self._waiters))
     extra2 = str(f('{extra},waiters:{len(self._waiters)}'))
     assert extra1 == extra2