summaryrefslogtreecommitdiff
path: root/distbuild/sm_tests.py
blob: 59b9c023765af12c4ab320ec0f6d692214940cd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# distbuild/sm_tests.py -- unit tests for state machine abstraction
#
# Copyright (C) 2012, 2014  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA..


import unittest

import distbuild


class DummyEventSource(object):

    pass
    
    
class DummyEvent(object):

    pass


class StateMachineTests(unittest.TestCase):

    def setUp(self):
        self.sm = distbuild.StateMachine('init')
        self.sm.distbuild = None
        self.sm.setup()
        self.event_source = DummyEventSource()
        self.event = DummyEvent()
        self.event_sources = []
        self.events = []
        self.callback_result = None
    
    def callback(self, event_source, event):
        self.event_sources.append(event_source)
        self.events.append(event)
        return self.callback_result
    
    def test_ignores_event_when_there_are_no_transitions(self):
        new_events = self.sm.handle_event(self.event_source, self.event)
        self.assertEqual(new_events, [])
        self.assertEqual(self.event_sources, [])
        self.assertEqual(self.events, [])

    def test_ignores_event_when_no_transition_matches(self):
        spec = [
            ('init', self.event_source, str, 'init', self.callback),
        ]
        self.sm.add_transitions(spec)
        new_events = self.sm.handle_event(self.event_source, self.event)
        self.assertEqual(new_events, [])
        self.assertEqual(self.event_sources, [])
        self.assertEqual(self.events, [])

    def test_handles_lack_of_callback_ok(self):
        spec = [
            ('init', self.event_source, DummyEvent, 'init', None),
        ]
        self.sm.add_transitions(spec)
        new_events = self.sm.handle_event(self.event_source, self.event)
        self.assertEqual(new_events, [])
        self.assertEqual(self.event_sources, [])
        self.assertEqual(self.events, [])

    def test_calls_registered_callback_for_right_event(self):
        spec = [
            ('init', self.event_source, DummyEvent, 'init', self.callback),
        ]
        self.sm.add_transitions(spec)
        new_events = self.sm.handle_event(self.event_source, self.event)
        self.assertEqual(new_events, [])
        self.assertEqual(self.event_sources, [self.event_source])
        self.assertEqual(self.events, [self.event])

    def test_handle_converts_nonlist_to_list(self):
        self.callback_result = ('foo', 'bar')

        spec = [
            ('init', self.event_source, DummyEvent, 'init', self.callback),
        ]
        self.sm.add_transitions(spec)
        new_events = self.sm.handle_event(self.event_source, self.event)
        self.assertEqual(new_events, ['foo', 'bar'])
        self.assertEqual(self.event_sources, [self.event_source])
        self.assertEqual(self.events, [self.event])