diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/multiline_transcript.txt | 10 | ||||
-rw-r--r-- | tests/test_transcript.py | 148 | ||||
-rw-r--r-- | tests/transcripts/bol_eol.txt | 6 | ||||
-rw-r--r-- | tests/transcripts/characterclass.txt | 6 | ||||
-rw-r--r-- | tests/transcripts/dotstar.txt | 4 | ||||
-rw-r--r-- | tests/transcripts/extension_notation.txt | 4 | ||||
-rw-r--r-- | tests/transcripts/from_cmdloop.txt (renamed from tests/transcript.txt) | 13 | ||||
-rw-r--r-- | tests/transcripts/multiline_no_regex.txt | 6 | ||||
-rw-r--r-- | tests/transcripts/multiline_regex.txt | 6 | ||||
-rw-r--r-- | tests/transcripts/regex_set.txt (renamed from tests/transcript_regex.txt) | 6 | ||||
-rw-r--r-- | tests/transcripts/singleslash.txt | 5 | ||||
-rw-r--r-- | tests/transcripts/slashes_escaped.txt | 6 | ||||
-rw-r--r-- | tests/transcripts/slashslash.txt | 4 | ||||
-rw-r--r-- | tests/transcripts/spaces.txt | 8 | ||||
-rw-r--r-- | tests/transcripts/word_boundaries.txt | 6 |
15 files changed, 155 insertions, 83 deletions
diff --git a/tests/multiline_transcript.txt b/tests/multiline_transcript.txt deleted file mode 100644 index 5fe9122c..00000000 --- a/tests/multiline_transcript.txt +++ /dev/null @@ -1,10 +0,0 @@ -# cmd2 will skip any lines -# which occur before a valid -# command prompt - -(Cmd) orate This is a test -> of the -> emergency broadcast system -This is a test -of the -emergency broadcast system diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 2400066e..5092a2cd 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -7,6 +7,7 @@ Released under MIT license, see LICENSE file """ import os import sys +import random import mock import pytest @@ -15,11 +16,17 @@ import six # Used for sm.input: raw_input() for Python 2 or input() for Python 3 import six.moves as sm -from cmd2 import Cmd, make_option, options, Cmd2TestCase, set_use_arg_list, set_posix_shlex, set_strip_quotes +from cmd2 import (Cmd, make_option, options, Cmd2TestCase, set_use_arg_list, + set_posix_shlex, set_strip_quotes) from conftest import run_cmd, StdOut, normalize class CmdLineApp(Cmd): + + MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh'] + MUMBLE_FIRST = ['so', 'like', 'well'] + MUMBLE_LAST = ['right?'] + def __init__(self, *args, **kwargs): self.abbrev = True self.multilineCommands = ['orate'] @@ -47,19 +54,36 @@ class CmdLineApp(Cmd): """Repeats what you tell me to.""" arg = ''.join(arg) if opts.piglatin: - arg = '%s%say' % (arg[1:].rstrip(), arg[0]) + arg = '%s%say' % (arg[1:], arg[0]) if opts.shout: arg = arg.upper() repetitions = opts.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): - self.stdout.write(arg) - self.stdout.write('\n') - # self.stdout.write is better than "print", because Cmd can be - # initialized with a non-standard output destination + self.poutput(arg) + # recommend using the poutput function instead of + # self.stdout.write or "print", because Cmd allows the user + # to redirect output do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input + @options([ make_option('-r', '--repeat', type="int", help="output [n] times") ]) + def do_mumble(self, arg, opts=None): + """Mumbles what you tell me to.""" + repetitions = opts.repeat or 1 + arg = arg.split() + for i in range(min(repetitions, self.maxrepeats)): + output = [] + if (random.random() < .33): + output.append(random.choice(self.MUMBLE_FIRST)) + for word in arg: + if (random.random() < .40): + output.append(random.choice(self.MUMBLES)) + output.append(word) + if (random.random() < .25): + output.append(random.choice(self.MUMBLE_LAST)) + self.poutput(' '.join(output)) + class DemoApp(Cmd): @options(make_option('-n', '--name', action="store", help="your name")) @@ -107,8 +131,9 @@ def test_base_with_transcript(_cmdline_app): Documented commands (type help <topic>): ======================================== -_relative_load edit history orate pyscript run say shell show -cmdenvironment help load py quit save set shortcuts speak +_relative_load help mumble pyscript save shell speak +cmdenvironment history orate quit say shortcuts +edit load py run set show (Cmd) help say Repeats what you tell me to. @@ -232,60 +257,6 @@ def test_commands_at_invocation(): out = app.stdout.buffer assert out == expected - -def test_transcript_from_cmdloop(request, capsys): - # Create a cmd2.Cmd() instance and make sure basic settings are like we want for test - app = CmdLineApp() - app.feedback_to_output = True - - # Get location of the transcript - test_dir = os.path.dirname(request.module.__file__) - transcript_file = os.path.join(test_dir, 'transcript.txt') - - # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args - testargs = ['prog', '-t', transcript_file] - with mock.patch.object(sys, 'argv', testargs): - # Run the command loop - app.cmdloop() - - # Check for the unittest "OK" condition for the 1 test which ran - expected_start = ".\n----------------------------------------------------------------------\nRan 1 test in" - expected_end = "s\n\nOK\n" - out, err = capsys.readouterr() - if six.PY3: - assert err.startswith(expected_start) - assert err.endswith(expected_end) - else: - assert err == '' - assert out == '' - - -def test_multiline_command_transcript_with_comments_at_beginning(request, capsys): - # Create a cmd2.Cmd() instance and make sure basic settings are like we want for test - app = CmdLineApp() - - # Get location of the transcript - test_dir = os.path.dirname(request.module.__file__) - transcript_file = os.path.join(test_dir, 'multiline_transcript.txt') - - # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args - testargs = ['prog', '-t', transcript_file] - with mock.patch.object(sys, 'argv', testargs): - # Run the command loop - app.cmdloop() - - # Check for the unittest "OK" condition for the 1 test which ran - expected_start = ".\n----------------------------------------------------------------------\nRan 1 test in" - expected_end = "s\n\nOK\n" - out, err = capsys.readouterr() - if six.PY3: - assert err.startswith(expected_start) - assert err.endswith(expected_end) - else: - assert err == '' - assert out == '' - - def test_invalid_syntax(_cmdline_app, capsys): run_cmd(_cmdline_app, 'speak "') out, err = capsys.readouterr() @@ -293,15 +264,33 @@ def test_invalid_syntax(_cmdline_app, capsys): assert normalize(str(err)) == expected -def test_regex_transcript(request, capsys): - # Create a cmd2.Cmd() instance and make sure basic settings are like we want for test +@pytest.mark.parametrize('filename, feedback_to_output', [ + ('bol_eol.txt', False), + ('characterclass.txt', False), + ('dotstar.txt', False), + ('extension_notation.txt', False), + ('from_cmdloop.txt', True), + ('multiline_no_regex.txt', False), + ('multiline_regex.txt', False), + ('regex_set.txt', False), + ('singleslash.txt', False), + ('slashes_escaped.txt', False), + ('slashslash.txt', False), + ('spaces.txt', False), + ('word_boundaries.txt', False), + ]) +def test_transcript(request, capsys, filename, feedback_to_output): + # Create a cmd2.Cmd() instance and make sure basic settings are + # like we want for test app = CmdLineApp() + app.feedback_to_output = feedback_to_output # Get location of the transcript test_dir = os.path.dirname(request.module.__file__) - transcript_file = os.path.join(test_dir, 'transcript_regex.txt') + transcript_file = os.path.join(test_dir, 'transcripts', filename) - # Need to patch sys.argv so cmd2 doesn't think it was called with arguments equal to the py.test args + # Need to patch sys.argv so cmd2 doesn't think it was called with + # arguments equal to the py.test args testargs = ['prog', '-t', transcript_file] with mock.patch.object(sys, 'argv', testargs): # Run the command loop @@ -317,3 +306,30 @@ def test_regex_transcript(request, capsys): else: assert err == '' assert out == '' + + +@pytest.mark.parametrize('expected, transformed', [ + ( 'text with no slashes', 'text\ with\ no\ slashes' ), + # stuff with just one slash + ( 'use 2/3 cup', 'use\ 2\/3\ cup' ), + ( '/tmp is nice', '\/tmp\ is\ nice'), + ( 'slash at end/', 'slash\ at\ end\/'), + # regexes + ( 'specials .*', 'specials\ \.\*' ), + ( '/.*/', '.*' ), + ( 'specials ^ and + /[0-9]+/', 'specials\ \^\ and\ \+\ [0-9]+' ), + ( '/a{6}/ but not \/a{6} with /.*?/ more', 'a{6}\ but\ not\ \/a\{6\}\ with\ .*?\ more' ), + ( 'not this slash\/ or this one\/', 'not\ this\ slash\\/\ or\ this\ one\\/' ), + ( 'not \/, use /\|?/, not \/', 'not\ \\/\,\ use\ \|?\,\ not\ \\/' ), + # inception: slashes in our regex. backslashed on input, bare on output + ( 'not \/, use /\/?/, not \/', 'not\ \\/\,\ use\ /?\,\ not\ \\/' ), + ( 'the /\/?/ more /.*/ stuff', 'the\ /?\ more\ .*\ stuff' ), + ]) +def test_parse_transcript_expected(expected, transformed): + app = CmdLineApp() + + class TestMyAppCase(Cmd2TestCase): + cmdapp = app + + testcase = TestMyAppCase() + assert testcase._transform_transcript_expected(expected) == transformed diff --git a/tests/transcripts/bol_eol.txt b/tests/transcripts/bol_eol.txt new file mode 100644 index 00000000..da21ac86 --- /dev/null +++ b/tests/transcripts/bol_eol.txt @@ -0,0 +1,6 @@ +# match the text with regular expressions and the newlines as literal text + +(Cmd) say -r 3 -s yabba dabba do +/^Y.*?$/ +/^Y.*?$/ +/^Y.*?$/ diff --git a/tests/transcripts/characterclass.txt b/tests/transcripts/characterclass.txt new file mode 100644 index 00000000..756044ea --- /dev/null +++ b/tests/transcripts/characterclass.txt @@ -0,0 +1,6 @@ +# match using character classes and special sequence for digits (\d) + +(Cmd) say 555-1212 +/[0-9]{3}-[0-9]{4}/ +(Cmd) say 555-1212 +/\d{3}-\d{4}/ diff --git a/tests/transcripts/dotstar.txt b/tests/transcripts/dotstar.txt new file mode 100644 index 00000000..55c15b75 --- /dev/null +++ b/tests/transcripts/dotstar.txt @@ -0,0 +1,4 @@ +# ensure the old standby .* works. We use the non-greedy flavor + +(Cmd) say Adopt the pace of nature: her secret is patience. +Adopt the pace of /.*?/ is patience. diff --git a/tests/transcripts/extension_notation.txt b/tests/transcripts/extension_notation.txt new file mode 100644 index 00000000..68e728ca --- /dev/null +++ b/tests/transcripts/extension_notation.txt @@ -0,0 +1,4 @@ +# inception: a regular expression that matches itself + +(Cmd) say (?:fred) +/(?:\(\?:fred\))/ diff --git a/tests/transcript.txt b/tests/transcripts/from_cmdloop.txt index e95b9b39..5a12ca03 100644 --- a/tests/transcript.txt +++ b/tests/transcripts/from_cmdloop.txt @@ -1,9 +1,13 @@ +# responses with trailing spaces have been matched with a regex +# so you can see where they are. + (Cmd) help Documented commands (type help <topic>): ======================================== -_relative_load edit history orate pyscript run say shell show -cmdenvironment help load py quit save set shortcuts speak +_relative_load help mumble pyscript save shell speak +cmdenvironment history orate quit say shortcuts +edit load py run set show/ */ (Cmd) help say Repeats what you tell me to. @@ -46,12 +50,11 @@ set maxrepeats 5 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: ---> +prompt - was: (Cmd)/ / +now: --->/ / diff --git a/tests/transcripts/multiline_no_regex.txt b/tests/transcripts/multiline_no_regex.txt new file mode 100644 index 00000000..490870cf --- /dev/null +++ b/tests/transcripts/multiline_no_regex.txt @@ -0,0 +1,6 @@ +# test a multi-line command + +(Cmd) orate This is a test +> of the +> emergency broadcast system +This is a test of the emergency broadcast system diff --git a/tests/transcripts/multiline_regex.txt b/tests/transcripts/multiline_regex.txt new file mode 100644 index 00000000..3487335f --- /dev/null +++ b/tests/transcripts/multiline_regex.txt @@ -0,0 +1,6 @@ +# these regular expressions match multiple lines of text + +(Cmd) say -r 3 -s yabba dabba do +/\A(YA.*?DO\n?){3}/ +(Cmd) say -r 5 -s yabba dabba do +/\A([A-Z\s]*$){3}/ diff --git a/tests/transcript_regex.txt b/tests/transcripts/regex_set.txt index a310224b..3a4a234d 100644 --- a/tests/transcript_regex.txt +++ b/tests/transcripts/regex_set.txt @@ -1,17 +1,19 @@ # Run this transcript with "python example.py -t transcript_regex.txt" # The regex for colors is because no color on Windows. # The regex for editor will match whatever program you use. +# Regexes on prompts just make the trailing space obvious + (Cmd) set abbrev: True autorun_on_edit: False colors: /(True|False)/ -continuation_prompt: > +continuation_prompt: >/ / debug: False echo: False editor: /.*/ feedback_to_output: False locals_in_py: True maxrepeats: 3 -prompt: (Cmd) +prompt: (Cmd)/ / quiet: False timing: False diff --git a/tests/transcripts/singleslash.txt b/tests/transcripts/singleslash.txt new file mode 100644 index 00000000..f3b291f9 --- /dev/null +++ b/tests/transcripts/singleslash.txt @@ -0,0 +1,5 @@ +# even if you only have a single slash, you have +# to escape it + +(Cmd) say use 2/3 cup of sugar +use 2\/3 cup of sugar diff --git a/tests/transcripts/slashes_escaped.txt b/tests/transcripts/slashes_escaped.txt new file mode 100644 index 00000000..09bbe3bb --- /dev/null +++ b/tests/transcripts/slashes_escaped.txt @@ -0,0 +1,6 @@ +# escape those slashes + +(Cmd) say /some/unix/path +\/some\/unix\/path +(Cmd) say mix 2/3 c. sugar, 1/2 c. butter, and 1/2 tsp. salt +mix 2\/3 c. sugar, 1\/2 c. butter, and 1\/2 tsp. salt diff --git a/tests/transcripts/slashslash.txt b/tests/transcripts/slashslash.txt new file mode 100644 index 00000000..2504b0ba --- /dev/null +++ b/tests/transcripts/slashslash.txt @@ -0,0 +1,4 @@ +# ensure consecutive slashes are parsed correctly + +(Cmd) say // +\/\/ diff --git a/tests/transcripts/spaces.txt b/tests/transcripts/spaces.txt new file mode 100644 index 00000000..615fcbd7 --- /dev/null +++ b/tests/transcripts/spaces.txt @@ -0,0 +1,8 @@ +# check spaces in all their forms + +(Cmd) say how many spaces +how many spaces +(Cmd) say how many spaces +how/\s{1}/many/\s{1}/spaces +(Cmd) say "how many spaces" +how/\s+/many/\s+/spaces diff --git a/tests/transcripts/word_boundaries.txt b/tests/transcripts/word_boundaries.txt new file mode 100644 index 00000000..e79cfc4f --- /dev/null +++ b/tests/transcripts/word_boundaries.txt @@ -0,0 +1,6 @@ +# use word boundaries to check for key words in the output + +(Cmd) mumble maybe we could go to lunch +/.*\bmaybe\b.*\bcould\b.*\blunch\b.*/ +(Cmd) mumble maybe we could go to lunch +/.*\bmaybe\b.*\bcould\b.*\blunch\b.*/ |