KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MUnit.m
Go to the documentation of this file.
1 
2 
3 /* (Autoinserted by mtoc++)
4  * This source code has been filtered by the mtoc++ executable,
5  * which generates code that can be processed by the doxygen documentation tool.
6  *
7  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
8  * Except for the comments, the function bodies of your M-file functions are untouched.
9  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
10  * attached source files that are highly readable by humans.
11  *
12  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
13  * the correct locations in the source code browser.
14  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
15  */
16 
17 class MUnit {
55  public: /* ( Constant ) */
56 
57  static const TestFunctionPrefix = "test_";
69  static const GreenCol = "[0 .5 0]";
78  static const BlueCol = "*"[0 0 .8]"";
87  static const WarnCol = "*"[1 .4 0]"";
96  public: /* ( Static ) */ /* ( Constant ) */
97 
98  static function succeeded = RunClassTests(dir,returnonerror,exclude) {
99 
100  /* Check for no specified path */
101  if nargin < 3
102  exclude = [];
103  if nargin < 2
104  returnonerror = false;
105  if nargin < 1
106  dir = cd;
107  end
108  end
109  end
110 
111  /* Check if the current directory path contains packages and
112  * extract them if necessary */
113  curPackage = ;
114  pckidx = strfind(dir," + ");
115  if ~isempty(pckidx)
116  curPackage = dir(pckidx(1)+1:end);
117  curPackage = [strrep(strrep(curPackage," + "," . ")," / ",) " . "];
118  end
119 
120  a = KerMor.App;
121  old = a.UseDPCM;
122  a.UseDPCM= false;
123 
124  /* Start recursive run */
125  [s,f,succeeded,abort] = MUnit.recursiveRun(dir, curPackage, returnonerror, exclude);
126 
127  a.UseDPCM= old;
128 
129  /* Summary */
130  if abort
131  fprintf(" \n\n Test runs aborted.\n ");
132  else
133  fprintf(" \n\n All Class Tests finished.\n ");
134  end
135  cprintf(MUnit.GreenCol," Successful:%d\n ",s);
136  cprintf(" Red "," Failed:%d\n ",f);
137  }
153  private: /* ( Static ) */ /* ( Constant ) */
154 
155  static function [s , f , cell<char>succeeded , abort ] = recursiveRun(folder,char currentPackage,returnonerror,cell<char> exclude) {
156 
157  s = 0; f = 0; succeeded = exclude;
158 
159  if exist(folder," file ") ~= 7
160  folder = fullfile(pwd,folder);
161  if exist(folder," file ") ~= 7
162  error(" Folder %s does not seem to exist. ",folder);
163  end
164  end
165 
166  /* Descend into subfolders */
167  dinf = dir(folder);
168  for idx = 1:length(dinf)
169  if dinf(idx).isdir
170  subdir = dinf(idx).name;
171  if ~any(strcmp(subdir,[" . "," .. "]))
172  subdir = fullfile(folder,subdir);
173  /* disp(['Descending into ' subdir]); */
174  [sa,fa,succeeded,abort] = ...
175  MUnit.recursiveRun(subdir, currentPackage, returnonerror, succeeded);
176  s = s + sa;
177  f = f + fa;
178  if abort
179  return;
180  end
181  end
182  end
183  end
184 
185  /* Descend into any package */
186  w = what(folder);
187  w=w(1); /* Some weird afs stuff goin on! */
188 
189  for idx = 1:length(w.packages)
190  subdir = fullfile(w.path,[" + " w.packages[idx]]);
191  /* disp(['Descending into ' subdir]); */
192  [sa,fa,succeeded,abort] = MUnit.recursiveRun(subdir, [currentPackage w.packages[idx] " . "], returnonerror, succeeded);
193  s = s + sa;
194  f = f + fa;
195  if abort
196  return;
197  end
198  end
199 
200  pref = MUnit.TestFunctionPrefix;
201  pl = length(pref);
202 
203  targets = [w.m; w.classes];
204 
205  /* Run tests in current directory */
206  for idx = 1:length(targets)
207  [~,n] = fileparts(targets[idx]);
208 
209  /* disp(['Checking ' n '...']); */
210 
211  try
212  mc = meta.class.fromName([currentPackage n]);
213  catch ME
214  warning(" Could not instantiate class %s: %s ",[currentPackage n],ME.message);
215  abort = true;
216  return;
217  end
218  /* mc is empty if m-file wasnt a class */
219  if ~isempty(mc)
220 
221  /* disp(['Checking ' mc.Name '...']); */
222 
223  for midx=1:length(mc.Methods)
224  m = mc.Methods[midx];
225 
226  /* disp(['Method: ' mc.Name '.' m.Name]); */
227 
228  /* check for test function and if the method is
229  * actually declared within the current class
230  * (subclassing would otherwise lead to a repeated
231  * call to the test) */
232  if length(m.Name) >= pl+1 ...
233  && strcmp(m.Access," public ") ...
234  && ~m.Abstract ...
235  && strcmpi(m.Name(1:pl),pref) ...
236  && strcmp(m.DefiningClass.Name,mc.Name)
237  /* check if the method is static [non-statics
238  * cant be run without instances.. :-)] */
239  if m.Static
240  fullname = [mc.Name " . " m.Name];
241  if any(strcmp(fullname,exclude))
242  continue;
243  end
244  lines = " @--------------@---------------@ ";
245  fprintf(2,[lines " Running "...
246  " <a href='matlab: " mc.Name " . " m.Name " '> " m.Name(6:end) " </a> " ...
247  " in <a href='matlab:edit " mc.Name " '> " mc.Name " </a> " ...
248  " ... " lines " \n "]);
249  try
250  eval([" outargs = nargout(@ " fullname " ); "]);
251  if outargs > 0
252  command = [" succ = " fullname " ; "];
253  else
254  command = [fullname " ; "];
255  end
256  eval(command);
257  if outargs == 0 || succ
258  cprintf(MUnit.GreenCol,[" Test " mc.Name " -> " m.Name(6:end) " succeeded!\n "]);
259  succeeded[end+1] = fullname;/* #ok */
260 
261  s = s+1;
262  elseif ~succ
263  cprintf(" Red "," Failure!\n ");
264  f = f+1;
265  end
266  catch ME
267  f = f+1;
268  cprintf(MUnit.WarnCol,[" Test " mc.Name " -> " m.Name(6:end) " failed!\nExeption information:\n "]);
269  disp(getReport(ME));
270  if returnonerror
271  abort = true;
272  return;
273  end
274  end
275  else
276  cprintf(MUnit.WarnCol,[" Non-static test '%s' in %s found. "...
277  " Should this be static?\n "],...
278  m.Name((length(MUnit.TestFunctionPrefix)+1):end),...
279  mc.Name);
280  end
281  end
282  end
283  end
284  end
285  abort = false;
286  }
306 };
307 
308 
309 
static const BlueCol
Blue color.
Definition: MUnit.m:78
Class Unit Testing Framework for Matlab.
Definition: MUnit.m:17
static function succeeded = RunClassTests(dir, returnonerror, exclude)
Runs class tests recursively within the specified path.
Definition: MUnit.m:98
static const TestFunctionPrefix
The prefix for any function that will be detected in the MUnit testing process.
Definition: MUnit.m:57
function count = cprintf(style, format, varargin)
CPRINTF displays styled formatted text in the Command Window.
Definition: cprintf.m:17
static const WarnCol
Warning color.
Definition: MUnit.m:87
Global configuration class for all KerMor run-time settings.
Definition: KerMor.m:17
static function KerMor theinstance = App()
The singleton KerMor instance.
Definition: KerMor.m:910
static const GreenCol
Green color (the default is hardly readable)
Definition: MUnit.m:69