]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blob - t/07-context_info.t
Make t/07-context_info.t pass with perls that have usesitecustomize set
[perl/modules/Scope-Upper.git] / t / 07-context_info.t
1 #!perl -T
2
3 my $exp0 = ::expected('block', 0, undef);
4
5 use strict;
6 use warnings;
7
8 use Config qw<%Config>;
9
10 # We're using Test::Leaner here because Test::More loads overload, which itself
11 # uses warning::register, which may cause the "all warnings on" bitmask to
12 # change ; and that doesn't fit well with how we're testing things.
13
14 use lib 't/lib';
15 use Test::Leaner tests => 19 + 6;
16
17 use Scope::Upper qw<context_info UP HERE CALLER>;
18
19 sub HINT_BLOCK_SCOPE () { 0x100 }
20
21 sub expected {
22  my ($type, $line, $want) = @_;
23
24  my $top;
25
26  my @caller = caller 1;
27  my @here   = caller 0;
28  unless (@caller) {
29   @caller   = @here;
30   $top++;
31  }
32
33  my $pkg = $here[0];
34  my ($file, $eval, $require, $hints, $warnings, $hinthash)
35                                                    = @caller[1, 6, 7, 8, 9, 10];
36
37  $line = $caller[2] unless defined $line;
38
39  my ($sub, $hasargs);
40  if ($type eq 'sub' or $type eq 'eval' or $type eq 'format') {
41   $sub     = $caller[3];
42   $hasargs = $caller[4];
43   $want    = $caller[5];
44   $want    = '' if defined $want and not $want;
45  }
46
47  if ($top) {
48   $want   = "$]" < 5.015_001 ? '' : undef;
49   $hints &= ~HINT_BLOCK_SCOPE if $Config{usesitecustomize};
50  }
51
52  my @exp = (
53   $pkg,
54   $file,
55   $line,
56   $sub,
57   $hasargs,
58   $want,
59   $eval,
60   $require,
61   $hints,
62   $warnings,
63  );
64  push @exp, $hinthash if "$]" >= 5.010;
65
66  return \@exp;
67 }
68
69 sub setup () {
70  my $pkg = caller;
71
72  for my $sub (qw<context_info UP HERE is_deeply expected>) {
73   no strict 'refs';
74   *{"${pkg}::$sub"} = \&{"main::$sub"};
75  }
76 }
77
78 is_deeply [ context_info       ], $exp0, 'main : context_info';
79 is_deeply [ context_info(HERE) ], $exp0, 'main : context_info HERE';
80 is_deeply [ context_info(UP)   ], $exp0, 'main : context_info UP';
81 is_deeply [ context_info(-1)   ], $exp0, 'main : context_info -1';
82
83 package Scope::Upper::TestPkg::A; BEGIN { ::setup }
84 my @a = sub {
85  my $exp1 = expected('sub', undef);
86  is_deeply [ context_info ], $exp1, 'sub0 : context_info';
87  package Scope::Upper::TestPkg::B; BEGIN { ::setup }
88  {
89   my $exp2 = expected('block', __LINE__, 1);
90   is_deeply [ context_info     ], $exp2, 'sub : context_info';
91   is_deeply [ context_info(UP) ], $exp1, 'sub : context_info UP';
92   package Scope::Upper::TestPkg::C; BEGIN { ::setup }
93   for (1) {
94    my $exp3 = expected('loop', __LINE__ - 1, undef);
95    is_deeply [ context_info        ], $exp3, 'for : context_info';
96    is_deeply [ context_info(UP)    ], $exp2, 'for : context_info UP';
97    is_deeply [ context_info(UP UP) ], $exp1, 'for : context_info UP UP';
98   }
99   package Scope::Upper::TestPkg::D; BEGIN { ::setup }
100   my $eval_line = __LINE__+1;
101   eval <<'CODE';
102    my $exp4 = expected('eval', $eval_line);
103    is_deeply [ context_info        ], $exp4, 'eval string : context_info';
104    is_deeply [ context_info(UP)    ], $exp2, 'eval string : context_info UP';
105    is_deeply [ context_info(UP UP) ], $exp1, 'eval string : context_info UP UP';
106 CODE
107   die $@ if $@;
108   package Scope::Upper::TestPkg::E; BEGIN { ::setup }
109   my $x = eval {
110    my $exp5 = expected('eval', __LINE__ - 1);
111    package Scope::Upper::TestPkg::F; BEGIN { ::setup }
112    do {
113     my $exp6 = expected('block', __LINE__ - 1, undef);
114     is_deeply [ context_info        ], $exp6, 'do : context_info';
115     is_deeply [ context_info(UP)    ], $exp5, 'do : context_info UP';
116     is_deeply [ context_info(UP UP) ], $exp2, 'do : context_info UP UP';
117    };
118    is_deeply [ context_info        ], $exp5, 'eval : context_info';
119    is_deeply [ context_info(UP)    ], $exp2, 'eval : context_info UP';
120    is_deeply [ context_info(UP UP) ], $exp1, 'eval : context_info UP UP';
121   };
122  }
123 }->(1);
124
125 package main;
126
127 sub first {
128  do {
129   second(@_);
130  }
131 }
132
133 my $fourth;
134
135 sub second {
136  my $x = eval {
137   my @y = $fourth->();
138  };
139  die $@ if $@;
140 }
141
142 $fourth = sub {
143  my $z = do {
144   my $dummy;
145   eval q[
146    call(@_);
147   ];
148   die $@ if $@;
149  }
150 };
151
152 sub call {
153  for my $depth (0 .. 5) {
154   my @got = context_info(CALLER $depth);
155   my @exp = caller $depth;
156   defined and not $_ and $_ = '' for $exp[5];
157   is_deeply \@got, \@exp, "context_info vs caller $depth";
158  }
159 }
160
161 first();