| 1 |
use strict; |
| 2 |
use Test::More tests => 22; |
| 3 |
|
| 4 |
# Disable all ReadLine functionality |
| 5 |
$ENV{PERL_RL} = 0; |
| 6 |
$ENV{COLUMNS} = 80; |
| 7 |
$ENV{LINES} = 24; |
| 8 |
|
| 9 |
use_ok("WWW::Mechanize::Shell") or BAIL_OUT('Does not compile correctly'); |
| 10 |
|
| 11 |
diag "Running under $]"; |
| 12 |
for (qw(WWW::Mechanize LWP::UserAgent)) { |
| 13 |
diag "Using '$_' version " . $_->VERSION; |
| 14 |
}; |
| 15 |
|
| 16 |
my $s = do { |
| 17 |
WWW::Mechanize::Shell->new("shell",rcfile => undef, warnings => undef); |
| 18 |
}; |
| 19 |
isa_ok($s,"WWW::Mechanize::Shell"); |
| 20 |
|
| 21 |
# Now check our published API : |
| 22 |
for my $meth (qw( source_file cmdloop agent option restart_shell option cmd )) { |
| 23 |
can_ok($s,$meth); |
| 24 |
}; |
| 25 |
|
| 26 |
# Check that we can set known options |
| 27 |
# See also t/05-options.t |
| 28 |
my $oldvalue = $s->option('autosync'); |
| 29 |
$s->option('autosync',"foo"); |
| 30 |
is($s->option('autosync'),"foo","Setting an option works"); |
| 31 |
$s->option('autosync',$oldvalue); |
| 32 |
is($s->option('autosync'),$oldvalue,"Setting an option still works"); |
| 33 |
|
| 34 |
# Check that trying to set an unknown option gives an error |
| 35 |
{ |
| 36 |
no warnings 'redefine'; |
| 37 |
my $called; |
| 38 |
local *Carp::carp = sub { |
| 39 |
$called++; |
| 40 |
}; |
| 41 |
$s->option('nonexistingoption',"foo"); |
| 42 |
is($called,1,"Setting an nonexisting option calls Carp::carp"); |
| 43 |
} |
| 44 |
|
| 45 |
{ |
| 46 |
no warnings 'redefine'; |
| 47 |
my $called; |
| 48 |
my $filename; |
| 49 |
local *WWW::Mechanize::Shell::source_file = sub { |
| 50 |
$filename = $_[1]; |
| 51 |
$called++; |
| 52 |
}; |
| 53 |
my $test_filename = '/does/not/need/to/exist'; |
| 54 |
my $s = do { |
| 55 |
WWW::Mechanize::Shell->new("shell",rcfile => $test_filename, warnings => undef); |
| 56 |
}; |
| 57 |
isa_ok($s,"WWW::Mechanize::Shell"); |
| 58 |
ok($called,"Passing an .rc file tries to load it"); |
| 59 |
is($filename,$test_filename,"Passing an .rc file tries to load the right file"); |
| 60 |
}; |
| 61 |
|
| 62 |
{ |
| 63 |
no warnings 'redefine'; |
| 64 |
my $called = 0; |
| 65 |
my $filename; |
| 66 |
local *WWW::Mechanize::Shell::source_file = sub { |
| 67 |
$filename = $_[1]; |
| 68 |
$called++; |
| 69 |
}; |
| 70 |
my $s = do { |
| 71 |
WWW::Mechanize::Shell->new("shell",rcfile => undef, warnings => undef); |
| 72 |
}; |
| 73 |
isa_ok($s,"WWW::Mechanize::Shell"); |
| 74 |
diag "Tried to load '$filename'" unless is($called,0,"Passing in no .rc file tries not to load it"); |
| 75 |
}; |
| 76 |
|
| 77 |
$s = WWW::Mechanize::Shell->new("shell",rcfile => undef, cookiefile => 'test.cookiefile', warnings => undef); |
| 78 |
isa_ok($s,"WWW::Mechanize::Shell"); |
| 79 |
is($s->option('cookiefile'),'test.cookiefile',"Passing in a cookiefile filename works"); |
| 80 |
|
| 81 |
# Also check what gets exported: |
| 82 |
ok(defined *main::shell{CODE},"'shell' gets exported"); |
| 83 |
{ |
| 84 |
no warnings 'once'; |
| 85 |
is(*main::shell{CODE},*WWW::Mechanize::Shell::shell{CODE},"'shell' is the right sub"); |
| 86 |
}; |
| 87 |
|
| 88 |
{ |
| 89 |
no warnings 'redefine','once'; |
| 90 |
my $called; |
| 91 |
local *WWW::Mechanize::Shell::cmdloop = sub { $called++ }; |
| 92 |
# Need to suppress status warnings here |
| 93 |
shell(warnings => undef); |
| 94 |
is($called,1,"Shell function works"); |
| 95 |
}; |