Subject: | Feature request: Test the coercion result |
I use coercion a lot. Its not enough to test that something passes a type constraint, but also that it gets coerced into the expected value. I can think of several approaches.
One would be to pass in the value as an extra option. This is simple but limits how the value can be tested.
One way would be to return the coerced value. This lets the user do whatever they want with the value, but it breaks the testing interface that a passing test should return if it passed or failed.
The one I prefer is to let the user pass in a code reference which contains whatever tests they like. The user can then test the value however they like. For compactness, the code ref can be passed in as the coerce value. This encourages folks to test their coercions rather than just turning it on.
type_isa "/foo/bar/baz.txt", "Path::Class::File", "str to file coercion", coerce => sub {
my $value = shift;
isa_ok $value, "Path::Class::File";
is $value, "/foo/bar/baz.txt";
};
What do you think?
One would be to pass in the value as an extra option. This is simple but limits how the value can be tested.
One way would be to return the coerced value. This lets the user do whatever they want with the value, but it breaks the testing interface that a passing test should return if it passed or failed.
The one I prefer is to let the user pass in a code reference which contains whatever tests they like. The user can then test the value however they like. For compactness, the code ref can be passed in as the coerce value. This encourages folks to test their coercions rather than just turning it on.
type_isa "/foo/bar/baz.txt", "Path::Class::File", "str to file coercion", coerce => sub {
my $value = shift;
isa_ok $value, "Path::Class::File";
is $value, "/foo/bar/baz.txt";
};
What do you think?