In Part 6 of this series I began rewriting some of the embed code from the UpdateDetail procedure in the Invoice app as a reusable, testable class. Although I simply showed some test code on its own, that code lives inside a ClarionTest-compatible test DLL.
...
Now, right-click on the procedure and choose Source to bring up the Embeditor
There really are only two embeds you need to be concerned about in the test procedure. One is the data embed, where you declare any data you need for the test. The other is Priority 5000, right before the return statement. That's where you place your test code.
Here's the code for the data embed:
...
There are a number of ways you can declare classes in Clarion; some of them are fraught with danger. For instance, if you create classes that you export from a DLL so they can be used elsewhere, you had better get everything just right or you'll probably be looking at GPFs.
The approach I'll show you is simple and lends itself to class reuse - it also makes it easier to move your classes into a DLL in the future should you want to do so.
First, add a new Clarion source file to your test DLL project (you can move it later). Now, adding a new source file to C7 via the solution explorer is pretty buggy.
Right-click on the project entry in the solution explorer (not the .APP itself). Choose Add | New Item, and pick any of the Clarion file types, it doesn't matter which.
C7 will add the file to the project tree, and it will open the file in the editor. But the file doesn't yet exist on disk. So you have to save it first, making sure you give it exactly the same name again.
One more thing - highlight the file in the solution explorer, and on the Properties pad change the Build Action from Compile to None. You don't want the project system compiling this file; it's going to be an INCLUDEd file.
With this new source file open (I called mine InvoiceDetail.clw) delete everything in the file and replace it with this code:For purposes of this article, I'll follow the approach I outlined in Quick starts for classes.
Using the Quick Starts described in that article (or by creating a file from scratch, create a file called InvoiceDetail.inc with the following contents:
Code Block |
---|
InvoiceDetail Class,Type,module('InvoiceDetail.clw'),link('InvoiceDetail.clw',1)
GetExtended procedure,real
Init procedure(real price,long quantity)
End |
Next create InvoiceDetail.clw:
Code Block |
---|
|
...
member |
...
|
...
|
...
|
...
map |
...
|
...
|
...
|
...
end |
...
include(' |
...
InvoiceDetail.inc'),once InvoiceDetail.GetExtended |
...
PROCEDURE |
...
code |
...
|
...
return 0 InvoiceDetail.Init procedure(real price,long quantity) |
...
code |
Now go back to the DLL APP file again. On the global embeds choose the After Global INCLUDEs embed point and insert the following source:
Include('InvoiceDetail.clwinc','Header')
Save the embed. Go to the Program Routines global embed and add the following source:
Include('InvoiceDetail.clw','Methods')
Notice that the Include statements both use a second parameter which corresponds to the Section heading in the source file. What you've done is include just the class header in the first embed, and the class methods in the second embed. You have to do this because it's a Clarion language requirement that class headers appear in the data section and class methods in the code section, and Program Routines is just a handy location in the program's code section.
You should now be able to compile the DLL.
Setting up ClarionTest
There's one more thing you should do now to make unit testing as painless as possible, and that's to set up ClarionTest in the C7 Tools menu. Choose Tools | Options, and from the list of Options choose Tools again. Click on Add to create an new external tool entry. Set the Command field to point to ClarionTest.exe, and set the Arguments field to ${TargetPath} as in Figure 8.
...