summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-01-30 09:44:00 -0500
committerGitHub <noreply@github.com>2017-01-30 09:44:00 -0500
commit97bde4901070048a676bd8acff5d492fd587c58c (patch)
tree015ef2bf82f0d61ce3a9961a89833ded1bd5ef7c
parent1383b0dd66b740a03545c57c05bb8810a5d18d80 (diff)
parent1fb1733049614bda8c3fa59c57c547319c149623 (diff)
downloadcmd2-git-97bde4901070048a676bd8acff5d492fd587c58c.tar.gz
Merge pull request #34 from tleonhardt/unit_test
Unit test improvements
-rw-r--r--tests/test_cmd2.py81
-rw-r--r--tests/test_transcript.py61
-rw-r--r--tests/transcript.txt62
3 files changed, 193 insertions, 11 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 194892f7..ad9aeed7 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -1,10 +1,11 @@
# coding=utf-8
-#
-# Cmd2 unit/functional testing
-#
-# Copyright 2016 Federico Ceratto <federico.ceratto@gmail.com>
-# Released under MIT license, see LICENSE file
+"""
+Cmd2 unit/functional testing
+Copyright 2016 Federico Ceratto <federico.ceratto@gmail.com>
+Released under MIT license, see LICENSE file
+"""
+import os
import mock
from conftest import run_cmd, _normalize
from six import StringIO
@@ -180,3 +181,73 @@ def test_base_load(base_app):
assert m.called
m.assert_called_once_with('myfname')
# TODO: Figure out how to check stdout or stderr during a do_load()
+
+
+def test_base_cmdenvironment(base_app):
+ out = run_cmd(base_app, 'cmdenvironment')
+ expected = _normalize("""
+
+ Commands are not case-sensitive.
+ Commands may be terminated with: [';']
+""")
+ assert out[:2] == expected[:2]
+ assert out[2].strip().startswith('Settable parameters: ')
+
+ # Settable parameters can be listed in any order, so need to validate carefully using unordered sets
+ settable_params = set(['continuation_prompt', 'default_file_name', 'prompt', 'abbrev', 'quiet',
+ 'case_insensitive', 'colors', 'echo', 'timing', 'editor',
+ 'feedback_to_output', 'debug'])
+ out_params = set(out[2].split("Settable parameters: ")[1].split())
+
+ assert settable_params == out_params
+
+
+def test_base_save(base_app, capsys):
+ # TODO: Use a temporary directory for the file
+ filename = 'deleteme.txt'
+ run_cmd(base_app, 'help')
+ run_cmd(base_app, 'help save')
+ run_cmd(base_app, 'save * {}'.format(filename))
+ out, err = capsys.readouterr()
+ assert out == 'Saved to {}\n'.format(filename)
+
+ expected = _normalize("""
+help
+
+help save
+
+save * deleteme.txt
+""")
+
+ with open(filename) as f:
+ content = _normalize(f.read())
+
+ assert content == expected
+
+ # Delete file that was created
+ os.remove(filename)
+
+
+def test_output_redirection(base_app):
+ # TODO: Use a temporary directory/file for this file
+ filename = 'out.txt'
+ run_cmd(base_app, 'help > {}'.format(filename))
+ expected = _normalize("""
+Documented commands (type help <topic>):
+========================================
+_load ed history list py save shortcuts
+_relative_load edit l load r set show
+cmdenvironment hi li pause run shell
+
+Undocumented commands:
+======================
+EOF eof exit help q quit
+""")
+
+ with open(filename) as f:
+ content = _normalize(f.read())
+
+ assert content == expected
+
+ # Delete file that was created
+ os.remove(filename)
diff --git a/tests/test_transcript.py b/tests/test_transcript.py
index d7e40295..01451b95 100644
--- a/tests/test_transcript.py
+++ b/tests/test_transcript.py
@@ -1,13 +1,14 @@
# coding=utf-8
-#
-# Cmd2 functional testing based on transcript
-#
-# Copyright 2016 Federico Ceratto <federico.ceratto@gmail.com>
-# Released under MIT license, see LICENSE file
+"""
+Cmd2 functional testing based on transcript
+
+Copyright 2016 Federico Ceratto <federico.ceratto@gmail.com>
+Released under MIT license, see LICENSE file
+"""
import pytest
-from cmd2 import Cmd, make_option, options
+from cmd2 import Cmd, make_option, options, Cmd2TestCase
from conftest import run_cmd, StdOut, _normalize
@@ -134,3 +135,51 @@ now: --->
for cmd, expected in _get_transcript_blocks(transcript):
out = run_cmd(app, cmd)
assert out == expected
+
+
+class TestMyAppCase(Cmd2TestCase):
+ CmdApp = CmdLineApp
+ CmdApp.testfiles = ['tests/transcript.txt']
+
+
+def test_optparser(_cmdline_app, capsys):
+ run_cmd(_cmdline_app, 'say -h')
+ out, err = capsys.readouterr()
+ expected = _normalize("""
+Repeats what you tell me to.
+Usage: speak [options] (text to say)
+
+Options:
+ -h, --help show this help message and exit
+ -p, --piglatin atinLay
+ -s, --shout N00B EMULATION MODE
+ -r REPEAT, --repeat=REPEAT
+ output [n] times""")
+ # NOTE: For some reason this extra cast to str is required for Python 2.7 but not 3.x
+ assert _normalize(str(out)) == expected
+
+
+def test_optparser_nosuchoption(_cmdline_app, capsys):
+ run_cmd(_cmdline_app, 'say -a')
+ out, err = capsys.readouterr()
+ expected = _normalize("""
+no such option: -a
+Repeats what you tell me to.
+Usage: speak [options] (text to say)
+
+Options:
+ -h, --help show this help message and exit
+ -p, --piglatin atinLay
+ -s, --shout N00B EMULATION MODE
+ -r REPEAT, --repeat=REPEAT
+ output [n] times""")
+ assert _normalize(str(out)) == expected
+
+
+def test_comment_stripping(_cmdline_app):
+ out = run_cmd(_cmdline_app, 'speak it was /* not */ delicious! # Yuck!')
+ expected = _normalize("""it was delicious!""")
+ assert out == expected
+
+
+
diff --git a/tests/transcript.txt b/tests/transcript.txt
new file mode 100644
index 00000000..91f27dff
--- /dev/null
+++ b/tests/transcript.txt
@@ -0,0 +1,62 @@
+(Cmd) help
+
+Documented commands (type help <topic>):
+========================================
+_load ed history list pause run set show
+_relative_load edit l load py save shell speak
+cmdenvironment hi li orate r say shortcuts
+
+Undocumented commands:
+======================
+EOF eof exit help q quit
+
+(Cmd) help say
+Repeats what you tell me to.
+Usage: speak [options] (text to say)
+
+Options:
+ -h, --help show this help message and exit
+ -p, --piglatin atinLay
+ -s, --shout N00B EMULATION MODE
+ -r REPEAT, --repeat=REPEAT
+ output [n] times
+
+(Cmd) say goodnight, Gracie
+goodnight, Gracie
+(Cmd) say -ps --repeat=5 goodnight, Gracie
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+(Cmd) set maxrepeats 5
+maxrepeats - was: 3
+now: 5
+(Cmd) say -ps --repeat=5 goodnight, Gracie
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+(Cmd) hi
+-------------------------[1]
+help
+-------------------------[2]
+help say
+-------------------------[3]
+say goodnight, Gracie
+-------------------------[4]
+say -ps --repeat=5 goodnight, Gracie
+-------------------------[5]
+set maxrepeats 5
+-------------------------[6]
+say -ps --repeat=5 goodnight, Gracie
+(Cmd) run 4
+say -ps --repeat=5 goodnight, Gracie
+
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+(Cmd) set prompt "---> "
+prompt - was: (Cmd)
+now: --->