|
7 | 7 | """ |
8 | 8 | import os |
9 | 9 | import sys |
| 10 | +import random |
10 | 11 |
|
11 | 12 | import mock |
12 | 13 | import pytest |
|
21 | 22 |
|
22 | 23 |
|
23 | 24 | class CmdLineApp(Cmd): |
| 25 | + |
| 26 | + MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh'] |
| 27 | + MUMBLE_FIRST = ['so', 'like', 'well'] |
| 28 | + MUMBLE_LAST = ['right?'] |
| 29 | + |
24 | 30 | def __init__(self, *args, **kwargs): |
25 | 31 | self.abbrev = True |
26 | 32 | self.multilineCommands = ['orate'] |
@@ -61,6 +67,23 @@ def do_speak(self, arg, opts=None): |
61 | 67 | do_say = do_speak # now "say" is a synonym for "speak" |
62 | 68 | do_orate = do_speak # another synonym, but this one takes multi-line input |
63 | 69 |
|
| 70 | + @options([ make_option('-r', '--repeat', type="int", help="output [n] times") ]) |
| 71 | + def do_mumble(self, arg, opts=None): |
| 72 | + """Mumbles what you tell me to.""" |
| 73 | + repetitions = opts.repeat or 1 |
| 74 | + arg = arg.split() |
| 75 | + for i in range(min(repetitions, self.maxrepeats)): |
| 76 | + output = [] |
| 77 | + if (random.random() < .33): |
| 78 | + output.append(random.choice(self.MUMBLE_FIRST)) |
| 79 | + for word in arg: |
| 80 | + if (random.random() < .40): |
| 81 | + output.append(random.choice(self.MUMBLES)) |
| 82 | + output.append(word) |
| 83 | + if (random.random() < .25): |
| 84 | + output.append(random.choice(self.MUMBLE_LAST)) |
| 85 | + self.poutput(' '.join(output)) |
| 86 | + |
64 | 87 |
|
65 | 88 | class DemoApp(Cmd): |
66 | 89 | @options(make_option('-n', '--name', action="store", help="your name")) |
@@ -108,8 +131,9 @@ def test_base_with_transcript(_cmdline_app): |
108 | 131 |
|
109 | 132 | Documented commands (type help <topic>): |
110 | 133 | ======================================== |
111 | | -_relative_load edit history orate pyscript run say shell show |
112 | | -cmdenvironment help load py quit save set shortcuts speak |
| 134 | +_relative_load help mumble pyscript save shell speak |
| 135 | +cmdenvironment history orate quit say shortcuts |
| 136 | +edit load py run set show |
113 | 137 |
|
114 | 138 | (Cmd) help say |
115 | 139 | Repeats what you tell me to. |
@@ -251,9 +275,10 @@ def test_invalid_syntax(_cmdline_app, capsys): |
251 | 275 | ('regex_set.txt', False), |
252 | 276 | ('singleslash.txt', False), |
253 | 277 | ('slashdot.txt', False), |
254 | | - ('slashslash_escaped.txt', False), |
| 278 | + ('slashes_escaped.txt', False), |
255 | 279 | ('slashslash.txt', False), |
256 | 280 | ('spaces.txt', False), |
| 281 | + ('word_boundaries.txt', False), |
257 | 282 | ]) |
258 | 283 | def test_transcript(request, capsys, filename, feedback_to_output): |
259 | 284 | # Create a cmd2.Cmd() instance and make sure basic settings are |
@@ -285,16 +310,17 @@ def test_transcript(request, capsys, filename, feedback_to_output): |
285 | 310 |
|
286 | 311 |
|
287 | 312 | @pytest.mark.parametrize('expected, transformed', [ |
288 | | - ( 'text with no slashes', 'text\ with\ no\ slashes'), |
289 | | - ( 'specials .*', 'specials\ \.\*'), |
290 | | - ( '/.*/', '.*'), |
291 | | - ( 'specials ^ and + /[0-9]+/', 'specials\ \^\ and\ \+\ [0-9]+'), |
292 | | - ( '/a{6}/ but not a{6} with /.*?/ more', 'a{6}\ but\ not\ a\{6\}\ with\ .*?\ more'), |
293 | | - ( 'not this slash\/ or this one\/', 'not\ this\ slash\\/\ or\ this\ one\\/'), |
294 | | - ( 'not \/, use /\|?/, not \/', 'not\ \\/\,\ use\ \|?\,\ not\ \\/'), |
| 313 | + ( 'text with no slashes', 'text\ with\ no\ slashes' ), |
| 314 | + ( 'specials .*', 'specials\ \.\*' ), |
| 315 | + ( '/.*/', '.*' ), |
| 316 | + ( 'use 2\/3 cup', 'use\ 2\/3\ cup' ), |
| 317 | + ( 'specials ^ and + /[0-9]+/', 'specials\ \^\ and\ \+\ [0-9]+' ), |
| 318 | + ( '/a{6}/ but not a{6} with /.*?/ more', 'a{6}\ but\ not\ a\{6\}\ with\ .*?\ more' ), |
| 319 | + ( 'not this slash\/ or this one\/', 'not\ this\ slash\\/\ or\ this\ one\\/' ), |
| 320 | + ( 'not \/, use /\|?/, not \/', 'not\ \\/\,\ use\ \|?\,\ not\ \\/' ), |
295 | 321 | # inception: we have a slashes in our regex: backslashed on input, bare on output |
296 | | - ( 'not \/, use /\/?/, not \/', 'not\ \\/\,\ use\ /?\,\ not\ \\/'), |
297 | | - ( 'the /\/?/ more /.*/ stuff', 'the\ /?\ more\ .*\ stuff') |
| 322 | + ( 'not \/, use /\/?/, not \/', 'not\ \\/\,\ use\ /?\,\ not\ \\/' ), |
| 323 | + ( 'the /\/?/ more /.*/ stuff', 'the\ /?\ more\ .*\ stuff' ), |
298 | 324 | ]) |
299 | 325 | def test_parse_transcript_expected(expected, transformed): |
300 | 326 | app = CmdLineApp() |
|
0 commit comments