Skip to content

Commit

Permalink
Add more format string tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svenklemm committed Nov 3, 2024
1 parent d4e16e9 commit 63fc6c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/pgspot/pg_catalog/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def parse_format_string(fmt_string):
ret = []
current_index = 1
for i in re.findall(
r"(%(([1-9])$)?[-]?([1-9][0-9]*|[*]|[*][1-9]$)?([sIL]))", fmt_string
r"(%(([1-9])[$])?[-]?([1-9][0-9]*|[*]|[*][1-9][$])?([sIL]))", fmt_string
):
# i[0] is the full match
# i[1] is the position specifier
Expand Down
35 changes: 28 additions & 7 deletions tests/format_string_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@

from pgspot.pg_catalog.format import parse_format_string

# %[position][flags][width]type


def test_no_variables():
ret = parse_format_string("")
assert ret == []
assert parse_format_string("") == []
assert parse_format_string("SELECT * FROM table") == []
assert parse_format_string("%%") == []


def test_single_variable():
assert parse_format_string("%s") == [("s", 1)]
assert parse_format_string("%I") == [("I", 1)]
assert parse_format_string("%L") == [("L", 1)]

assert parse_format_string("%%%s%%") == [("s", 1)]

assert parse_format_string("%1$s") == [("s", 1)]
assert parse_format_string("%3$s") == [("s", 3)]

assert parse_format_string("%-s") == [("s", 1)]
assert parse_format_string("%-10s") == [("s", 1)]
assert parse_format_string("%-*s") == [("s", 2)]
assert parse_format_string("%-*1$s") == [("s", 1)]

ret = parse_format_string("SELECT * FROM table")
assert ret == []
assert parse_format_string("%7$-s") == [("s", 7)]
assert parse_format_string("%7$-10s") == [("s", 7)]
assert parse_format_string("%7$-*s") == [("s", 7)]
assert parse_format_string("%7$-*1$s") == [("s", 7)]

ret = parse_format_string("%%")
assert ret == []

def test_multiple_variable():
assert parse_format_string("%s%I%L") == [("s", 1), ("I", 2), ("L", 3)]
assert parse_format_string("%3$s%2$I%1$L") == [("s", 3), ("I", 2), ("L", 1)]

0 comments on commit 63fc6c6

Please sign in to comment.