Merge: doc: fixed some typos and other misc. corrections
[nit.git] / lib / popcorn / tests / 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
18
19 import pop_routes
20
21 class TestAppRoute
22 test
23
24 fun test_root_match_only_one_uri is test do
25 var r = new AppRoute("/")
26 assert r.match("")
27 assert r.match("/")
28 assert not r.match("/user")
29 end
30
31 fun test_strict_route_match_only_one_uri is test do
32 var r = new AppRoute("/user")
33 assert not r.match("/")
34 assert r.match("/user")
35 assert r.match("/user/")
36 assert not r.match("/user/10")
37 assert not r.match("/foo")
38 end
39 end
40
41 class TestAppParamRoute
42 test
43
44 fun test_param_route_match_good_uri_params_1 is test do
45 var r = new AppParamRoute("/:id")
46 assert not r.match("/")
47 assert r.match("/user")
48 assert r.match("/user/")
49 assert not r.match("/user/10")
50 end
51
52 fun test_param_route_match_good_uri_params_2 is test do
53 var r = new AppParamRoute("/user/:id")
54 assert not r.match("/")
55 assert not r.match("/user")
56 assert not r.match("/user/")
57 assert r.match("/user/10")
58 assert r.match("/user/10/")
59 assert not r.match("/user/10/profile")
60 end
61
62 fun test_param_route_match_good_uri_params_3 is test do
63 var r = new AppParamRoute("/user/:id/profile")
64 assert not r.match("/")
65 assert not r.match("/user")
66 assert not r.match("/user/")
67 assert not r.match("/user/10")
68 assert not r.match("/user/10/")
69 assert r.match("/user/10/profile")
70 assert r.match("/user/10/profile/")
71 assert not r.match("/user/10/profile/settings")
72 assert not r.match("/user/10/foo")
73 end
74
75 fun test_param_route_match_good_uri_params_4 is test do
76 var r = new AppParamRoute("/:id/:foo")
77 assert not r.match("/")
78 assert not r.match("/user")
79 assert not r.match("/user/")
80 assert r.match("/user/10")
81 assert r.match("/user/10/")
82 assert not r.match("/user/10/10")
83 end
84
85 fun test_param_route_match_good_uri_params_5 is test do
86 var r = new AppParamRoute("/user/:id/:foo")
87 assert not r.match("/")
88 assert not r.match("/user")
89 assert not r.match("/foo")
90 assert not r.match("/user/10")
91 assert r.match("/user/10/10")
92 assert r.match("/user/10/10/")
93 assert not r.match("/user/10/10/profile")
94 end
95
96 fun test_param_route_match_good_uri_params_6 is test do
97 var r = new AppParamRoute("/user/:id/settings/:foo")
98 assert not r.match("/")
99 assert not r.match("/user")
100 assert not r.match("/foo")
101 assert not r.match("/user/10")
102 assert not r.match("/user/10/10")
103 assert not r.match("/user/10/10/")
104 assert not r.match("/user/10/10/profile")
105 assert r.match("/user/10/settings/profile")
106 assert r.match("/user/10/settings/profile/")
107 assert not r.match("/user/10/settings/profile/10")
108 end
109 end
110
111 class TestRouteMatching
112 test
113
114 fun test_glob_route_match_good_uri_prefix1 is test do
115 var r = new AppGlobRoute("/*")
116 assert r.match("/")
117 assert r.match("/user")
118 assert r.match("/user/10")
119 end
120
121 fun test_glob_route_match_good_uri_prefix2 is test do
122 var r = new AppGlobRoute("/user/*")
123 assert not r.match("/")
124 assert r.match("/user")
125 assert r.match("/user/10")
126 end
127
128 fun test_glob_route_match_good_uri_prefix3 is test do
129 var r = new AppGlobRoute("/user*")
130 assert not r.match("/")
131 assert r.match("/user")
132 assert r.match("/user/10")
133 end
134
135 fun test_glob_route_work_with_parameters_1 is test do
136 var r = new AppGlobRoute("/:id/*")
137 assert not r.match("/")
138 assert r.match("/user")
139 assert r.match("/user/10")
140 assert r.match("/user/10/profile")
141 end
142
143 fun test_glob_route_work_with_parameters_2 is test do
144 var r = new AppGlobRoute("/:id*")
145 assert not r.match("/")
146 assert r.match("/user")
147 assert r.match("/user/10")
148 end
149
150 fun test_glob_route_work_with_parameters_3 is test do
151 var r = new AppGlobRoute("/user/:id/*")
152 assert not r.match("/")
153 assert not r.match("/user")
154 assert r.match("/user/10")
155 assert r.match("/user/10/profile")
156 end
157
158 fun test_glob_route_work_with_parameters_4 is test do
159 var r = new AppGlobRoute("/user/:id*")
160 assert not r.match("/")
161 assert not r.match("/user")
162 assert r.match("/user/10")
163 assert r.match("/user/10/profile")
164 end
165 end