Mercurial > pub > Impl
annotate _test/Test/Web/ViewSelector.pm @ 339:97628101b765
refactoring: application now holds a security object factory rather than a security object
author | cin |
---|---|
date | Wed, 19 Jun 2013 03:25:44 +0400 |
parents | 546957c50a36 |
children | 608e74bc309f |
rev | line source |
---|---|
285
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
1 package Test::Web::ViewSelector; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
2 use strict; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
3 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
4 use Scalar::Util qw(reftype); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
5 use IMPL::Test qw(test assert assertarray); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
6 use IMPL::declare { |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
7 require => { |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
8 TTView => 'IMPL::Web::Handler::TTView' |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
9 }, |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
10 base => [ |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
11 'IMPL::Test::Unit' => '@_' |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
12 ] |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
13 }; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
14 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
15 test TestParseRules => sub { |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
16 my $rule = TTView->ParseRule('/category/item => item.html'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
17 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
18 assert(reftype($rule) eq 'HASH'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
19 assert(reftype($rule->{selector}) eq 'ARRAY'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
20 assertarray([map $_->{name}, @{$rule->{selector}}], [qw(category item)]); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
21 assert($rule->{data} eq 'item.html'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
22 assert($rule->{weight} == 2); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
23 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
24 $rule = TTView->ParseRule('category//item => item.html'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
25 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
26 assert(reftype($rule) eq 'HASH'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
27 assert(reftype($rule->{selector}) eq 'ARRAY'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
28 assertarray([map $_->{name}, @{$rule->{selector}}], [undef, 'category', undef ,'item']); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
29 assert($rule->{data} eq 'item.html'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
30 assert($rule->{weight} == 2); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
31 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
32 $rule = TTView->ParseRule('///category//item///// => item.html'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
33 # trailing slashes must be ignored |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
34 assertarray([map $_->{name}, @{$rule->{selector}}], [undef,undef, 'category', undef ,'item']); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
35 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
36 $rule = TTView->ParseRule('{cat:\w+}@OrgClass/products/@My::PoductClass/view => view.html'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
37 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
38 assert(reftype($rule) eq 'HASH'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
39 assert(reftype($rule->{selector}) eq 'ARRAY'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
40 assert($rule->{data} eq 'view.html'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
41 assert($rule->{weight} == 4); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
42 assert($rule->{selector}[0]->{any}); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
43 assert($rule->{selector}[1]->{rx}); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
44 assert($rule->{selector}[1]->{var} eq 'cat'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
45 assert($rule->{selector}[1]->{class} eq 'OrgClass'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
46 assert($rule->{selector}[3]->{class} eq 'My::PoductClass'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
47 assertarray([map $_->{name}, @{$rule->{selector}}], [undef,undef, 'products', undef ,'view']); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
48 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
49 $rule = TTView->ParseRule('/ => index.html'); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
50 assert($rule->{weight} == 0); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
51 }; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
52 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
53 test TestNamesMatch => sub { |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
54 my @rules = map TTView->ParseRule($_), |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
55 'view => view.html', # weight 1 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
56 'shoes/view => shoes/view.html', # weight 2 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
57 '/root/org/items/add => add.html'; # weight 4 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
58 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
59 assert( |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
60 TTView->MatchPath( |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
61 [ map { name => $_ }, qw(root view)], |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
62 \@rules |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
63 ) eq 'view.html' |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
64 ); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
65 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
66 assert( |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
67 TTView->MatchPath( |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
68 [ map { name => $_ }, qw(root shoes view)], |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
69 \@rules |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
70 ) eq 'shoes/view.html' |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
71 ); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
72 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
73 assert( |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
74 TTView->MatchPath( |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
75 [ map { name => $_ }, qw(root org products shoes view)], |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
76 \@rules |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
77 ) eq 'shoes/view.html' |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
78 ); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
79 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
80 assert( |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
81 TTView->MatchPath( |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
82 [ map { name => $_ }, qw(root org items add)], |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
83 \@rules |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
84 ) eq 'add.html' |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
85 ); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
86 }; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
87 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
88 { |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
89 package Test::Web::ViewSelector::Container; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
90 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
91 package Test::Web::ViewSelector::Orgs; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
92 use IMPL::declare { |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
93 base => ['-Test::Web::ViewSelector::Container' => undef] |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
94 }; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
95 } |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
96 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
97 test TestComplexMatch => sub { |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
98 my @rules = map TTView->ParseRule($_), |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
99 '{container:.*}@Test::Web::ViewSelector::Container/{item:.*}/{action:.*} => {container}/{item}/{action}.html', # weight 3 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
100 '/root//orgs/{org:.*}/info => orgs/{org}.html', # weight 4 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
101 '@Test::Web::ViewSelector::Container => container.html'; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
102 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
103 my $path = [ |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
104 { name => 'root'}, |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
105 { name => 'list', class => 'Test::Web::ViewSelector::Container'}, |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
106 { name => 'hp' }, |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
107 { name => 'info'} |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
108 ]; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
109 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
110 my $result = TTView->MatchPath($path,\@rules); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
111 my $expected = 'list/hp/info.html'; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
112 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
113 assert( $result eq $expected, "Expected: $expected", "Got: $result" ); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
114 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
115 $path = [ |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
116 { name => 'root'}, |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
117 { name => 'orgs', class => 'Test::Web::ViewSelector::Orgs'}, |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
118 { name => 'ms' }, |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
119 { name => 'info'} |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
120 ]; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
121 $result = TTView->MatchPath($path,\@rules); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
122 $expected = 'orgs/ms.html'; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
123 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
124 assert( $result eq $expected, "Expected: $expected", "Got: $result" ); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
125 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
126 $path = [ |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
127 { name => 'root'}, |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
128 { name => 'service'}, |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
129 { name => 'orgs', class => 'Test::Web::ViewSelector::Container' } |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
130 ]; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
131 $result = TTView->MatchPath($path,\@rules); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
132 $expected = 'container.html'; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
133 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
134 assert( $result eq $expected, "Expected: $expected", "Got: $result" ); |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
135 }; |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
136 |
546957c50a36
*IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents:
diff
changeset
|
137 1; |