/
00field.t
executable file
·110 lines (86 loc) · 2.52 KB
/
00field.t
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
use Mojo::Base '-strict';
use Mojolicious::Lite;
use Test::More tests => 30;
use Test::Mojo;
use TestHelper;
plugin 'FormFields';
get '/with_object_as_target' => sub {
my $self = shift;
$self->stash(user => user());
$self->render(text => $self->field('user.name'));
};
get '/with_hash_as_target' => sub {
my $self = shift;
$self->stash(user => { name => 'skye' });
$self->render(text => $self->field('user.name'));
};
get '/with_array_as_target' => sub {
my $self = shift;
my @users = ({ name => 'coelinha' });
$self->stash(users => \@users);
$self->render(text => $self->field('users.0.name'));
};
get '/with_hash_argument_as_target' => sub {
my $self = shift;
my $object = { name => 'xxx' };
$self->stash(user => user());
# should use $object not user()
$self->render(text => $self->field('user.name', $object));
};
get '/with_object_argument_as_target' => sub {
my $self = shift;
my $object = user(name => 'xxx');
$self->stash(user => user());
# should use $object not user()
$self->render(text => $self->field('user.name', $object));
};
# Error cases
get '/with_a_missing_param_name' => sub {
shift->field
};
get '/with_a_invalid_param_name' => sub {
shift->field('not_in_stash')
};
get '/with_a_non_reference'=> sub {
shift->field('x.y', 123)
};
get '/with_a_non_existant_accessor' => sub {
shift->field('user.x', user())
};
get '/array_with_a_non_numeric_index' => sub {
shift->field('array.x', [])
};
my $t = Test::Mojo->new;
$t->get_ok('/with_object_as_target')
->status_is(200)
->content_is('sshaw');
$t->get_ok('/with_hash_as_target')
->status_is(200)
->content_is('skye');
$t->get_ok('/with_array_as_target')
->status_is(200)
->content_is('coelinha');
$t->get_ok('/with_hash_argument_as_target')
->status_is(200)
->content_is('xxx');
$t->get_ok('/with_object_argument_as_target')
->status_is(200)
->content_is('xxx');
$t->get_ok('/with_a_missing_param_name')
->status_is(500)
->content_like(qr/name required/);
$t->get_ok('/with_a_invalid_param_name')
->status_is(500)
->content_like(qr/nothing in the stash/);
$t->get_ok('/with_a_non_reference')
->status_is(500)
->content_like(qr/not a reference/);
$t->get_ok('/with_a_non_existant_accessor')
->status_is(500)
->content_like(qr/on a User/);
$t->get_ok('/array_with_a_non_numeric_index')
->status_is(500)
->content_like(qr/non-numeric index/);
__DATA__
@@ exception.html.ep
%= stash('exception')