From 87de997a78bf5afba50a4ba9b8e5a4094d5bd555 Mon Sep 17 00:00:00 2001 From: Mike Naberezny Date: Fri, 21 Aug 2009 20:49:26 -0700 Subject: [PATCH] Command shortcuts now work when the command has no args. --- src/py65/monitor.py | 28 ++++++++++++++------------ src/py65/tests/test_monitor.py | 36 +++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/py65/monitor.py b/src/py65/monitor.py index f44bd6b..2c2f129 100644 --- a/src/py65/monitor.py +++ b/src/py65/monitor.py @@ -68,28 +68,30 @@ class Monitor(cmd.Cmd): 'z': 'step'} def _preprocess_line(self, line): - # ignore leading dots - line = line.lstrip('.') + # line comments + quoted = False + for pos, char in enumerate(line): + if char in ('"', "'"): + quoted = not quoted + if (not quoted) and (char == ';'): + line = line[:pos] + break + + # whitepsace & leading dots + line = line.strip(' \t').lstrip('.') # command shortcuts for shortcut, command in self._shortcuts.iteritems(): + if line == shortcut: + line = command + break + pattern = '^%s\s+' % re.escape(shortcut) matches = re.match(pattern, line) if matches: start, end = matches.span() line = "%s %s" % (command, line[end:]) - - # line comments - quoted = False - for pos in range(0, len(line)): - if line[pos] in ('"', "'"): - quoted = not quoted - if (not quoted) and (line[pos] == ';'): - line = line[:pos] break - - if line == 'a': - line = 'assemble' return line diff --git a/src/py65/tests/test_monitor.py b/src/py65/tests/test_monitor.py index 6858f5a..2e8ec3c 100644 --- a/src/py65/tests/test_monitor.py +++ b/src/py65/tests/test_monitor.py @@ -9,13 +9,35 @@ class MonitorTests(unittest.TestCase): # line processing - def test_preprocess_line_removes_leading_dots(self): - stdout = StringIO() - mon = Monitor(stdout=stdout) - mon.onecmd('...help') - - out = stdout.getvalue() - self.assert_('Documented commands' in out) + def test__preprocess_line_removes_leading_dots_after_whitespace(self): + mon = Monitor() + self.assertEqual('help', mon._preprocess_line(' ...help')) + + def test__preprocess_line_removes_leading_and_trailing_whitespace(self): + mon = Monitor() + self.assertEqual('help', mon._preprocess_line(' \t help \t ')) + + def test__preprocess_line_rewrites_shortcut_when_alone_on_line(self): + mon = Monitor() + self.assertEqual('assemble', mon._preprocess_line(' a')) + + def test__preprocess_line_rewrites_shortcut_with_arguments_on_line(self): + mon = Monitor() + self.assertEqual('assemble c000', mon._preprocess_line('a c000')) + + def test__preprocess_line_removes_semicolon_comments(self): + mon = Monitor() + self.assertEqual('assemble', mon._preprocess_line('a ;comment')) + + def test__preprocess_line_does_not_remove_semicolons_in_quotes(self): + mon = Monitor() + self.assertEqual('assemble lda #$";"', + mon._preprocess_line('a lda #$";" ;comment')) + + def test__preprocess_line_does_not_remove_semicolons_in_apostrophes(self): + mon = Monitor() + self.assertEqual("assemble lda #$';'", + mon._preprocess_line("assemble lda #$';' ;comment")) # assemble