~didrocks/ubuntuone-client/dont-suffer-zg-crash

« back to all changes in this revision

Viewing changes to tests/syncdaemon/fsm/test_fsm_run.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2009-06-30 12:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090630120000-by806ovmw3193qe8
Tags: upstream-0.90.3
ImportĀ upstreamĀ versionĀ 0.90.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ubuntuone.syncdaemon.fsm.tests.test_fsm_run
 
2
#
 
3
# Author: Lucio Torre <lucio.torre@canonical.com>
 
4
#
 
5
# Copyright 2009 Canonical Ltd.
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
# PURPOSE.  See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
"""
 
19
tests for running fsms
 
20
"""
 
21
 
 
22
import unittest
 
23
import os
 
24
 
 
25
from ubuntuone.syncdaemon.fsm import fsm
 
26
 
 
27
def p(name):
 
28
    """make a full path from here."""
 
29
    if "HAS_OOFICE" in os.environ:
 
30
        return os.path.join(os.path.dirname(__file__), name+".ods")
 
31
    else:
 
32
        return os.path.join(os.path.dirname(__file__), name+".py")
 
33
 
 
34
class TestRun(unittest.TestCase):
 
35
    'Test fsm running'
 
36
 
 
37
    def test_hello(self):
 
38
        'test running a hello world machine'
 
39
        f = fsm.StateMachine(p("test_run_hello"))
 
40
        f.validate()
 
41
 
 
42
        result = []
 
43
        def make(out, outstates):
 
44
            "make action_func functions"
 
45
            def maker(self, event, params):
 
46
                "inner"
 
47
                result.append(out)
 
48
                self.state = outstates[int(params["MV1"])-1]
 
49
            return maker
 
50
 
 
51
        class HelloRunner(fsm.StateMachineRunner):
 
52
            "our implementation of the runner"
 
53
            state = "H"
 
54
            H = make("h", "EEE")
 
55
            E = make("e", "LLL")
 
56
            L = make("l", "LOD")
 
57
            O = make("o", "WRR")
 
58
            W = make("w", "OOO")
 
59
            R = make("r", "LLL")
 
60
            D = make("d", ["NL"]*3)
 
61
            newline = make("\n", "HHH")
 
62
 
 
63
            def get_state_values(self):
 
64
                "return the stateval of this fsm."
 
65
                return dict(SV1=self.state)
 
66
 
 
67
 
 
68
        runner = HelloRunner(f)
 
69
        for i in [1, 1, 1, 2, 1, 2, 2, 3, 3, 1, 1]:
 
70
            runner.on_event("EVENT_1", dict(MV1=str(i)))
 
71
        self.assertEquals("helloworld\n", "".join(result))
 
72
 
 
73
 
 
74
if __name__ == "__main__":
 
75
    unittest.main()