82f5e7c1f62a72ea4f84f19be66bf3bc980de255
[nit.git] / src / doc / test_doc_commands.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_doc_commands is test
16
17 import doc_commands
18
19 class TestDocCommandParser
20 test
21
22 var parser: DocCommandParser
23
24 fun init_parser is before do
25 parser = new DocCommandParser
26 end
27
28 fun test_empty_string is test do
29 var command = parser.parse("")
30 assert command == null
31 assert parser.errors.length == 1
32 assert parser.errors.first.to_s == "Error: empty command name (col: 0)"
33 end
34
35 fun test_bad_string is test do
36 var command = parser.parse(":")
37 assert command == null
38 assert parser.errors.length == 1
39 assert parser.errors.first.to_s == "Error: empty command name (col: 0)"
40 end
41
42 fun test_unknown_command is test do
43 var command = parser.parse("foo: foo")
44 assert command == null
45 assert parser.errors.length == 1
46 assert parser.errors.first.to_s == "Error: unknown command name (col: 0)"
47 end
48
49 fun test_unallowed_command is test do
50 parser.allowed_commands.clear
51 var command = parser.parse("comment: core::Array")
52 assert command == null
53 assert parser.errors.length == 1
54 assert parser.errors.first.to_s == "Error: unknown command name (col: 0)"
55 end
56
57 fun test_no_arg is test do
58 var command = parser.parse("doc:")
59 assert command == null
60 assert parser.errors.length == 1
61 print parser.errors.first
62 assert parser.errors.first.to_s == "Error: empty command arg (col: 4)"
63 end
64
65 fun test_no_opts is test do
66 var command = parser.parse("doc: core::Array")
67 assert command isa CommentCommand
68 assert command.name == "doc"
69 assert command.arg == "core::Array"
70 assert parser.errors.is_empty
71 end
72
73 fun test_opts_empty is test do
74 var command = parser.parse("doc: core::Array | ")
75 assert command isa CommentCommand
76 assert command.name == "doc"
77 assert command.arg == "core::Array"
78 assert parser.errors.is_empty
79 end
80
81 fun test_1_opt is test do
82 var command = parser.parse("doc: core::Array | opt1: val1 ")
83 assert command isa CommentCommand
84 assert command.name == "doc"
85 assert command.arg == "core::Array"
86 assert command.opts.length == 1
87 assert command.opts["opt1"] == "val1"
88 assert parser.errors.is_empty
89 end
90
91 fun test_2_opts is test do
92 var command = parser.parse("doc: core::Array | opt1: val1 , opt2: val2, ")
93 assert command isa CommentCommand
94 assert command.name == "doc"
95 assert command.arg == "core::Array"
96 assert command.opts.length == 2
97 assert command.opts["opt1"] == "val1"
98 assert command.opts["opt2"] == "val2"
99 assert parser.errors.is_empty
100 end
101
102 fun test_empty_opt_name is test do
103 var command = parser.parse("doc: core::Array | opt1: val1 , :")
104 assert command isa CommentCommand
105 assert command.name == "doc"
106 assert command.arg == "core::Array"
107 assert command.opts.length == 1
108 assert command.opts["opt1"] == "val1"
109 assert parser.errors.is_empty
110 end
111
112 fun test_empty_opt_value is test do
113 var command = parser.parse("doc: core::Array | opt1: , opt2: val2, ")
114 assert command isa CommentCommand
115 assert command.name == "doc"
116 assert command.arg == "core::Array"
117 assert command.opts.length == 2
118 assert command.opts["opt1"] == ""
119 assert command.opts["opt2"] == "val2"
120 assert parser.errors.is_empty
121 end
122
123 fun test_empty_opt_value2 is test do
124 var command = parser.parse("doc: core::Array | opt1")
125 assert command isa CommentCommand
126 assert command.name == "doc"
127 assert command.arg == "core::Array"
128 assert command.opts.length == 1
129 assert command.opts["opt1"] == ""
130 assert parser.errors.is_empty
131 end
132
133 fun test_empty_opt_value3 is test do
134 var command = parser.parse("doc: core::Array | opt1, opt2: val2")
135 assert command isa CommentCommand
136 assert command.name == "doc"
137 assert command.arg == "core::Array"
138 assert command.opts.length == 2
139 assert command.opts["opt1"] == ""
140 assert command.opts["opt2"] == "val2"
141 assert parser.errors.is_empty
142 end
143 end