Hi. I'm looking for a universal way to declare a test flag to object oriented code regardless if it's Moose or traditional Perl OO, or whatever, and although rare, even if the blessed reference is an array. I'm pretty sure I'm reinventing the wheel here so the code below is merely to illustrate what is wanted.
Anyway, the idea is that from a test script one could can call this method and it would set a test flag that can later be used in the actual code to condition things or in cases the code needs to know it's being tested. So, the test script would call _test_mode to set the attribute and then the actual code will use _test_mode to condition things (e.g. do foo unless($self->_test_mode)). This could perhaps be added as a simple sub in in modules that need to be test-aware:
sub _test_mode { my $self = shift; use Scalar::Util 'reftype'; my $tkey = '_'.__PACKAGE__.'_TEST_MODE'; if(reftype($self) eq 'HASH'){ if(defined $self->{$tkey}){ return 1; } else{ $self->{$tkey} = 1; } } elsif(reftype($self) eq 'ARRAY'){ if($self->[-1] eq $tkey){ return 1; } else{ push @{$self},$tkey; } } }
There are probably dozens of ways to do this, and more that one published on the CPAN, so I ask for your Wisdom not so much on the code above but rather on recommendations on techniques to accomplish the objective of setting a universal flag in the code so it's aware of being tested, regardless if we are testing a module built with Moose, or an extension to some DBIx::Class:ResultSet.
In reply to Universal test flag by ait
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |