Add tests, clean up Tie::Scalar
authorchromatic <chromatic@wgz.org>
Fri, 28 Sep 2001 21:20:12 +0000 (15:20 -0600)
committerJarkko Hietaniemi <jhi@iki.fi>
Sat, 29 Sep 2001 13:49:43 +0000 (13:49 +0000)
Message-ID: <20010929032543.58322.qmail@onion.perl.org>

p4raw-id: //depot/perl@12265

MANIFEST
lib/Tie/Scalar.pm
lib/Tie/Scalar.t [new file with mode: 0644]

index 8d1c8a0..5f45512 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -1225,6 +1225,7 @@ lib/Tie/Hash.pm                   Base class for tied hashes
 lib/Tie/RefHash.pm             Base class for tied hashes with references as keys
 lib/Tie/RefHash.t              Test for Tie::RefHash and Tie::RefHash::Nestable
 lib/Tie/Scalar.pm              Base class for tied scalars
+lib/Tie/Scalar.t               See if Tie::Scalar works
 lib/Tie/SubstrHash.pm          Compact hash for known key, value and table size
 lib/Tie/SubstrHash.t           Test for Tie::SubstrHash
 lib/Time/gmtime.pm             By-name interface to Perl's builtin gmtime
index bcaad0b..c23c121 100644 (file)
@@ -92,7 +92,7 @@ sub new {
 
 sub TIESCALAR {
     my $pkg = shift;
-    if (defined &{"{$pkg}::new"}) {
+       if ($pkg->can('new') and $pkg ne __PACKAGE__) {
        warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIESCALAR is missing");
        $pkg->new(@_);
     }
diff --git a/lib/Tie/Scalar.t b/lib/Tie/Scalar.t
new file mode 100644 (file)
index 0000000..3c5d9b6
--- /dev/null
@@ -0,0 +1,76 @@
+#!./perl
+
+BEGIN {
+       chdir 't' if -d 't';
+       @INC = '../lib';
+}
+
+# this must come before main, or tests will fail
+package TieTest;
+
+use Tie::Scalar;
+use vars qw( @ISA );
+@ISA = qw( Tie::Scalar );
+
+sub new { 'Fooled you.' }
+
+package main;
+
+use vars qw( $flag );
+use Test::More tests => 13;
+
+use_ok( 'Tie::Scalar' );
+
+# these are "abstract virtual" parent methods
+for my $method qw( TIESCALAR FETCH STORE ) {
+       eval { Tie::Scalar->$method() };
+       like( $@, qr/doesn't define a $method/, "croaks on inherited $method()" );
+}
+
+# the default value is undef
+my $scalar = Tie::StdScalar->TIESCALAR();
+is( $$scalar, undef, 'used TIESCALAR, default value is still undef' );
+
+# Tie::StdScalar redirects to TIESCALAR
+$scalar = Tie::StdScalar->new();
+is( $$scalar, undef, 'used new(), default value is still undef' );
+
+# this approach should work as well
+tie $scalar, 'Tie::StdScalar';
+is( $$scalar, undef, 'tied a scalar, default value is undef' );
+
+# first set, then read
+$scalar = 'fetch me';
+is( $scalar, 'fetch me', 'STORE() and FETCH() verified with one test!' );
+
+# test DESTROY with an object that signals its destruction
+{
+       my $scalar = 'foo';
+       tie $scalar, 'Tie::StdScalar', DestroyAction->new();
+       ok( $scalar, 'tied once more' );
+       is( $flag, undef, 'destroy flag not set' );
+}
+
+# $scalar out of scope, Tie::StdScalar::DESTROY() called, DestroyAction set flag
+is( $flag, 1, 'and DESTROY() works' );
+
+# we want some noise, and some way to capture it
+use warnings;
+my $warn;
+local $SIG{__WARN__} = sub {
+       $warn = $_[0];
+};
+
+# Tie::Scalar::TIEHANDLE should find and call TieTest::new and complain
+is( tie( my $foo, 'TieTest'), 'Fooled you.', 'delegated to new()' );
+like( $warn, qr/WARNING: calling TieTest->new/, 'caught warning fine' );
+
+package DestroyAction;
+
+sub new {
+       bless( \(my $self), $_[0] );
+}
+
+sub DESTROY {
+       $main::flag = 1;
+}