This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perly-fixer
[perl5.git] / lib / Tie / Scalar.t
CommitLineData
c6c73c78 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8# this must come before main, or tests will fail
9package TieTest;
10
11use Tie::Scalar;
12use vars qw( @ISA );
13@ISA = qw( Tie::Scalar );
14
15sub new { 'Fooled you.' }
16
17package main;
18
19use vars qw( $flag );
20use Test::More tests => 13;
21
22use_ok( 'Tie::Scalar' );
23
24# these are "abstract virtual" parent methods
25for my $method qw( TIESCALAR FETCH STORE ) {
26 eval { Tie::Scalar->$method() };
27 like( $@, qr/doesn't define a $method/, "croaks on inherited $method()" );
28}
29
30# the default value is undef
31my $scalar = Tie::StdScalar->TIESCALAR();
32is( $$scalar, undef, 'used TIESCALAR, default value is still undef' );
33
34# Tie::StdScalar redirects to TIESCALAR
35$scalar = Tie::StdScalar->new();
36is( $$scalar, undef, 'used new(), default value is still undef' );
37
38# this approach should work as well
39tie $scalar, 'Tie::StdScalar';
40is( $$scalar, undef, 'tied a scalar, default value is undef' );
41
42# first set, then read
43$scalar = 'fetch me';
44is( $scalar, 'fetch me', 'STORE() and FETCH() verified with one test!' );
45
46# test DESTROY with an object that signals its destruction
47{
48 my $scalar = 'foo';
49 tie $scalar, 'Tie::StdScalar', DestroyAction->new();
50 ok( $scalar, 'tied once more' );
51 is( $flag, undef, 'destroy flag not set' );
52}
53
54# $scalar out of scope, Tie::StdScalar::DESTROY() called, DestroyAction set flag
55is( $flag, 1, 'and DESTROY() works' );
56
57# we want some noise, and some way to capture it
58use warnings;
59my $warn;
60local $SIG{__WARN__} = sub {
61 $warn = $_[0];
62};
63
64# Tie::Scalar::TIEHANDLE should find and call TieTest::new and complain
65is( tie( my $foo, 'TieTest'), 'Fooled you.', 'delegated to new()' );
66like( $warn, qr/WARNING: calling TieTest->new/, 'caught warning fine' );
67
68package DestroyAction;
69
70sub new {
71 bless( \(my $self), $_[0] );
72}
73
74sub DESTROY {
75 $main::flag = 1;
76}