Ran update_copyright.py.
[hooke.git] / test / command_stack.py
1 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 """
20 >>> import logging
21 >>> import sys
22 >>> from hooke.hooke import Hooke, HookeRunner
23 >>> h = Hooke()
24 >>> r = HookeRunner()
25
26 The command stack starts off empty.
27
28 >>> h = r.run_lines(h, ['get_command_stack'])
29 []
30 Success
31 <BLANKLINE>
32
33 And inactive, so you can't stop it.
34
35 >>> h = r.run_lines(h, ['get_command_capture_state'])
36 inactive
37 Success
38 <BLANKLINE>
39 >>> h = r.run_lines(h, ['stop_command_capture'])
40 Failure
41 invalid state change: inactive -> inactive
42
43 Because :meth:`hooke.hooke.HookeRunner.run_lines` spawns and closes
44 its own engine subprocess, we need to run the whole capture session in
45 a single call.  The command stack, on the other hand, will be
46 preserved between calls.
47
48 You can't restart recording.
49
50 >>> h = r.run_lines(h, ['start_command_capture',
51 ...                     'get_command_capture_state',
52 ...                     'start_command_capture',
53 ...                     'restart_command_capture'])  # doctest: +REPORT_UDIFF
54 Success
55 <BLANKLINE>
56 active
57 Success
58 <BLANKLINE>
59 Failure
60 invalid state change: active -> active
61 Failure
62 invalid state change: active -> active
63
64 But you can stop and restart.
65
66 >>> h = r.run_lines(h, ['start_command_capture',
67 ...                     'stop_command_capture',
68 ...                     'restart_command_capture'])  # doctest: +REPORT_UDIFF
69 Success
70 <BLANKLINE>
71 Success
72 <BLANKLINE>
73 Success
74 <BLANKLINE>
75
76 Lets add some commands to the stack.
77
78 >>> h = r.run_lines(h, ['start_command_capture',
79 ...                     'load_playlist test/data/test',
80 ...                     'get_curve',
81 ...                     'stop_command_capture'])  # doctest: +REPORT_UDIFF
82 Success
83 <BLANKLINE>
84 Success
85 <BLANKLINE>
86 Success
87 <BLANKLINE>
88 Success
89 <BLANKLINE>
90 >>> h = r.run_lines(h, ['get_command_stack'])  # doctest: +NORMALIZE_WHITESPACE
91 [<CommandMessage load playlist {input: test/data/test}>,
92  <CommandMessage get curve>]
93 Success
94 <BLANKLINE>
95
96 When capture is stopped, command execution is normal.
97
98 >>> h = r.run_lines(h, ['restart_command_capture',
99 ...                     'curve_info',
100 ...                     'stop_command_capture',
101 ...                     'version',
102 ...                     'restart_command_capture',
103 ...                     'previous_curve',
104 ...                     'stop_command_capture']
105 ...     )  # doctest: +ELLIPSIS, +REPORT_UDIFF
106 Success
107 <BLANKLINE>
108 Success
109 <BLANKLINE>
110 Success
111 <BLANKLINE>
112 Hooke 1.0.0.alpha (Ninken)
113 ...
114 Success
115 <BLANKLINE>
116 Success
117 <BLANKLINE>
118 Success
119 <BLANKLINE>
120
121 You can pop commands regardless of the recording state.
122
123 >>> h = r.run_lines(h, ['pop_command_from_stack'])
124 <CommandMessage previous curve>
125 Success
126 <BLANKLINE>
127 >>> h = r.run_lines(h, ['get_command_stack'])  # doctest: +NORMALIZE_WHITESPACE
128 [<CommandMessage load playlist {input: test/data/test}>,
129  <CommandMessage get curve>,
130  <CommandMessage curve info>]
131 Success
132 <BLANKLINE>
133
134 >>> h = r.run_lines(h, ['restart_command_capture',
135 ...                     'pop_command_from_stack',
136 ...                     'get_command_stack',
137 ...                     'stop_command_capture']
138 ...     )  # doctest: +NORMALIZE_WHITESPACE, +REPORT_UDIFF
139 Success
140 <BLANKLINE>
141 <CommandMessage curve info>
142 Success
143 <BLANKLINE>
144 [<CommandMessage load playlist {input: test/data/test}>,
145  <CommandMessage get curve>]
146 Success
147 <BLANKLINE>
148 Success
149 <BLANKLINE>
150
151 If you start up again (using `start` not `restart`), the stack is cleared.
152
153 >>> h = r.run_lines(h, ['start_command_capture',
154 ...                     'stop_command_capture'])
155 Success
156 <BLANKLINE>
157 Success
158 <BLANKLINE>
159 >>> h = r.run_lines(h, ['get_command_stack'])
160 []
161 Success
162 <BLANKLINE>
163
164 Building command stacks is fun, but its useless if you can't execute
165 them.  First, lets repopulate the in-memory stack.
166
167 >>> h = r.run_lines(h, ['start_command_capture',
168 ...                     'debug --attribute config',
169 ...                     'version',
170 ...                     'stop_command_capture']
171 ...     )  # doctest: +REPORT_UDIFF
172 Success
173 <BLANKLINE>
174 Success
175 <BLANKLINE>
176 Success
177 <BLANKLINE>
178 Success
179 <BLANKLINE>
180
181 Setup logging so we can check command output in the doctest.
182
183 >>> log = logging.getLogger('hooke')
184 >>> stdout_handler = logging.StreamHandler(sys.stdout)
185 >>> log.addHandler(stdout_handler)
186
187 Execute the stack.  We use `h.run_command` because `sys.stdout` is
188 replaced by a `doctest._SpoofOut`, and doctest has no way to collect
189 those results from `run_lines`'s engine subprocess.
190
191 >>> h.run_command('execute command stack', arguments={}
192 ...     )  # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE, +REPORT_UDIFF
193 engine running internal <CommandMessage execute command stack>
194 engine running internal <CommandMessage debug {attribute: config, ...}>
195 engine message from debug (<type 'instance'>):
196  <hooke.config.HookeConfigParser instance at 0x...>
197 engine message from debug (<class 'hooke.command.Success'>):
198 engine running internal <CommandMessage version {...}>
199 engine message from version (<type 'str'>): Hooke 1.0.0.alpha (Ninken)
200 ----
201 ...
202 engine message from version (<class 'hooke.command.Success'>):
203 engine message from execute command stack (<class 'hooke.command.Success'>): 
204 """