File: 04-FF-api.t

package info (click to toggle)
libwww-mechanize-formfiller-perl 0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 292 kB
  • ctags: 46
  • sloc: perl: 1,504; sh: 6; makefile: 2
file content (128 lines) | stat: -rwxr-xr-x 5,167 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use strict;
use Test::More tests => 33;

use_ok("WWW::Mechanize::FormFiller");

my $f = WWW::Mechanize::FormFiller->new();
isa_ok($f,"WWW::Mechanize::FormFiller");

# Now check our published API :
my $meth;
for $meth (qw(add_filler add_value fill_form fillout )) {
  can_ok($f,$meth);
};

$f = WWW::Mechanize::FormFiller->new( default => [ Fixed => "foo" ]);
isa_ok($f,"WWW::Mechanize::FormFiller");
isa_ok($f->{default},"WWW::Mechanize::FormFiller::Value::Fixed","Default value");

$f = WWW::Mechanize::FormFiller->new( default => [Default => "foo"],
                                      values => [[ foo => Fixed => "foo"],
                                                 [ bar => Default => "bar"],
                                                 [ baz => Random => "1","2","3" ],
                                                ]);
isa_ok($f,"WWW::Mechanize::FormFiller");
isa_ok($f->{default},"WWW::Mechanize::FormFiller::Value::Default","Default value");
is(scalar keys %{$f->{values}->{byname}}, 3, "Correct number of values gets stored");

$f = WWW::Mechanize::FormFiller->new(values => [[ login => Fixed => "root" ]]);
my $v = WWW::Mechanize::FormFiller::Value::Fixed->new(undef,"secret");
$f->add_value(password => $v);
$f->add_value(password_confirm => $v);
isa_ok($f,"WWW::Mechanize::FormFiller");
is($f->{default},undef,"Passing in no default results in no default being set");
is(scalar keys %{$f->{values}->{byname}}, 3, "Correct number of values gets stored");
is($f->{values}->{byname}->{password}, $f->{values}->{byname}->{password_confirm}, "Duplicate values get stored only once");

my $croaked;
{
  local *Carp::croak = sub {$croaked .= $_[0]};
  $f = WWW::Mechanize::FormFiller->new(values => "don't know");
  isnt($croaked,undef,"We croaked on invalid parameters");
  like($croaked,qr"values parameter must be an array reference","Passing no array reference as values raises an error");
  undef $croaked;
};

{
  local *Carp::croak = sub {$croaked .= $_[0]};
  $f = WWW::Mechanize::FormFiller->new(values => ["don't know"]);
  isnt($croaked,undef,"We croaked on invalid parameters");
  like($croaked,qr"Each element of the values array must be an array reference","Passing no array reference as element of values raises an error");
  undef $croaked;
};

{
  local *Carp::croak = sub {$croaked .= $_[0]};
  $f = WWW::Mechanize::FormFiller->new(values => [["don't know"]]);
  isnt($croaked,undef,"We croaked on invalid parameters");
  like($croaked,qr"Each element of the values array must have at least 2 elements \(name and class\)","Passing too few array elements raises an error");
  undef $croaked;
};

{
  local *Carp::croak = sub {$croaked .= $_[0]};
  $f = WWW::Mechanize::FormFiller->new(values => [[undef,""]]);
  isnt($croaked,undef,"We croaked on invalid parameters");
  like($croaked,qr"Each element of the values array must have a class name","Passing an empty classname raises an error");
  undef $croaked;
};

{
  local *Carp::croak = sub {$croaked .= $_[0]};
  $f = WWW::Mechanize::FormFiller->new();
  $f->add_filler( foo => "" => "bar" );
  isnt($croaked,undef,"add_filler croaks on invalid parameters");
  like($croaked,qr"A value must have at least a class name and a field name \(which may be undef though\)","Passing an empty classname to add_filler raises an error");
  undef $croaked;
};

SKIP: {
  eval { require HTML::Form; };
  skip "Need HTML::Form to test fillout()", 2
    if $@;
  my $form = HTML::Form->parse('<form></form>','http://www.example.com');
  {
    local *Carp::croak = sub { die @_};
    eval { $f = WWW::Mechanize::FormFiller->fillout($form,$form); };
    $croaked = $@;
    isnt($croaked,undef,"fillout croaks on double form");
    like($croaked,qr"Two HTML::Form objects passed into fillout\(\)","Passing two forms to fillout raises an error");
    undef $croaked;
  };
};

SKIP: {
  eval { require HTML::Form; };
  skip "Need HTML::Form to test fillout()", 2
    if $@;
  my $form = HTML::Form->parse('<form>
  <input name=name value=none />
  </form>','http://www.example.com');
  $f = WWW::Mechanize::FormFiller->fillout($form, name => 'Mark' );
  isa_ok($f,'WWW::Mechanize::FormFiller');
  is($form->value('name'),'Mark','fillout has a default of Fixed');
};

SKIP: {
  eval { require HTML::Form; };
  skip "Need HTML::Form to test fillout()", 2
    if $@;
  my $form = HTML::Form->parse('<form>
  <input name=name value=none />
  </form>','http://www.example.com');
  $f = WWW::Mechanize::FormFiller->fillout($form, name => [ Random => 'Mark' ]);
  isa_ok($f,'WWW::Mechanize::FormFiller');
  is($form->value('name'),'Mark','Other classes work as well');
};

SKIP: {
  eval { require HTML::Form; };
  skip "Need HTML::Form to test fillout()", 2
    if $@;
  my $form = HTML::Form->parse('<form>
  <input name=name value=none />
  </form>','http://www.example.com');
  $f = WWW::Mechanize::FormFiller->fillout(name => [ Random => 'Mark' ], $form);
  isa_ok($f,'WWW::Mechanize::FormFiller');
  is($form->value('name'),'Mark','The place of $form is irrelevant');
};