forked from peterkty/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunitTest.m
650 lines (568 loc) · 17.7 KB
/
unitTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
function tree=unitTest(options)
% Opens a gui for running unit tests
%
% Recurses through the Drake directories, locating all test scripts
% (in test subdirectories) and all testable methods/scripts/class static methods
% in the examples directory.
%
% A test function is any valid .m file which calls error() if the test
% fails, and returns without error if the test passes.
%
% crawls static methods, looks for NOTEST, ...
%
% @option autorun if true, starts running the tests immediately. @default
% false
% @option gui if true, shows the gui. set to false for the text only, non-interactive version
% @default true
% @option additional_dirs a cell matrix of strings specifying additional
% directories to use as unit tests.
% NOTE: you may also add additional unit test directories by using
% editDrakeConfig('additional_unit_test_dirs',dirs)
% where dirs is the cell matrix of strings.
%
% All unit tests should always be run (and all tests should pass) before
% anything is committed back into the Drake repository.
% todo: figure out a good way to clear classes before/after runTest,
% without doing the clear all that zaps the gui content, too.
if (nargin<1) options=struct(); end
if ~isfield(options,'autorun') options.autorun = false; end
if ~isfield(options,'gui') options.gui = true; end
if ~isfield(options,'additional_dirs') options.additional_dirs = {};
elseif ~iscell(options.additional_dirs)
options.additional_dirs = {options.additional_dirs};
end
load drake_config.mat;
if isfield(conf,'additional_unit_test_dirs')
if ~iscell(conf.additional_unit_test_dirs)
conf.additional_unit_test_dirs = {conf.additional_unit_test_dirs};
end
options.additional_dirs = {options.additional_dirs{:},conf.additional_unit_test_dirs{:}};
end
if (options.gui)
warning('off','MATLAB:uitree:DeprecatedFunction');
h=figure(1302);
clear global runNode_mutex;
set(h,'HandleVisibility','on');
clf;
data=struct('pass',0,'fail',0,'wait',0,'path',pwd);
root = uitreenode('v0','All Tests',sprintf('<html>All Tests <i>(passed:%d, failed:%d, not yet run:%d)</i></html>',data.pass,data.fail,data.wait),[matlabroot, '/toolbox/matlab/icons/greenarrowicon.gif'],false);
set(root,'UserData',data);
else
root = [];
end
crawlDir('systems',root,true,options);
crawlDir('drivers',root,true,options);
crawlDir('util',root,true,options);
crawlDir('examples',root,false,options);
for i=1:numel(options.additional_dirs)
crawlDir(options.additional_dirs{1},root,false,options);
end
if (options.gui)
[tree,treecont] = uitree('v0','Root',root);
expandAll(tree,root);
hb = [];
hb = [hb,uicontrol('String','Dock','callback',@dock)]; if strcmp(get(h,'WindowStyle'),'docked'), set(hb(end),'String','Undock'); end
hb = [hb,uicontrol('String','Reload (keep history)','callback',@(h,env) cleanup(h,env,tree),'BusyAction','cancel')];
hb = [hb,uicontrol('String','Reload (flush history)','callback',@(h,env) reloadGui(h,env,tree),'BusyAction','cancel')];
htext = uicontrol('Style','text','String','Click triangles to run tests. Shift-click to edit test file.','BackgroundColor',[1 1 1]);
set(tree,'NodeSelectedCallback', @runSelectedNode);
set(treecont,'BusyAction','queue');
resizeFcn([],[],treecont,hb,htext);
set(h,'MenuBar','none','ToolBar','none','Name','Drake Unit Tests','NumberTitle','off','ResizeFcn',@(src,ev)resizeFcn(src,ev,treecont,hb,htext));
set(h,'HandleVisibility','off');%,'WindowStyle','docked'%,'CloseRequestFcn',[]);
jtree = handle(tree.getTree,'CallbackProperties');
set(jtree,'KeyPressedCallback', @keyPressedCallback);
set(jtree,'KeyReleasedCallback', @keyReleasedCallback);
if (options.autorun)
tree.setSelectedNode(root);
end
end
end
function dock(h,env)
% todo: replace string with the dock / undock icons?
if (strcmp(get(1302,'WindowStyle'),'docked'))
set(1302,'WindowStyle','normal')
set(h,'String','Dock');
else
set(1302,'WindowStyle','dock')
set(h,'String','Undock');
end
end
function keyPressedCallback(hTree,eventData)
global shift_down;
if (eventData.getKeyCode()==16)
shift_down = true;
end
end
function keyReleasedCallback(hTree,eventData)
global shift_down;
if (eventData.getKeyCode()==16)
shift_down = [];
end
end
function tree=reloadGui(h,env,tree)
megaclear;
cd(getDrakePath);
tree=unitTest;
end
function cleanup(h,env,tree)
saveTree(tree);
tree=reloadGui(h,env,tree);
loadTree(tree);
end
function saveTree(tree)
p=pwd;
cd(getDrakePath);
backupname = '.unitTestData.mat';
userdata={};
iter = tree.getRoot.breadthFirstEnumeration;
while iter.hasMoreElements;
node = iter.nextElement;
if (node.isLeaf)
d = get(node,'UserData');
if ~isfield(d,'test'), continue; end % false leaf
userdata{end+1} = get(node,'UserData');
end
end
backupname = '.unitTestData.mat';
save(backupname,'userdata');
cd(p);
end
function loadTree(tree)
p=pwd;
cd(getDrakePath);
backupname = '.unitTestData.mat';
load(backupname);
iter = tree.getRoot.breadthFirstEnumeration;
while iter.hasMoreElements;
node = iter.nextElement;
if (node.isLeaf)
d = get(node,'UserData');
if ~isfield(d,'test'), continue; end % false leaf
pathmatches = userdata(strcmp(d.path,cellfun(@(a) getfield(a,'path'),userdata,'UniformOutput',false)));
if (~isempty(pathmatches))
testmatch = pathmatches(strcmp(d.test,cellfun(@(a) getfield(a,'test'),pathmatches,'UniformOutput',false)));
if (~isempty(testmatch))
set(node,'UserData',testmatch{1});
switch (testmatch{1}.status)
case 0
% do nothing, already set to wait
case 1
updateParentNodes(node.getParent,'wait',-1);
updateParentNodes(node.getParent,'pass',1);
node.setName(['<html><font color="green">[PASSED]</font> ',d.test,'</html>']);
case 2
updateParentNodes(node.getParent,'wait',-1);
updateParentNodes(node.getParent,'fail',1);
node.setName(['<html><font color="red"><b>[FAILED]</b></font> ',d.test,' <font color="red"><i>',testmatch{1}.errmsg,'</i></font></html>']);
end
tree.reloadNode(node);
end
end
end
end
cd(p);
end
function saveMutex()
p=pwd;
cd(getDrakePath);
backupname = '.drake_unit_test_mutex.mat';
global runNode_mutex;
node_data = cellfun(@(a) get(a,'UserData'),runNode_mutex,'UniformOutput',false);
save(backupname,'node_data');
cd(p);
end
function loadMutex(tree)
p=pwd;
cd(getDrakePath);
backupname = '.drake_unit_test_mutex.mat';
global runNode_mutex;
load(backupname);
if ~isempty(node_data)
runNode_mutex = cellfun(@(a) findNode(tree,a),node_data,'UniformOutput',false);
end
cd(p);
end
function node=findNode(tree,data)
iter = tree.getRoot.breadthFirstEnumeration;
while iter.hasMoreElements
node = iter.nextElement;
d = get(node,'UserData');
if strcmp(d.path,data.path)
if isfield(data,'test') && isfield(d,'test') && strcmp(d.test,data.test)
return;
elseif ~isfield(data,'test') && ~isfield(d,'test') % matching directory node
return;
end
end
end
node=[];
end
function resizeFcn(src,ev,treecont,hb,htext)
pos = get(1302,'Position');
for i=1:length(hb)
set(hb(i),'Position',[(i-1)*pos(3)/length(hb),pos(4)-20,pos(3)/length(hb),20]);
end
set(htext,'Position',[0,-1,pos(3),18]);
set(treecont,'Position',[0,17,pos(3),pos(4)-38]);
end
function expandAll(tree,node)
tree.expand(node);
iter = node.breadthFirstEnumeration;
while iter.hasMoreElements
tree.expand(iter.nextElement);
end
end
function pnode = crawlDir(pdir,pnode,only_test_dirs,options)
p = pwd;
cd(pdir);
if exist('.NOTEST','file')
cd(p);
return;
end
if (options.gui)
data=struct('pass',0,'fail',0,'wait',0,'path',pdir);
htmldir = strrep(pdir,'/','/');
node = uitreenode('v0',htmldir,['<html>',htmldir,sprintf(' <i>(passed:%d, failed:%d, not yet run:%d)</i></html>',data.pass,data.fail,data.wait)],[matlabroot, '/toolbox/matlab/icons/greenarrowicon.gif'],false);
set(node,'UserData',data);
pnode.add(node);
else
node=[p,'/',pdir];
end
files=dir('.');
for i=1:length(files)
if (files(i).isdir)
% then recurse into the directory
if (files(i).name(1)~='.' && ~any(strcmpi(files(i).name,{'dev','slprj','autogen-matlab','autogen-posters'}))) % skip . and dev directories
crawlDir(files(i).name,node,only_test_dirs && ~strcmpi(files(i).name,'test'),options);
end
continue;
end
if (only_test_dirs) continue; end
if (~strcmpi(files(i).name(end-1:end),'.m')) continue; end
if (strcmpi(files(i).name,'Contents.m')) continue; end
testname = files(i).name;
ind=find(testname=='.',1);
testname=testname(1:(ind-1));
isClass = checkFile(files(i).name,'classdef');
if (isClass)
if (checkClass(files(i).name,'NOTEST'))
continue;
end
m = staticMethods(testname);
for j=1:length(m)
if (checkMethod(files(i).name,m{j},'NOTEST'))
continue;
end
if (options.gui)
node = addTest(node,[testname,'.',m{j}]);
else
runCommandLineTest(node,[testname,'.',m{j}]);
end
end
else
% reject if there is a notest
if (checkFile(files(i).name,'NOTEST'))
continue;
end
if (options.gui)
node = addTest(node,testname);
else
runCommandLineTest(node,testname);
end
end
end
if (options.gui)
data = get(node,'UserData');
if (data.wait==0) % then there was nothing underneath me
pnode.remove(node);
end
end
cd(p);
end
function node = addTest(node,testname)
data=struct('path',pwd,'test',testname,'status',0,'errmsg','');
n = uitreenode('v0',testname,['<html>',testname,'</html>'],[matlabroot, '/toolbox/matlab/icons/greenarrowicon.gif'],true);
set(n,'UserData',data);
node.add(n);
updateParentNodes(node,'wait',1);
end
function updateParentNodes(node,field,delta)
data = get(node,'UserData');
data = setfield(data,field,getfield(data,field)+delta);
set(node,'UserData',data);
v = get(node,'Value');
if (data.fail>0)
node.setName(['<html>',v,sprintf(' <i>(passed:%d, <font color="red"><b>failed:%d</b></font>, not yet run:%d)</i></html>',data.pass,data.fail,data.wait)]);
else
node.setName(['<html>',v,sprintf(' <i>(passed:%d, failed:%d, not yet run:%d)</i></html>',data.pass,data.fail,data.wait)]);
end
if ~isempty(node.getParent)
updateParentNodes(node.getParent,field,delta);
end
end
function runSelectedNode(tree,ev)
nodes = tree.getSelectedNodes;
if (~isempty(nodes))
node=nodes(1);
tree.setSelectedNode([]);
global shift_down;
if ~isempty(shift_down) && shift_down
editNode(node);
shift_down = []; % because I'm about to move focus away and will miss the keyRelease event
else
tree=runNode(tree,node);
end
end
end
function editNode(node)
data=get(node,'UserData');
if isfield(data,'test')
p = pwd;
cd (data.path);
edit(data.test);
cd(p);
else
cd(data.path);
end
end
function tree=runNode(tree,node)
data = get(node,'UserData');
if (isfield(data,'test'))
global runNode_mutex;
if ~isempty(runNode_mutex)
node.setName(['<html><font color="gray">[WAITING]</font> ',data.test,'</html>']);
tree.reloadNode(node);
runNode_mutex{end+1}=node;
saveMutex();
return;
end
runNode_mutex{1}=node;
saveMutex();
node.setName(['<html><font color="blue">[RUNNING]</font> ',data.test,'</html>']);
tree.reloadNode(node);
saveTree(tree);
pass = runTest(data.path,data.test);
if ~isa(tree,'javahandle_withcallbacks.com.mathworks.hg.peer.UITreePeer')
% somebody did a close all
tree=reloadGui([],[],tree);
loadTree(tree);
end
if ~exist('runNode_mutex')
% somebody did a clear all.
global runNode_mutex;
loadMutex(tree);
node=findNode(tree,data);
end
switch(data.status)
case 0
updateParentNodes(node.getParent,'wait',-1);
case 1
updateParentNodes(node.getParent,'pass',-1);
case 2
updateParentNodes(node.getParent,'fail',-1);
end
if (pass)
node.setName(['<html><font color="green">[PASSED]</font> ',data.test,'</html>']);
data.status = 1;
updateParentNodes(node.getParent,'pass',1);
set(node,'UserData',data);
else
data.status = 2;
data.errmsg = lasterr;
node.setName(['<html><font color="red"><b>[FAILED]</b></font> ',data.test,' <font color="red"><i>',data.errmsg,'</i></font></html>']);
updateParentNodes(node.getParent,'fail',1);
set(node,'UserData',data);
end
tree.reloadNode(node);
torun = runNode_mutex(2:end);
runNode_mutex = [];
for i=1:length(torun)
tree=runNode(tree,torun{i});
end
else
iter = node.depthFirstEnumeration;
while iter.hasMoreElements
n=iter.nextElement;
d=get(n,'UserData');
n=findNode(tree,d); % in case I had to reload
if isfield(d,'test')
tree=runNode(tree,n);
end
end
end
end
function pass = runCommandLineTest(path,test)
fprintf(1,'%-40s ',test);
pass = runTest(path,test);
if (pass)
fprintf(1,'[PASSED]\n');
else
fprintf(1,'\n[FAILED]\n'); % \n because runTest will print things
end
end
function pass = runTest(path,test)
p=pwd;
cd(path);
% disp(['running ',path,'/',test,'...']);
if (exist('rng'))
rng('shuffle'); % init rng to current date
else % for older versions of matlab
rand('seed',sum(100*clock));
end
force_close_system();
close all;
clearvars -global -except runNode_mutex;
s=dbstatus;
% useful for debugging: if 'dbstop if error' is on, then don't use try catch.
if any(strcmp('error',{s.cond})) ||any(strcmp('warning',{s.cond}))
feval_in_contained_workspace(test);
else
try
feval_in_contained_workspace(test);
catch ex
pass = false;
cd(p);
disp(' ');
disp(' ');
disp('*******************************************************');
disp(['* error in ',path,'/',test]);
disp('*******************************************************');
disp(getReport(ex,'extended'));
disp('*******************************************************');
lasterr(ex.message,ex.identifier);
% rethrow(ex);
return;
end
end
a=warning;
if (~strcmp(a(1).state,'on'))
error('somebody turned off warnings on me!'); % see bug
end
force_close_system();
close all;
clearvars -global -except runNode_mutex;
t = timerfind;
if (~isempty(t)) stop(t); end
pass = true;
cd(p);
end
function feval_in_contained_workspace(f) % helps with scripts
feval(f);
end
function bfound = checkFile(filename,strings)
% opens the file and checks for the existence of the string (or strings)
if ~iscell(strings), strings = {strings}; end
bfound = false;
fid=fopen(filename);
if (fid<0) return; end % couldn't open the file. skip it.
while true % check the file for the strings
tline = fgetl(fid);
if (~ischar(tline))
break;
end
for i=1:length(strings)
if (~isempty(strfind(tline,strings{i})))
fclose(fid);
bfound = true;
return;
end
end
end
fclose(fid);
end
function bfound = checkClass(filename,strings)
if ~iscell(strings), strings = {strings}; end
strings = lower(strings);
bfound = false;
bInMethod = false;
endcount = 0;
fid=fopen(filename);
if (fid<0) return; end % couldn't open the file. skip it.
while true % check the file for the strings
tline = fgetl(fid);
if (~ischar(tline))
break;
end
tline = lower(tline);
commentind = strfind(tline,'%');
if (~isempty(commentind)) tline = tline(1:commentind(1)-1); end
if (~bInMethod && ~isempty(strfind(tline,'function')))
bInMethod = true;
endcount=0;
end
if (bInMethod)
strings={'for','while','switch','try','if'};
endcount = endcount + length(keywordfind(tline,strings));
endcount = endcount - length(keywordfind(tline,'end'));
% disp([num2str(endcount,'%2d'),': ',tline]);
if endcount<0
bInMethod=false;
end
end
end
fclose(fid);
end
function bfound = checkMethod(filename,methodname,strings)
if ~iscell(strings), strings = {strings}; end
strings = lower(strings);
bfound = false;
bInMethod = false;
endcount = 0;
fid=fopen(filename);
if (fid<0) return; end % couldn't open the file. skip it.
while true % check the file for the strings
tline = fgetl(fid);
if (~ischar(tline))
break;
end
tline = lower(tline);
if (~bInMethod && ~isempty(keywordfind(tline,'function')))
if (~isempty(strfind(tline,lower(methodname))))
bInMethod = true;
endcount=0;
end
end
if (bInMethod)
endcount = endcount + length(keywordfind(tline,{'for','while','switch','try','if'}));
endcount = endcount - length(keywordfind(tline,'end'));
for i=1:length(strings)
if (~isempty(strfind(tline,strings{i})))
fclose(fid);
bfound = true;
return;
end
end
% disp([num2str(endcount,'%2d'),': ',tline]);
if endcount<0
fclose(fid);
return;
end
end
end
fclose(fid);
end
function inds = keywordfind(line,strs)
if (~iscell(strs)) strs={strs}; end
commentind = strfind(line,'%');
if (~isempty(commentind)) line = line(1:commentind(1)-1); end
inds=[];
for i=1:length(strs)
s = strs{i};
a = strfind(line,s);
% check that it is bracketed by a non-letter
j=1;
while j<=length(a)
if (a(j)~=1 && isletter(line(a(j)-1)))
a(j)=[];
continue;
end
if (a(j)+length(s)<=length(line) && isletter(line(a(j)+length(s))))
a(j)=[];
continue;
end
inds=[inds,a(j)];
j=j+1;
end
end
end