Debian Codegraph @

t/07-history-items.t

raw
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 # Disable all ReadLine functionality
15 $ENV{PERL_RL} = 0;
16 };
17
18 our %tests = (
19 'autofill' => 'autofill test Fixed value',
20 'back' => 'back',
21 'click' => 'click',
22 'content' => 'content',
23 'eval' => 'eval 1',
24 'fillout' => 'fillout',
25 'form' => 'form 1',
26 'form' => 'form test',
27 'get @' => 'get http://[email protected]/',
28 'get plain' => 'get http://www.google.com/',
29 'open' => 'open "foo link"',
30 'reload' => 'reload',
31 'referer' => 'referer ""',
32 'referrer' => 'referrer ""',
33 'save' => 'save /.../',
34 'submit' => 'submit',
35 'value' => 'value key value',
36 'ua' => 'ua foo/1.0',
37 'tick' => 'tick key value',
38 'tick_all' => 'tick key',
39 'timeout' => 'timeout 60',
40 'untick' => 'untick key value',
41 'untick_all' => 'untick key',
42 );
43
44 eval {
45 require HTML::TableExtract;
46 $tests{table} = 'table';
47 $tests{table params} = 'table foo bar';
48 ;
49 };
50
51 plan tests => scalar (keys %tests) +1;
52 SKIP: {
53
54 eval {
55 require Test::MockObject;
56 Test::MockObject->import();
57 };
58 skip "Test::MockObject not installed", scalar keys(%tests) +1
59 if $@;
60
61 my $mock_result = Test::MockObject->new;
62 $mock_result->set_always( code => 200 );
63
64 my $mock_form = Test::MockObject->new;
65 $mock_form->mock( value => sub {} )
66 ->set_list( inputs => ())
67 ->set_list( find_input => ())
68 ->mock( dump => sub {} )
69 ->set_always( form_name => 'foo' );
70
71 my $mock_uri = Test::MockObject->new;
72 $mock_uri->set_always( abs => 'http://example.com/' )
73 ->set_always( path => '/' );
74 $mock_uri->fake_module( 'URI::URL', new => sub {$mock_uri} );
75
76 my $mock_agent = Test::MockObject->new;
77 $mock_agent->set_true($_)
78 for qw( back content get mirror open follow );
79 $mock_agent->set_false($_)
80 for qw( form forms );
81 $mock_agent->set_always( res => $mock_result )
82 ->set_always( add_header => 1 )
83 ->set_always( submit => $mock_result )
84 ->set_always( click => $mock_result )
85 ->set_always( reload => $mock_result )
86 ->set_always( current_form => $mock_form )
87 ->set_always( form_name => 'test form name' )
88 ->set_always( follow_link => 1 )
89 ->set_list( links => WWW::Mechanize::Link->new('foo','foo link','foo_link',""),
90 WWW::Mechanize::Link->new('foo2','foo2 link','foo2_link',""))
91 ->set_always( agent => 'mocked/1.0')
92 ->set_always( uri => $mock_uri )
93 ->set_always( request => $mock_result )
94 ->set_always( tick => 1 )
95 ->set_always( timeout => 1 )
96 ->set_always( untick => 1 )
97 ;
98
99 use_ok('WWW::Mechanize::Shell');
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','once';
105 *WWW::Mechanize::Shell::add_history = sub {
106 my $shell = shift;
107 push @history, $shell->line;
108 };
109 };
110
111 sub exactly_one_line {
112 my ($command,$testname) = @_;
113 $testname ||= $command;
114 @history = ();
115 $s->cmd($command);
116 is_deeply([@history],[$command],"$testname adds one line to history");
117 };
118
119
120 foreach my $name (sort keys %tests) {
121 exactly_one_line( $tests{$name},$name );
122 };
123 };