示例#1
0
def test_string():
    tools.eq_(
        Formatter.format_string('''a = 'b' '''),
        '''a = 'b'\n\n''',
    )

    formatter = Formatter()
    formatter.format_path('tests/samples/continuations.py')
def test_format_path():
    formatter = Formatter()
    file_contents = dict(get_contents('tests/samples/'))
    formatter.format_path('tests/samples/', recursive=True)
    for k, v in get_contents('tests/samples/'):
        check_or_revert(file_contents, k, v)

    formatter.format_path('tests/samples/generators.py')
示例#3
0
def test_list_comprehension():
    tools.eq_(
        Formatter.format_string('[x for x in range(5) if x]'),
        '[x for x in range(5) if x]\n\n',
    )
    tools.eq_(
        Formatter.format_string('[(x, y) for x, y in enumerate(range(5))]'),
        '[(x, y) for x, y in enumerate(range(5))]\n\n',
    )
示例#4
0
def test_list():
    tools.eq_(
        Formatter.format_string('[]'),
        '[]\n\n',
    )
    tools.eq_(
        Formatter.format_string('[0]'),
        '[0]\n\n',
    )
示例#5
0
def test_generator():
    tools.eq_(
        Formatter.format_string('(x for x in range(5) if x)'),
        '(x for x in range(5) if x)\n\n',
    )
    tools.eq_(
        Formatter.format_string('((x, y) for x, y in enumerate(range(5)))'),
        '((x, y) for x, y in enumerate(range(5)))\n\n',
    )
示例#6
0
def test_brace_simple():
    tools.eq_(
        Formatter.format_string('{1:2,}'),
        '{1: 2, }\n\n',
    )
    tools.eq_(
        Formatter.format_string('{}'),
        '{}\n\n',
    )
    tools.eq_(
        Formatter.format_string('{"a": {}}'),
        '''{'a': {}}\n\n''',
    )
示例#7
0
def test_paren_simple():
    tools.eq_(
        Formatter.format_string('(1,)'),
        '(1, )\n\n',
    )
    tools.eq_(
        Formatter.format_string('()'),
        '()\n\n',
    )
    tools.eq_(
        Formatter.format_string('(())'),
        '(())\n\n',
    )
示例#8
0
def test_stdin():
    fh = StringIO()
    formatter = Formatter()
    sys.stdin, stdin = fh, sys.stdin

    print(u"test = 123", file=fh)

    fh.seek(0)
    formatter("-"),
    fh.seek(0)
    formatter.format_file("-"),
    fh.seek(0)
    formatter(sys.stdin),

    filename = "tests/test_brace.py"
    test_brace_contents = open(filename, "r").read()
    formatter(filename)
    assert test_brace_contents == open(filename, "r").read()

    sys.stdin = stdin
示例#9
0
def test_stdin():
    fh = StringIO()
    formatter = Formatter()
    sys.stdin, stdin = fh, sys.stdin

    print(u'test = 123', file=fh)

    fh.seek(0)
    formatter('-'),
    fh.seek(0)
    formatter.format_file('-'),
    fh.seek(0)
    formatter(sys.stdin),

    filename = 'tests/test_brace.py'
    test_brace_contents = open(filename, 'r').read()
    formatter(filename)
    assert test_brace_contents == open(filename, 'r').read()

    sys.stdin = stdin
示例#10
0
def test_format_path():
    formatter = Formatter()
    file_contents = dict(get_contents('tests/samples/'))
    formatter.format_path('tests/samples/', recursive=True)
    for k, v in get_contents('tests/samples/'):
        check_or_revert(file_contents, k, v)

    formatter.format_path('tests/samples/generators.py')
示例#11
0
def test_stdin():
    fh = StringIO()
    formatter = Formatter()
    sys.stdin, stdin = fh, sys.stdin

    print(u'test = 123', file=fh)

    fh.seek(0)
    formatter('-'),
    fh.seek(0)
    formatter.format_file('-'),
    fh.seek(0)
    formatter(sys.stdin),

    filename = 'tests/test_brace.py'
    test_brace_contents = open(filename, 'r').read()
    formatter(filename)
    tools.eq_(
        test_brace_contents,
        open(filename, 'r').read(),
    )

    sys.stdin = stdin
示例#12
0
def test_slice():
    tools.eq_(
        Formatter.format_string('x[:]'),
        'x[:]\n\n',
    )
    tools.eq_(
        Formatter.format_string('x[1:]'),
        'x[1:]\n\n',
    )
    tools.eq_(
        Formatter.format_string('x[:1]'),
        'x[:1]\n\n',
    )
    tools.eq_(
        Formatter.format_string('x[1:1]'),
        'x[1:1]\n\n',
    )
    tools.eq_(
        Formatter.format_string('x[-1:1]'),
        'x[-1:1]\n\n',
    )
    tools.eq_(
        Formatter.format_string('x[1:-1]'),
        'x[1:-1]\n\n',
    )
    tools.eq_(
        Formatter.format_string('x[-1:-1]'),
        'x[-1:-1]\n\n',
    )
    tools.eq_(
        Formatter.format_string('x[-1:]'),
        'x[-1:]\n\n',
    )
    tools.eq_(
        Formatter.format_string('x[:-1]'),
        'x[:-1]\n\n',
    )
