-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_moxunit_util_elem2str.m
168 lines (128 loc) · 4.97 KB
/
test_moxunit_util_elem2str.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
function test_suite=test_moxunit_util_elem2str
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
test_functions=localfunctions();
catch % no problem; early Matlab versions can use initTestSuite fine
end
initTestSuite;
function test_moxunit_util_elem2str_tiny
aeq=@assert_expected_output;
% empty string
aeq('0x0 char (empty)\n''''','');
% string in row vector form
aeq('1x3 char\n''abc''','abc');
% matrix
aeq('2x3 double\n[4 3 2;3 4 1]',[4 3 2; 3 4 1]);
% random data
x=randn(2);
precision=5;
aeq(['2x2 double\n' mat2str(x,precision)],x);
function test_moxunit_util_elem2str_big_matrix
aeq=@assert_expected_output;
aeq( ['4x4 double\n'...
'[1 -1e-08 -1e-08 -1e-08;'...
'-1e-08 1 -1e-08 -1e-08;'...
'-1e-08 -1e-08 1 -1e-08;'...
'-1e-08 -1e-08 -1e-08 1]'...
],...
eye(4)-1e-8);
function test_moxunit_util_elem2str_big_cell
aeq=@assert_expected_output;
aeq('100x2 cell',cell(100,2))
function test_moxunit_util_elem2str_tiny_with_evalc
aeq=@assert_expected_output_evalc_if_present;
% string with non-row vector form
aeq('2x3 char\nabc\ndef',...
'2x3 char',...
['abc';'def']);
% 3D string array
aeq('2x3x2 char\n\n(:,:,1) =\n\nabc\ncde\n\n(:,:,2) =\n\nefg\nghi',...
'2x3x2 char',...
cat(3,['abc';'cde'],['efg';'ghi']))
% logical array
aeq('1x2 logical\n 0 1','1x2 logical',[false true]);
% cell array
aeq({'1x2 cell\n ''foo'' [1]',...
'1x2 cell\n\n{\n[1,1] = foo\n[1,2] = 1\n}',...
'1x2 cell\n {''foo''} {[1]}'},...
'1x2 cell',{'foo',1});
function test_moxunit_util_elem2str_custom_class
% Make a temporary class with a size function that throws an error;
% This tests the functionality of util_elem2str for the case that not
% even a class' 'size' function is available
tmpdir=tempname(tempdir);
cleaner=onCleanup(@()remove_path_directory(tmpdir));
classname=sprintf('my_class');
classdir=fullfile(tmpdir,sprintf('@%s',classname));
if ~exist(tmpdir,'dir')
% GNU Octave returns a temporary directory that does not exist;
% so create it first
mkdir(tmpdir);
end
% make subdirectory for the class
mkdir(classdir);
write_contents(classdir,classname,...
['function obj=%s()\n'...
'obj=class(struct(),''%s'');'],...
classname,classname);
write_contents(classdir,'size',...
['function size(obj)\n'...
'error(''raises error'');']);
addpath(tmpdir);
constructor=str2func(classname);
% make object instance
obj=constructor();
aeq=@assert_expected_output;
aeq(sprintf('%s',classname),obj);
function write_contents(dirname,fname,pat,varargin)
% helper function to write .m file
fn=fullfile(dirname,sprintf('%s.m',fname));
fid=fopen(fn,'w');
cleaner=onCleanup(@()fclose(fid));
fprintf(fid,pat,varargin{:});
function remove_path_directory(dir_name)
% removes dir_name from the search path and from the file system
rmpath(dir_name);
if moxunit_util_platform_is_octave()
% GNU Octave requires, by defaualt, confirmation when using rmdir.
% The state of confirm_recursive_rmdir is stored, and set back
% to its original value when leaving this function.
confirm_val=confirm_recursive_rmdir(false);
cleaner=onCleanup(@()confirm_recursive_rmdir(confirm_val));
end
rmdir(dir_name,'s');
function assert_expected_output_evalc_if_present(a,b,varargin)
mex_file_code=3;
if exist('evalc','builtin') || ...
exist('evalc')==mex_file_code
to_compare=a;
else
to_compare=b;
end
assert_expected_output(to_compare,varargin{:});
function assert_expected_output(to_compare,varargin)
result=moxunit_util_elem2str(varargin{:});
if islogical(varargin{1})
assert(ischar(to_compare));
expected=sprintf(to_compare);
if ~is_equal_modulo_whitespace(result,expected);
assertEqual(to_compare,expected,'Not equal modulo whitespace');
end
else
if ~iscell(to_compare)
to_compare={to_compare};
end
expected_cell=cellfun(@sprintf,to_compare,'UniformOutput',false);
for k=1:numel(expected_cell)
if is_equal_modulo_whitespace(expected_cell{k},result)
return;
end
end
msg=sprintf(['Output ''%s'' is not equal modulo whitespace '...
'to any of: ''%s'''],...
result,...
moxunit_util_strjoin(expected_cell,''', '''));
error(msg);
end
function tf=is_equal_modulo_whitespace(a,b)
simplify_whitespace=@(x)regexprep(x,'\s+',' ');
tf=isequal(simplify_whitespace(a),simplify_whitespace(b));