tests: Fix tests
[nit.git] / src / doc / commands / tests / test_commands_parser.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_parser is test
16
17 import test_commands
18 import doc::commands::tests::test_commands_catalog
19 import doc::commands::commands_parser
20
21 class TestCommandsParser
22 super TestCommandsCatalog
23 test
24
25 # CmdEntity
26
27 fun test_cmd_parser_comment is test do
28 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
29 var cmd = parser.parse("doc: test_prog::Character")
30 assert cmd isa CmdComment
31 assert parser.error == null
32 assert cmd.mdoc != null
33 end
34
35 fun test_cmd_parser_link is test do
36 var parser = new CommandParser(test_model, test_main, test_builder)
37 var cmd = parser.parse("link: test_prog::Character")
38 assert cmd isa CmdEntityLink
39 assert parser.error == null
40 assert cmd.text == "Character"
41 assert cmd.title == "Characters can be played by both the human or the machine."
42 end
43
44 fun test_cmd_parser_link_with_text is test do
45 var parser = new CommandParser(test_model, test_main, test_builder)
46 var cmd = parser.parse("link: test_prog::Character | text: foo")
47 assert cmd isa CmdEntityLink
48 assert parser.error == null
49 assert cmd.text == "foo"
50 assert cmd.title == "Characters can be played by both the human or the machine."
51 end
52
53 fun test_cmd_parser_link_with_title is test do
54 var parser = new CommandParser(test_model, test_main, test_builder)
55 var cmd = parser.parse("link: test_prog::Character | title: bar")
56 assert cmd isa CmdEntityLink
57 assert parser.error == null
58 assert cmd.text == "Character"
59 assert cmd.title == "bar"
60 end
61
62 fun test_cmd_parser_link_with_text_and_title is test do
63 var parser = new CommandParser(test_model, test_main, test_builder)
64 var cmd = parser.parse("link: test_prog::Character | text: foo, title: bar")
65 assert cmd isa CmdEntityLink
66 assert parser.error == null
67 assert cmd.text == "foo"
68 assert cmd.title == "bar"
69 end
70
71 fun test_cmd_parser_short_link is test do
72 var parser = new CommandParser(test_model, test_main, test_builder)
73 var cmd = parser.parse("test_prog::Character")
74 assert cmd isa CmdEntityLink
75 assert parser.error == null
76 assert cmd.text == "Character"
77 assert cmd.title == "Characters can be played by both the human or the machine."
78 end
79
80 fun test_cmd_parser_short_link_with_text is test do
81 var parser = new CommandParser(test_model, test_main, test_builder)
82 var cmd = parser.parse("test_prog::Character | text: foo")
83 assert cmd isa CmdEntityLink
84 assert parser.error == null
85 assert cmd.text == "foo"
86 assert cmd.title == "Characters can be played by both the human or the machine."
87 end
88
89 fun test_cmd_parser_short_link_with_title is test do
90 var parser = new CommandParser(test_model, test_main, test_builder)
91 var cmd = parser.parse("test_prog::Character | title: bar")
92 assert cmd isa CmdEntityLink
93 assert parser.error == null
94 assert cmd.text == "Character"
95 assert cmd.title == "bar"
96 end
97
98 fun test_cmd_parser_short_link_with_text_and_title is test do
99 var parser = new CommandParser(test_model, test_main, test_builder)
100 var cmd = parser.parse("test_prog::Character | text: foo, title: bar")
101 assert cmd isa CmdEntityLink
102 assert parser.error == null
103 assert cmd.text == "foo"
104 assert cmd.title == "bar"
105 end
106
107 fun test_cmd_parser_short_link_with_name is test do
108 var parser = new CommandParser(test_model, test_main, test_builder)
109 var cmd = parser.parse("Character")
110 assert cmd isa CmdEntityLink
111 assert parser.error == null
112 assert cmd.text == "Character"
113 assert cmd.title == "Characters can be played by both the human or the machine."
114 end
115
116 fun test_cmd_parser_short_link_with_name_and_text is test do
117 var parser = new CommandParser(test_model, test_main, test_builder)
118 var cmd = parser.parse("Character | text: foo")
119 assert cmd isa CmdEntityLink
120 assert parser.error == null
121 assert cmd.text == "foo"
122 assert cmd.title == "Characters can be played by both the human or the machine."
123 end
124
125 fun test_cmd_parser_short_link_with_name_and_title is test do
126 var parser = new CommandParser(test_model, test_main, test_builder)
127 var cmd = parser.parse("Character | title: bar")
128 assert cmd isa CmdEntityLink
129 assert parser.error == null
130 assert cmd.text == "Character"
131 assert cmd.title == "bar"
132 end
133
134 fun test_cmd_parser_short_link_with_name_and_text_and_title is test do
135 var parser = new CommandParser(test_model, test_main, test_builder)
136 var cmd = parser.parse("Character | text: foo, title: bar")
137 assert cmd isa CmdEntityLink
138 assert parser.error == null
139 assert cmd.text == "foo"
140 assert cmd.title == "bar"
141 end
142
143 # CmdInheritance
144
145 fun test_cmd_parser_parents is test do
146 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
147 var cmd = parser.parse("parents: test_prog::Warrior")
148 assert cmd isa CmdParents
149 assert parser.error == null
150 assert cmd.results.as(not null).length == 1
151 end
152
153 fun test_cmd_parser_ancestors is test do
154 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
155 var cmd = parser.parse("ancestors: test_prog::Warrior")
156 assert cmd isa CmdAncestors
157 assert parser.error == null
158 assert cmd.results.as(not null).length == 2
159 end
160
161 fun test_cmd_parser_ancestors_without_parents is test do
162 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
163 var cmd = parser.parse("ancestors: test_prog::Warrior | no-parents")
164 assert cmd isa CmdAncestors
165 assert parser.error == null
166 assert cmd.results.as(not null).length == 1
167 end
168
169 fun test_cmd_parser_children is test do
170 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
171 var cmd = parser.parse("children: test_prog::Career")
172 assert cmd isa CmdChildren
173 assert parser.error == null
174 assert cmd.results.as(not null).length == 3
175 end
176
177 fun test_cmd_parser_descendants is test do
178 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
179 var cmd = parser.parse("descendants: Object")
180 assert cmd isa CmdDescendants
181 assert parser.error == null
182 assert cmd.results.as(not null).length == 22
183 end
184
185 fun test_cmd_parser_descendants_without_children is test do
186 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
187 var cmd = parser.parse("descendants: Object | no-children: true")
188 assert cmd isa CmdDescendants
189 assert parser.error == null
190 assert cmd.results.as(not null).length == 9
191 end
192
193 fun test_cmd_parser_ancestors_with_filter_match is test do
194 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
195 var cmd = parser.parse("ancestors: test_prog::Warrior | match: Object")
196 assert cmd isa CmdAncestors
197 assert parser.error == null
198 assert cmd.results.as(not null).length == 1
199 end
200
201 # CmdSearch
202
203 fun test_cmd_parser_search is test do
204 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
205 var cmd = parser.parse("search: Caracter")
206 assert cmd isa CmdSearch
207 assert parser.error == null
208 assert cmd.results != null
209 end
210
211 fun test_cmd_parser_search_limit is test do
212 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
213 var cmd = parser.parse("search: strength | limit: 2")
214 assert cmd isa CmdSearch
215 assert parser.error == null
216 assert cmd.results.as(not null).length == 2
217 end
218
219 # CmdFeatures
220
221 fun test_cmd_parser_features is test do
222 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
223 var cmd = parser.parse("defs: test_prog::Character")
224 assert cmd isa CmdFeatures
225 assert parser.error == null
226 assert cmd.results != null
227 end
228
229 fun test_cmd_parser_features_limit is test do
230 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
231 var cmd = parser.parse("defs: test_prog::Character | limit: 2")
232 assert cmd isa CmdFeatures
233 assert parser.error == null
234 assert cmd.results.as(not null).length == 2
235 end
236
237 fun test_cmd_parser_with_filter_inherited is test do
238 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
239 var cmd = parser.parse("defs: test_prog::TestGame | inherited: TestGame")
240 assert cmd isa CmdFeatures
241 assert parser.error == null
242 assert cmd.results.as(not null).length == 4
243 end
244
245 # CmdLinearization
246
247 fun test_cmd_parser_lin is test do
248 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
249 var cmd = parser.parse("lin: test_prog::Character")
250 assert cmd isa CmdLinearization
251 assert parser.error == null
252 assert cmd.results != null
253 end
254
255 fun test_cmd_parser_lin_limit is test do
256 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
257 var cmd = parser.parse("lin: test_prog::Character | limit: 2")
258 assert cmd isa CmdLinearization
259 assert parser.error == null
260 assert cmd.results.as(not null).length == 2
261 end
262
263 # CmdCode
264
265 fun test_cmd_parser_code is test do
266 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
267 var cmd = parser.parse("code: test_prog::Character")
268 assert cmd isa CmdEntityCode
269 assert parser.error == null
270 assert cmd.node != null
271 end
272
273 # CmdModel
274
275 fun test_cmd_parser_mentities is test do
276 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
277 var cmd = parser.parse("list: modules")
278 assert cmd isa CmdModelEntities
279 assert parser.error == null
280 assert cmd.results != null
281 end
282
283 fun test_cmd_parser_results_mentities is test do
284 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
285 var cmd = parser.parse("random: modules")
286 assert cmd isa CmdRandomEntities
287 assert parser.error == null
288 assert cmd.results != null
289 end
290
291 # CmdGraph
292
293 fun test_cmd_parser_uml is test do
294 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
295 var cmd = parser.parse("uml: test_prog::Career")
296 assert cmd isa CmdUML
297 assert parser.error == null
298 assert cmd.uml != null
299 end
300
301 fun test_cmd_parser_inh_graph is test do
302 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
303 var cmd = parser.parse("graph: test_prog::Career")
304 assert cmd isa CmdInheritanceGraph
305 assert parser.error == null
306 assert cmd.graph != null
307 end
308
309 fun test_cmd_parser_inh_graph_opts is test do
310 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
311 var cmd = parser.parse("graph: test_prog::Career | cdepth: 2, pdepth: 5")
312 assert cmd isa CmdInheritanceGraph
313 assert parser.error == null
314 assert cmd.graph != null
315 assert cmd.cdepth == 2
316 assert cmd.pdepth == 5
317 end
318
319 # CmdUsage
320
321 fun test_cmd_parser_new is test do
322 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
323 var cmd = parser.parse("new: test_prog::Career")
324 assert cmd isa CmdNew
325 assert parser.error == null
326 assert cmd.results != null
327 end
328
329 fun test_cmd_parser_call is test do
330 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
331 var cmd = parser.parse("call: strength_bonus")
332 assert cmd isa CmdCall
333 assert parser.error == null
334 assert cmd.results != null
335 end
336
337 fun test_cmd_parser_return is test do
338 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
339 var cmd = parser.parse("return: test_prog::Career")
340 assert cmd isa CmdReturn
341 assert parser.error == null
342 assert cmd.results != null
343 end
344
345 fun test_cmd_parser_param is test do
346 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
347 var cmd = parser.parse("param: test_prog::Career")
348 assert cmd isa CmdParam
349 assert parser.error == null
350 assert cmd.results != null
351 end
352
353 # CmdCatalog
354
355 fun test_parser_catalog_search is test do
356 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
357 var cmd = parser.parse("search: Caracter")
358 assert cmd isa CmdSearch
359 assert parser.error == null
360 assert cmd.results != null
361 end
362
363 fun test_cmd_parser_catalog_packages is test do
364 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
365 var cmd = parser.parse("catalog:")
366 assert cmd isa CmdCatalogPackages
367 assert parser.error == null
368 assert cmd.results != null
369 end
370
371 fun test_cmd_parser_catalog_stats is test do
372 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
373 var cmd = parser.parse("stats:")
374 assert cmd isa CmdCatalogStats
375 assert parser.error == null
376 assert cmd.stats != null
377 end
378
379 fun test_cmd_parser_catalog_tags is test do
380 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
381 var cmd = parser.parse("tags:")
382 assert cmd isa CmdCatalogTags
383 assert parser.error == null
384 assert cmd.packages_count_by_tags != null
385 end
386
387 fun test_cmd_parser_catalog_tag is test do
388 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
389 var cmd = parser.parse("tag: test")
390 assert cmd isa CmdCatalogTag
391 assert parser.error == null
392 assert cmd.tag == "test"
393 assert cmd.results != null
394 end
395
396 fun test_cmd_parser_catalog_person is test do
397 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
398 var cmd = parser.parse("person: Alexandre Terrasa")
399 assert cmd isa CmdCatalogPerson
400 assert parser.error == null
401 assert cmd.person.as(not null).name == "Alexandre Terrasa"
402 end
403
404 fun test_cmd_parser_catalog_contributing is test do
405 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
406 var cmd = parser.parse("contrib: Alexandre Terrasa")
407 assert cmd isa CmdCatalogContributing
408 assert parser.error == null
409 assert cmd.person.as(not null).name == "Alexandre Terrasa"
410 assert cmd.results != null
411 end
412
413 fun test_cmd_parser_catalog_maintaining is test do
414 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
415 var cmd = parser.parse("maintain: Alexandre Terrasa")
416 assert cmd isa CmdCatalogMaintaining
417 assert parser.error == null
418 assert cmd.person.as(not null).name == "Alexandre Terrasa"
419 assert cmd.results != null
420 end
421
422 # CmdInit
423
424 fun test_cmd_parser_ini_desc is test do
425 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
426 var cmd = parser.parse("ini-desc: test_prog")
427 assert cmd isa CmdIniDescription
428 assert parser.error == null
429 assert cmd.desc.as(not null) == "Dummy program used for testing Nit tools"
430 end
431
432 fun test_cmd_parser_ini_git is test do
433 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
434 var cmd = parser.parse("ini-git: test_prog")
435 assert cmd isa CmdIniGitUrl
436 assert parser.error == null
437 assert cmd.url == "https://github.com/nitlang/nit.git"
438 end
439
440 fun test_cmd_parser_ini_clone is test do
441 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
442 var cmd = parser.parse("git-clone: test_prog")
443 assert cmd isa CmdIniCloneCommand
444 assert parser.error == null
445 assert cmd.url == "https://github.com/nitlang/nit.git"
446 assert cmd.command == "git clone https://github.com/nitlang/nit.git"
447 end
448
449 fun test_cmd_parser_ini_issues is test do
450 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
451 var cmd = parser.parse("ini-issues: test_prog")
452 assert cmd isa CmdIniIssuesUrl
453 assert parser.error == null
454 assert cmd.url == "https://github.com/nitlang/nit/issues"
455 end
456
457 fun test_cmd_parser_ini_maintainer is test do
458 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
459 var cmd = parser.parse("ini-maintainer: test_prog")
460 assert cmd isa CmdIniMaintainer
461 assert parser.error == null
462 assert cmd.maintainer == "John Doe <jdoe@example.com> (http://www.example.com/~jdoe), Spider-Man"
463 end
464
465 fun test_cmd_parser_ini_contributors is test do
466 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
467 var cmd = parser.parse("ini-contributors: test_prog")
468 assert cmd isa CmdIniContributors
469 assert parser.error == null
470 assert cmd.contributors == [
471 "Riri <riri@example.com>",
472 "Fifi (http://www.example.com/~fifi)",
473 "Loulou"]
474 end
475
476 fun test_cmd_parser_ini_license is test do
477 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
478 var cmd = parser.parse("ini-license: test_prog")
479 assert cmd isa CmdIniLicense
480 assert parser.error == null
481 assert cmd.license == "Apache-2.0"
482 end
483
484 fun test_cmd_parser_ini_license_file is test do
485 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
486 var cmd = parser.parse("license-file: test_prog")
487 assert cmd isa CmdLicenseFile
488 assert parser.error == null
489 var file = cmd.file
490 assert file != null
491 assert file.basename == "LICENSE.md"
492 end
493
494 fun test_cmd_parser_ini_license_content is test do
495 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
496 var cmd = parser.parse("license-content: test_prog")
497 assert cmd isa CmdLicenseFileContent
498 assert parser.error == null
499 var content = cmd.content
500 assert content != null
501 assert not content.is_empty
502 end
503
504 fun test_cmd_parser_ini_contrib_file is test do
505 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
506 var cmd = parser.parse("contrib-file: test_prog")
507 assert cmd isa CmdContribFile
508 assert parser.error == null
509 var file = cmd.file
510 assert file != null
511 assert file.basename == "CONTRIBUTING.md"
512 end
513
514 fun test_cmd_parser_ini_contrib_content is test do
515 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
516 var cmd = parser.parse("contrib-content: test_prog")
517 assert cmd isa CmdContribFileContent
518 assert parser.error == null
519 var content = cmd.content
520 assert content != null
521 assert not content.is_empty
522 end
523
524 # CmdMain
525
526 fun test_cmd_parser_mains is test do
527 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
528 var cmd = parser.parse("mains: test_prog")
529 assert cmd isa CmdMains
530 assert parser.error == null
531
532 var results = cmd.results
533 assert results != null
534 assert results.length == 1
535 assert results.first.full_name == "test_prog::test_prog"
536 end
537
538 fun test_cmd_parser_main_compile is test do
539 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
540 var cmd = parser.parse("main-compile: test_prog::test_prog")
541 assert cmd isa CmdMainCompile
542 assert parser.error == null
543
544 var command = cmd.command
545 assert command != null
546 assert command.has_prefix("nitc ")
547 assert command.has_suffix("test_prog.nit")
548 end
549
550 fun test_cmd_parser_testing is test do
551 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
552 var cmd = parser.parse("testing: test_prog")
553 assert cmd isa CmdTesting
554 assert parser.error == null
555
556 var command = cmd.command
557 assert command != null
558 assert command.has_prefix("nitunit ")
559 assert command.has_suffix("/tests")
560 end
561
562 fun test_cmd_man_synopsis is test do
563 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
564 var cmd = parser.parse("main-run: test_prog")
565 assert cmd isa CmdManSynopsis
566 assert parser.error == null
567
568 assert cmd.synopsis == "test_prog [*options*] ARGS..."
569 end
570
571 fun test_cmd_man_opions is test do
572 var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
573 var cmd = parser.parse("main-opts: test_prog")
574 assert cmd isa CmdManOptions
575 assert parser.error == null
576
577 var options = cmd.options
578 assert options != null
579 assert options["--opt1"] == "Option 1."
580 assert options["--opt2"] == "Option 2."
581 end
582 end