Unit test - how do you check if a variable is undefined or null?
Hi everyone
I'm new in Cache development language and I'm starting setting up Unit Tests.
How do you check with an Assert whether a certain variable is null or undefined?
The following assert throws an error on the "null": do $$$AssertEquals(myObj, null, "myObj is null")
Thanks a lot for your help and best regards
Milena
Comments
Try "" instead of null which would be a variable named null
do $$$AssertEquals(myObj, "", "myObj is null")
or
set null="" do $$$AssertEquals(myObj, null, "myObj is null")
or to cover undefined as well
do $$$AssertEquals($GET(myObj), "", "myObj is null")
Also possible to use $$$NULL macro:
do $$$AssertEquals(myObj, $$$NULL, "myObj is null")
I like macros but I'm always careful concerning their public availability.
This has changed to often over recent releases.
You are right, the macro $$$NULL present only in %sqlMigration.inc and this is not the file that developers often include to its project.
I prefer to use the macro $$$NULLOREF/$$$NULLOID from %occExtent.inc, which is available by default in the class that inherits from %Library.Base, and for routines is enough to include %systemInclude.inc.
Undefined variable and the variable contains "" (null) is two different situations, e.g. (see $DATA):
<FONT COLOR="#0000ff">kill </FONT><FONT COLOR="#800000">myObj </FONT><FONT COLOR="#0000ff">write $data</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">myObj</FONT><FONT COLOR="#000000">),! </FONT><FONT COLOR="#008000">; -> 0 </FONT><FONT COLOR="#0000ff">set </FONT><FONT COLOR="#800000">myObj</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#0000ff">$$$NULLOREF write $data</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">myObj</FONT><FONT COLOR="#000000">),! </FONT><FONT COLOR="#008000">; -> 1</FONT>In your case it would be better to use $IsObject:
<FONT COLOR="#0000ff">kill </FONT><FONT COLOR="#800000">myObj
</FONT><FONT COLOR="#0000ff">write $IsObject</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">myObj</FONT><FONT COLOR="#000000">),! </FONT><FONT COLOR="#008000">; -> 0
</FONT><FONT COLOR="#0000ff">set </FONT><FONT COLOR="#800000">d</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#0000ff">$$$NULLOREF
write $IsObject</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">myObj</FONT><FONT COLOR="#000000">),! </FONT><FONT COLOR="#008000">; -> 0
</FONT><FONT COLOR="#0000ff">set </FONT><FONT COLOR="#800000">myObj</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#ff00ff">{}
</FONT><FONT COLOR="#0000ff">write $IsObject</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">myObj</FONT><FONT COLOR="#000000">),! </FONT><FONT COLOR="#008000">; -> 1</FONT>Accordingly, should be <FONT COLOR="#0000ff">do $$$AssertTrue</FONT><FONT COLOR="#000000">('</FONT><FONT COLOR="#0000ff">$IsObject</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">myObj</FONT><FONT COLOR="#000000">), </FONT><FONT COLOR="#008000">"myObj is null"</FONT><FONT COLOR="#000000">)</FONT>