示例#13
0
def test_string_type():
    x = types.StringTokenType(-255, 'TEST')
    t = tokens.Token({(0, ''): None}, 0, '', (0, 0), (0, 0), '')
    x.preprocess(t)

    tools.eq_(
        Formatter.format_string("a = '''b'''"),
        '''a = 'b'\n\n''',
    )

    tools.eq_(
        Formatter.format_string('''a = """b'c"""'''),
        "a = '''b'c'''\n\n",
    )

    tools.eq_(
        Formatter.format_string('''a = "b'c"'''),
        "a = '''b'c'''\n\n",
    )

    tools.eq_(
        Formatter.format_string('a = """b"""'),
        '''a = 'b'\n\n''',
    )

    tools.eq_(
        Formatter.format_string('''a = """b'c"""'''),
        "a = '''b'c'''\n\n",
    )

    tools.eq_(
        Formatter.format_string('''a = """b\'\'\'c"""'''),
        'a = """b\'\'\'c"""\n\n',
    )

    tools.eq_(
        Formatter.format_string('''a = "b\'\'\'c"'''),
        'a = "b\'\'\'c"\n\n',
    )
示例#14
0
def test_paren_complex():
    tools.eq_(
        Formatter.format_string('((1,2,3,(4,5),6,(7,(8,))),)'),
        '((1, 2, 3, (4, 5), 6, (7, (8, ))), )\n\n',
    )
示例#15
0
def test_syntax_error():
    Formatter.format_string('a = '),
示例#16
0
def test_syntax_error():
    with pytest.raises(SyntaxError):
        Formatter.format_string('a = '),
示例#17
0
def test_paren(input_, expected):
    actual = Formatter.format_string(input_)
    assert actual == expected
示例#18
0
def test_paren(input_, expected):
    actual = Formatter.format_string(input_)
    assert actual == expected
示例#19
0
def test_brace_complex():
    tools.eq_(
        Formatter.format_string('''{1: {'a': 123, 'b': 'c'}}'''),
        '''{1: {'a': 123, 'b': 'c'}}\n\n''',
    )
示例#20
0
def test_assignment():
    assert Formatter.format_string('a = 1') == 'a = 1\n\n'
    assert Formatter.format_string('a != b') == 'a != b\n\n'
    assert Formatter.format_string('a % b') == 'a % b\n\n'
    assert Formatter.format_string('a %= b') == 'a %= b\n\n'
    assert Formatter.format_string('a & b') == 'a & b\n\n'
    assert Formatter.format_string('a &= b') == 'a &= b\n\n'
    assert Formatter.format_string('a * b') == 'a * b\n\n'
    assert Formatter.format_string('a ** b') == 'a ** b\n\n'
    assert Formatter.format_string('a **= b') == 'a **= b\n\n'
    assert Formatter.format_string('a *= b') == 'a *= b\n\n'
    assert Formatter.format_string('a + b') == 'a + b\n\n'
    assert Formatter.format_string('a += b') == 'a += b\n\n'
    assert Formatter.format_string('a - b') == 'a - b\n\n'
    assert Formatter.format_string('a -= b') == 'a -= b\n\n'
    assert Formatter.format_string('a / b') == 'a / b\n\n'
    assert Formatter.format_string('a // b') == 'a // b\n\n'
    assert Formatter.format_string('a //= b') == 'a //= b\n\n'
    assert Formatter.format_string('a /= b') == 'a /= b\n\n'
    assert Formatter.format_string('a < b') == 'a < b\n\n'
    assert Formatter.format_string('a << b') == 'a << b\n\n'
    assert Formatter.format_string('a <<= b') == 'a <<= b\n\n'
    assert Formatter.format_string('a <= b') == 'a <= b\n\n'
    assert Formatter.format_string('a == b') == 'a == b\n\n'
    assert Formatter.format_string('a > b') == 'a > b\n\n'
    assert Formatter.format_string('a >= b') == 'a >= b\n\n'
    assert Formatter.format_string('a >> b') == 'a >> b\n\n'
    assert Formatter.format_string('a >>= b') == 'a >>= b\n\n'
    assert Formatter.format_string('a ^ b') == 'a ^ b\n\n'
    assert Formatter.format_string('a ^= b') == 'a ^= b\n\n'
    assert Formatter.format_string('a is b') == 'a is b\n\n'
    assert Formatter.format_string('a is not b') == 'a is not b\n\n'
    assert Formatter.format_string('a | b') == 'a | b\n\n'
    assert Formatter.format_string('a |= b') == 'a |= b\n\n'
    assert Formatter.format_string('a = b') == 'a = b\n\n'
示例#21
0
def test_comments(input_, expected):
    actual = Formatter.format_string(input_)
    assert actual == expected
示例#22
0
def test_syntax_error():
    Formatter.format_string('a = '),