Assert

Assert Commit Details

Date:2011-04-13 14:11:19 (7 years 6 months ago)
Author:Michael Baudin
Commit:120
Parents: 119
Message:* Back-ported the changes from Scilab/master/assert_checkerror.
Changes:
M/macros/assert_checkerror.sci

File differences

macros/assert_checkerror.sci
1
1
22
33
44
......
1212
1313
1414
15
1516
1617
1718
1819
1920
2021
22
2123
2224
2325
......
3739
3840
3941
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
4071
4172
4273
......
5687
5788
5889
59
90
91
92
93
94
95
96
97
98
99
100
101
102
103
60104
61105
62106
......
75119
76120
77121
78
122
79123
80124
81
82
125
126
83127
84128
85129
......
90134
91135
92136
93
137
94138
95139
96140
97
141
98142
99143
100144
101
145
102146
103147
104148
105149
106150
107
151
108152
109153
110154
111
155
112156
113157
114158
115159
116
160
117161
118162
119163
......
121165
122166
123167
124
168
125169
126170
127171
......
143187
144188
145189
190
191
192
193
194
195
196
197
198
199
200
201
202
146203
147204
148205
......
185242
186243
187244
188
// Copyright (C) 2010 - DIGITEO - Michael Baudin
// Copyright (C) 2010 - 2011 - DIGITEO - Michael Baudin
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// Calling Sequence
// flag = assert_checkerror ( instr , expectedmsg )
// flag = assert_checkerror ( instr , expectedmsg , expectederrnb )
// flag = assert_checkerror ( instr , expectedmsg , expectederrnb , a1, ... )
// [flag,errmsg] = assert_checkerror ( ... )
//
// Parameters
// instr: a 1x1 matrix of strings, an expression to evaluate
// expectedmsg : a 1x1 matrix of strings, the error message to be produced
// expectederrnb : a 1x1 matrix of floating point integers, the error number (default expectederrnb=[]).
// a1 : an extra localization argument, passed to the <literal>msprintf</literal> function.
// flag : a 1x1 matrix of boolean, %t if computed is close to expected, %f if not
// errmsg : a 1x1 matrix of strings, the error message. If flag==%t, then errormsg=="". If flag==%f, then errmsg contains the error message.
//
//
// The goal of this function is to enable the tester to check the error
// cases in a simplified framework.
// Checking the error messages of a function has the following advantages:
// <itemizedlist>
// <listitem>
// <para>
// it checks that the correct error is generated when needed,
// </para>
// </listitem>
// <listitem>
// <para>
// it checks that the error message is correctly formatted,
// </para>
// </listitem>
// <listitem>
// <para>
// it checks that the error message is correctly localized.
// </para>
// </listitem>
// </itemizedlist>
//
// The expected message can be a localized message.
// In this case, the message is compared with the output of
// the <literal>gettext</literal> function,
// and depends on the local language.
// The extra argument <literal>a1</literal> can be used for this purpose,
// for example to format the localized message.
// All the extra arguments <literal>a1</literal>,
// <literal>a2</literal>, ... are automatically passed to the
// <literal>msprintf</literal> function before being compared to
// the computed error message.
//
// Examples
// // This function generates an error when
// y = x
// endfunction
// // Our task is to check the error messages produced by this function
// // Test which pass : the error message is the expected one
// // The two following tests pass: the error message is the expected one.
// // We let <literal>assert_checkerror</literal> manage the
// // localization, by providing only the localized message and
// // the extra parameter <literal>a1</literal>.
// lcl1="%s: Wrong number of input argument: %d expected.\n";
// lcl2="%s: Wrong type for argument %d: Matrix expected.\n";
// assert_checkerror ( "y=f()" , lcl1 , [] , "f" , 1);
// assert_checkerror ( "y=f(""a"")" , lcl2 , [] , "f" , 1);
//
// // Test which pass : the error message is the expected one.
// // Indeed, if we make the assumption that the current locale language
// // is english, we could as well use the following statement.
// // But this is not a recommended practice, since the test will
// // fail if the current locale is french, for example.
// msg1="f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected.";
// msg2="f: Unexpected type of input argument #1 : variable x has type string while constant is expected.";
// assert_checkerror ( "y=f()" , msg1 );
// [flag,errmsg] = assert_checkerror ( "y=f()" , "oups" )
//
// Authors
// Copyright (C) 2010 - DIGITEO - Michael Baudin
// Copyright (C) 2010 - 2011 - DIGITEO - Michael Baudin
[lhs,rhs]=argn()
if ( and(rhs <> [2 3] ) ) then
errmsg = sprintf ( gettext ( "%s: Unexpected number of input arguments : %d provided while %d to %d are expected.") , "assert_checkerror" , rhs , 2 , 3 )
if ( rhs < 2 ) then
errmsg = sprintf ( gettext ( "%s: Wrong number of input argument: At least %d expected.\n") , "assert_checkerror" , 2 )
error(errmsg)
end
//
//
// Check types of variables
if ( typeof(instr) <> "string" ) then
errmsg = sprintf ( gettext ( "%s: Unexpected type of input argument #%d : variable %s has type %s while %s is expected.") , "assert_checkerror" , 1 , "instr" , typeof(instr) , "string" )
errmsg = sprintf ( gettext ( "%s: Wrong type for argument %d: Matrix of strings expected.\n") , "assert_checkerror" , 1 )
error(errmsg)
end
if ( typeof(expectedmsg) <> "string" ) then
errmsg = sprintf ( gettext ( "%s: Unexpected type of input argument #%d : variable %s has type %s while %s is expected.") , "assert_checkerror" , 2 , "expectedmsg" , typeof(expectedmsg) , "string" )
errmsg = sprintf ( gettext ( "%s: Wrong type for argument %d: Matrix of strings expected.\n") , "assert_checkerror" , 2 )
error(errmsg)
end
if ( typeof(expectederrnb) <> "constant" ) then
errmsg = sprintf ( gettext ( "%s: Unexpected type of input argument #%d : variable %s has type %s while %s is expected.") , "assert_checkerror" , 3 , "expectederrnb" , typeof(expectederrnb) , "constant" )
errmsg = sprintf ( gettext ( "%s: Wrong type for argument %d: Matrix expected.\n") , "assert_checkerror" , 3 )
error(errmsg)
end
//
// Check sizes of variables
if ( size(instr,"*") <> 1 ) then
errmsg = sprintf ( gettext ( "%s: Unexpected size of input argument #%d : variable %s has size %d while %d is expected.") , "assert_checkerror" , 1 , "instr" , size(instr,"*") , 1 )
errmsg = sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkerror" , 1 , 1 , 1 )
error(errmsg)
end
if ( size(expectedmsg,"*") <> 1 ) then
errmsg = sprintf ( gettext ( "%s: Unexpected size of input argument #%d : variable %s has size %d while %d is expected.") , "assert_checkerror" , 2 , "expectedmsg" , size(expectedmsg,"*") , 1 )
errmsg = sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkerror" , 2 , 1 , 1 )
error(errmsg)
end
if ( expectederrnb <> [] ) then
if ( size(expectederrnb,"*") <> 1 ) then
errmsg = sprintf ( gettext ( "%s: Unexpected size of input argument #%d : variable %s has size %d while %d is expected.") , "assert_checkerror" , 3 , "expectederrnb" , size(expectederrnb,"*") , 1 )
errmsg = sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkerror" , 3 , 1 , 1 )
error(errmsg)
end
end
// Check values of variables
if ( expectederrnb <> [] ) then
if ( expectederrnb < 0 ) then
errmsg = sprintf ( gettext ( "%s: Unexpected value input argument #%d : variable %s has negative entries.") , "assert_checkerror" , 3 , "expectederrnb" )
errmsg = sprintf ( gettext ( "%s: Wrong value for input argument #%d: Non-negative integers expected.\n" ) , "assert_checkerror" , 3 )
error(errmsg)
end
end
flag = %t
errmsg = ""
//
// Localize the message, if necessary.
if ( rhs >= 4 ) then
localmsg = gettext(expectedmsg)
instr = "expectedmsg = msprintf(localmsg, varargin(4:$))"
ierr = execstr(instr,"errcatch")
if ( ierr <> 0 ) then
fmterrmsg = lasterror()
localstr = gettext ( "%s: Error while formatting the error message: ""%s""")
errmsg = sprintf ( localstr , "assert_checkerror" , fmterrmsg )
error(errmsg)
end
end
//
// Check the error message
if ( expectedmsg <> compmsg ) then
flag = %f
end
end
endfunction

Archive Download the corresponding diff file

Revision: 120