| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use File::Temp qw( tempfile ); |
| 6 |
use WWW::Mechanize::Link; |
| 7 |
use lib './inc'; |
| 8 |
use IO::Catch; |
| 9 |
|
| 10 |
# pre-5.8.0's warns aren't caught by a tied STDERR. |
| 11 |
tie *STDOUT, 'IO::Catch', '_STDOUT_' or die $!; |
| 12 |
|
| 13 |
BEGIN { |
| 14 |
# Choose a nonannoying HTML displayer: |
| 15 |
$ENV{PERL_HTML_DISPLAY_CLASS} = 'HTML::Display::Dump'; |
| 16 |
# Disable all ReadLine functionality |
| 17 |
$ENV{PERL_RL} = 0; |
| 18 |
}; |
| 19 |
|
| 20 |
our %tests = ( |
| 21 |
'autofill' => 'autofill test Fixed value', |
| 22 |
'back' => 'back', |
| 23 |
'click' => 'click', |
| 24 |
'content' => 'content', |
| 25 |
'eval' => 'eval 1', |
| 26 |
'fillout' => 'fillout', |
| 27 |
'get @' => 'get http://[email protected]/', |
| 28 |
'get plain' => 'get http://www.google.com/', |
| 29 |
'open' => 'open "foo link"', |
| 30 |
'reload' => 'reload', |
| 31 |
'referrer' => 'referrer ""', |
| 32 |
'referrer val' => 'referrer "foo"', |
| 33 |
'referer' => 'referer ""', |
| 34 |
'save' => 'save 0', |
| 35 |
'save re' => 'save /.../', |
| 36 |
'submit' => 'submit', |
| 37 |
'tick' => 'tick key value', |
| 38 |
'tick_all' => 'tick key', |
| 39 |
'timeout' => 'timeout 60', |
| 40 |
'value' => 'value key value', |
| 41 |
'ua' => 'ua foo/1.1', |
| 42 |
'untick' => 'untick key value', |
| 43 |
'untick_all' => 'untick key', |
| 44 |
); |
| 45 |
|
| 46 |
eval { |
| 47 |
require HTML::TableExtract; |
| 48 |
$HTML::TableExtract::VERSION >= 2 |
| 49 |
or die "Need HTML::TableExtract version >= 2"; |
| 50 |
$tests{table} = 'table'; |
| 51 |
$tests{'table params'} = 'table foo bar'; |
| 52 |
}; |
| 53 |
|
| 54 |
plan tests => scalar (keys %tests)*2; |
| 55 |
|
| 56 |
use WWW::Mechanize::Shell; |
| 57 |
|
| 58 |
SKIP: { |
| 59 |
|
| 60 |
eval { |
| 61 |
require Test::MockObject; |
| 62 |
Test::MockObject->import(); |
| 63 |
}; |
| 64 |
skip "Test::MockObject not installed", scalar (keys %tests)*2 |
| 65 |
if $@; |
| 66 |
|
| 67 |
my $mock_result = Test::MockObject->new; |
| 68 |
$mock_result->set_always( code => 200 ); |
| 69 |
|
| 70 |
my $mock_form = Test::MockObject->new; |
| 71 |
$mock_form->mock( value => sub {} ) |
| 72 |
->set_list( inputs => ()) |
| 73 |
->set_list( find_input => ()); |
| 74 |
|
| 75 |
my $mock_agent = Test::MockObject->new; |
| 76 |
$mock_agent->set_true($_) |
| 77 |
for qw( back content get open ); |
| 78 |
$mock_agent->set_false($_) |
| 79 |
for qw( form forms ); |
| 80 |
my $mock_uri = Test::MockObject->new; |
| 81 |
$mock_uri->set_always( abs => 'http://example.com/' ) |
| 82 |
->set_always( path => '/' ); |
| 83 |
$mock_uri->fake_module( 'URI::URL', new => sub {$mock_uri} ); |
| 84 |
|
| 85 |
$mock_agent->set_always( res => $mock_result ) |
| 86 |
->set_always( add_header => 1 ) |
| 87 |
->set_always( submit => $mock_result ) |
| 88 |
->set_always( click => $mock_result ) |
| 89 |
->set_always( reload => $mock_result ) |
| 90 |
->set_always( current_form => $mock_form ) |
| 91 |
->set_always( follow_link => 1 ) |
| 92 |
->set_list( links => WWW::Mechanize::Link->new('foo','foo link','foo_link',""), |
| 93 |
WWW::Mechanize::Link->new('foo2','foo2 link','foo2_link',"")) |
| 94 |
->set_always( agent => 'foo/1.0' ) |
| 95 |
->set_always( tick => 1 ) |
| 96 |
->set_always( timeout => 1 ) |
| 97 |
->set_always( untick => 1 ) |
| 98 |
->set_always( uri => $mock_uri ); |
| 99 |
|
| 100 |
my $s = WWW::Mechanize::Shell->new( 'test', rcfile => undef, warnings => undef, watchfiles => undef ); |
| 101 |
$s->{agent} = $mock_agent; |
| 102 |
|
| 103 |
my @history; |
| 104 |
{ no warnings 'redefine'; |
| 105 |
*WWW::Mechanize::Shell::add_history = sub { |
| 106 |
shift; |
| 107 |
# warn $_ for @_; |
| 108 |
push @history, join "", @_; |
| 109 |
}; |
| 110 |
}; |
| 111 |
|
| 112 |
sub compiles_ok { |
| 113 |
my ($command,$testname) = @_; |
| 114 |
$testname ||= $command; |
| 115 |
@history = (); |
| 116 |
$s->cmd($command); |
| 117 |
local $, = "\n"; |
| 118 |
my ($fh,$name) = tempfile(); |
| 119 |
print $fh ( "@history" ); |
| 120 |
close $fh; |
| 121 |
ok( scalar @history != 0, "$testname is history relevant"); |
| 122 |
|
| 123 |
my $output = `$^X -Ilib -c $name 2>&1`; |
| 124 |
chomp $output; |
| 125 |
is( $output, "$name syntax OK", "$testname compiles") |
| 126 |
or diag "Created file was :\n@history"; |
| 127 |
unlink $name |
| 128 |
or diag "Couldn't remove tempfile '$name' : $!"; |
| 129 |
}; |
| 130 |
|
| 131 |
foreach my $name (sort keys %tests) { |
| 132 |
compiles_ok( $tests{$name},$name ); |
| 133 |
}; |
| 134 |
}; |