lib/popcorn: introduce pop_routes
[nit.git] / lib / popcorn / test_pop_routes.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 module test_pop_routes is test_suite
18
19 import pop_routes
20 import test_suite
21
22 class TestAppRoute
23 super TestSuite
24
25 fun test_root_match_only_one_uri do
26 var r = new AppRoute("/")
27 assert r.match("")
28 assert r.match("/")
29 assert not r.match("/user")
30 end
31
32 fun test_strict_route_match_only_one_uri do
33 var r = new AppRoute("/user")
34 assert not r.match("/")
35 assert r.match("/user")
36 assert r.match("/user/")
37 assert not r.match("/user/10")
38 assert not r.match("/foo")
39 end
40 end
41
42 class TestAppParamRoute
43 super TestSuite
44
45 fun test_param_route_match_good_uri_params_1 do
46 var r = new AppParamRoute("/:id")
47 assert not r.match("/")
48 assert r.match("/user")
49 assert r.match("/user/")
50 assert not r.match("/user/10")
51 end
52
53 fun test_param_route_match_good_uri_params_2 do
54 var r = new AppParamRoute("/user/:id")
55 assert not r.match("/")
56 assert not r.match("/user")
57 assert not r.match("/user/")
58 assert r.match("/user/10")
59 assert r.match("/user/10/")
60 assert not r.match("/user/10/profile")
61 end
62
63 fun test_param_route_match_good_uri_params_3 do
64 var r = new AppParamRoute("/user/:id/profile")
65 assert not r.match("/")
66 assert not r.match("/user")
67 assert not r.match("/user/")
68 assert not r.match("/user/10")
69 assert not r.match("/user/10/")
70 assert r.match("/user/10/profile")
71 assert r.match("/user/10/profile/")
72 assert not r.match("/user/10/profile/settings")
73 assert not r.match("/user/10/foo")
74 end
75
76 fun test_param_route_match_good_uri_params_4 do
77 var r = new AppParamRoute("/:id/:foo")
78 assert not r.match("/")
79 assert not r.match("/user")
80 assert not r.match("/user/")
81 assert r.match("/user/10")
82 assert r.match("/user/10/")
83 assert not r.match("/user/10/10")
84 end
85
86 fun test_param_route_match_good_uri_params_5 do
87 var r = new AppParamRoute("/user/:id/:foo")
88 assert not r.match("/")
89 assert not r.match("/user")
90 assert not r.match("/foo")
91 assert not r.match("/user/10")
92 assert r.match("/user/10/10")
93 assert r.match("/user/10/10/")
94 assert not r.match("/user/10/10/profile")
95 end
96
97 fun test_param_route_match_good_uri_params_6 do
98 var r = new AppParamRoute("/user/:id/settings/:foo")
99 assert not r.match("/")
100 assert not r.match("/user")
101 assert not r.match("/foo")
102 assert not r.match("/user/10")
103 assert not r.match("/user/10/10")
104 assert not r.match("/user/10/10/")
105 assert not r.match("/user/10/10/profile")
106 assert r.match("/user/10/settings/profile")
107 assert r.match("/user/10/settings/profile/")
108 assert not r.match("/user/10/settings/profile/10")
109 end
110 end
111
112 class TestRouteMatching
113 super TestSuite
114
115 fun test_glob_route_match_good_uri_prefix1 do
116 var r = new AppGlobRoute("/*")
117 assert r.match("/")
118 assert r.match("/user")
119 assert r.match("/user/10")
120 end
121
122 fun test_glob_route_match_good_uri_prefix2 do
123 var r = new AppGlobRoute("/user/*")
124 assert not r.match("/")
125 assert r.match("/user")
126 assert r.match("/user/10")
127 end
128
129 fun test_glob_route_match_good_uri_prefix3 do
130 var r = new AppGlobRoute("/user*")
131 assert not r.match("/")
132 assert r.match("/user")
133 assert r.match("/user/10")
134 end
135
136 fun test_glob_route_work_with_parameters_1 do
137 var r = new AppGlobRoute("/:id/*")
138 assert not r.match("/")
139 assert r.match("/user")
140 assert r.match("/user/10")
141 assert r.match("/user/10/profile")
142 end
143
144 fun test_glob_route_work_with_parameters_2 do
145 var r = new AppGlobRoute("/:id*")
146 assert not r.match("/")
147 assert r.match("/user")
148 assert r.match("/user/10")
149 end
150
151 fun test_glob_route_work_with_parameters_3 do
152 var r = new AppGlobRoute("/user/:id/*")
153 assert not r.match("/")
154 assert not r.match("/user")
155 assert r.match("/user/10")
156 assert r.match("/user/10/profile")
157 end
158
159 fun test_glob_route_work_with_parameters_4 do
160 var r = new AppGlobRoute("/user/:id*")
161 assert not r.match("/")
162 assert not r.match("/user")
163 assert r.match("/user/10")
164 assert r.match("/user/10/profile")
165 end
166 end