Hello, all.

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.  As we integrate inline testing into all of
our internally developed modules, we will continue to expand on
it.  Currently it resides on an internally managed CVS server,
but it can be moved somewhere public on request.

I will include the document in its entirety (it's still pretty
short), and make available a tarball if anyone is interested in
it (it is in an installable form currently).

(darren)

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

-- 
The limits of my language mean the limits of my world.
    -- Ludwig Wittgenstein

Reply via email to