tests: Fix tests
[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_model)
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("/Characterz")
49 var cmd = new CmdEntity(test_model)
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_model)
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_model)
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_model)
76 var res = cmd.http_init(req)
77 assert res isa WarningNoMDoc
78 end
79
80 # CmdEntityLink
81
82 fun test_cmd_http_link is test do
83 var req = new_request("/test_prog::Character")
84 var cmd = new CmdEntityLink(test_model)
85 var res = cmd.http_init(req)
86 assert res isa CmdSuccess
87 assert cmd.text == "Character"
88 assert cmd.title == "Characters can be played by both the human or the machine."
89 end
90
91 fun test_cmd_http_link_with_text is test do
92 var req = new_request("/test_prog::Character?text=foo")
93 var cmd = new CmdEntityLink(test_model)
94 var res = cmd.http_init(req)
95 assert res isa CmdSuccess
96 assert cmd.text == "foo"
97 assert cmd.title == "Characters can be played by both the human or the machine."
98 end
99
100 fun test_cmd_http_link_with_title is test do
101 var req = new_request("/test_prog::Character?title=bar")
102 var cmd = new CmdEntityLink(test_model)
103 var res = cmd.http_init(req)
104 assert res isa CmdSuccess
105 assert cmd.text == "Character"
106 assert cmd.title == "bar"
107 end
108
109 fun test_cmd_http_link_with_text_and_title is test do
110 var req = new_request("/test_prog::Character?text=foo&title=bar")
111 var cmd = new CmdEntityLink(test_model)
112 var res = cmd.http_init(req)
113 assert res isa CmdSuccess
114 assert cmd.text == "foo"
115 assert cmd.title == "bar"
116 end
117
118 # CmdInheritance
119
120 fun test_cmd_http_parents is test do
121 var req = new_request("/test_prog::Warrior")
122 var cmd = new CmdParents(test_model, test_main)
123 var res = cmd.http_init(req)
124 assert res isa CmdSuccess
125 assert cmd.results.as(not null).length == 1
126 end
127
128 fun test_cmd_http_ancestors is test do
129 var req = new_request("/test_prog::Warrior")
130 var cmd = new CmdAncestors(test_model, test_main)
131 var res = cmd.http_init(req)
132 assert res isa CmdSuccess
133 assert cmd.results.as(not null).length == 2
134 end
135
136 fun test_cmd_http_ancestorsi_without_parents is test do
137 var req = new_request("/test_prog::Warrior?parents=false")
138 var cmd = new CmdAncestors(test_model, test_main)
139 var res = cmd.http_init(req)
140 assert res isa CmdSuccess
141 assert cmd.results.as(not null).length == 1
142 end
143
144 fun test_cmd_http_children is test do
145 var req = new_request("/test_prog::Career")
146 var cmd = new CmdChildren(test_model, test_main)
147 var res = cmd.http_init(req)
148 assert res isa CmdSuccess
149 assert cmd.results.as(not null).length == 3
150 end
151
152 fun test_cmd_http_descendants is test do
153 var req = new_request("/test_prog::Career?children=false")
154 var cmd = new CmdDescendants(test_model, test_main)
155 var res = cmd.http_init(req)
156 assert res isa CmdSuccess
157 assert cmd.results.as(not null).length == 0
158 end
159
160 fun test_cmd_http_ancestors_with_filter_match is test do
161 var req = new_request("/test_prog::Warrior?match=Object")
162 var cmd = new CmdAncestors(test_model, test_main)
163 var res = cmd.http_init(req)
164 assert res isa CmdSuccess
165 assert cmd.results.as(not null).length == 1
166 end
167
168 # CmdSearch
169
170 fun test_cmd_http_search is test do
171 var req = new_request("/?q=Carer&l=1")
172 var cmd = new CmdSearch(test_model)
173 var res = cmd.http_init(req)
174 assert res isa CmdSuccess
175 assert cmd.results.as(not null).first.full_name == "test_prog::Career"
176 assert cmd.results.as(not null).length == 1
177 end
178
179 fun test_cmd_http_search_no_query is test do
180 var req = new_request("/")
181 var cmd = new CmdSearch(test_model)
182 var res = cmd.http_init(req)
183 assert res isa ErrorNoQuery
184 end
185
186 # CmdFeatures
187
188 fun test_cmd_http_features is test do
189 var req = new_request("/test_prog::Character?l=10")
190 var cmd = new CmdFeatures(test_model)
191 var res = cmd.http_init(req)
192 assert res isa CmdSuccess
193 assert cmd.results.as(not null).length == 10
194 end
195
196 fun test_cmd_http_features_no_features is test do
197 var req = new_request("/test_prog$Career$strength_bonus?l=10")
198 var cmd = new CmdFeatures(test_model)
199 var res = cmd.http_init(req)
200 assert res isa WarningNoFeatures
201 end
202
203 fun test_cmd_http_features_with_filter_inherited is test do
204 var req = new_request("/test_prog::TestGame?inherited=TestGame")
205 var cmd = new CmdFeatures(test_model)
206 var res = cmd.http_init(req)
207 assert res isa CmdSuccess
208 assert cmd.results.as(not null).length == 4
209 end
210
211 # CmdLinearization
212
213 fun test_cmd_http_lin is test do
214 var req = new_request("/init?l=10")
215 var cmd = new CmdLinearization(test_model, test_main)
216 var res = cmd.http_init(req)
217 assert res isa CmdSuccess
218 assert cmd.results.as(not null).length == 9
219 end
220
221 fun test_cmd_http_lin_no_lin is test do
222 var req = new_request("/test_prog?l=10")
223 var cmd = new CmdLinearization(test_model, test_main)
224 var res = cmd.http_init(req)
225 assert res isa WarningNoLinearization
226 end
227
228 # CmdCode
229
230 fun test_cmd_http_code is test do
231 var req = new_request("/test_prog::Career")
232 var cmd = new CmdEntityCode(test_model, test_builder)
233 var res = cmd.http_init(req)
234 assert res isa CmdSuccess
235 assert cmd.node isa AStdClassdef
236 assert cmd.format == "raw"
237 end
238
239 fun test_cmd_http_code_format is test do
240 var req = new_request("/test_prog::Career?format=html")
241 var cmd = new CmdEntityCode(test_model, test_builder)
242 var res = cmd.http_init(req)
243 assert res isa CmdSuccess
244 assert cmd.node isa AStdClassdef
245 assert cmd.format == "html"
246 end
247
248 fun test_cmd_http_code_no_code is test do
249 var req = new_request("/test_prog")
250 var cmd = new CmdEntityCode(test_model, test_builder)
251 var res = cmd.http_init(req)
252 assert res isa WarningNoCode
253 end
254
255 # CmdModel
256
257 fun test_cmd_http_results is test do
258 var req = new_request("/?kind=modules&l=2")
259 var cmd = new CmdModelEntities(test_model, kind = "modules")
260 var res = cmd.http_init(req)
261 assert res isa CmdSuccess
262 assert cmd.results.as(not null).length == 2
263 end
264
265 fun test_cmd_http_results_random is test do
266 var req = new_request("/?kind=packages&l=1")
267 var cmd = new CmdRandomEntities(test_model, kind = "packages")
268 var res = cmd.http_init(req)
269 assert res isa CmdSuccess
270 assert cmd.results.as(not null).length == 1
271 end
272
273 # CmdGraph
274
275 fun test_cmd_http_uml is test do
276 var req = new_request("/test_prog::Character?format=svg")
277 var cmd = new CmdUML(test_model, test_main)
278 var res = cmd.http_init(req)
279 assert res isa CmdSuccess
280 assert cmd.uml != null
281 assert cmd.format == "svg"
282 end
283
284 fun test_cmd_http_uml_not_found is test do
285 var req = new_request("/strength_bonus")
286 var cmd = new CmdUML(test_model, test_main)
287 var res = cmd.http_init(req)
288 assert res isa WarningNoUML
289 assert cmd.format == "dot"
290 assert cmd.uml == null
291 end
292
293 fun test_cmd_http_inh_graph is test do
294 var req = new_request("/test_prog::Character?pdepth=1&cdepth=1")
295 var cmd = new CmdInheritanceGraph(test_model, test_main)
296 var res = cmd.http_init(req)
297 assert res isa CmdSuccess
298 assert cmd.graph != null
299 assert cmd.pdepth == 1
300 assert cmd.cdepth == 1
301 end
302
303 # CmdCatalog
304
305 fun test_cmd_http_catalog_search is test do
306 var req = new_request("/?q=testprog&l=1")
307 var cmd = new CmdCatalogSearch(test_model, test_catalog)
308 var res = cmd.http_init(req)
309 assert res isa CmdSuccess
310 assert cmd.results.as(not null).first.full_name == "test_prog"
311 assert cmd.results.as(not null).first isa MPackage
312 assert cmd.results.as(not null).length == 1
313 end
314
315 fun test_cmd_http_catalog_tag is test do
316 var req = new_request("/test", "/:tid")
317 var cmd = new CmdCatalogTag(test_model, test_catalog)
318 var res = cmd.http_init(req)
319 assert res isa CmdSuccess
320 assert cmd.tag == "test"
321 assert cmd.results.as(not null).length == 1
322 end
323
324 fun test_cmd_http_catalog_person is test do
325 var req = new_request("/Alexandre%20Terrasa", "/:pid")
326 var cmd = new CmdCatalogPerson(test_model, test_catalog)
327 var res = cmd.http_init(req)
328 assert res isa CmdSuccess
329 assert cmd.person.as(not null).name == "Alexandre Terrasa"
330 end
331 end
332
333 # Dummy action that does nothing
334 #
335 # Used to build the test route / http request.
336 private class TestDummyAction
337 super Action
338 end
339
340 redef class CmdUML
341 # FIXME linerarization
342 redef fun http_init(req) do return super
343 end