Test code in includes

Everything else that doesn't fall into one of the other PB categories.
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Test code in includes

Post 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
%101010 = $2A = 42
User avatar
Demivec
Addict
Addict
Posts: 4283
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Test code in includes

Post 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.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Test code in includes

Post by IdeasVacuum »

Huh? :shock:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

Re: Test code in includes

Post 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
User avatar
Bisonte
Addict
Addict
Posts: 1335
Joined: Tue Oct 09, 2007 2:15 am

Re: Test code in includes

Post 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 ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

Re: Test code in includes

Post by xorc1zt »

i don't understand

when you press f5, you run with the debugger so the code is not ignored
User avatar
Bisonte
Addict
Addict
Posts: 1335
Joined: Tue Oct 09, 2007 2:15 am

Re: Test code in includes

Post 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 ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Test code in includes

Post 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.
%101010 = $2A = 42
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Test code in includes

Post 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.
%101010 = $2A = 42
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Test code in includes

Post 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
%101010 = $2A = 42
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Test code in includes

Post 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 :lol:
%101010 = $2A = 42
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Test code in includes

Post 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.
%101010 = $2A = 42
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Test code in includes

Post by IdeasVacuum »

....would it be OK to just comment-out MyTestProc2 when not in use?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Bisonte
Addict
Addict
Posts: 1335
Joined: Tue Oct 09, 2007 2:15 am

Re: Test code in includes

Post 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...)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Test code in includes

Post 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.
%101010 = $2A = 42
Post Reply