develooper Front page | perl.qa | Postings from April 2012

Re: Revert use_ok() change to allow lexical effects?

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
April 11, 2012 09:32
Subject:
Re: Revert use_ok() change to allow lexical effects?
Message ID:
4F85B230.8050409@pobox.com
On 2012.4.10 6:21 PM, The Sidhekin wrote:
> * How would you rewrite a test script such as my own
> http://cpansearch.perl.org/src/EBHANSSEN/Test-Trap-v0.2.2/t/00-load.t so
> that it does not use use_ok()?

  use Test::More tests => 1;

  use Test::Trap::Builder::TempFile;
  use Test::Trap::Builder::SystemSafe;
  SKIP: {
      skip 'Lacking PerlIO', 1 unless eval "use PerlIO; 1";
      require Test::Trap::Builder::PerlIO;
      Test::Trap::Builder::PerlIO->import;
  }
  use Test::Trap::Builder;
  require Test::Trap
      or BAIL_OUT( "Nothing to test without the Test::Trap class" );

  diag( "Testing Test::Trap $Test::Trap::VERSION, Perl $], $^X" );
  pass("Test::Trap loaded successfully");

Or a slimmed down version, recognizing that Test::Trap loads all the above
already and will tell you just which module failed to load.  It also makes the
test black box.

  eval {
      require Test::Trap;
      Test::Trap->import;
      1;
  } or BAIL_OUT("Test::Trap failed to load");

  diag( "Testing Test::Trap $Test::Trap::VERSION, Perl $], $^X" );
  pass("Test::Trap loaded successfully");

This can be boxed up in a module.  There's already Test::Requires.  It skips
instead of bails, it would be a good target for test_must_have() or something
that bails.


> * Why would you? :-\

Because it reads like normal Perl and doesn't rely on more code to do
something rather simple: load a module.

TAP (the ok 1 stuff) has us very much convinced that we need to output
something for every assert.  The rest of the testing universe relies on
exceptions to tell them something's failed and seems to get along just fine
with that.

But quite honestly, if use_ok() works for you, if all you need is require +
import + test, continue to use it.  I don't plan on eliminating it.


-- 
Hating the web since 1994.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About