79afa94d8d81bdc515f1bd4ec3d1cd754d4f04b3
[hooke.git] / test / command_stack_save_load.py
1 # Copyright (C) 2010 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 We run this test in a temporary directory for easy cleanup.
21
22 >>> import os
23 >>> import shutil
24 >>> import tempfile
25 >>> temp_dir = tempfile.mkdtemp(prefix='tmp-hooke-')
26
27 >>> from hooke.hooke import Hooke, HookeRunner
28 >>> h = Hooke()
29 >>> r = HookeRunner()
30
31 Add add some commands to the stack.
32
33 >>> h = r.run_lines(h, ['start_command_capture',
34 ...                     'load_playlist test/data/test',
35 ...                     'get_curve',
36 ...                     'stop_command_capture'])  # doctest: +REPORT_UDIFF
37 Success
38 <BLANKLINE>
39 Success
40 <BLANKLINE>
41 Success
42 <BLANKLINE>
43 Success
44 <BLANKLINE>
45 >>> h = r.run_lines(h, ['get_command_stack'])  # doctest: +NORMALIZE_WHITESPACE
46 [<CommandMessage load playlist {input: test/data/test}>,
47  <CommandMessage get curve>]
48 Success
49 <BLANKLINE>
50
51 Ensure we'll be saving in our temporary directory.
52
53 >>> target_dir = os.path.join(temp_dir, 'resources', 'command_stack')
54 >>> h = r.run_lines(h, ['set_config "command_stack plugin" path %s'
55 ...                     % target_dir])
56 Success
57 <BLANKLINE>
58 >>> h = r.run_lines(h, ['get_command_stack'])  # doctest: +NORMALIZE_WHITESPACE
59 [<CommandMessage load playlist {input: test/data/test}>,
60  <CommandMessage get curve>]
61 Success
62 <BLANKLINE>
63
64 Save the stack.
65
66 >>> h = r.run_lines(h, ['get_command_stack'])  # doctest: +NORMALIZE_WHITESPACE
67 [<CommandMessage load playlist {input: test/data/test}>,
68  <CommandMessage get curve>]
69 Success
70 <BLANKLINE>
71 >>> h = r.run_lines(h, ['save_command_stack'])
72 Success
73 <BLANKLINE>
74 >>> os.listdir(temp_dir)
75 ['resources']
76 >>> os.listdir(target_dir)
77 ['default']
78 >>> with open(os.path.join(target_dir, 'default'), 'r') as f:
79 ...     print f.read()
80 - arguments: {input: test/data/test}
81   command: load playlist
82 - arguments: {}
83   command: get curve
84 <BLANKLINE>
85
86 You can also specify the name explicitly.
87
88 >>> h = r.run_lines(h, ['save_command_stack --output my_stack'])
89 Success
90 <BLANKLINE>
91 >>> sorted(os.listdir(target_dir))
92 ['default', 'my_stack']
93 >>> with open(os.path.join(target_dir, 'my_stack'), 'r') as f:
94 ...     print f.read()
95 - arguments: {input: test/data/test}
96   command: load playlist
97 - arguments: {}
98   command: get curve
99 <BLANKLINE>
100
101 Further saves overwrite the last save/load path by default.
102
103 >>> h = r.run_lines(h, ['restart_command_capture',
104 ...                     'curve_info',
105 ...                     'stop_command_capture'])  # doctest: +REPORT_UDIFF
106 Success
107 <BLANKLINE>
108 Success
109 <BLANKLINE>
110 Success
111 <BLANKLINE>
112 >>> h = r.run_lines(h, ['save_command_stack'])
113 Success
114 <BLANKLINE>
115 >>> with open(os.path.join(target_dir, 'default'), 'r') as f:
116 ...     print f.read()
117 - arguments: {input: test/data/test}
118   command: load playlist
119 - arguments: {}
120   command: get curve
121 <BLANKLINE>
122 >>> with open(os.path.join(target_dir, 'my_stack'), 'r') as f:
123 ...     print f.read()
124 - arguments: {input: test/data/test}
125   command: load playlist
126 - arguments: {}
127   command: get curve
128 - arguments: {}
129   command: curve info
130 <BLANKLINE>
131
132 But starting command capture (which clears the stack), reverts the
133 default save name to `default`.
134
135 >>> h = r.run_lines(h, ['start_command_capture',
136 ...                     'version',
137 ...                     'stop_command_capture'])  # doctest: +REPORT_UDIFF
138 Success
139 <BLANKLINE>
140 Success
141 <BLANKLINE>
142 Success
143 <BLANKLINE>
144 >>> h = r.run_lines(h, ['save_command_stack'])
145 Success
146 <BLANKLINE>
147 >>> with open(os.path.join(target_dir, 'default'), 'r') as f:
148 ...     print f.read()
149 - arguments: {}
150   command: version
151 <BLANKLINE>
152
153 Clear the stack so loading behavior is more obvious.
154
155 >>> h = r.run_lines(h, ['start_command_capture',
156 ...                     'stop_command_capture'])  # doctest: +REPORT_UDIFF
157 Success
158 <BLANKLINE>
159 Success
160 <BLANKLINE>
161
162 Loading is just the inverse of saving.
163
164 >>> h = r.run_lines(h, ['load_command_stack'])
165 Success
166 <BLANKLINE>
167 >>> h = r.run_lines(h, ['get_command_stack'])
168 [<CommandMessage version>]
169 Success
170 <BLANKLINE>
171 >>> h = r.run_lines(h, ['load_command_stack --input my_stack'])
172 Success
173 <BLANKLINE>
174 >>> h = r.run_lines(h, ['get_command_stack'])  # doctest: +NORMALIZE_WHITESPACE
175 [<CommandMessage load playlist {input: test/data/test}>,
176  <CommandMessage get curve>,
177  <CommandMessage curve info>]
178 Success
179 <BLANKLINE>
180
181 Now that the current stack's name is `my_stack`, that will be the
182 default stack loaded if you `load_command_stack` without `--input`.
183
184 >>> with open(os.path.join(target_dir, 'my_stack'), 'w') as f:
185 ...     f.write('\\n'.join([
186 ...             '- arguments: {}',
187 ...             '  command: debug',
188 ...             '']))
189 >>> h = r.run_lines(h, ['load_command_stack'])
190 Success
191 <BLANKLINE>
192 >>> h = r.run_lines(h, ['get_command_stack'])  # doctest: +NORMALIZE_WHITESPACE
193 [<CommandMessage debug>]
194 Success
195 <BLANKLINE>
196
197 Cleanup the temporary directory.
198
199 >>> shutil.rmtree(temp_dir)
200 """