summaryrefslogtreecommitdiff
path: root/t/op/studytied.t
blob: 288bcd755139fad89e7115f9fb9a7df0e1b62c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!./perl

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
    require './test.pl';
}

use strict;
use warnings;

plan tests => 14;

{
    package J;
    my $c = 0;
    sub reset { $c = 0 }
    sub TIESCALAR { bless [] }
    sub FETCH { $c++ ? "next" : "first" }
}

# This test makes sure that we can't pull a fast one on study().  If we
# study() a tied variable, perl should know that the studying isn't
# valid on subsequent references, and should account for it.

for my $do_study qw( 0 1 ) {
    J::reset();
    my $x;
    tie $x, "J";

    if ($do_study) {
        study $x;
        pass( "Studying..." );
    } else {
        my $first_fetch = $x;
        pass( "Not studying..." );
    }

    # When it was studied (or first_fetched), $x was "first", but is now "next", so
    # should not match /f/.
    ok( $x !~ /f/,              qq{"next" doesn't match /f/} );
    is( index( $x, 'f' ), -1,   qq{"next" doesn't contain "f"} );

    # Subsequent references to $x are "next", so should match /n/
    ok( $x =~ /n/,              qq{"next" matches /n/} );
    is( index( $x, 'n' ), 0,    qq{"next" contains "n" at pos 0} );

    # The letter "t" is in both, but in different positions
    ok( $x =~ /t/,              qq{"next" matches /t/} );
    is( index( $x, 't' ), 3,    qq{"next" contains "t" at pos 3} );
}