doc/commands: remove ModelView dependency
[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, test_filter)
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_model, test_filter)
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, test_filter)
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, test_filter)
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, test_filter)
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, test_filter)
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, test_filter)
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, test_filter)
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, test_filter)
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, test_filter)
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 # CmdSearch
161
162 fun test_cmd_http_search is test do
163 var req = new_request("/?q=Carer&l=1")
164 var cmd = new CmdSearch(test_model, test_filter)
165 var res = cmd.http_init(req)
166 assert res isa CmdSuccess
167 assert cmd.results.as(not null).first.full_name == "test_prog::Career"
168 assert cmd.results.as(not null).length == 1
169 end
170
171 fun test_cmd_http_search_no_query is test do
172 var req = new_request("/")
173 var cmd = new CmdSearch(test_model, test_filter)
174 var res = cmd.http_init(req)
175 assert res isa ErrorNoQuery
176 end
177
178 # CmdFeatures
179
180 fun test_cmd_http_features is test do
181 var req = new_request("/test_prog::Character?l=10")
182 var cmd = new CmdFeatures(test_model, test_filter)
183 var res = cmd.http_init(req)
184 assert res isa CmdSuccess
185 assert cmd.results.as(not null).length == 10
186 end
187
188 fun test_cmd_http_features_no_features is test do
189 var req = new_request("/test_prog$Career$strength_bonus?l=10")
190 var cmd = new CmdFeatures(test_model, test_filter)
191 var res = cmd.http_init(req)
192 assert res isa WarningNoFeatures
193 end
194
195 # CmdLinearization
196
197 fun test_cmd_http_lin is test do
198 var req = new_request("/init?l=10")
199 var cmd = new CmdLinearization(test_model, test_main, test_filter)
200 var res = cmd.http_init(req)
201 assert res isa CmdSuccess
202 assert cmd.results.as(not null).length == 10
203 end
204
205 fun test_cmd_http_lin_no_lin is test do
206 var req = new_request("/test_prog?l=10")
207 var cmd = new CmdLinearization(test_model, test_main, test_filter)
208 var res = cmd.http_init(req)
209 assert res isa WarningNoLinearization
210 end
211
212 # CmdCode
213
214 fun test_cmd_http_code is test do
215 var req = new_request("/test_prog::Career")
216 var cmd = new CmdEntityCode(test_model, test_builder, test_filter)
217 var res = cmd.http_init(req)
218 assert res isa CmdSuccess
219 assert cmd.node isa AStdClassdef
220 assert cmd.format == "raw"
221 end
222
223 fun test_cmd_http_code_format is test do
224 var req = new_request("/test_prog::Career?format=html")
225 var cmd = new CmdEntityCode(test_model, test_builder, test_filter)
226 var res = cmd.http_init(req)
227 assert res isa CmdSuccess
228 assert cmd.node isa AStdClassdef
229 assert cmd.format == "html"
230 end
231
232 fun test_cmd_http_code_no_code is test do
233 var req = new_request("/test_prog")
234 var cmd = new CmdEntityCode(test_model, test_builder, test_filter)
235 var res = cmd.http_init(req)
236 assert res isa WarningNoCode
237 end
238
239 # CmdModel
240
241 fun test_cmd_http_results is test do
242 var req = new_request("/?kind=modules&l=2")
243 var cmd = new CmdModelEntities(test_model, test_filter, kind = "modules")
244 var res = cmd.http_init(req)
245 assert res isa CmdSuccess
246 assert cmd.results.as(not null).length == 2
247 end
248
249 fun test_cmd_http_results_random is test do
250 var req = new_request("/?kind=packages&l=1")
251 var cmd = new CmdRandomEntities(test_model, test_filter, kind = "packages")
252 var res = cmd.http_init(req)
253 assert res isa CmdSuccess
254 assert cmd.results.as(not null).length == 1
255 end
256
257 # CmdGraph
258
259 fun test_cmd_http_uml is test do
260 var req = new_request("/test_prog::Character?format=svg")
261 var cmd = new CmdUML(test_model, test_main, test_filter)
262 var res = cmd.http_init(req)
263 assert res isa CmdSuccess
264 assert cmd.uml != null
265 assert cmd.format == "svg"
266 end
267
268 fun test_cmd_http_uml_not_found is test do
269 var req = new_request("/strength_bonus")
270 var cmd = new CmdUML(test_model, test_main, test_filter)
271 var res = cmd.http_init(req)
272 assert res isa WarningNoUML
273 assert cmd.format == "dot"
274 assert cmd.uml == null
275 end
276
277 fun test_cmd_http_inh_graph is test do
278 var req = new_request("/test_prog::Character?pdepth=1&cdepth=1")
279 var cmd = new CmdInheritanceGraph(test_model, test_main, test_filter)
280 var res = cmd.http_init(req)
281 assert res isa CmdSuccess
282 assert cmd.graph != null
283 assert cmd.pdepth == 1
284 assert cmd.cdepth == 1
285 end
286
287 # CmdCatalog
288
289 fun test_cmd_http_catalog_search is test do
290 var req = new_request("/?q=testprog&l=1")
291 var cmd = new CmdCatalogSearch(test_model, test_catalog, test_filter)
292 var res = cmd.http_init(req)
293 assert res isa CmdSuccess
294 assert cmd.results.as(not null).first.full_name == "test_prog"
295 assert cmd.results.as(not null).first isa MPackage
296 assert cmd.results.as(not null).length == 1
297 end
298
299 fun test_cmd_http_catalog_tag is test do
300 var req = new_request("/test", "/:tid")
301 var cmd = new CmdCatalogTag(test_model, test_catalog, test_filter)
302 var res = cmd.http_init(req)
303 assert res isa CmdSuccess
304 assert cmd.tag == "test"
305 assert cmd.results.as(not null).length == 1
306 end
307
308 fun test_cmd_http_catalog_person is test do
309 var req = new_request("/Alexandre%20Terrasa", "/:pid")
310 var cmd = new CmdCatalogPerson(test_model, test_catalog, test_filter)
311 var res = cmd.http_init(req)
312 assert res isa CmdSuccess
313 assert cmd.person.as(not null).name == "Alexandre Terrasa"
314 end
315 end
316
317 # Dummy action that does nothing
318 #
319 # Used to build the test route / http request.
320 private class TestDummyAction
321 super Action
322 end
323
324 redef class CmdUML
325 # FIXME linerarization
326 redef fun http_init(req) do return super
327 end