diff options
author | Jared Crapo <jared@kotfu.net> | 2017-08-21 20:39:21 -0600 |
---|---|---|
committer | Jared Crapo <jared@kotfu.net> | 2017-08-21 20:39:21 -0600 |
commit | 208fd69238c97a83c373343573e9f19c59124adc (patch) | |
tree | 5284ee48614a4d836ce0518244d01ab4bf52af9e | |
parent | d3766eb29f728b5e9d60645bbbac9062c683870e (diff) | |
download | cmd2-git-208fd69238c97a83c373343573e9f19c59124adc.tar.gz |
regex implementation complete. Still a few bugs.
-rw-r--r-- | docs/transcript.rst | 8 | ||||
-rw-r--r-- | examples/transcript_regex.txt | 7 | ||||
-rw-r--r-- | tests/test_transcript.py | 50 | ||||
-rw-r--r-- | tests/transcripts/from_cmdloop.txt | 10 | ||||
-rw-r--r-- | tests/transcripts/multiline_no_regex.txt | 4 | ||||
-rw-r--r-- | tests/transcripts/multiline_regex.txt | 4 | ||||
-rw-r--r-- | tests/transcripts/regex_set.txt | 5 | ||||
-rw-r--r-- | tests/transcripts/singleslash.txt | 2 | ||||
-rw-r--r-- | tests/transcripts/slashdot.txt | 2 | ||||
-rw-r--r-- | tests/transcripts/slashes_escaped.txt (renamed from tests/transcripts/slashslash_escaped.txt) | 3 | ||||
-rw-r--r-- | tests/transcripts/slashslash.txt | 2 | ||||
-rw-r--r-- | tests/transcripts/word_boundaries.txt | 4 |
12 files changed, 67 insertions, 34 deletions
diff --git a/docs/transcript.rst b/docs/transcript.rst index 9aa050b1..5445dcd1 100644 --- a/docs/transcript.rst +++ b/docs/transcript.rst @@ -94,6 +94,14 @@ match the path instead of specifying it verbatim, or we can escape the slashes:: (Cmd) say cd /usr/local/lib/python3.6/site-packages \/usr\/local\/lib\/python3.6\/site-packages +.. note:: + + Be careful with trailing spaces and newlines. Some terminal emulators strip + trailing space when you copy text from them. This could make the actual data + generated by your app different than the text you pasted into the + transcript, and it might not be readily obvious why the transcript is not + passing. + Running a transcript ==================== diff --git a/examples/transcript_regex.txt b/examples/transcript_regex.txt index a310224b..27b4c639 100644 --- a/examples/transcript_regex.txt +++ b/examples/transcript_regex.txt @@ -1,17 +1,18 @@ # 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: /.*/ +editor: /.*?/ feedback_to_output: False locals_in_py: True maxrepeats: 3 -prompt: (Cmd) +prompt: (Cmd)/ / quiet: False timing: False diff --git a/tests/test_transcript.py b/tests/test_transcript.py index caa5174d..12d539ea 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 @@ -21,6 +22,11 @@ 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'] @@ -61,6 +67,23 @@ class CmdLineApp(Cmd): 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")) @@ -108,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. @@ -251,9 +275,10 @@ def test_invalid_syntax(_cmdline_app, capsys): ('regex_set.txt', False), ('singleslash.txt', False), ('slashdot.txt', False), - ('slashslash_escaped.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 @@ -285,16 +310,17 @@ def test_transcript(request, capsys, filename, feedback_to_output): @pytest.mark.parametrize('expected, transformed', [ - ( 'text with no slashes', 'text\ with\ no\ slashes'), - ( '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\ \\/'), + ( 'text with no slashes', 'text\ with\ no\ slashes' ), + ( 'specials .*', 'specials\ \.\*' ), + ( '/.*/', '.*' ), + ( 'use 2\/3 cup', 'use\ 2\/3\ cup' ), + ( '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: we have a slashes in our regex: backslashed on input, bare on output - ( 'not \/, use /\/?/, not \/', 'not\ \\/\,\ use\ /?\,\ not\ \\/'), - ( 'the /\/?/ more /.*/ stuff', 'the\ /?\ more\ .*\ stuff') + ( 'not \/, use /\/?/, not \/', 'not\ \\/\,\ use\ /?\,\ not\ \\/' ), + ( 'the /\/?/ more /.*/ stuff', 'the\ /?\ more\ .*\ stuff' ), ]) def test_parse_transcript_expected(expected, transformed): app = CmdLineApp() diff --git a/tests/transcripts/from_cmdloop.txt b/tests/transcripts/from_cmdloop.txt index e95b9b39..649c8c4b 100644 --- a/tests/transcripts/from_cmdloop.txt +++ b/tests/transcripts/from_cmdloop.txt @@ -2,8 +2,9 @@ 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 +47,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 index 5fe9122c..72ad453b 100644 --- a/tests/transcripts/multiline_no_regex.txt +++ b/tests/transcripts/multiline_no_regex.txt @@ -5,6 +5,4 @@ (Cmd) orate This is a test > of the > emergency broadcast system -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 index c568bd37..e2a36762 100644 --- a/tests/transcripts/multiline_regex.txt +++ b/tests/transcripts/multiline_regex.txt @@ -1,6 +1,4 @@ (Cmd) say -r 3 -s yabba dabba do /\A(YA.*?DO\n?){3}/ (Cmd) say -r 5 -s yabba dabba do -YABBA DABBA DO -YABBA /[A-Z ]*?/ -DO +/\A([A-Z\s]*$){3}/ diff --git a/tests/transcripts/regex_set.txt b/tests/transcripts/regex_set.txt index a310224b..da515f31 100644 --- a/tests/transcripts/regex_set.txt +++ b/tests/transcripts/regex_set.txt @@ -1,17 +1,18 @@ # 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 index 00d0ba21..dfa06bc1 100644 --- a/tests/transcripts/singleslash.txt +++ b/tests/transcripts/singleslash.txt @@ -1,2 +1,2 @@ (Cmd) say use 2/3 cup of sugar -use 2/3 cup of sugar +use 2\/3 cup of sugar diff --git a/tests/transcripts/slashdot.txt b/tests/transcripts/slashdot.txt index 83b5870e..0c1ec747 100644 --- a/tests/transcripts/slashdot.txt +++ b/tests/transcripts/slashdot.txt @@ -1,2 +1,2 @@ (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 +mix 2\/3 c. sugar, 1\/2 c. butter, and 1\/2 tsp. salt diff --git a/tests/transcripts/slashslash_escaped.txt b/tests/transcripts/slashes_escaped.txt index 3bed129d..f21556fe 100644 --- a/tests/transcripts/slashslash_escaped.txt +++ b/tests/transcripts/slashes_escaped.txt @@ -1,5 +1,2 @@ -(Cmd) say // -\/\/ (Cmd) say /some/unix/path \/some\/unix\/path - diff --git a/tests/transcripts/slashslash.txt b/tests/transcripts/slashslash.txt index a6d37456..706f1079 100644 --- a/tests/transcripts/slashslash.txt +++ b/tests/transcripts/slashslash.txt @@ -1,2 +1,2 @@ (Cmd) say // -// +\/\/ diff --git a/tests/transcripts/word_boundaries.txt b/tests/transcripts/word_boundaries.txt new file mode 100644 index 00000000..0f9f2166 --- /dev/null +++ b/tests/transcripts/word_boundaries.txt @@ -0,0 +1,4 @@ +(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.*/ |