|
165 | 165 | end |
166 | 166 |
|
167 | 167 | function entries = testEntries() |
168 | | - % Parse Test_plotlyfig.m next to this file and extract, for every |
169 | | - % test method, the plot-generating code between the |
| 168 | + % Parse the Test_plotlyfig*.m files next to this file and extract, |
| 169 | + % for every test method, the plot-generating code between the |
170 | 170 | % `fig = figure("Visible","off");` line and the |
171 | 171 | % `p = plotlyfig(fig,...)` line. Also record whether the test is |
172 | 172 | % guarded with `if is_octave() return` (Octave-unsupported feature). |
173 | | - testFile = fullfile(fileparts(mfilename('fullpath')), 'Test_plotlyfig.m'); |
174 | | - fid = fopen(testFile, 'r'); |
175 | | - if fid < 0 |
176 | | - error('makegallerytests:noTestFile', ... |
177 | | - 'Cannot open %s', testFile); |
178 | | - end |
179 | | - text = fread(fid, Inf, '*char')'; |
180 | | - fclose(fid); |
181 | | - lines = strsplit(text, sprintf('\n')); |
| 173 | + folder = fileparts(mfilename('fullpath')); |
| 174 | + testFiles = dir(fullfile(folder, 'Test_plotlyfig*.m')); |
182 | 175 |
|
183 | 176 | entries = struct('name', {}, 'code', {}, 'guarded', {}, 'guardReason', {}); |
184 | 177 |
|
185 | | - i = 1; |
186 | | - n = numel(lines); |
187 | | - while i <= n |
188 | | - toks = regexp(lines{i}, '^\s{8}function\s+(test\w+)\s*\(tc\)\s*$', ... |
189 | | - 'tokens', 'once'); |
190 | | - if isempty(toks) |
191 | | - i = i + 1; |
192 | | - continue; |
| 178 | + for fi = 1:numel(testFiles) |
| 179 | + if strcmp(testFiles(fi).name, 'Test_plotlyfig_perf.m') |
| 180 | + continue |
193 | 181 | end |
194 | | - name = toks{1}; |
195 | | - |
196 | | - % the method ends at the first later line of exactly 8 spaces + end |
197 | | - j = i + 1; |
198 | | - while j <= n && isempty(regexp(lines{j}, '^\s{8}end\s*$', 'once')) |
199 | | - j = j + 1; |
| 182 | + testFile = fullfile(folder, testFiles(fi).name); |
| 183 | + fid = fopen(testFile, 'r'); |
| 184 | + if fid < 0 |
| 185 | + error('makegallerytests:noTestFile', ... |
| 186 | + 'Cannot open %s', testFile); |
200 | 187 | end |
201 | | - body = lines(i + 1:j - 1); |
202 | | - |
203 | | - figIdx = []; |
204 | | - pIdx = []; |
205 | | - for k = 1:numel(body) |
206 | | - if isempty(figIdx) && ... |
207 | | - ~isempty(regexp(body{k}, '^\s*fig\s*=\s*figure\(', 'once')) |
208 | | - figIdx = k; |
| 188 | + text = fread(fid, Inf, '*char')'; |
| 189 | + fclose(fid); |
| 190 | + lines = strsplit(text, sprintf('\n')); |
| 191 | + |
| 192 | + i = 1; |
| 193 | + n = numel(lines); |
| 194 | + while i <= n |
| 195 | + toks = regexp(lines{i}, '^\s{8}function\s+(test\w+)\s*\(tc\)\s*$', ... |
| 196 | + 'tokens', 'once'); |
| 197 | + if isempty(toks) |
| 198 | + i = i + 1; |
| 199 | + continue; |
209 | 200 | end |
210 | | - if ~isempty(regexp(body{k}, '^\s*p\s*=\s*plotlyfig\(fig,', 'once')) |
211 | | - pIdx = k; |
212 | | - break; |
| 201 | + name = toks{1}; |
| 202 | + |
| 203 | + % the method ends at the first later line of exactly 8 spaces + end |
| 204 | + j = i + 1; |
| 205 | + while j <= n && isempty(regexp(lines{j}, '^\s{8}end\s*$', 'once')) |
| 206 | + j = j + 1; |
| 207 | + end |
| 208 | + body = lines(i + 1:j - 1); |
| 209 | + |
| 210 | + figIdx = []; |
| 211 | + pIdx = []; |
| 212 | + for k = 1:numel(body) |
| 213 | + if isempty(figIdx) && ... |
| 214 | + ~isempty(regexp(body{k}, '^\s*fig\s*=\s*figure\(', 'once')) |
| 215 | + figIdx = k; |
| 216 | + end |
| 217 | + if ~isempty(regexp(body{k}, '^\s*p\s*=\s*plotlyfig\(fig,', 'once')) |
| 218 | + pIdx = k; |
| 219 | + break; |
| 220 | + end |
213 | 221 | end |
214 | | - end |
215 | 222 |
|
216 | | - % extraction starts after any leading comments and the Octave |
217 | | - % guard block (`if is_octave() return ... end`), so the code |
218 | | - % runs standalone; setup statements before `fig` (tables, |
219 | | - % digraphs, legend labels, ...) are kept |
220 | | - startIdx = 1; |
221 | | - k = 1; |
222 | | - while k <= numel(body) && ... |
223 | | - ~isempty(regexp(body{k}, '^\s*%', 'once')) |
224 | | - k = k + 1; |
225 | | - end |
226 | | - if k <= numel(body) && ... |
227 | | - ~isempty(regexp(body{k}, '^\s*if\s+is_octave\(\)', 'once')) |
| 223 | + % extraction starts after any leading comments and the Octave |
| 224 | + % guard block (`if is_octave() return ... end`), so the code |
| 225 | + % runs standalone; setup statements before `fig` (tables, |
| 226 | + % digraphs, legend labels, ...) are kept |
| 227 | + startIdx = 1; |
| 228 | + k = 1; |
228 | 229 | while k <= numel(body) && ... |
229 | | - isempty(regexp(body{k}, '^\s*end\s*$', 'once')) |
| 230 | + ~isempty(regexp(body{k}, '^\s*%', 'once')) |
230 | 231 | k = k + 1; |
231 | 232 | end |
232 | | - k = k + 1; % skip the guard's end line |
233 | | - end |
234 | | - startIdx = k; |
235 | | - |
236 | | - guarded = false; |
237 | | - guardReason = ''; |
238 | | - if ~isempty(figIdx) |
239 | | - for k = 1:figIdx - 1 |
240 | | - if ~isempty(regexp(body{k}, '^\s*if\s+is_octave\(\)', 'once')) ... |
241 | | - && k + 1 <= numel(body) ... |
242 | | - && ~isempty(regexp(body{k + 1}, '^\s*return', 'once')) |
243 | | - guarded = true; |
244 | | - m = regexp(body{k + 1}, '^\s*return\s*%\s*(.*)$', ... |
245 | | - 'tokens', 'once'); |
246 | | - if ~isempty(m) |
247 | | - guardReason = strtrim(m{1}); |
| 233 | + if k <= numel(body) && ... |
| 234 | + ~isempty(regexp(body{k}, '^\s*if\s+is_octave\(\)', 'once')) |
| 235 | + while k <= numel(body) && ... |
| 236 | + isempty(regexp(body{k}, '^\s*end\s*$', 'once')) |
| 237 | + k = k + 1; |
| 238 | + end |
| 239 | + k = k + 1; % skip the guard's end line |
| 240 | + end |
| 241 | + startIdx = k; |
| 242 | + |
| 243 | + guarded = false; |
| 244 | + guardReason = ''; |
| 245 | + if ~isempty(figIdx) |
| 246 | + for k = 1:figIdx - 1 |
| 247 | + if ~isempty(regexp(body{k}, '^\s*if\s+is_octave\(\)', 'once')) ... |
| 248 | + && k + 1 <= numel(body) ... |
| 249 | + && ~isempty(regexp(body{k + 1}, '^\s*return', 'once')) |
| 250 | + guarded = true; |
| 251 | + m = regexp(body{k + 1}, '^\s*return\s*%\s*(.*)$', ... |
| 252 | + 'tokens', 'once'); |
| 253 | + if ~isempty(m) |
| 254 | + guardReason = strtrim(m{1}); |
| 255 | + end |
| 256 | + break; |
248 | 257 | end |
249 | | - break; |
250 | 258 | end |
251 | 259 | end |
252 | | - end |
253 | 260 |
|
254 | | - if ~isempty(figIdx) && ~isempty(pIdx) && pIdx > figIdx |
255 | | - entries(end + 1) = struct( ... %#ok<AGROW> |
256 | | - 'name', name, ... |
257 | | - 'code', strjoin(body(startIdx:pIdx - 1), sprintf('\n')), ... |
258 | | - 'guarded', guarded, ... |
259 | | - 'guardReason', guardReason ... |
260 | | - ); |
| 261 | + if ~isempty(figIdx) && ~isempty(pIdx) && pIdx > figIdx |
| 262 | + entries(end + 1) = struct( ... %#ok<AGROW> |
| 263 | + 'name', name, ... |
| 264 | + 'code', strjoin(body(startIdx:pIdx - 1), sprintf('\n')), ... |
| 265 | + 'guarded', guarded, ... |
| 266 | + 'guardReason', guardReason ... |
| 267 | + ); |
| 268 | + end |
| 269 | + i = j; |
261 | 270 | end |
262 | | - i = j; |
263 | 271 | end |
264 | 272 | end |
265 | 273 |
|
|
0 commit comments