summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-23 23:59:53 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-23 23:59:53 -0400
commit7f6dad1daa3626598a38a16ac373b9a8f4e472da (patch)
treece8036bd915451f6993c13e5fc41775d42e8ced7
parent8ed685e54d897b00666581a07f0b889588a28bb1 (diff)
downloadcmd2-git-7f6dad1daa3626598a38a16ac373b9a8f4e472da.tar.gz
Removed restriction on macros named after non-multiline commands
Added unit tests
-rw-r--r--cmd2/cmd2.py7
-rw-r--r--cmd2/parsing.py6
-rw-r--r--tests/test_cmd2.py39
3 files changed, 41 insertions, 11 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 79e7aad8..f2da6726 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -1988,8 +1988,7 @@ class Cmd(cmd.Cmd):
suffix=statement.suffix,
pipe_to=statement.pipe_to,
output=statement.output,
- output_to=statement.output_to,
- )
+ output_to=statement.output_to)
return statement
def _resolve_macro(self, statement: Statement) -> Optional[str]:
@@ -2516,8 +2515,8 @@ class Cmd(cmd.Cmd):
self.perror("Invalid macro name: {}".format(errmsg))
return
- if args.name in self.get_all_commands():
- self.perror("Macro cannot have the same name as a command")
+ if args.name in self.statement_parser.multiline_commands:
+ self.perror("Macro cannot have the same name as a multiline command")
return
if args.name in self.aliases:
diff --git a/cmd2/parsing.py b/cmd2/parsing.py
index 5c0bec06..dfa248e9 100644
--- a/cmd2/parsing.py
+++ b/cmd2/parsing.py
@@ -529,8 +529,7 @@ class StatementParser:
suffix=suffix,
pipe_to=pipe_to,
output=output,
- output_to=output_to,
- )
+ output_to=output_to)
return statement
def parse_command_only(self, rawinput: str, *, expand: bool = True) -> Statement:
@@ -595,8 +594,7 @@ class StatementParser:
statement = Statement(args,
raw=rawinput,
command=command,
- multiline_command=multiline_command,
- )
+ multiline_command=multiline_command)
return statement
def get_command_arg_list(self, command_name: str, to_parse: Union[Statement, str],
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 3f8c43b7..1f63e234 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -1662,9 +1662,12 @@ def test_macro_create_with_alias_name(base_app):
out, err = run_cmd(base_app, 'macro create {} help'.format(macro))
assert "Macro cannot have the same name as an alias" in err[0]
-def test_macro_create_with_command_name(base_app):
- out, err = run_cmd(base_app, 'macro create help stuff')
- assert "Macro cannot have the same name as a command" in err[0]
+def test_macro_create_with_command_name(multiline_app):
+ out, err = run_cmd(multiline_app, 'macro create help stuff')
+ assert out == normalize("Macro 'help' created")
+
+ out, err = run_cmd(multiline_app, 'macro create orate stuff')
+ assert "Macro cannot have the same name as a multiline command" in err[0]
def test_macro_create_with_args(base_app):
# Create the macro
@@ -1783,6 +1786,36 @@ def test_nonexistent_macro(base_app):
assert exception is not None
+def test_input_line_to_statement_expand(base_app):
+ # Enable/Disable expansion of shortcuts
+ line = '!ls'
+ statement = base_app._input_line_to_statement(line, expand=True)
+ assert statement.command == 'shell'
+
+ statement = base_app._input_line_to_statement(line, expand=False)
+ assert statement.command == '!ls'
+
+ # Enable/Disable expansion of aliases
+ run_cmd(base_app, 'alias create help macro')
+
+ line = 'help'
+ statement = base_app._input_line_to_statement(line, expand=True)
+ assert statement.command == 'macro'
+
+ statement = base_app._input_line_to_statement(line, expand=False)
+ assert statement.command == 'help'
+
+ run_cmd(base_app, 'alias delete help')
+
+ # Enable/Disable expansion of macros
+ run_cmd(base_app, 'macro create help alias')
+
+ line = 'help'
+ statement = base_app._input_line_to_statement(line, expand=True)
+ assert statement.command == 'alias'
+
+ statement = base_app._input_line_to_statement(line, expand=False)
+ assert statement.command == 'help'
def test_ppaged(outsim_app):
msg = 'testing...'