Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_indented():
s_expected = ''' a = f"my string {var}"'''
s_out, count = process.fstringify_code_by_line(indented)
assert count == 1
assert s_out.split("\n")[2] == s_expected
def test_empty_line():
s_expected = """ attrs = {'r': f'{row_idx}'}"""
s_out, count = process.fstringify_code_by_line(code_empty_line)
assert count == 1
assert s_out.split("\n")[2] == s_expected
def test_two_strings():
s = (
'a = "my string {}, but also {} and {}".format(var, f, cada_bra)\n'
+ 'b = "my string {}, but also {} and {}".format(var, what, cada_bra)'
)
chunks_gen = split.get_fstringify_chunks(s)
assert len(list(chunks_gen)) == 2
generator = split.get_fstringify_chunks(s)
lines = s.split("\n")
chunk = next(generator)
assert chunk.start_line == 0
assert lines[0][: chunk.end_idx] == lines[0]
chunk = next(generator)
assert chunk.start_line == 1
assert lines[1][: chunk.end_idx] == lines[1]
def test_empty_line():
code_empty_line = """
def write_row(self, xf, row, row_idx):
attrs = {'r': '{}'.format(row_idx)}""".strip()
generator = split.get_fstringify_chunks(code_empty_line)
lines = code_empty_line.split("\n")
chunk = next(generator)
assert chunk.start_line == 2
assert lines[2][chunk.start_idx : chunk.end_idx] == "'{}'.format(row_idx)"
def test_two_strings():
s = (
'a = "my string {}, but also {} and {}".format(var, f, cada_bra)\n'
+ 'b = "my string {}, but also {} and {}".format(var, what, cada_bra)'
)
chunks_gen = split.get_fstringify_chunks(s)
assert len(list(chunks_gen)) == 2
generator = split.get_fstringify_chunks(s)
lines = s.split("\n")
chunk = next(generator)
assert chunk.start_line == 0
assert lines[0][: chunk.end_idx] == lines[0]
chunk = next(generator)
assert chunk.start_line == 1
assert lines[1][: chunk.end_idx] == lines[1]
def test_chain_fmt_3():
s_in = """a = "Hello {}".format(d["a{}".format( d["a{}".format(key) ]) ] )"""
s_out, count = process.fstringify_code_by_line(s_in)
assert count == 0
def test_invalid_conversion():
s_in = """a = 'my string {}, but also {!b} and {!a}'.format(var, f, cada_bra)"""
s_expected = s_in
s_out, count = process.fstringify_code_by_line(s_in)
assert s_out == s_expected
def test_percent_tuple():
s_in = """print("%s %s " % (var+var, abc))"""
s_expected = """print(f"{var + var} {abc} ")"""
s_out, count = process.fstringify_code_by_line(s_in)
assert s_out == s_expected
def test_legacy_fmtspec(aggressive):
s_in = """d = '%i' % var"""
s_expected = """d = f'{int(var)}'"""
out, count = process.fstringify_code_by_line(s_in)
assert out == s_expected
def test_noqa_other():
s_in = """a = '%s\\n' % var # noqa: W731, flynt"""
s_expected = """a = '%s\\n' % var # noqa: W731, flynt"""
s_out, count = process.fstringify_code_by_line(s_in)
assert s_out == s_expected