Day 006 - a few more unit tests, then back on track
In a comment to the Day 4 installment, Richard Rose pointed out that WD17 is in fact out of beta. I need to crawl out from under my rock more often. Thanks Richard! I'm downloading the update now, but today's hour will still be with WD16.
Yesterday I got sidetracked by my wish to do some unit testing, and today I feel the itch to run just one more little test. I'd like to find out a bit more about how WinDev handles strings.Â
Here's my first attempt a test procedure.Â
PROCEDURE ReplaceCharactersInAString_VerifyResult(baseString,newCharacters,startPos) endPos is int endPos = startPos + newCharacters..TextLength - 1 baseString[[startPos TO endPos]] = newCharacters RESULT baseString
After writing this procedure I right-clicked on the procedure in the Project Explorer and chose Run the procedure test. This isn't an automated test, it's a handy way to manually test the code.Â
I set some values and hit the Go button. And the procedure crashed:
It turns out that although TextLength is a property, I'm not allowed to use it on strings. But just for fun I decided to hit the Debug button, which brought up the debugger window so I could step through the code.Â
Â
It's easy to set break points in WinDev - just click in the far left column. When you run the code (or at least a procedure, as in this example) you're automatically in debug mode.Â
As you can see from the screen shot, I rewrote my code to use the Length() function and that worked. Here are the results of the procedure call:
Automated testing
I don't want to have to run manual tests all the time. But as I've previously noted, it's easy to create automated tests. Again, from the Project Explorer I added a new test:
At this point I realized that I'd named my global procedure as if it were the test. So I renamed it to
ReplaceCharactersInAString
and I called my new testÂ
ReplaceCharactersInAString_VerifyResult
Then I realized that by creating the test via the Tests node in the Project Explorer I was getting a completely blank test; instead, I wanted a test already configured to use my procedure. So I decided to delete the test.
And I found that I couldn't delete just one test. I could delete the entire set, but not the one test. If someone knows a way to do this, please let me know. Perhaps it's a safety feature to prevent tests from being accidentally deleted. But I'd still like to be the one making that decision.
As a workaround I renamed the empty test I'd created. Then I right-clicked on my ReplaceCharactersInAString procedure and chose Create a unit test.Â
I changed the new unit test's properties to specify the data types and a more meaningful name for the return value:
When I tried running the test, however, I got an "Unable to assign the element" error. I had to change my code to copy the passed string to a local string, modify that, then return the local string.Â
PROCEDURE ReplaceCharactersInAString(baseString,newCharacters,startPos) endPos is int newString is string newString = baseString endPos = startPos + Length(newCharacters) - 1 newString[[startPos TO endPos]] = newCharacters RESULT newString
I set up a few test iterations with different parameters. In particular I wondered what would happen with what seemed like invalid values.Â
The first three iterations all passed. The last two did not:
This suggests to me that strings in WinDev are more like .NET strings than like Clarion strings. In .NET, strings are immutable, meaning you can't change them; when you make what appears to be a change to an existing string, you're actually creating a new string to contain the result. I'm thinking a similar process is at work in the W language.Â
Resume your normal (string) route
As much as I like unit testing, I'm going to leave that for now and go back to the tutorial, which on page 57 is still on the subject of strings.
WinDev has the usual complement of string handling functions, including Upper(), Lower(), Left(), Right(), Length() etc. Trimming space characters is often important: the equivalent to a Clarion Clip(Left()) is NoSpace() which removes leading and trailing spaces. The string handling library is extensive - you can find a complete listing of functions here.Â
Handling numeric values
Numeric values are assigned using the usual operators, and you can also concatenate strings and numerics directly without having to cast the numeric values. Since + is both the concatenation and the addition operator, if you want to do addition instead of concatenation you should enclose the operation in parentheses.Â
As with strings, the online help lists the extensive numeric function library.Â
And just like that, I'm out of time again. Friday is my regular day off in this series, so I'll back on Monday on page 60, and I hope not to stay on track for at least a couple of days!, this is the Victoria Day long weekend here in Canada, and we're off camping until Tuesday, so the next installment will be on Wednesday May 23.Â
Â
Â