Closed lptr closed 10 years ago
One more thing: omitting the optional parameter like this:
m.returnSomething(cast anyString).returns("tibor");
…makes it break for both JS and Flash, regardless of the default value (0
or 7
) of the optional parameter.
The only workaround I found right now is this:
var OPTIONAL_INT = #if flash 0 #else null #end;
m.returnSomething(cast anyString, OPTIONAL_INT).returns("tibor");
This way both JS and Flash compile, but it's ugly.
One more note: this doesn't work in JS, but works in Flash:
m.returnSomething(cast anyString, cast anyInt).returns("tibor");
This is due to the differences on how default values are resolved on static and dynamic platforms (see http://haxe.org/manual/basic_types#statically-typed-platforms). Mockatoo detects if the platform is static or dynamic and ensures that valid 'nullified' values are used on optional function args.
In this case, the other
arg is matching against null
on js, and 0
on flash.
When the mock class is being generated at macro time, ClassField args (e.g. ?other:Int=0
) dont expose the default value expr (e.g. 0
), so I cant automatically use this value as the default across all platforms.
This limitation shouldn't matter for mocking anyway, as you should be matching on what is passed through, not what it defaults to internally when nullified.
To explicitly match across all platforms perhaps you should either use the any
matcher
var m = Mockatoo.mock(Something);
assertThat(m.returnSomething("lajos"), is(null));
m.returnSomething(cast anyString,cast any).returns("tibor");
assertThat(m.returnSomething("lajos"), is("tibor"));
Or make the interface null safe across static and dynamic platforms
interface Something {
public function returnSomething(something:String, ?other:Null<Int> = 0):String;
}
And then match explicitly against null
var m = Mockatoo.mock(Something);
assertThat(m.returnSomething("lajos"), is(null));
m.returnSomething(cast anyString,null).returns("tibor");
assertThat(m.returnSomething("lajos"), is("tibor"));
Thanks for the clarification. What you say makes sense for explicitly checking for the value. Maybe you could mention this in the Mockatoo documentation? Then it would make a lot of sense.
This limitation shouldn't matter for mocking anyway, as you should be matching on what is passed through, not what it defaults to internally when nullified.
As I wrote above in the first comment, this didn't work either:
m.returnSomething(cast anyString).returns("tibor");
Could Mockatoo inject any
for any unspecified optional parameters to make it work?
This is now implemented on master (only 7 months later!)
This works running the tests with Flash:
The same code breaks in JS:
However, using
null
instead of0
makes it work in JS:…but breaks it in Flash:
A side note: changing the default value given (
0
in the above example) does not make any difference. If I change it to7
like this:…nothing changes:
m.returnSomething(cast anyString, 0).returns("tibor");
is still the only way to make Flash work, andm.returnSomething(cast anyString, null).returns("tibor");
is still the only way to make JS work.