Day 046 - Method overloading and testing issues
As you'll recall from yesterday, I had a class like this:
MyClass is class END PROCEDURE Constructor() PROCEDURE Destructor() PROCEDURE MethodA(intValue is int) RESULT intValue PROCEDURE MethodA(strValue is string) RESULT strValue PROCEDURE MethodB(value) RESULT value
and I was doing some unit testing. Specifically, I wanted to explore procedure overloading. And to that end I decided to try passing a string to MethodA to verify that overloading works as I expect.Â
But I had a problem. The unit test interface only showed me options for the int syntax. I tried entering a string, with or without quotes, and although the entry value was accepted, after I ran the test the parameter and the expected result had both been set to 0.Â
But there is a solution for testing overloaded methods. Over on the right side there's a button to edit the parameters.Â
On the edit parameters window I set the input and control parameters to <automatic>.Â
I was then able to pass in either a string or a numeric value. I verified that the appropriate method was called with some debug statements. (I could just as easily have set a return value based on which method was called, but I've often used OutputDebugString when debugging complex processing, and it's a nice feature to have.)
To recap, parameters can be of automatic type (determined at runtime) or they can have a defined type. And you can overload methods. But what if you have a method that takes an automatic type? Can you overload it with a syntax for a specific type?Â
Here are my two methods:
PROCEDURE MethodB(value) Debug("automatic syntax used for value " + value) RESULT value PROCEDURE MethodB(value is int) Debug("int syntax used for value " + value) RESULT value
I ran the test twice, once with a string and once with an int. Here's the trace output:
[3444] automatic syntax used for value abc [3444] int syntax used for value 35
I really didn't expect that to work, but it did!Â
Besides parameters, return value types can, apparently, also be determined at runtime. I wrote the following code:
PROCEDURE MethodC(nParam is int) nValue is int sValue is string nValue = 12 sValue = "abc" IF nParam = 1 THEN RESULT nValue END RESULT sValue
and set up a test case. But I was unable to run the test, and I also found that I could no longer run the MethodA test. I fiddled around with this for a while, but no joy.Â
I'll take another kick at the unrunnable tests tomorrow.Â