From 63fc6c6a32200101fc18c022e08b33097619df4b Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Sun, 3 Nov 2024 22:12:28 +0100 Subject: [PATCH] Add more format string tests --- src/pgspot/pg_catalog/format.py | 2 +- tests/format_string_test.py | 35 ++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/pgspot/pg_catalog/format.py b/src/pgspot/pg_catalog/format.py index dda9063..32052b4 100644 --- a/src/pgspot/pg_catalog/format.py +++ b/src/pgspot/pg_catalog/format.py @@ -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 diff --git a/tests/format_string_test.py b/tests/format_string_test.py index 005e041..8155e69 100644 --- a/tests/format_string_test.py +++ b/tests/format_string_test.py @@ -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)]