comparison testing/utp_1.1/document_utp_results.m @ 44:409a22968d5e default

Add unit tests
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Tue, 06 Dec 2011 18:42:11 +0100
parents
children
comparison
equal deleted inserted replaced
43:bc767aaa99a8 44:409a22968d5e
1 % Given a set of results from utp_run, this function attaches additional
2 % documentation for each unit test.
3 function newResults = document_utp_results(results)
4
5
6 for kk=1:numel(results)
7 fprintf(1, '+ %d documenting %s/%s/%s\n', kk, results(kk).class, results(kk).method, results(kk).num);
8 newResults(kk) = readUTP(results(kk));
9 end
10
11 end
12
13
14 function result = readUTP(result)
15
16 filename = result.file;
17 utpFcn = result.utp;
18
19
20 % parse the utp fcn
21 parts = regexp(utpFcn, filesep, 'split');
22 utp = parts{end};
23
24 if strcmp(utp, 'xx')
25 % the utp failed to run
26 result.doc.desc = '';
27 result.doc.syntax = '';
28 result.doc.syntaxCode = '';
29 result.doc.algo = '';
30 result.doc.algoCode = '';
31 return;
32 end
33
34
35 fd = fopen(filename);
36 if fd < 0
37 warning('Failed to open %s', filename);
38
39 end
40
41 fulltxt = {};
42 while ~feof(fd)
43 fulltxt = [fulltxt {fgetl(fd)}];
44 end
45
46 fclose(fd);
47
48 % parse the txt for this test only
49
50 % 1) find the utp_XX
51 % 2) backtrack to find <TestDescription>
52 % 3) read forward to get the end of the fcn (count even closing end's)
53
54 res = regexp(fulltxt, ['result\s*=\s*' utp]);
55 fcnLine = find(~cellfun('isempty', res));
56
57 % backtrack to look for the test description start
58 line = findLine(fulltxt, '<TestDescription>', fcnLine, true);
59
60 % parse out the test description
61 result.doc.desc = parseSection(fulltxt(line:end), utp, '<TestDescription>', '</TestDescription>', result.class, result.method, true, true);
62
63 % parse out the syntax test
64 result.doc.syntax = parseSection(fulltxt(line:end), utp, '<SyntaxDescription>', '</SyntaxDescription>', result.class, result.method, true, true);
65
66 % parse out syntax code
67 result.doc.syntaxCode = parseSection(fulltxt(line:end), utp, '<SyntaxCode>', '</SyntaxCode>', result.class, result.method, false, false);
68
69 % parse out the algorithm test
70 result.doc.algo = parseSection(fulltxt(line:end), utp, '<AlgoDescription>', '</AlgoDescription>', result.class, result.method, true, true);
71
72 % parse out algorithm code
73 result.doc.algoCode = parseSection(fulltxt(line:end), utp, '<AlgoCode>', '</AlgoCode>', result.class, result.method, false, false);
74
75
76
77 end
78
79 function line = findLine(lines, tag, startLine, backwards)
80
81
82 % backtrack to look for the test description start
83 line = startLine;
84 if backwards
85 while line > 0
86 try
87 if strfind(lines{line}, tag)
88 break;
89 end
90 catch
91 lines
92 line
93 tag
94 end
95 line = line - 1;
96 end
97 else
98 while line <= numel(lines)
99 if strfind(lines{line}, tag)
100 break;
101 end
102 line = line + 1;
103 end
104 end
105
106 end
107
108 function desc = parseSection(lines, utp, startTag, endTag, class, method, stripComments, trimLines)
109
110 % go forward capturing the description until we hit end of the
111 % description
112 line = 1;
113 desc = '';
114 foundStart = false;
115 while line < numel(lines)
116 if strfind(lines{line}, startTag)
117 foundStart = true;
118 end
119 if foundStart
120 lineTxt = lines{line};
121 if stripComments
122 lineTxt = strrep(lineTxt, '%', '');
123 end
124 lineTxt = regexprep(lineTxt, ['%*\s*' startTag], '');
125 lineTxt = regexprep(lineTxt, ['%*\s*' endTag], '');
126 lineTxt = strrep(lineTxt, '<METHOD>', ['[' class '/' method ']']);
127 if trimLines
128 lineTxt = strtrim(lineTxt);
129 end
130 desc = [desc sprintf('%s\n', lineTxt)];
131 end
132 if strfind(lines{line}, endTag)
133 break;
134 end
135 line = line + 1;
136 end
137
138 desc = strtrim(desc);
139
140
141 end
142
143
144
145