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