summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd2/parsing.py6
-rw-r--r--tests/test_parsing.py93
2 files changed, 97 insertions, 2 deletions
diff --git a/cmd2/parsing.py b/cmd2/parsing.py
index 696904ff..e1d7b76b 100644
--- a/cmd2/parsing.py
+++ b/cmd2/parsing.py
@@ -30,11 +30,13 @@ class MacroArg:
is_escaped = attr.ib(validator=attr.validators.instance_of(bool), type=bool)
# Pattern used to find normal argument
+ # Digits surrounded by exactly 1 brace on a side and 1 or more braces on the opposite side
# Match strings like: {5}, {{{{{4}, {2}}}}}
macro_normal_arg_pattern = re.compile(r'(?<!\{)\{\d+\}|\{\d+\}(?!\})')
- # Pattern used to find escaped arguments (2 or more braces on each side of digit)
- # Match strings like: {{5}}, {{{{{4}}, {{2}}}}}, {{{4}}}
+ # Pattern used to find escaped arguments
+ # Digits surrounded by 2 or more braces on both sides
+ # Match strings like: {{5}}, {{{{{4}}, {{2}}}}}
macro_escaped_arg_pattern = re.compile(r'\{{2}\d+\}{2}')
# Finds a string of digits
diff --git a/tests/test_parsing.py b/tests/test_parsing.py
index 9cf9429a..15da9b7a 100644
--- a/tests/test_parsing.py
+++ b/tests/test_parsing.py
@@ -754,3 +754,96 @@ def test_statement_is_immutable():
statement.args = 'bar'
with pytest.raises(attr.exceptions.FrozenInstanceError):
statement.raw = 'baz'
+
+
+def test_macro_normal_arg_pattern():
+ # This pattern matches digits surrounded by exactly 1 brace on a side and 1 or more braces on the opposite side
+ from cmd2.parsing import MacroArg
+ pattern = MacroArg.macro_normal_arg_pattern
+
+ # Valid strings
+ matches = pattern.findall('{5}')
+ assert matches == ['{5}']
+
+ matches = pattern.findall('{233}')
+ assert matches == ['{233}']
+
+ matches = pattern.findall('{{{{{4}')
+ assert matches == ['{4}']
+
+ matches = pattern.findall('{2}}}}}')
+ assert matches == ['{2}']
+
+ matches = pattern.findall('{3}{4}{5}')
+ assert matches == ['{3}', '{4}', '{5}']
+
+ matches = pattern.findall('{3} {4} {5}')
+ assert matches == ['{3}', '{4}', '{5}']
+
+ matches = pattern.findall('{3} {{{4} {5}}}}')
+ assert matches == ['{3}', '{4}', '{5}']
+
+ matches = pattern.findall('{3} text {4} stuff {5}}}}')
+ assert matches == ['{3}', '{4}', '{5}']
+
+ # Invalid strings
+ matches = pattern.findall('5')
+ assert not matches
+
+ matches = pattern.findall('{5')
+ assert not matches
+
+ matches = pattern.findall('5}')
+ assert not matches
+
+ matches = pattern.findall('{{5}}')
+ assert not matches
+
+ matches = pattern.findall('{5text}')
+ assert not matches
+
+def test_macro_escaped_arg_pattern():
+ # This pattern matches digits surrounded by 2 or more braces on both sides
+ from cmd2.parsing import MacroArg
+ pattern = MacroArg.macro_escaped_arg_pattern
+
+ # Valid strings
+ matches = pattern.findall('{{5}}')
+ assert matches == ['{{5}}']
+
+ matches = pattern.findall('{{233}}')
+ assert matches == ['{{233}}']
+
+ matches = pattern.findall('{{{{{4}}')
+ assert matches == ['{{4}}']
+
+ matches = pattern.findall('{{2}}}}}')
+ assert matches == ['{{2}}']
+
+ matches = pattern.findall('{{3}}{{4}}{{5}}')
+ assert matches == ['{{3}}', '{{4}}', '{{5}}']
+
+ matches = pattern.findall('{{3}} {{4}} {{5}}')
+ assert matches == ['{{3}}', '{{4}}', '{{5}}']
+
+ matches = pattern.findall('{{3}} {{{4}} {{5}}}}')
+ assert matches == ['{{3}}', '{{4}}', '{{5}}']
+
+ matches = pattern.findall('{{3}} text {{4}} stuff {{5}}}}')
+ assert matches == ['{{3}}', '{{4}}', '{{5}}']
+
+ # Invalid strings
+ matches = pattern.findall('5')
+ assert not matches
+
+ matches = pattern.findall('{{5')
+ assert not matches
+
+ matches = pattern.findall('5}}')
+ assert not matches
+
+ matches = pattern.findall('{5}')
+ assert not matches
+
+ matches = pattern.findall('{{5text}}')
+ assert not matches