gitcmds: "globals to locals" micro-optimization
[git-cola.git] / test / test_cola_models_observable.py
blobcf5e404504f98e1faf6713631e49d76d7382cec2
1 #!/usr/bin/env python
2 import unittest
3 import copy
5 from cola.models.observable import ObservableModel
7 class ConcreteModel(ObservableModel):
8 """A concrete model for testing."""
9 message_copy = 'copy'
10 def copy(self, *args, **opts):
11 """Send notification from the model."""
12 self.notify_message_observers(self.message_copy, *args, **opts)
14 class ConcreteObserver(object):
15 """A concrete observer for testing."""
16 def __init__(self, model):
17 self.args = None
18 self.opts = None
19 model.add_message_observer(ConcreteModel.message_copy,
20 self.receive_message)
22 def reset(self):
23 """Reset test data back to a known good state."""
24 self.args = None
25 self.opts = None
27 def receive_message(self, *args, **opts):
28 """Receive notification from the model."""
29 self.args = args
30 self.opts = opts
33 class ObservableModelTestCase(unittest.TestCase):
34 """Tests the ObservableModel class."""
35 def setUp(self):
36 self.model = ConcreteModel()
37 self.observer = ConcreteObserver(self.model)
39 def test_notify_message_observers(self):
40 """Test that notifications are delivered."""
41 self.model.copy(hello='world')
42 self.assertEqual(self.observer.args, (),)
43 self.assertEqual(self.observer.opts, {'hello': 'world'})
45 def test_observing_multiple_models(self):
46 """Test that we can attach to multiple models."""
47 self.model.copy(self.model, foo='bar')
48 self.assertEqual(self.observer.args, (self.model,))
49 self.assertEqual(self.observer.opts, {'foo': 'bar'})
51 model = ConcreteModel()
52 model.add_message_observer(model.message_copy,
53 self.observer.receive_message)
54 model.copy(model, baz='quux')
55 self.assertEqual(self.observer.args, (model,))
56 self.assertEqual(self.observer.opts, {'baz': 'quux'})
58 self.model.copy(foo='bar')
59 self.assertEqual(self.observer.args, (),)
60 self.assertEqual(self.observer.opts, {'foo': 'bar'})
63 if __name__ == '__main__':
64 unittest.main()