Skip to content

Commit

Permalink
Merge pull request #34 from tleonhardt/unit_test
Browse files Browse the repository at this point in the history
Unit test improvements
  • Loading branch information
tleonhardt committed Jan 30, 2017
2 parents 1383b0d + 1fb1733 commit 97bde49
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 11 deletions.
81 changes: 76 additions & 5 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
61 changes: 55 additions & 6 deletions tests/test_transcript.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -134,3 +135,51 @@ def test_base_with_transcript(_cmdline_app):
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



62 changes: 62 additions & 0 deletions tests/transcript.txt
Original file line number Diff line number Diff line change
@@ -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: --->

0 comments on commit 97bde49

Please sign in to comment.