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')
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', )
def test_list(): tools.eq_( Formatter.format_string('[]'), '[]\n\n', ) tools.eq_( Formatter.format_string('[0]'), '[0]\n\n', )
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', )
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''', )
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', )
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
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
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
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', )
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', )
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', )
def test_syntax_error(): Formatter.format_string('a = '),
def test_syntax_error(): with pytest.raises(SyntaxError): Formatter.format_string('a = '),
def test_paren(input_, expected): actual = Formatter.format_string(input_) assert actual == expected
def test_brace_complex(): tools.eq_( Formatter.format_string('''{1: {'a': 123, 'b': 'c'}}'''), '''{1: {'a': 123, 'b': 'c'}}\n\n''', )
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'
def test_comments(input_, expected): actual = Formatter.format_string(input_) assert actual == expected