21.10.2015, 00:30
You can do it like this:
Put each module into a separate include file, like I did for the AB3 includes.
You can write the unit tests directly into the include file guarding with "CNIF #__include = 0 ... CEND", so that the code will only be compiled if the include is the main code. If the include is actually included, the unit test code will be ignored.
[ab3]XINCLUDE "other_dependencies.include.ab3"
; ... my modules code ...
Function.s my_ModuleFunc{}
Function Return "foo"
End Function
CNIF #__include = 0
NPrint "Here comes the unit test for \\__THIS_INCLUDE..."
!TEST_EQ{my_ModuleFunc{}, "foo"}
!TEST_NE{my_ModuleFunc{}, "bar"}
NPrint "Unit test successfully passed."
End
CEND[/ab3]
You can write your own test macros like this
[ab3]Macro TEST_EQ ; { a = b }
If (`1) = (`2)
NPrint "\\__THIS_INCLUDE Unit test successful: \\22`1\\22 = \\22`2\\!"
Else
NPrint "\\__THIS_INCLUDE Unit test failed: \\22`1\\22 = \\22`2\\!"
PutD0 -1 ; exit with error code -1
End D0
End If
End Macro[/ab3]
It is also a good practice to add parameter tests to your functions. You can eliminate the overhead if you guard them with the #__debug constant, so they don't slow your release executable down:
[ab3]Macro _ASSERT ; {condition}
CNIF #__debug
If ((`1)=0) Then NPrint "\\__THIS_INCLUDE/\\__THIS_FUNCTION(\\__CODELINE): Assert failed: \\22`1\\22!" : End
CEND
End Macro
Function.l DrawObject(*myobj.object, x.l, y.l}
!_ASSERT{*myobj >< Null}
!_ASSERT{x >= 0}
!_ASSERT{y >= 0}
...
End Function[/ab3]
Put each module into a separate include file, like I did for the AB3 includes.
You can write the unit tests directly into the include file guarding with "CNIF #__include = 0 ... CEND", so that the code will only be compiled if the include is the main code. If the include is actually included, the unit test code will be ignored.
[ab3]XINCLUDE "other_dependencies.include.ab3"
; ... my modules code ...
Function.s my_ModuleFunc{}
Function Return "foo"
End Function
CNIF #__include = 0
NPrint "Here comes the unit test for \\__THIS_INCLUDE..."
!TEST_EQ{my_ModuleFunc{}, "foo"}
!TEST_NE{my_ModuleFunc{}, "bar"}
NPrint "Unit test successfully passed."
End
CEND[/ab3]
You can write your own test macros like this
[ab3]Macro TEST_EQ ; { a = b }
If (`1) = (`2)
NPrint "\\__THIS_INCLUDE Unit test successful: \\22`1\\22 = \\22`2\\!"
Else
NPrint "\\__THIS_INCLUDE Unit test failed: \\22`1\\22 = \\22`2\\!"
PutD0 -1 ; exit with error code -1
End D0
End If
End Macro[/ab3]
It is also a good practice to add parameter tests to your functions. You can eliminate the overhead if you guard them with the #__debug constant, so they don't slow your release executable down:
[ab3]Macro _ASSERT ; {condition}
CNIF #__debug
If ((`1)=0) Then NPrint "\\__THIS_INCLUDE/\\__THIS_FUNCTION(\\__CODELINE): Assert failed: \\22`1\\22!" : End
CEND
End Macro
Function.l DrawObject(*myobj.object, x.l, y.l}
!_ASSERT{*myobj >< Null}
!_ASSERT{x >= 0}
!_ASSERT{y >= 0}
...
End Function[/ab3]