Dne 15. 04. 19 v 18:29 Pavel Hofman napsal(a):
> Hi,
>
> Please is there any way to source a script defining global variables in
> unit test (%!test) and have these available in functions called within
> the test?
>
> This works:
>
> %!test
> %! global dataDir = '/tmp';
> %! func_using_global_dataDir();
>
> But this does not work, dataDir is not available in
> func_using_global_dataDir():
>
>
> %!test
> %! source 'consts.m';
> %! func_using_global_dataDir();
>
> Where script consts.m contains
> global dataDir = '/tmp';
>
>
> Yet when consts.m is sourced from another script (in the main code, not
> the unit script %! section), the global vars defined in consts.m are
> available in the script as well as all functions called by the script.
>
> Tested with Octave 4.2.2 in Linux.
It may be related to this problem:
%!test
%! global A;
%! A = 1;
%! funcX_calling_funcY();
This global variable A is available in function funcY() called from
funcX_calling_funcY()
But when defining global vars with a one-liner:
%!test
%! global A = 1;
%! funcX_calling_funcY();
the global var A is NOT available in funcY(), ONLY in funcX_calling_funcY().
The same holds if the sourced/included file defines the globals with two
lines:
consts.m:
global A;
A = 1;
instead of the one-liner
global A = 1;
But the oneliner global works OK in regular code, just unit tests have
this second-level problem.
Again, my Octave is a bit older 4.2.2.
Thanks a lot.
Pavel.
Can you provide a small, comprehensible, and complete test within a ZIP file? Maybe this already works with Octave 5.1.0.
Otherwise, why don't you call a function "my_consts" on the workspace, providing the necessary test constants?
function [const_a, const_b, ....] = my_consts ()
const_a = pi;
#...
endfunction
In some test context:
%!test
%! [~,b] = my_consts ();
%! ## Some useful test
HTH,
Kai