Debian Codegraph @

test/private/testMatlab2tikz.m

raw
1 function [status, parameters] = testMatlab2tikz(varargin)
2 %TESTMATLAB2TIKZ unit test driver for matlab2tikz
3 %
4 % This function should NOT be called directly by the user (or even developer).
5 % If you are a developer, please use some of the following functions instead:
6 % * `testHeadless`
7 % * `testGraphical`
8 %
9 % The following arguments are supported, also for the functions above.
10 %
11 % TESTMATLAB2TIKZ('testFunctionIndices', INDICES, ...) or
12 % TESTMATLAB2TIKZ(INDICES, ...) runs the test only for the specified
13 % indices. When empty, all tests are run. (Default: []).
14 %
15 % TESTMATLAB2TIKZ('extraOptions', {'name',value, ...}, ...)
16 % passes the cell array of options to MATLAB2TIKZ. Default: {}
17 %
18 % TESTMATLAB2TIKZ('figureVisible', LOGICAL, ...)
19 % plots the figure visibly during the test process. Default: false
20 %
21 % TESTMATLAB2TIKZ('testsuite', FUNCTION_HANDLE, ...)
22 % Determines which test suite is to be run. Default: @ACID
23 % A test suite is a function that takes a single integer argument, which:
24 % when 0: returns a cell array containing the N function handles to the tests
25 % when >=1 and <=N: runs the appropriate test function
26 % when >N: throws an error
27 %
28 % TESTMATLAB2TIKZ('output', DIRECTORY, ...)
29 % Sets the output directory where the output files are places.
30 % The default directory is $M2TROOT/test/output/current
31 %
32 % See also matlab2tikz, ACID
33
34 % In which environment are we?
35 env = getEnvironment();
36
37 % -----------------------------------------------------------------------
38 ipp = m2tInputParser;
39
40 ipp = ipp.addOptional(ipp, 'testFunctionIndices', [], @isfloat);
41 ipp = ipp.addParamValue(ipp, 'extraOptions', {}, @iscell);
42 ipp = ipp.addParamValue(ipp, 'figureVisible', false, @islogical);
43 ipp = ipp.addParamValue(ipp, 'actionsToExecute', @(varargin) varargin{1}, @isFunction);
44 ipp = ipp.addParamValue(ipp, 'testsuite', @ACID, @isFunction );
45 ipp = ipp.addParamValue(ipp, 'output', m2troot('test','output','current'), @ischar);
46
47 ipp = ipp.parse(ipp, varargin{:});
48
49 ipp = sanitizeInputs(ipp);
50 parameters = ipp.Results;
51
52 % -----------------------------------------------------------------------
53 if strcmpi(env, 'Octave')
54 if ~ipp.Results.figureVisible
55 % Use the gnuplot backend to work around an fltk bug, see
56 % <http://savannah.gnu.org/bugs/?43429>.
57 graphics_toolkit gnuplot
58 end
59
60 if ispc
61 % Prevent three digit exponent on Windows Octave
62 % See https://github.com/matlab2tikz/matlab2tikz/pull/602
63 setenv ('PRINTF_EXPONENT_DIGITS', '2')
64 end
65 end
66
67 % copy output template into output directory
68 if ~exist(ipp.Results.output,'dir')
69 mkdir(ipp.Results.output);
70 end
71 template = m2troot('test','template');
72 copyfile(fullfile(template,'*'), ipp.Results.output);
73
74 % start overall timing
75 elapsedTimeOverall = tic;
76 status = runIndicatedTests(ipp);
77
78 % print out overall timing
79 elapsedTimeOverall = toc(elapsedTimeOverall);
80 stdout = 1;
81 fprintf(stdout, 'overall time: %4.2fs\n\n', elapsedTimeOverall);
82 end
83 % INPUT VALIDATION =============================================================
84 function bool = isFunction(f)
85 bool = isa(f,'function_handle');
86 end
87 function ipp = sanitizeInputs(ipp)
88 % sanitize all input arguments
89 ipp = sanitizeFunctionIndices(ipp);
90 ipp = sanitizeFigureVisible(ipp);
91 end
92 function ipp = sanitizeFunctionIndices(ipp)
93 % sanitize the passed function indices to the range of the test suite
94 % query the number of test functions
95 testsuite = ipp.Results.testsuite;
96 n = length(testsuite(0));
97
98 if ~isempty(ipp.Results.testFunctionIndices)
99 indices = ipp.Results.testFunctionIndices;
100 % kick out the illegal stuff
101 I = find(indices>=1 & indices<=n);
102 indices = indices(I); %#ok
103 else
104 indices = 1:n;
105 end
106 ipp.Results.testFunctionIndices = indices;
107 end
108 function ipp = sanitizeFigureVisible(ipp)
109 % sanitizes the figure visible option from boolean to ON/OFF
110 if ipp.Results.figureVisible
111 ipp.Results.figureVisible = 'on';
112 else
113 ipp.Results.figureVisible = 'off';
114 end
115 end
116 % TEST RUNNER ==================================================================
117 function status = runIndicatedTests(ipp)
118 % run all indicated tests in the test suite
119 % cell array to accomodate different structure
120 indices = ipp.Results.testFunctionIndices;
121 testsuite = ipp.Results.testsuite;
122 testsuiteName = func2str(testsuite);
123 stdout = 1;
124 status = cell(length(indices), 1);
125
126 for k = 1:length(indices)
127 testNumber = indices(k);
128
129 fprintf(stdout, 'Executing %s test no. %d...\n', testsuiteName, indices(k));
130
131 status{k} = emptyStatus(testsuite, testNumber);
132
133 elapsedTime = tic;
134
135 status{k} = feval(ipp.Results.actionsToExecute, status{k}, ipp);
136
137 elapsedTime = toc(elapsedTime);
138 status{k}.elapsedTime = elapsedTime;
139 fprintf(stdout, '%s ', status{k}.function);
140 if status{k}.skip
141 fprintf(stdout, 'skipped (%4.2fs).\n\n', elapsedTime);
142 else
143 fprintf(stdout, 'done (%4.2fs).\n\n', elapsedTime);
144 end
145 end
146 end