changeset 23847:0d5fc6e4a96c

run.m: New tests for script and function file execution.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Wed, 09 Aug 2017 11:24:54 +0200
parents 12203140139f
children cbea10ff137e
files scripts/miscellaneous/run.m
diffstat 1 files changed, 41 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/run.m	Tue Aug 08 17:09:37 2017 -0700
+++ b/scripts/miscellaneous/run.m	Wed Aug 09 11:24:54 2017 +0200
@@ -94,3 +94,44 @@
 %!error run ()
 %!error run ("a", "b")
 %!error <SCRIPT must exist> run ("__A_very_#unlikely#_file_name__")
+
+## Test script file execution
+%!test
+%! unwind_protect
+%!   clear A  # the variable "A" should be set by the script
+%!   assert (exist ("A"), 0);
+%!   tmp_dir = tempname ();
+%!   mkdir (tmp_dir);
+%!   test_script = fullfile (tmp_dir, "test_script.m");
+%!   fid = fopen (test_script, "w");
+%!   fprintf (fid, "A = 1337;\n");
+%!   fclose (fid);
+%!   run (test_script);
+%!   assert (exist ("A", "var"), 1);
+%!   assert (A, 1337);
+%! unwind_protect_cleanup
+%!   rmdir (tmp_dir);
+%! end_unwind_protect
+
+## Test function file execution
+%!test
+%! unwind_protect
+%!   tmp_dir = tempname ();
+%!   mkdir (tmp_dir);
+%!   test_function = fullfile (tmp_dir, "tf.m");
+%!   fid = fopen (test_function, "w");
+%!   fprintf (fid, "function tf ()\n");
+%!   fprintf (fid, "addpath (\"%s\");\n", tmp_dir);
+%!   fprintf (fid, "endfunction\n");
+%!   fclose (fid);
+%!   ## Check if temporary directory is on the loadpath.
+%!   ## Function `dir_in_loadpath` seems not applicable for this check.
+%!   dirs = strsplit (path (), ":");
+%!   assert (any (strcmp (tmp_dir, dirs)), false)
+%!   run (test_function);
+%!   dirs = strsplit (path (), ":");
+%!   assert (any (strcmp (tmp_dir, dirs)))
+%! unwind_protect_cleanup
+%!   rmdir (tmp_dir);
+%!   rmpath (tmp_dir);
+%! end_unwind_protect