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