On Tue, Mar 08, 2005 at 01:42:49PM -0800, Michael G Schwern wrote:
> On Tue, Mar 08, 2005 at 09:36:24PM +0000, Fergal Daly wrote:
> > Cool, so actually T::B::Counter and T::B::Run are not singletons and
> > Test::Builder is.
> 
> No, other way around.  When a TB instance needs a TB::Counter it just says 
> $tb->counter which, normally, returns a singleton but you can alter counter()
> so it returns something else.

A singleton is a class that only ever has 1 instance in existence. If there
can be multiple instances of TB::Counter or TB::Run then by definition
neither of them are singletons. Conversely if there is only ever 1 instance
of the Test::Builder class, as chromatic said in his reply, then it is a
singleton.

> > If so, what can I do if some are from depth 3 or 1?
> 
> You temporarily change the level inside the scope of that function.

So I'll have to call local on the default_level field of the TB object?

> > 2 - What happens if Test::C::do_multiple_tests() calls Test::D::some_test()?
> > The correct level is now
> > Test::C's default level + Test::D's default level - 1
> 
> This is why the idiom is currently:
> 
>       local $Level = $Level + 2;
> 
> or whatever.  You add to the existing level rather than just setting it anew.

But without a global variable there is no existing level. Here's a simpler
example of the problem. Currently I can do

package Test::AllArray;

use Test::More;

sub is_all
{
        my $array = shift;
        my $exp = shift;
        my $name = shift;
        local $Test::Builder::Level = $Test::Builder::Level + 1;
        
        for (my $i=0; $i < @$array; $i++)
        {
                is($array[$i], $exp, "$name - $i");
        }
}

How can I do that without a global variable?

F

Reply via email to