Skip to content

Commit

Permalink
speak entire sentence on period; superclass AudioAppTest
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbro committed Jan 13, 2014
1 parent 7db7dda commit 2b03db8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
31 changes: 22 additions & 9 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@
from text_editor import TextEditor


class TextEditorTest(unittest.TestCase):
class AudioAppTest(unittest.TestCase):
def setUp(self):
self.outfile = tempfile.NamedTemporaryFile()
self.args = ['text_editor.py', self.outfile.name]
self.app = TextEditor(self.args)

# Override the sound-generating methods with silent recordkeeping.
self.utterances = []
def instead_of_sounds(phrase, a=0, b=0, c=0, d=0, e=0):
if type(phrase) is str:
self.utterances.append(phrase)
self.utterances.append(phrase.lower())
else:
self.utterances.append('[tone]')
self.app.speak = instead_of_sounds
Expand All @@ -30,18 +26,31 @@ def tearDown(self):
#print repr(self.utterances)
pass

def assertJustSaid(self, phrase):
if self.utterances[-1] != phrase.lower():
raise AssertionError(
'last said %r, not %r' % (self.utterances[-1], phrase))


class TextEditorTest(AudioAppTest):
def setUp(self):
self.outfile = tempfile.NamedTemporaryFile()
self.args = ['text_editor.py', self.outfile.name]
self.app = TextEditor(self.args)
super(TextEditorTest, self).setUp()

def test_simple(self):
# should speak previous word after non-alpha character
self.app.simulate_typing('hello world ')
self.assertEqual(self.utterances[-1], 'world')
self.assertJustSaid('world')

# should speak character just deleted
self.app.simulate_keystroke(curses.ascii.DEL)
self.assertEqual(self.utterances[-1], 'space')
self.assertJustSaid('space')

# should play tone when attempting to move cursor beyond bounds
self.app.simulate_keystroke(curses.KEY_RIGHT)
self.assertEqual(self.utterances[-1], '[tone]')
self.assertJustSaid('[tone]')

for i in range(5): # move cursor five characters left
self.app.simulate_keystroke(curses.KEY_LEFT)
Expand Down Expand Up @@ -74,5 +83,9 @@ def test_gibberish(self):

self.assertEqual(n, len(self.outfile.read()))

def test_sentence(self):
self.app.simulate_typing('Hello there. This is a second sentence.')
self.assertJustSaid('this is a second sentence')

if __name__ == '__main__':
unittest.main()
18 changes: 17 additions & 1 deletion text_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ def last_word(self, ending_at=None):

return self.buffer[start:ending_at + 1].strip()

def last_sentence(self, ending_at=None):
if ending_at is None:
ending_at = self.cursor - 1

if self.cursor < 2:
return self.buffer[0]

start = ending_at - 1
while self.buffer[start] != '.' and start > 0:
start -= 1

return self.buffer[start:ending_at + 1].strip('. ')

def close(self):
if hasattr(self, 'fd'):
self.fd.write(self.buffer) # write changes to disk
Expand All @@ -99,7 +112,10 @@ def handle_key(self, key):

elif curses.ascii.isprint(key) or curses.ascii.isspace(key):
self.insert_char(chr(key))
self.speak(self.last_word())
if chr(key) == '.':
self.speak(self.last_sentence())
else:
self.speak(self.last_word())
#sys.stderr.write(self.buffer + '\r')

return True # keep reading keystrokes
Expand Down

0 comments on commit 2b03db8

Please sign in to comment.