MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_roundtrip

Method test_roundtrip

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

Source from the content-addressed store, hash-verified

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