doc/commands: introduce command initialization from HTTP requests
[nit.git] / src / doc / commands / tests / test_commands_http.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 module test_commands_http is test
16
17 import doc::commands::test_commands_catalog
18 import doc::commands::commands_http
19
20 class TestCommandsHttp
21 super TestCommandsCatalog
22 test
23
24 # Http parser to create Http requests
25 var http_parser = new HttpRequestParser
26
27 # Create a new and initialized http request from the `url` string
28 fun new_request(url: String, route_pattern: nullable String): HttpRequest do
29 if route_pattern == null then route_pattern = "/:id"
30 var route = new Route(route_pattern, new TestDummyAction)
31 var req = http_parser.parse_http_request("GET {url} HTTP/1.0")
32 assert req != null
33 req.uri_params = route.parse_params(req.uri)
34 return req
35 end
36
37 # CmdEntity
38
39 fun test_cmd_http_entity is test do
40 var req = new_request("/test_prog::Character")
41 var cmd = new CmdEntity(test_view)
42 var res = cmd.http_init(req)
43 assert res isa CmdSuccess
44 assert cmd.mentity.as(not null).full_name == "test_prog::Character"
45 end
46
47 fun test_cmd_http_entity_not_found is test do
48 var req = new_request("/Characterzzz")
49 var cmd = new CmdEntity(test_view)
50 var res = cmd.http_init(req)
51 assert res isa ErrorMEntityNotFound
52 assert res.suggestions.first.full_name == "test_prog::Character"
53 end
54
55 fun test_cmd_http_entity_conflict is test do
56 var req = new_request("/+")
57 var cmd = new CmdEntity(test_view)
58 var res = cmd.http_init(req)
59 assert res isa ErrorMEntityConflict
60 assert res.conflicts.length == 2
61 end
62
63 # CmdComment
64
65 fun test_cmd_http_comment is test do
66 var req = new_request("/test_prog::Character")
67 var cmd = new CmdComment(test_view)
68 var res = cmd.http_init(req)
69 assert res isa CmdSuccess
70 assert cmd.mdoc != null
71 end
72
73 fun test_cmd_http_comment_no_mdoc is test do
74 var req = new_request("/test_prog::Character?fallback=false")
75 var cmd = new CmdComment(test_view)
76 var res = cmd.http_init(req)
77 assert res isa WarningNoMDoc
78 end
79
80 # CmdInheritance
81
82 fun test_cmd_http_parents is test do
83 var req = new_request("/test_prog::Warrior")
84 var cmd = new CmdParents(test_view)
85 var res = cmd.http_init(req)
86 assert res isa CmdSuccess
87 assert cmd.results.as(not null).length == 1
88 end
89
90 fun test_cmd_http_ancestors is test do
91 var req = new_request("/test_prog::Warrior")
92 var cmd = new CmdAncestors(test_view)
93 var res = cmd.http_init(req)
94 assert res isa CmdSuccess
95 assert cmd.results.as(not null).length == 2
96 end
97
98 fun test_cmd_http_ancestorsi_without_parents is test do
99 var req = new_request("/test_prog::Warrior?parents=false")
100 var cmd = new CmdAncestors(test_view)
101 var res = cmd.http_init(req)
102 assert res isa CmdSuccess
103 assert cmd.results.as(not null).length == 1
104 end
105
106 fun test_cmd_http_children is test do
107 var req = new_request("/test_prog::Career")
108 var cmd = new CmdChildren(test_view)
109 var res = cmd.http_init(req)
110 assert res isa CmdSuccess
111 assert cmd.results.as(not null).length == 3
112 end
113
114 fun test_cmd_http_descendants is test do
115 var req = new_request("/test_prog::Career?children=false")
116 var cmd = new CmdDescendants(test_view)
117 var res = cmd.http_init(req)
118 assert res isa CmdSuccess
119 assert cmd.results.as(not null).length == 0
120 end
121
122 # CmdSearch
123
124 fun test_cmd_http_search is test do
125 var req = new_request("/?q=Carer&l=1")
126 var cmd = new CmdSearch(test_view)
127 var res = cmd.http_init(req)
128 assert res isa CmdSuccess
129 assert cmd.results.as(not null).first.full_name == "test_prog::Career"
130 assert cmd.results.as(not null).length == 1
131 end
132
133 fun test_cmd_http_search_no_query is test do
134 var req = new_request("/")
135 var cmd = new CmdSearch(test_view)
136 var res = cmd.http_init(req)
137 assert res isa ErrorNoQuery
138 end
139
140 # CmdFeatures
141
142 fun test_cmd_http_features is test do
143 var req = new_request("/test_prog::Character?l=10")
144 var cmd = new CmdFeatures(test_view)
145 var res = cmd.http_init(req)
146 assert res isa CmdSuccess
147 assert cmd.results.as(not null).length == 10
148 end
149
150 fun test_cmd_http_features_no_features is test do
151 var req = new_request("/test_prog$Career$strength_bonus?l=10")
152 var cmd = new CmdFeatures(test_view)
153 var res = cmd.http_init(req)
154 assert res isa WarningNoFeatures
155 end
156
157 # CmdLinearization
158
159 fun test_cmd_http_lin is test do
160 var req = new_request("/init?l=10")
161 var cmd = new CmdLinearization(test_view)
162 var res = cmd.http_init(req)
163 assert res isa CmdSuccess
164 assert cmd.results.as(not null).length == 10
165 end
166
167 fun test_cmd_http_lin_no_lin is test do
168 var req = new_request("/test_prog?l=10")
169 var cmd = new CmdLinearization(test_view)
170 var res = cmd.http_init(req)
171 assert res isa WarningNoLinearization
172 end
173
174 # CmdCode
175
176 fun test_cmd_http_code is test do
177 var req = new_request("/test_prog::Career")
178 var cmd = new CmdCode(test_view, test_builder)
179 var res = cmd.http_init(req)
180 assert res isa CmdSuccess
181 assert cmd.node isa AStdClassdef
182 assert cmd.format == "raw"
183 end
184
185 fun test_cmd_http_code_format is test do
186 var req = new_request("/test_prog::Career?format=html")
187 var cmd = new CmdCode(test_view, test_builder)
188 var res = cmd.http_init(req)
189 assert res isa CmdSuccess
190 assert cmd.node isa AStdClassdef
191 assert cmd.format == "html"
192 end
193
194 fun test_cmd_http_code_no_code is test do
195 var req = new_request("/test_prog")
196 var cmd = new CmdCode(test_view, test_builder)
197 var res = cmd.http_init(req)
198 assert res isa WarningNoCode
199 end
200
201 # CmdModel
202
203 fun test_cmd_http_results is test do
204 var req = new_request("/?kind=modules&l=2")
205 var cmd = new CmdModelEntities(test_view, kind = "modules")
206 var res = cmd.http_init(req)
207 assert res isa CmdSuccess
208 assert cmd.results.as(not null).length == 2
209 end
210
211 fun test_cmd_http_results_random is test do
212 var req = new_request("/?kind=packages&l=1")
213 var cmd = new CmdRandomEntities(test_view, kind = "packages")
214 var res = cmd.http_init(req)
215 assert res isa CmdSuccess
216 assert cmd.results.as(not null).length == 1
217 end
218
219 # CmdGraph
220
221 fun test_cmd_http_uml is test do
222 var req = new_request("/test_prog::Character?format=svg")
223 var cmd = new CmdUML(test_view)
224 var res = cmd.http_init(req)
225 assert res isa CmdSuccess
226 assert cmd.uml != null
227 assert cmd.format == "svg"
228 end
229
230 fun test_cmd_http_uml_not_found is test do
231 var req = new_request("/strength_bonus")
232 var cmd = new CmdUML(test_view)
233 var res = cmd.http_init(req)
234 assert res isa WarningNoUML
235 assert cmd.format == "dot"
236 assert cmd.uml == null
237 end
238
239 fun test_cmd_http_inh_graph is test do
240 var req = new_request("/test_prog::Character?pdepth=1&cdepth=1")
241 var cmd = new CmdInheritanceGraph(test_view)
242 var res = cmd.http_init(req)
243 assert res isa CmdSuccess
244 assert cmd.graph != null
245 assert cmd.pdepth == 1
246 assert cmd.cdepth == 1
247 end
248
249 # CmdCatalog
250
251 fun test_cmd_http_catalog_search is test do
252 var req = new_request("/?q=test&l=1")
253 var cmd = new CmdCatalogSearch(test_view, test_catalog)
254 var res = cmd.http_init(req)
255 assert res isa CmdSuccess
256 assert cmd.results.as(not null).first.full_name == "test_prog"
257 assert cmd.results.as(not null).first isa MPackage
258 assert cmd.results.as(not null).length == 1
259 end
260
261 fun test_cmd_http_catalog_tag is test do
262 var req = new_request("/test", "/:tid")
263 var cmd = new CmdCatalogTag(test_view, test_catalog)
264 var res = cmd.http_init(req)
265 assert res isa CmdSuccess
266 assert cmd.tag == "test"
267 assert cmd.results.as(not null).length == 1
268 end
269
270 fun test_cmd_http_catalog_person is test do
271 var req = new_request("/Alexandre%20Terrasa", "/:pid")
272 var cmd = new CmdCatalogPerson(test_view, test_catalog)
273 var res = cmd.http_init(req)
274 assert res isa CmdSuccess
275 assert cmd.person.as(not null).name == "Alexandre Terrasa"
276 end
277 end
278
279 # Dummy action that does nothing
280 #
281 # Used to build the test route / http request.
282 private class TestDummyAction
283 super Action
284 end
285
286 redef class CmdUML
287 # FIXME linerarization
288 redef fun http_init(req) do return super
289 end