On Fri, Aug 31, 2001 at 01:42:48PM -0400, darren chamberlain wrote:
> While beginning to use inline tests, Shane Landrum and I have
> started writing Test::Inline::Cookbook. It contains a few small
> snippets of code which we found useful while writing inline tests
> for a local module.
Hmmm, these aren't really specific to Test::Inline. They should go
into Test::Tutorial or better yet a general Test::HOWTO or
Test::Cookbook.
I'lll just s/Test::Inline/Test/ and this thing can become the start of
Test::Cookbook.
> NAME
> Test::Inline::Cookbook
>
> DESCRIPTION
> This file lists some ways you might want to use inline tests. Mostly,
> it's things you wouldn't know just from reading Test::Inline::Tutorial.
>
> Testing caller() scope
>
> So, let's say I have a routine that uses caller() to do something, and I
> need to test it. Here's my code:
>
> sub new {
> my $class = shift;
> my $name = shift || caller;
> my $value = shift || time;
>
> bless [ $name, [ $value ], ], $class;
> }
>
> sub name {
> my $self = shift;
> return $self->[0];
> }
>
> Because this routine uses caller() as a default, I need to be in another
> package to test that it's doing the right thing with caller. Here's a
> snippet of testing code that shifts to another package before testing.
>
> =begin testing
>
> my $x;
> {
> package Foo;
> $x = local::Config::Var->new;
> }
> is($x->name, "Foo", "new() with no arguments");
>
> =end testing
>
> Testing tied interfaces
>
> So, if I have a tied interface to my module, sometimes I need to test
> that the tying routines are doing the right things. Here's some code:
>
> sub TIESCALAR { shift->new(@_) }
> sub FETCH { shift->value(@_) }
> sub STORE { shift->set(@_) }
> sub DESTROY { }
>
> And here's how to test it:
>
> =begin testing
>
> tie $v, 'local::Config::Var', "Foo", 42;
>
> is(ref tied($v), 'local::Config::Var', "TIESCALAR works");
>
> $v = 43;
> is ((tied $v)->value, 43, "STORE works");
>
> $x = $v;
> ok(($x == (tied $v)->value), "FETCH works with numbers");
>
> $v = "string";
> ok(($v eq "string"), "FETCH works with strings");
>
> =end testing
>
> =cut
>
> AUTHORS
> Shane Landrum <[EMAIL PROTECTED]> Darren Chamberlain <[EMAIL PROTECTED]>
>
> SEE ALSO
> the Test::Inline::Tutorial manpage
--
Michael G. Schwern <[EMAIL PROTECTED]> http://www.pobox.com/~schwern/
Perl6 Quality Assurance <[EMAIL PROTECTED]> Kwalitee Is Job One
...and I pull out the Magnum from under the desk where I keep it in case
someone laughs at a joke that's so dry it's got a built in
water-fountain, and blow the lot of them away as a community Service.
-- BOFH