This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
use re qw(debug foo) should warn
[perl5.git] / ext / re / t / re.t
1 #!./perl
2
3 BEGIN {
4         require Config;
5         if (($Config::Config{'extensions'} !~ /\bre\b/) ){
6                 print "1..0 # Skip -- Perl configured without re module\n";
7                 exit 0;
8         }
9 }
10
11 use strict;
12
13 my $re_taint_bit = 0x00100000;
14 my $re_eval_bit = 0x00200000;
15
16 use Test::More tests => 16;
17 require_ok( 're' );
18
19 # setcolor
20 $INC{ 'Term/Cap.pm' } = 1;
21 local $ENV{PERL_RE_TC};
22 re::setcolor();
23 is( $ENV{PERL_RE_COLORS}, "md\tme\tso\tse\tus\tue", 
24         'setcolor() should provide default colors' );
25 $ENV{PERL_RE_TC} = 'su,n,ny';
26 re::setcolor();
27 is( $ENV{PERL_RE_COLORS}, "su\tn\tny", '... or use $ENV{PERL_RE_COLORS}' );
28
29 # bits
30 # get on
31 my $warn;
32 local $SIG{__WARN__} = sub {
33         $warn = shift;
34 };
35 #eval { re::bits(1) };
36 #like( $warn, qr/Useless use/, 'bits() should warn with no args' );
37
38 delete $ENV{PERL_RE_COLORS};
39 re::bits(0, 'debug');
40 is( $ENV{PERL_RE_COLORS}, undef,
41         "... should not set regex colors given 'debug'" );
42 re::bits(0, 'debugcolor');
43 isnt( $ENV{PERL_RE_COLORS}, '', 
44         "... should set regex colors given 'debugcolor'" );
45 re::bits(0, 'nosuchsubpragma');
46 like( $warn, qr/Unknown "re" subpragma/, 
47         '... should warn about unknown subpragma' );
48 ok( re::bits(0, 'taint') & $re_taint_bit, '... should set taint bits' );
49 ok( re::bits(0, 'eval')  & $re_eval_bit, '... should set eval bits' );
50
51 undef $warn;
52 eval "use re qw(debug ALL)";
53 like( $warn, qr/"Debug" not "debug"/, 'debug with debugging type should warn');
54
55 local $^H;
56
57 # import
58 re->import('taint', 'eval');
59 ok( $^H & $re_taint_bit, 'import should set taint bits in $^H when requested' );
60 ok( $^H & $re_eval_bit, 'import should set eval bits in $^H when requested' );
61
62 re->unimport('taint');
63 ok( !( $^H & $re_taint_bit ), 'unimport should clear bits in $^H when requested' );
64 re->unimport('eval');
65 ok( !( $^H & $re_eval_bit ), '... and again' );
66 my $reg=qr/(foo|bar|baz|blah)/;
67 close STDERR;
68 eval"use re Debug=>'ALL'";
69 my $ok='foo'=~/$reg/;
70 eval"no re Debug=>'ALL'";
71 ok( $ok, 'No segv!' );
72
73 my $message = "Don't tread on me";
74 $_ = $message;
75 re->import("/aa");
76 is($_, $message, "re doesn't clobber \$_");
77
78 package Term::Cap;
79
80 sub Tgetent {
81         bless({}, $_[0]);
82 }
83
84 sub Tputs {
85         return $_[1];
86 }
87
88 package main;
89
90 {
91   my $w;
92   local $SIG{__WARN__} = sub { warn shift; ++$w };
93   re->import();
94   is $w, undef, 'no warning for "use re;" (which is not useless)';
95 }