## erreval() extends the built-in function eval(). Return 0 if
## try_str does not raise the error of type error_prefix, return 1
## otherwise.
-
+
global unittest_results;
for k = 1:length(unittest_results.eval_globals)
eval(unittest_results.eval_globals{k});
end
-
+
rv = 0;
try
eval(try_str);
catch
rv = errcheck(error_prefix);
- end
+ end
endfunction
## names of the global variables used in the tests.
##
## e.g. unit_init(1, {"g", "a", "x"})
-
+
global unittest_results;
-
+
unittest_results.verbose = 0;
unittest_results.eval_globals = {};
if (nargin > 0)
else
unittest_results.verbose = verbosity;
endif
-
+
if (nargin == 2 && iscell(global_vars))
for i = 1:length(global_vars)
unittest_results.eval_globals{i} = strcat("global ", global_vars{i}, ";");
else
error("global_vars must be a cell array");
endif
-
+
if (nargin > 2)
usage("expecting 2 arguments only");
end
endif
-
+
unittest_results.total = 0; # number of testcases attempted
unittest_results.pass = 0; # number of expected passed
unittest_results.fail = 0; # number of unexpected failures
unittest_results.upass = 0; # number of unexpected passes
unittest_results.xfail = 0; # number of expected failures
unittest_results.unresolved = 0; # number of unresolved testcases
-
+
default_eval_print_flag = 0;
endfunction
function unit_results()
## Print the results from previous unittest calls in pretty format.
-
+
global unittest_results;
-
+
printf("\n");
printf("# of testcases attempted %d\n", unittest_results.total);
printf("# of expected passes %d\n", unittest_results.pass);
printf("# of unexpected failures %d\n", unittest_results.fail);
printf("# of unresolved testcases %d\n", unittest_results.unresolved);
printf("\n");
-
+
if (unittest_results.total == unittest_results.pass + unittest_results.xfail)
printf("Unit testing completed successfully!\n");
else
## A call to unit_test typically looks like this:
##
## unit_test("scalar integer addition", 1, eval("1 + 1 == 2;"));
-
+
global unittest_results;
-
+
## Sanity check input parameters
if ( nargin < 3 || nargin > 4 )
error("Function run_rest expects 3 or 4 parameters.");
endif
-
+
if (!ischar(test_title))
error("Expecting TEST_TITLE (arg 1) to be a string.");
endif
-
+
if (expect_pass != 0 && expect_pass != 1)
error("Expecting EXPECT_PASS (arg 2) to be 0 or 1.");
endif
-
+
unittest_results.total++;
## Take actions depending on what test result we expect
endif
return;
endif
-
+
if (expect_pass == 1 && actual_result == 1)
result = "PASS";
if (unittest_results.verbose != 0)