r76662 - in /branches/upstream/libtest-spec-perl/current: MANIFEST META.yml lib/Test/Spec.pm t/helper_test.pl t/spec_helper.t

carnil at users.alioth.debian.org carnil at users.alioth.debian.org
Tue Jun 28 09:32:21 UTC 2011


Author: carnil
Date: Tue Jun 28 09:32:19 2011
New Revision: 76662

URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=76662
Log:
[svn-upgrade] new version libtest-spec-perl (0.34)

Added:
    branches/upstream/libtest-spec-perl/current/t/helper_test.pl
    branches/upstream/libtest-spec-perl/current/t/spec_helper.t   (with props)
Modified:
    branches/upstream/libtest-spec-perl/current/MANIFEST
    branches/upstream/libtest-spec-perl/current/META.yml
    branches/upstream/libtest-spec-perl/current/lib/Test/Spec.pm

Modified: branches/upstream/libtest-spec-perl/current/MANIFEST
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtest-spec-perl/current/MANIFEST?rev=76662&op=diff
==============================================================================
--- branches/upstream/libtest-spec-perl/current/MANIFEST (original)
+++ branches/upstream/libtest-spec-perl/current/MANIFEST Tue Jun 28 09:32:19 2011
@@ -9,6 +9,7 @@
 t/define.t
 t/dying_spec.pl
 t/empty.t
+t/helper_test.pl
 t/import_strict.t
 t/import_warnings.t
 t/mocks.t
@@ -17,6 +18,7 @@
 t/shared_examples.t
 t/shared_examples_spec.pl
 t/show_exeptions.t
+t/spec_helper.t
 t/strict_violating_spec.pl
 t/test_helper.pl
 META.yml                                 Module meta-data (added by MakeMaker)

Modified: branches/upstream/libtest-spec-perl/current/META.yml
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtest-spec-perl/current/META.yml?rev=76662&op=diff
==============================================================================
--- branches/upstream/libtest-spec-perl/current/META.yml (original)
+++ branches/upstream/libtest-spec-perl/current/META.yml Tue Jun 28 09:32:19 2011
@@ -1,6 +1,6 @@
 --- #YAML:1.0
 name:               Test-Spec
-version:            0.33
+version:            0.34
 abstract:           Write tests in a declarative specification style
 author:
     - Philip Garrett <philip.garrett at icainformatics.com>

Modified: branches/upstream/libtest-spec-perl/current/lib/Test/Spec.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtest-spec-perl/current/lib/Test/Spec.pm?rev=76662&op=diff
==============================================================================
--- branches/upstream/libtest-spec-perl/current/lib/Test/Spec.pm (original)
+++ branches/upstream/libtest-spec-perl/current/lib/Test/Spec.pm Tue Jun 28 09:32:19 2011
@@ -3,12 +3,13 @@
 use warnings;
 use Test::Trap ();        # load as early as possible to override CORE::exit
 
-our $VERSION = '0.33';
+our $VERSION = '0.34';
 
 use base qw(Exporter);
 
 use Carp ();
 use Exporter ();
+use File::Spec ();
 use Tie::IxHash ();
 
 use constant { DEFINITION_PHASE => 0, EXECUTION_PHASE => 1 };
@@ -17,7 +18,8 @@
 our $Debug = $ENV{TEST_SPEC_DEBUG} || 0;
 
 our @EXPORT      = qw(runtests describe before after it they *TODO
-                      shared_examples_for it_should_behave_like);
+                      shared_examples_for it_should_behave_like
+                      spec_helper);
 our @EXPORT_OK   = ( @EXPORT, qw(DEFINITION_PHASE EXECUTION_PHASE $Debug) );
 our %EXPORT_TAGS = ( all => \@EXPORT_OK,
                      constants => [qw(DEFINITION_PHASE EXECUTION_PHASE)] );
@@ -305,6 +307,30 @@
   push @{ $context->after_blocks }, { type => $type, code => $code };
 }
 
