Skip to content

Commit e991c20

Browse files
committed
Added some unit tests to cover new code
1 parent 2aa1d9b commit e991c20

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

tests/test_cmd2.py

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,29 @@ def test_edit_file_with_spaces(base_app, request, monkeypatch):
812812
# We think we have an editor, so should expect a system call
813813
m.assert_called_once_with('"{}" "{}"'.format(base_app.editor, filename))
814814

815-
def test_edit_number(base_app, monkeypatch):
815+
def test_edit_blank(base_app, monkeypatch):
816+
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
817+
base_app.editor = 'fooedit'
818+
819+
# Mock out the os.system call so we don't actually open an editor
820+
m = mock.MagicMock(name='system')
821+
monkeypatch.setattr("os.system", m)
822+
823+
# Run help command just so we have a command in history
824+
run_cmd(base_app, 'help')
825+
826+
run_cmd(base_app, 'edit')
827+
828+
# We have an editor, so should expect a system call
829+
m.assert_called_once()
830+
831+
def test_edit_empty_history(base_app, capsys):
832+
run_cmd(base_app, 'edit')
833+
out, err = capsys.readouterr()
834+
assert out == ''
835+
assert err == 'ERROR: edit must be called with argument if history is empty\n'
836+
837+
def test_edit_valid_positive_number(base_app, monkeypatch):
816838
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
817839
base_app.editor = 'fooedit'
818840

@@ -828,7 +850,7 @@ def test_edit_number(base_app, monkeypatch):
828850
# We have an editor, so should expect a system call
829851
m.assert_called_once()
830852

831-
def test_edit_blank(base_app, monkeypatch):
853+
def test_edit_valid_negative_number(base_app, monkeypatch):
832854
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
833855
base_app.editor = 'fooedit'
834856

@@ -839,16 +861,42 @@ def test_edit_blank(base_app, monkeypatch):
839861
# Run help command just so we have a command in history
840862
run_cmd(base_app, 'help')
841863

842-
run_cmd(base_app, 'edit')
864+
run_cmd(base_app, 'edit "-1"')
843865

844866
# We have an editor, so should expect a system call
845867
m.assert_called_once()
846868

847-
def test_edit_empty_history(base_app, capsys):
848-
run_cmd(base_app, 'edit')
849-
out, err = capsys.readouterr()
850-
assert out == ''
851-
assert err == 'ERROR: edit must be called with argument if history is empty\n'
869+
def test_edit_invalid_positive_number(base_app, monkeypatch):
870+
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
871+
base_app.editor = 'fooedit'
872+
873+
# Mock out the os.system call so we don't actually open an editor
874+
m = mock.MagicMock(name='system')
875+
monkeypatch.setattr("os.system", m)
876+
877+
# Run help command just so we have a command in history
878+
run_cmd(base_app, 'help')
879+
880+
run_cmd(base_app, 'edit 23')
881+
882+
# History index is invalid, so should expect a system call
883+
m.assert_not_called()
884+
885+
def test_edit_invalid_negative_number(base_app, monkeypatch):
886+
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
887+
base_app.editor = 'fooedit'
888+
889+
# Mock out the os.system call so we don't actually open an editor
890+
m = mock.MagicMock(name='system')
891+
monkeypatch.setattr("os.system", m)
892+
893+
# Run help command just so we have a command in history
894+
run_cmd(base_app, 'help')
895+
896+
run_cmd(base_app, 'edit "-23"')
897+
898+
# History index is invalid, so should expect a system call
899+
m.assert_not_called()
852900

853901

854902
def test_base_py_interactive(base_app):

0 commit comments

Comments
 (0)