I have a library module (lib) with functions in which variables from another module (const) are used. I now want to test the functions from the lib module in a test module. I have tried changing the variables from the const module for certain tests. I am not sure if this is even possible. Here is the code:
const Module:
xquery version "3.1" encoding "utf-8";
module namespace const = "Constant";
declare variable $const:numbers:=
<numbers>
<value s='one'>1</value>
<value s='two'>2</value>
<value s='three'>3</value>
<value s='four'>4</value>
<value s='five'>5</value>
<value s='other'></value>
</numbers>;
lib Module:
xquery version "3.1" encoding "utf-8";
module namespace lib = "Library";
import module namespace con="Constant" at "const.xqm";
declare function lib:inc5($val as xs:string) as xs:integer {
5+xs:integer($con:numbers//value[@s=$val])
};
test Module:
xquery version "3.1" encoding "utf-8";
module namespace test='http://basex.org/modules/xqunit-tests';
import module namespace con="Constant" at "const.xqm";
import module namespace lib="Library" at "lib.xqm";
declare %unit:test function test:inc5_add_one() {
unit:assert-equals(lib:inc5('one'), 6)
};
declare %unit:test function test:inc5_set_other_as_10_add_ten() {
(: replace node $con:numbers//value[@s='other'] with <value s='ten'>10</value> :)
(:
$con:numbers=<numbers>
<value s='one'>1</value>
<value s='two'>2</value>
<value s='three'>3</value>
<value s='four'>4</value>
<value s='five'>5</value>
<value s='ten'>10</value>
</numbers>,
:)
unit:assert-equals(lib:inc5('ten'), 15)
};
I wanted to replace the node <value s='other'></value> with <value s='ten'>10</value> in the test:inc5_set_other_as_10_add_ten in order to test lib:inc5('ten') with the expected value 15.
What I have tried is resetting $con:numbers or changing the value with replace node with statement. But neither of these works.
My question is if it is somehow possible to change $const:numbers at this point so that it is used the next time lib:inc5 is called.
Please note that it’s not possible to changes the values of variables in XQuery and functional languages in general. Instead, you can pass on the result of an update expression to another operation. Your example could be rewritten as follows:
lib Module:
test Module:
lib:inc5function has two parameters, one for the string to be found and another one for the context element.$con:numberselement, the result is passed on to the lib function, and the result is compared with the expected value.