Mercurial > hg > octave-nkf
comparison scripts/testfun/test.m @ 16977:0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
* scripts/testfun/test.m: Initialize lasterr and lastwarn to "" before running
code block. Rewrite trimleft which generated its own error with "" input.
Change output messages to correctly print "error" or "warning" depending on
code block type. Workaround really odd bug with lastwarn function when embedded
in try/catch blocks.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 13 Jul 2013 16:27:49 -0700 |
parents | 13affad7347c |
children | 4e8f49304059 |
comparison
equal
deleted
inserted
replaced
16976:13affad7347c | 16977:0d5d3e53255f |
---|---|
393 __patstr = ["id=",__id]; | 393 __patstr = ["id=",__id]; |
394 else | 394 else |
395 if (! strcmp (__pattern, '.')) | 395 if (! strcmp (__pattern, '.')) |
396 __patstr = ["<",__pattern,">"]; | 396 __patstr = ["<",__pattern,">"]; |
397 else | 397 else |
398 __patstr = "an error"; | 398 __patstr = ifelse (__warning, "a warning", "an error"); |
399 endif | 399 endif |
400 endif | 400 endif |
401 try | 401 try |
402 eval (sprintf ("function __test__(%s)\n%s\nendfunction", | 402 eval (sprintf ("function __test__(%s)\n%s\nendfunction", |
403 __shared, __code)); | 403 __shared, __code)); |
409 | 409 |
410 if (__success) | 410 if (__success) |
411 __success = 0; | 411 __success = 0; |
412 __warnstate = warning ("query", "quiet"); | 412 __warnstate = warning ("query", "quiet"); |
413 warning ("on", "quiet"); | 413 warning ("on", "quiet"); |
414 ## Clear error and warning strings before starting | |
415 lasterr (""); | |
416 lastwarn (""); | |
414 try | 417 try |
418 ## FIXME: lastwarn () must be called once from *WITHIN* the try block | |
419 ## or subsequent warning/lastwarn statements may fail. | |
420 ## Likely this is something to do with the specialness of | |
421 ## the try block which is disabling normal errors. | |
422 lastwarn (); | |
415 eval (sprintf ("__test__(%s);", __shared)); | 423 eval (sprintf ("__test__(%s);", __shared)); |
416 if (! __warning) | 424 if (! __warning) |
417 __msg = sprintf ("%serror failed.\nExpected %s but got no error\n", | 425 __msg = sprintf ("%serror failed.\nExpected %s but got no error\n", |
418 __signal_fail, __patstr); | 426 __signal_fail, __patstr); |
419 else | 427 else |
420 if (! isempty (__id)) | 428 if (! isempty (__id)) |
421 [~, __err] = lastwarn; | 429 [~, __err] = lastwarn (); |
422 __mismatch = ! strcmp (__err, __id); | 430 __mismatch = ! strcmp (__err, __id); |
423 else | 431 else |
424 __err = trimerr (lastwarn, "warning"); | 432 __err = trimerr (lastwarn (), "warning"); |
425 __mismatch = isempty (regexp (__err, __pattern, "once")); | 433 __mismatch = isempty (regexp (__err, __pattern, "once")); |
426 endif | 434 endif |
427 warning (__warnstate.state, "quiet"); | 435 warning (__warnstate.state, "quiet"); |
428 if (isempty (__err)) | 436 if (isempty (__err)) |
429 __msg = sprintf (["%swarning failed.\n" \ | 437 __msg = sprintf (["%swarning failed.\n" \ |
430 "Expected %s but got no warning\n"], | 438 "Expected %s but got no warning\n"], |
431 __signal_fail, __patstr); | 439 __signal_fail, __patstr); |
432 elseif (__mismatch) | 440 elseif (__mismatch) |
433 __msg = sprintf ("%serror failed.\nExpected %s but got <%s>\n", | 441 __msg = sprintf ("%swarning failed.\nExpected %s but got <%s>\n", |
434 __signal_fail, __patstr, __err); | 442 __signal_fail, __patstr, __err); |
435 else | 443 else |
436 __success = 1; | 444 __success = 1; |
437 endif | 445 endif |
438 endif | 446 endif |
439 | 447 |
440 catch | 448 catch |
441 if (! isempty (__id)) | 449 if (! isempty (__id)) |
442 [~, __err] = lasterr; | 450 [~, __err] = lasterr (); |
443 __mismatch = ! strcmp (__err, __id); | 451 __mismatch = ! strcmp (__err, __id); |
444 else | 452 else |
445 __err = trimerr (lasterr, "error"); | 453 __err = trimerr (lasterr (), "error"); |
446 __mismatch = isempty (regexp (__err, __pattern, "once")); | 454 __mismatch = isempty (regexp (__err, __pattern, "once")); |
447 endif | 455 endif |
448 warning (__warnstate.state, "quiet"); | 456 warning (__warnstate.state, "quiet"); |
449 if (__warning) | 457 if (__warning) |
450 __msg = sprintf (["%swarning failed.\n" \ | 458 __msg = sprintf (["%swarning failed.\n" \ |
643 endif | 651 endif |
644 endfunction | 652 endfunction |
645 | 653 |
646 ## Strip '.*prefix:' from '.*prefix: msg\n' and strip trailing blanks. | 654 ## Strip '.*prefix:' from '.*prefix: msg\n' and strip trailing blanks. |
647 function msg = trimerr (msg, prefix) | 655 function msg = trimerr (msg, prefix) |
648 idx = index (msg, cstrcat (prefix, ":")); | 656 idx = index (msg, [prefix ":"]); |
649 if (idx > 0) | 657 if (idx > 0) |
650 msg(1:idx+length(prefix)) = []; | 658 msg(1:idx+length(prefix)) = []; |
651 endif | 659 endif |
652 msg = trimleft (deblank (msg)); | 660 msg = strtrim (msg); |
653 endfunction | 661 endfunction |
654 | 662 |
655 ## Strip leading blanks from string. | 663 ## Strip leading blanks from string. |
656 function str = trimleft (str) | 664 function str = trimleft (str) |
657 idx = find (isspace (str)); | 665 idx = find (! isspace (str), 1); |
658 leading = find (idx == 1:length (idx)); | 666 str = str(idx:end); |
659 if (! isempty (leading)) | |
660 str = str(leading(end)+1:end); | |
661 endif | |
662 endfunction | 667 endfunction |
663 | 668 |
664 ## Make a structure out of the named variables | 669 ## Make a structure out of the named variables |
665 ## (based on Etienne Grossmann's tar function). | 670 ## (based on Etienne Grossmann's tar function). |
666 function s = bundle (varargin) | 671 function s = bundle (varargin) |
683 endif | 688 endif |
684 endwhile | 689 endwhile |
685 fclose (fid); | 690 fclose (fid); |
686 endif | 691 endif |
687 endfunction | 692 endfunction |
693 | |
688 | 694 |
689 ### example from toeplitz | 695 ### example from toeplitz |
690 %!shared msg1,msg2 | 696 %!shared msg1,msg2 |
691 %! msg1="C must be a vector"; | 697 %! msg1="C must be a vector"; |
692 %! msg2="C and R must be vectors"; | 698 %! msg2="C and R must be vectors"; |