Mercurial > octave-antonio
comparison scripts/testfun/fail.m @ 5589:f812a0680d05
[project @ 2006-01-06 00:14:42 by jwe]
author | jwe |
---|---|
date | Fri, 06 Jan 2006 00:14:42 +0000 |
parents | |
children | 34f96dd5441b |
comparison
equal
deleted
inserted
replaced
5588:79ec73a1ff15 | 5589:f812a0680d05 |
---|---|
1 ## -*- texinfo -*- | |
2 ## @deftypefn {Function File} {} fail (@var{code},@var{pattern}) | |
3 ## @deftypefnx {Function File} {} fail (@var{code},'warning',@var{pattern}) | |
4 ## | |
5 ## Return true if @var{code} fails with an error message matching | |
6 ## @var{pattern}, otherwise produce an error. Note that @var{code} | |
7 ## is a string and if @var{code} runs successfully, the error produced is: | |
8 ## | |
9 ## @example | |
10 ## expected error but got none | |
11 ## @end example | |
12 ## | |
13 ## If the code fails with a different error, the message produced is: | |
14 ## | |
15 ## @example | |
16 ## expected <pattern> | |
17 ## but got <text of actual error> | |
18 ## @end example | |
19 ## | |
20 ## The angle brackets are not part of the output. | |
21 ## | |
22 ## Called with three arguments, the behavior is similar to | |
23 ## @code{fail(@var{code}, @var{pattern})}, but produces an error if no | |
24 ## warning is given during code execution or if the code fails. | |
25 ## | |
26 ## @end deftypefn | |
27 | |
28 ## This program is public domain | |
29 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
30 | |
31 ## PKG_ADD mark_as_command fail | |
32 function ret=fail(code,pattern,warning_pattern) | |
33 if nargin < 1 || nargin > 3 | |
34 usage("fail(code [, 'warning'] [, pattern])"); | |
35 endif | |
36 | |
37 ## sort out arguments | |
38 test_warning = (nargin > 1 && strcmp(pattern,'warning')); | |
39 if nargin == 3 | |
40 pattern = warning_pattern; | |
41 elseif nargin == 1 || (nargin==2 && test_warning) | |
42 pattern = ""; | |
43 endif | |
44 if isempty(pattern), pattern = "."; endif # match any nonempty message | |
45 | |
46 ## allow assert(fail()) | |
47 if nargout, ret=1; endif | |
48 | |
49 ## don't test failure if evalin doesn't exist | |
50 if !exist('evalin') || !exist('lastwarn'), return; endif | |
51 | |
52 if test_warning | |
53 ## perform the warning test | |
54 lastwarn(); # clear old warnings | |
55 state = warning("query","quiet"); # make sure warnings are turned on | |
56 warning("on","quiet"); | |
57 try | |
58 ## printf("lastwarn before %s: %s\n",code,lastwarn); | |
59 evalin("caller",sprintf("%s;",code)); | |
60 ## printf("lastwarn after %s: %s\n",code,lastwarn); | |
61 err = lastwarn; # retrieve new warnings | |
62 warning(state.state,"quiet"); | |
63 if isempty(err), | |
64 msg = sprintf("expected warning <%s> but got none", pattern); | |
65 else | |
66 err([1:9,end]) = []; # transform "warning: ...\n" to "..." | |
67 if !isempty(regexp(err,pattern,"once")), return; end | |
68 msg = sprintf("expected warning <%s>\nbut got <%s>", pattern,err); | |
69 endif | |
70 catch | |
71 warning(state.state,"quiet"); | |
72 err = lasterr; | |
73 err([1:7,end]) = []; # transform "error: ...\n", to "..." | |
74 msg = sprintf("expected warning <%s> but got error <%s>", pattern, err); | |
75 end | |
76 | |
77 else | |
78 ## perform the error test | |
79 try | |
80 evalin("caller",sprintf("%s;",code)); | |
81 msg = sprintf("expected error <%s> but got none", pattern); | |
82 catch | |
83 err=lasterr; | |
84 if (strcmp(err(1:7),"error:")) | |
85 err([1:6,end]) = []; # transform "error: ...\n", to "..." | |
86 endif | |
87 if !isempty(regexp(err,pattern,"once")), return; end | |
88 msg = sprintf("expected error <%s>\nbut got <%s>",pattern,err); | |
89 end | |
90 endif | |
91 | |
92 ## if we get here, then code didn't fail or error didn't match | |
93 error(msg); | |
94 endfunction | |
95 | |
96 %!fail ('[1,2]*[2,3]','nonconformant') | |
97 %!fail ("fail('[1,2]*[2;3]','nonconformant')","expected error <nonconformant> but got none") | |
98 %!fail ("fail('[1,2]*[2,3]','usage:')","expected error <usage:>\nbut got.*nonconformant") | |
99 %!fail ("warning('test warning')",'warning','test warning'); | |
100 | |
101 %!# fail ("warning('next test')",'warning','next test'); ## only allowed one warning test?!? | |
102 | |
103 ## Comment out the following tests if you don't want to see what | |
104 ## errors look like | |
105 % !fail ('a*[2;3]', 'nonconformant') | |
106 % !fail ('a*[2,3]', 'usage:') | |
107 % !fail ("warning('warning failure')", 'warning', 'success') |