+# spec_helper FILESPEC
+sub spec_helper ($) {
+  my $filespec = shift;
+  my ($callpkg,$callfile) = caller;
+  my $load_path;
+  if (File::Spec->file_name_is_absolute($filespec)) {
+    $load_path = $filespec;
+  }
+  else {
+    my ($callvol,$calldir,undef)  = File::Spec->splitpath($callfile);
+    my (undef,$filedir,$filename) = File::Spec->splitpath($filespec);
+    my $newdir = File::Spec->catdir($calldir,$filedir);
+    $load_path = File::Spec->catpath($callvol,$newdir,$filename);
+  }
+  my $sub = eval "package $callpkg;\n" . q[sub {
+    my ($file,$origpath) = @_;
+    if (not defined(do $file)) {
+      my $err = $! || $@;
+      die "could not load spec_helper '$origpath': $err";
+    }
+  }];
+  $sub->($load_path,$filespec);
+}
+
 sub _materialize_tests {
   my $class = shift;
   my $contexts = $_Package_Contexts->{$class};
@@ -662,6 +688,19 @@
 the current context. See L</Shared example groups> and
 L<shared_examples_for>.
 
+=item spec_helper FILESPEC
+
+Loads the Perl source in C<FILESPEC> into the current spec's package. If
+C<FILESPEC> is relative (no leading slash), it is treated as relative to
+the spec file (i.e. B<not> the currently running script). This lets you
+keep helper scripts near the specs they are used by without exercising
+your File::Spec skills in your specs.
+
+  # in foo/spec.t
+  spec_helper "helper.pl";          # loads foo/helper.pl
+  spec_helper "helpers/helper.pl";  # loads foo/helpers/helper.pl
+  spec_helper "/path/to/helper.pl"; # loads /path/to/helper.pl
+
 =back
 
 =head2 Shared example groups

Added: branches/upstream/libtest-spec-perl/current/t/helper_test.pl
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtest-spec-perl/current/t/helper_test.pl?rev=76662&op=file
==============================================================================
--- branches/upstream/libtest-spec-perl/current/t/helper_test.pl (added)
+++ branches/upstream/libtest-spec-perl/current/t/helper_test.pl Tue Jun 28 09:32:19 2011
@@ -1,0 +1,5 @@
+#
+# just increment the value of $foo in the current package.
+#
+no strict;
+$foo++;

Added: branches/upstream/libtest-spec-perl/current/t/spec_helper.t
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtest-spec-perl/current/t/spec_helper.t?rev=76662&op=file
==============================================================================
--- branches/upstream/libtest-spec-perl/current/t/spec_helper.t (added)
+++ branches/upstream/libtest-spec-perl/current/t/spec_helper.t Tue Jun 28 09:32:19 2011
@@ -1,0 +1,42 @@
+#!/usr/bin/env perl
+#
+# spec_helper.t
+#
+# Tests the spec_helper function, which loads helper files relative to
+# the current file.
+#
+########################################################################
+#
+
+package Testcase::Spec::SpecHelper;
+use Test::Spec;
+use base qw(Test::Spec);
+
+our $foo;
+
+describe "spec_helper" => sub {
+  before each => sub { $foo = 0 };
+  it "should load a Perl file into the calling package" => sub {
+    spec_helper "helper_test.pl";
+    is($foo, 1);
+  };
+  it "should load the file even if it has already been loaded" => sub {
+    spec_helper "helper_test.pl";
+    is($foo, 1);
+  };
+  it "should treat paths as relative to the spec, not the currently running executable" => sub {
+    spec_helper "../t/helper_test.pl";
+    is($foo, 1);
+  };
+  it "should treat absolute paths as absolute" => sub {
+    # checks the error message
+    eval { spec_helper "/foo/bar/does/not/exist" };
+    like($@, qr{'/foo/bar/does/not/exist'});
+  };
+  it "should raise an error containing the filename if the load fails" => sub {
+    eval { spec_helper "doesnotexist.pl" };
+    like($@, qr{'doesnotexist.pl'});
+  };
+};
+
+runtests unless caller;

Propchange: branches/upstream/libtest-spec-perl/current/t/spec_helper.t
------------------------------------------------------------------------------
    svn:executable = *




More information about the Pkg-perl-cvs-commits mailing list