-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewTest.m
126 lines (107 loc) · 3.12 KB
/
newTest.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
function newTest(varargin)
% Create a new test case for existing function.
%
% newTest(FUNNAME) opens the editor and pastes the content of a
% user-defined template into the file test_FUNNAME.m.
%
%
% References
% Based on file 'tedit' from Peter Bodin.
%
% ------
% Author: David Legland
% e-mail: [email protected]
% Created: 2011-07-26, using Matlab 7.9.0.529 (R2009b)
% Copyright 2011 INRA - Cepia Software Platform.
% HISTORY
% 2016-07-19 update for new version of Matlab
switch nargin
case 0
edit
warning('newTest:MissingArgument', ...
'newTest without argument is the same as edit');
return;
case 1
funName = varargin{1};
fileName = ['test_' funName];
edit(fileName);
otherwise
error('too many input arguments');
end
% Matlab interface changed with 7.12.0, so we need to switch
if verLessThan('matlab','7.12.0')
try
% Define the handle for the java commands:
edhandle = com.mathworks.mlservices.MLEditorServices;
% get editor active document
doc = edhandle.builtinGetActiveDocument;
% append template header
text = parse(funName);
edhandle.builtinAppendDocumentText(doc, text);
catch ex
rethrow(ex)
end
else
try
% get editor active document
editorObject = matlab.desktop.editor.getActive;
% append template header
text = parse(funName);
editorObject.appendText(text);
catch ex
rethrow(ex)
end
end
function out = parse(funName)
template = { ...
'function tests = $filename'
'% Test suite for the file $funname.'
'%'
'% Test suite for the file $funname'
'%'
'% Example'
'% $filename'
'%'
'% See also'
'% $funname'
''
'% ------'
'% Author: $author'
'% e-mail: $mail'
['% Created: $date, using Matlab ' version]
'% Copyright $year $company.'
''
'tests = functiontests(localfunctions);'
''
'function test_Simple(testCase) %#ok<*DEFNU>'
'% Test call of function without argument.'
'$funname();'
'value = 10;'
'assertEqual(testCase, value, 10);'
''
''};
repstr = {...
'$filename'
'$FILENAME'
'$funname'
'$date'
'$year'
'$author'
'$mail'
'$company'};
testName = ['test_' funName];
repwithstr = {...
testName
upper(testName)
funName
datestr(now, 29)
datestr(now, 10)
'David Legland'
'INRAE - BIA-BIBS'};
for k = 1:numel(repstr)
template = strrep(template, repstr{k}, repwithstr{k});
end
out = sprintf('%s\n', template{:});
end
end