File: next-action.t

package info (click to toggle)
libcatalyst-perl 5.90132-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: perl: 11,061; makefile: 7
file content (171 lines) | stat: -rw-r--r-- 3,776 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use Test::More;

{
    package MyApp::Controller::Root;
    $INC{'MyApp/Controller/Root.pm'} = __FILE__;

    use warnings;
    use strict;
    use base 'Catalyst::Controller';

    sub root :Chained('/') PathPart('') CaptureArgs(0) {
        my ($self, $c) = @_;
        my @a = (1);
        $a = $c->action->next(@a);
        push @$a, 7;
        $c->response->body(join ',',@$a);
    }

      sub a :Chained(root) PathPart('a') CaptureArgs(0) {
          my ($self, $c, @a) = @_;
          push @a, 2;
          my $a = $c->action->next(@a);
          push @$a, 6;
          return $a;
      }

          sub b :Chained(a) PathPart('b') CaptureArgs(1) {
              my ($self, $c, @a) = @_;
              push @a, 3;
              my $a = $c->action->next(@a);
              push @$a, 5;
              return $a;
          }

              sub c :Chained(b) PathPart('c') Args(0) {
                  my ($self, $c, @a) = @_;
                  push @a, 4;
                  return \@a;
              }

    package MyApp::Controller::User;
    $INC{'MyApp/Controller/User.pm'} = __FILE__;

    use warnings;
    use strict;
    use base 'Catalyst::Controller';
    use Data::Dumper;

    sub root :Chained('/') PathPart('user') CaptureArgs(0) {
      my ($self, $c) = @_;
      push @{ $c->stash->{state} }, $c->state;
      return 100;
    }

      sub a :Chained(root) PathPart(a) CaptureArgs(0) {
        my ($self, $c) = @_;
        push @{ $c->stash->{state} }, $c->state;
        return 101;
      }

        sub b :Chained(a) PathPart(b) Args(0) {
          my ($self, $c) = @_;
          push @{ $c->stash->{state} }, $c->state;
          $c->res->body(join ',', @{ $c->stash->{state} });
        }

    MyApp::Controller::Root->config(namespace=>'');

    package MyApp::Controller::Home;
    $INC{'MyApp/Controller/Home.pm'} = __FILE__;

    use warnings;
    use strict;
    use base 'Catalyst::Controller';
    use Data::Dumper;

    sub root :Chained('/') PathPart('') CaptureArgs(0) {
      my ($self, $c) = @_;
      my $before_state = $c->state;
      my $a = $c->action->next(100);
      my $after_state = $c->state;
      Test::More::is_deeply $a, $after_state;
      $c->res->body(Dumper [ 10, $before_state, $after_state ]);
    }

      sub d :Chained(root) PathPart('') CaptureArgs(0) {
        my ($self, $c) = @_;
        my $before_state = $c->state;
        my $a = $c->action->next(200);
        my $after_state = $c->state;
        Test::More::is_deeply $a, $after_state;
        return [20, $before_state, @$after_state];
      }

        sub dd :Chained(d) PathPart(dd) Args(0) {
          my ($self, $c) = @_;
          my $before_state = $c->state();
          return [30, $before_state];
        }

      sub e :Chained(root) PathPart(e) Args(0) {
        my ($self, $c) = @_;
        return 'foo';
      }

      sub f :Chained(root) PathPart(f) Args(0) {
        my ($self, $c) = @_;
        return +{ bar => 1, baz => [3,4,5] };
      }

    package MyApp;
    use Catalyst;

    MyApp->setup;
}

use Catalyst::Test 'MyApp';

{
  ok my $res = request '/user/a/b';
  is $res->content, '1,100,101';  ## ?? Not sure why state is 1 for the first action in the chain...
}

{
  ok my $res = request '/a/b/99/c';
  is $res->content, '1,2,99,3,4,5,6,7';
}

{
  ok my $res = request '/dd';
  my $data = eval $res->content;
  is_deeply $data, [
    10,
    1,
    [
      20,
      1,
      30,
      1,
    ],
  ];
}

{
  ok my $res = request '/e';
  my $data = eval $res->content;
  is_deeply $data, [
    10,
    1,
    "foo",
  ];
}

{
  ok my $res = request '/f';
  my $data = eval $res->content;
  is_deeply $data, [
    10,
    1,
    {
      bar => 1,
      baz => [
        3,
        4,
        5,
      ],
    },
  ];
}

done_testing;