From 1f429593cab708e2cd75270c33aa7fa8602e715f Mon Sep 17 00:00:00 2001 From: Paul Querna Date: 2013年9月23日 22:38:23 -0700 Subject: [PATCH] Add tests for event emitter class --- teeth_agent/tests/__init__.py | 3 + teeth_agent/tests/test_events.py | 103 +++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 teeth_agent/tests/__init__.py create mode 100644 teeth_agent/tests/test_events.py diff --git a/teeth_agent/tests/__init__.py b/teeth_agent/tests/__init__.py new file mode 100644 index 000000000..6ee7616a8 --- /dev/null +++ b/teeth_agent/tests/__init__.py @@ -0,0 +1,3 @@ +""" +Unit Tests for the Teeth Agent. +""" diff --git a/teeth_agent/tests/test_events.py b/teeth_agent/tests/test_events.py new file mode 100644 index 000000000..5ce13f7f5 --- /dev/null +++ b/teeth_agent/tests/test_events.py @@ -0,0 +1,103 @@ +""" +Copyright 2013 Rackspace, 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. +""" + +from twisted.trial import unittest +from teeth_agent.events import EventEmitter, EventEmitterUnhandledError + + +class EventEmitterTest(unittest.TestCase): + """Event Emitter tests.""" + + def setUp(self): + self.ee = EventEmitter() + + def tearDown(self): + del self.ee + + def test_empty_emit(self): + self.ee.emit("nothing.here", "some args") + self.ee.emit("nothing.here2") + + def test_single_event(self): + self.count = 0 + + def got_it(topic): + self.assertEqual(topic, "test") + self.count += 1 + + self.ee.on("test", got_it) + + self.ee.emit("test") + + self.ee.emit("other_test") + + self.assertEqual(self.count, 1) + + def test_multicb(self): + self.count = 0 + + def got_it(topic): + self.assertEqual(topic, "test") + self.count += 1 + + self.ee.on("test", got_it) + + self.ee.on("test", got_it) + + self.ee.emit("test") + + self.assertEqual(self.count, 2) + + def test_once(self): + self.count = 0 + + def got_it(topic): + self.assertEqual(topic, "test") + self.count += 1 + + self.ee.once("test", got_it) + + self.ee.emit("test") + + self.ee.emit("test") + + self.assertEqual(self.count, 1) + + def test_removeAllListeners(self): + self.count = 0 + + def got_it(topic): + self.assertEqual(topic, "test") + self.count += 1 + + self.ee.on("test", got_it) + + self.ee.emit("test") + + self.ee.removeAllListeners("test") + + self.ee.emit("test") + + self.assertEqual(self.count, 1) + + def test_error(self): + self.count = 0 + + try: + self.ee.emit("error") + except EventEmitterUnhandledError: + self.count += 1 + self.assertEqual(self.count, 1)

AltStyle によって変換されたページ (->オリジナル) /