comparison _test/Test/Web/ViewSelector.pm @ 285:546957c50a36

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