[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Sourcing global variables in unit tests
From: |
Pavel Hofman |
Subject: |
Re: Sourcing global variables in unit tests |
Date: |
Mon, 15 Apr 2019 18:47:09 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 |
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.