Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Commit

Permalink
Add tests for actionbase.py (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
ensonic authored and drigz committed May 18, 2017
1 parent facdd5f commit 4169573
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tests/test_actor_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

'''Test the action base classes.'''

import unittest

import actionbase


class TestAction(object):

def __init__(self):
self.voice_command = None

def run(self, voice_command):
self.voice_command = voice_command


class TestKeywordHandler(unittest.TestCase):

def test_keyword_phrases(self):
phrases = actionbase.KeywordHandler('FooBar', None).get_phrases()
self.assertEqual(phrases, ['foobar'])

def test_handle_keyword_with_mixed_case(self):
action = TestAction()
actionbase.KeywordHandler('FooBar', action).handle('foobar')
self.assertEqual(action.voice_command, 'foobar')

def test_handle_keyword_in_command(self):
action = TestAction()
actionbase.KeywordHandler('FooBar', action).handle('frobnicate the foobar')
self.assertEqual(action.voice_command, 'frobnicate the foobar')


class TestActor(unittest.TestCase):

def test_empty_actor_has_no_phrases(self):
phrases = actionbase.Actor().get_phrases()
self.assertEqual(phrases, [])

def test_empty_actor_does_not_handle_commands(self):
self.assertFalse(actionbase.Actor().handle('foobar'))

def test_actor_runs_action(self):
actor = actionbase.Actor()
actor.add_keyword('foo', TestAction())
self.assertTrue(actor.handle('moo foo'))

def test_actor_runs_matching_action(self):
actor = actionbase.Actor()
foo_action = TestAction()
actor.add_keyword('foo', foo_action)
bar_action = TestAction()
actor.add_keyword('bar', bar_action)
self.assertTrue(actor.handle('moo bar'))
self.assertIsNone(foo_action.voice_command)
self.assertIsNotNone(bar_action.voice_command)


if __name__ == '__main__':
unittest.main()

0 comments on commit 4169573

Please sign in to comment.