Page 1 of 2
Test code in includes
Posted: Tue Jan 17, 2012 4:51 pm
by Perkin
I want to add some test code in an include file, so if compiled on its own it runs a few tests, but if included from a main project that code is not included. I'm pretty sure I've got it right, but want to get some clarification and correction if I'm wrong. If the Test code uses all the procedures I don't want them all included in the project unless they're called from the main project or other procs which are to be included.
At moment in main project I'm defining a constant #INCLUDED=1 and in include files I'm adding
Code: Select all
CompilerIf Defined(INCLUDED,#PB_Constant)=0
;- Test code inserted here...
CompilerEndIf
Is that the correct way to do it or is there another way
Re: Test code in includes
Posted: Tue Jan 17, 2012 5:22 pm
by Demivec
Perkin wrote:but if included from a main project that code is not included
@Perkin: The example you describe seems a little confusing. You say you want the code to be added if compiled on its own but not to be added if it is included in a main project, wouldn't it be the other way around? Why else would you be including it if you didn't want it included?
Perkin wrote:At moment in main project I'm defining a constant #INCLUDED=1 and in include files I'm adding
Code: Select all
CompilerIf Defined(INCLUDED,#PB_Constant)=0
;- Test code inserted here...
CompilerEndIf
Is that the correct way to do it or is there another way
That is the typical way of including code only once or if you want the compiling of it to be optional (by defining or not defining the constant). I would also make the constant unique ('#INCLUDED' seems common and non-distict) so that it is only associated with your test code.
Re: Test code in includes
Posted: Tue Jan 17, 2012 5:31 pm
by IdeasVacuum
Huh?

Re: Test code in includes
Posted: Tue Jan 17, 2012 5:43 pm
by xorc1zt
you should use #PB_Compiler_Debugger
Code: Select all
CompilerIf #PB_Compiler_Debugger
;this code will not be included when you compile your final executable
Debug "Hello test mode"
CompilerEndIf
Re: Test code in includes
Posted: Tue Jan 17, 2012 6:56 pm
by Bisonte
xorc1zt wrote:you should use #PB_Compiler_Debugger
Code: Select all
CompilerIf #PB_Compiler_Debugger
;this code will not be included when you compile your final executable
Debug "Hello test mode"
CompilerEndIf
But if you want to test your code (F5)...
The method of the first post is right... so I do

Re: Test code in includes
Posted: Tue Jan 17, 2012 7:25 pm
by xorc1zt
i don't understand
when you press f5, you run with the debugger so the code is not ignored
Re: Test code in includes
Posted: Wed Jan 18, 2012 12:53 am
by Bisonte
yes... thats what I mean
If you have an examplecode in it ? it will executed.
like This...
Code: Select all
Procedure MyTestProc()
MessageRequester("Hey","I am here!")
EndProcedure
CompilerIf #PB_Compiler_Debugger
;this code will not be included when you compile your final executable
Debug "Hello test mode"
MyTestProc()
CompilerEndIf
If you want to test your code, and you ARE NOT compiling the final .exe so the example code is executed...
But the example code should only executed if the "includefile" is a single file to test whats in the include...
not if you included the includefile

huh strange sentences

Re: Test code in includes
Posted: Wed Jan 18, 2012 2:13 am
by Perkin
Demivec wrote:Perkin wrote:but if included from a main project that code is not included
@Perkin: The example you describe seems a little confusing. You say you want the code to be added if compiled on its own but not to be added if it is included in a main project, wouldn't it be the other way around? Why else would you be including it if you didn't want it included?
Should read as :
but if included from a main project that _testing_ code is not included.
Clarifying further - If in the test code I call all the procedures, all of them would be included if i compiled just this file to a test executable.
Now if compiling from main project, would they all be included if that testing code is in the compilerif - so only the procedures that are called directly (or from those called) are selected for inclusion.
Sorry for any (further) confusion, I'm not always particularly clear in explaining things.
Re: Test code in includes
Posted: Wed Jan 18, 2012 2:31 am
by Perkin
Say I've got
Code: Select all
Procedure MyTestProc()
MessageRequester("Hey","I am here!")
EndProcedure
Procedure MyTestProc2()
MessageRequester("Hey","I am here as well!")
EndProcedure
CompilerIf Defined(INCLUDED,#PB_Constant)=0
;this code will not be included when you compile your final executable
Debug "Hello test mode"
MyTestProc()
MyTestProc2()
CompilerEndIf
If compiling this file to run with debugger or without or to own test executable, both procedures will be included - and rightly so as they're both used.
If compiling main project which has #INCLUDED defined, then the code inside the CompilerIf won't be included.
If the main project only calls MyTestProc(), will MyTestProc2() still be included or not, it isn't called from the main project, only the test code inside of the CompilerIf.
IIRC I think from some other forum threads, that if a procedure is called from another procedure they're both included, even if the second one isn't called directly. As this test code is actually skipped would this cause the MyTestProc2() to still be added to the final main project exe.
Now I've really confused you.
Re: Test code in includes
Posted: Wed Jan 18, 2012 2:37 am
by Perkin
Demivec wrote:I would also make the constant unique ('#INCLUDED' seems common and non-distict) so that it is only associated with your test code.
So I should change it to something like #MainProjectCompile
Re: Test code in includes
Posted: Wed Jan 18, 2012 2:43 am
by Perkin
Bisonte wrote:
If you want to test your code, and you ARE NOT compiling the final .exe so the example code is executed...
But the example code should only executed if the "includefile" is a single file to test whats in the include...
not if you included the includefile
huh strange sentences 
I think you've got that right
I may be deluded. I think I've included enough includes now, I think I'll now go and be excluded and secluded for a while

Re: Test code in includes
Posted: Wed Jan 18, 2012 3:05 am
by Perkin
Saved as IncTest_include.pbi
Code: Select all
Procedure MyTestProc()
MessageRequester("Hey","I am here!")
EndProcedure
Procedure MyTestProc2()
MessageRequester("Hey","I am here as well!")
EndProcedure
CompilerIf Defined(INCLUDED,#PB_Constant)=0
;this code will not be included when you compile your final executable
Debug "Hello test mode"
MyTestProc()
MyTestProc2()
CompilerEndIf
As a test using above code included from a test main project which is just :
Code: Select all
#INCLUDED=1
XIncludeFile "IncTest_include.pbi"
MyTestProc()
Bumblebees!
The resultant main project code runs as it should, but viewing the exe in a hex viewer, it looks like the MyTestProc2 is still included, even though it isn't called directly - only from the now not included test code.
Re: Test code in includes
Posted: Wed Jan 18, 2012 3:21 am
by IdeasVacuum
....would it be OK to just comment-out MyTestProc2 when not in use?
Re: Test code in includes
Posted: Wed Jan 18, 2012 9:20 am
by Bisonte
If you want "not included" unused procedures...
jaPBe has a "Cutter" Plugin, that shredder the code to one big file with only the used procedures...
(for the final compile its very useful...)
Re: Test code in includes
Posted: Wed Jan 18, 2012 11:04 am
by Perkin
IdeasVacuum wrote:....would it be OK to just comment-out MyTestProc2 when not in use?
The include will be used by a few projects, some using different routines.
Bisonte wrote:If you want "not included" unused procedures...
jaPBe has a "Cutter" Plugin, that shredder the code to one big file with only the used procedures...
(for the final compile its very useful...)
I'll give jaPBe a try, haven't used it for a while.
If not I'll just have to comment out the test code, and uncomment it when testing, or have the test code in a separate file and do it as mini project.
Thanks to all for your input.