MCPcopy Index your code
hub / github.com/python/cpython / test_roundtrip

Method test_roundtrip

Lib/test/test_tokenize.py:2048–2145  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2046 self.assertEqual(tok.string, tok.line[tok.start[1]: tok.end[1]])
2047
2048 def test_roundtrip(self):
2049 # There are some standard formatting practices that are easy to get right.
2050
2051 self.check_roundtrip("if x == 1:\n"
2052 " print(x)\n")
2053 self.check_roundtrip("# This is a comment\n"
2054 "# This also\n")
2055
2056 # Some people use different formatting conventions, which makes
2057 # untokenize a little trickier. Note that this test involves trailing
2058 # whitespace after the colon. Note that we use hex escapes to make the
2059 # two trailing blanks apparent in the expected output.
2060
2061 self.check_roundtrip("if x == 1 : \n"
2062 " print(x)\n")
2063 fn = support.findfile("tokenize_tests.txt", subdir="tokenizedata")
2064 with open(fn, 'rb') as f:
2065 self.check_roundtrip(f)
2066 self.check_roundtrip("if x == 1:\n"
2067 " # A comment by itself.\n"
2068 " print(x) # Comment here, too.\n"
2069 " # Another comment.\n"
2070 "after_if = True\n")
2071 self.check_roundtrip("if (x # The comments need to go in the right place\n"
2072 " == 1):\n"
2073 " print('x==1')\n")
2074 self.check_roundtrip("class Test: # A comment here\n"
2075 " # A comment with weird indent\n"
2076 " after_com = 5\n"
2077 " def x(m): return m*5 # a one liner\n"
2078 " def y(m): # A whitespace after the colon\n"
2079 " return y*4 # 3-space indent\n")
2080
2081 # Some error-handling code
2082 self.check_roundtrip("try: import somemodule\n"
2083 "except ImportError: # comment\n"
2084 " print('Can not import' # comment2\n)"
2085 "else: print('Loaded')\n")
2086
2087 self.check_roundtrip("f'\\N{EXCLAMATION MARK}'")
2088 self.check_roundtrip(r"f'\\N{SNAKE}'")
2089 self.check_roundtrip(r"f'\\N{{SNAKE}}'")
2090 self.check_roundtrip(r"f'\N{SNAKE}'")
2091 self.check_roundtrip(r"f'\\\N{SNAKE}'")
2092 self.check_roundtrip(r"f'\\\\\N{SNAKE}'")
2093 self.check_roundtrip(r"f'\\\\\\\N{SNAKE}'")
2094
2095 self.check_roundtrip(r"f'\\N{1}'")
2096 self.check_roundtrip(r"f'\\\\N{2}'")
2097 self.check_roundtrip(r"f'\\\\\\N{3}'")
2098 self.check_roundtrip(r"f'\\\\\\\\N{4}'")
2099
2100 self.check_roundtrip(r"f'\\N{{'")
2101 self.check_roundtrip(r"f'\\\\N{{'")
2102 self.check_roundtrip(r"f'\\\\\\N{{'")
2103 self.check_roundtrip(r"f'\\\\\\\\N{{'")
2104
2105 self.check_roundtrip(r"f'\n{{foo}}'")

Callers

nothing calls this directly

Calls 2

check_roundtripMethod · 0.95
openFunction · 0.50

Tested by

no test coverage detected