Skip to content

Commit 5933b76

Browse files
committed
Unit Tests
- Added unit tests for all declarations - Currently, classes, modules, methods, aliases, attributes, includes, extends are supported and have been tested
1 parent f71aded commit 5933b76

File tree

4 files changed

+277
-22
lines changed

4 files changed

+277
-22
lines changed

lib/rdoc/parser/rbs.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ class RBS < Parser
88
parse_files_matching(/\.rbs$/)
99

1010
def scan
11-
RBS::RDocPlugin::RBSParser.new(@top_level, @content).scan
11+
::RBS::RDocPlugin::Parser.new(@top_level, @content).scan
1212
end
1313
end
1414
end
1515
end
1616

1717
module RBS
1818
module RDocPlugin
19-
class RBSParser
19+
class Parser
20+
2021
attr_reader :top_level, :content
22+
2123
def initialize(top_level, content)
2224
@top_level = top_level
2325
@content = content

sig/rdoc/rbs.rbs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
class RDoc::Parser::RBS < RDoc::Parser
2-
type allowed_decls = RBS::AST::Declarations::Class | RBS::AST::Declarations::Module | RBS::AST::Declarations::Constant | RBS::AST::Members::MethodDefinition | RBS::AST::Members::AttrReader | RBS::AST::Members::AttrAccessor | RBS::AST::Members::AttrWriter | RBS::AST::Members::Include | RBS::AST::Members::Extend
1+
module RBS
2+
module RDocPlugin
3+
class Parser
4+
type allowed_decls = RBS::AST::Declarations::Class | RBS::AST::Declarations::Module | RBS::AST::Declarations::Constant | RBS::AST::Members::MethodDefinition | RBS::AST::Members::AttrReader | RBS::AST::Members::AttrAccessor | RBS::AST::Members::AttrWriter | RBS::AST::Members::Include | RBS::AST::Members::Extend
35

4-
def initialize: (RDoc::TopLevel top_level, String filename, String content, Hash[untyped, untyped] options, RDoc::Stats stats) -> void
6+
def initialize: (RDoc::TopLevel top_level, String content) -> void
57

6-
def scan: () -> RDoc::TopLevel
8+
def scan: () -> RDoc::TopLevel
79

8-
def parse_member: (decl: RBS::AST::Declarations::t | RBS::AST::Members::t, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
10+
def parse_member: (decl: RBS::AST::Declarations::t | RBS::AST::Members::t, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
911

10-
def parse_class_decl: (decl: RBS::AST::Declarations::Class, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
12+
def parse_class_decl: (decl: RBS::AST::Declarations::Class, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
1113

12-
def parse_module_decl: (decl: RBS::AST::Declarations::Module | RBS::AST::Declarations::Interface, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
14+
def parse_module_decl: (decl: RBS::AST::Declarations::Module | RBS::AST::Declarations::Interface, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
1315

14-
def parse_constant_decl: (decl: RBS::AST::Declarations::Constant, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
16+
def parse_constant_decl: (decl: RBS::AST::Declarations::Constant, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
1517

16-
def parse_method_decl: (decl: RBS::AST::Members::MethodDefinition, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
18+
def parse_method_decl: (decl: RBS::AST::Members::MethodDefinition, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
1719

18-
def parse_method_alias_decl: (decl: RBS::AST::Members::Alias, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
20+
def parse_method_alias_decl: (decl: RBS::AST::Members::Alias, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
1921

20-
def parse_attr_decl: (decl: RBS::AST::Members::AttrReader | RBS::AST::Members::AttrAccessor | RBS::AST::Members::AttrWriter, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
22+
def parse_attr_decl: (decl: RBS::AST::Members::AttrReader | RBS::AST::Members::AttrAccessor | RBS::AST::Members::AttrWriter, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
2123

22-
def parse_include_decl: (decl: RBS::AST::Members::Include, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
24+
def parse_include_decl: (decl: RBS::AST::Members::Include, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
2325

24-
def parse_extend_decl: (decl: RBS::AST::Members::Extend, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
26+
def parse_extend_decl: (decl: RBS::AST::Members::Extend, context: RDoc::Context, ?outer_name: RBS::TypeName?) -> void
2527

26-
private
28+
private
2729

28-
def construct_comment: (context: RDoc::Context, comment: String) -> RDoc::Comment
30+
def construct_comment: (context: RDoc::Context, comment: String) -> RDoc::Comment
2931

30-
def fully_qualified_name: (outer_name: RBS::TypeName?, decl: allowed_decls) -> RBS::TypeName
32+
def fully_qualified_name: (outer_name: RBS::TypeName?, decl: allowed_decls) -> RBS::TypeName
33+
end
34+
end
3135
end

stdlib/rdoc/0/rdoc.rbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ module RDoc
55
def initialize: (RDoc::TopLevel top_level, String filename, String content, Hash[untyped, untyped] options, RDoc::Stats stats) -> void
66

77
def scan: () -> RDoc::TopLevel
8+
9+
class RBS < Parser
10+
end
811
end
912

1013
class CodeObject

test/rbs/rdoc/rbs_parser_test.rb

Lines changed: 250 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def parser(content)
99
top_level = RDoc::TopLevel.new("a.rbs")
1010
top_level.store = RDoc::Store.new()
1111

12-
RBS::RDocPlugin::RBSParser.new(top_level, content)
12+
RBS::RDocPlugin::Parser.new(top_level, content)
1313
end
1414

1515
def teardown
@@ -103,7 +103,7 @@ def foo: (Integer) -> void
103103
top_level = parser.top_level
104104

105105
klass = top_level.find_class_or_module("A")
106-
method = klass.method_list.find {|m| m.name == "foo" }
106+
method = klass.method_list.find { |m| m.name == "foo" }
107107

108108
assert_instance_of RDoc::AnyMethod, method
109109
assert_equal "", method.comment
@@ -122,7 +122,7 @@ def foo: (Integer) { (String) -> void } -> void
122122
top_level = parser.top_level
123123

124124
klass = top_level.find_class_or_module("A")
125-
method = klass.method_list.find {|m| m.name == "foo" }
125+
method = klass.method_list.find { |m| m.name == "foo" }
126126

127127
assert_instance_of RDoc::AnyMethod, method
128128
assert_equal "", method.comment
@@ -141,10 +141,256 @@ def foo: [A] (Integer) { (String) -> A } -> A
141141
top_level = parser.top_level
142142

143143
klass = top_level.find_class_or_module("A")
144-
method = klass.method_list.find {|m| m.name == "foo" }
144+
method = klass.method_list.find { |m| m.name == "foo" }
145145

146146
assert_instance_of RDoc::AnyMethod, method
147147
assert_equal "", method.comment
148148
assert_equal "foo[A] (Integer) { (String) -> A } -> A", method.call_seq
149149
end
150+
151+
def test_instance_method_comment_and_tokens
152+
parser = parser(<<~RBS)
153+
class A
154+
# Added comment for foo
155+
def foo: [A] (Integer) { (String) -> A } -> A
156+
end
157+
RBS
158+
159+
parser.scan()
160+
161+
top_level = parser.top_level
162+
163+
klass = top_level.find_class_or_module("A")
164+
method = klass.method_list.find { |m| m.name == "foo" }
165+
166+
assert_instance_of RDoc::AnyMethod, method
167+
assert_equal "Added comment for foo", method.comment.text
168+
assert_equal "foo[A] (Integer) { (String) -> A } -> A", method.call_seq
169+
assert_equal "# File a.rbs, line(s) 3:3\n" + "def foo: [A] (Integer) { (String) -> A } -> A", method.tokens_to_s
170+
end
171+
172+
def test_constant_decl_1
173+
parser = parser(<<~RBS)
174+
class A
175+
CONSTANT: Integer
176+
end
177+
RBS
178+
179+
parser.scan()
180+
181+
top_level = parser.top_level
182+
183+
klass = top_level.find_class_or_module("A")
184+
constant = klass.constants.first
185+
186+
assert_instance_of RDoc::Constant, constant
187+
assert_equal "CONSTANT", constant.name
188+
assert_equal "Integer", constant.value
189+
assert_equal "", constant.comment
190+
end
191+
192+
def test_constant_decl_2
193+
parser = parser(<<~RBS)
194+
class A
195+
# Constant comment test
196+
CONSTANT: ("Literal" | "Union check")
197+
end
198+
RBS
199+
200+
parser.scan()
201+
202+
top_level = parser.top_level
203+
204+
klass = top_level.find_class_or_module("A")
205+
constant = klass.constants.first
206+
207+
assert_instance_of RDoc::Constant, constant
208+
assert_equal "CONSTANT", constant.name
209+
assert_equal "\"Literal\" | \"Union check\"", constant.value
210+
assert_equal "Constant comment test", constant.comment.text
211+
end
212+
213+
def test_method_alias_decl_1
214+
parser = parser(<<~RBS)
215+
class A
216+
def foo: () -> void
217+
alias bar foo
218+
end
219+
RBS
220+
221+
parser.scan()
222+
223+
top_level = parser.top_level
224+
225+
klass = top_level.find_class_or_module("A")
226+
method = klass.method_list.find { |m| m.name == "bar" }
227+
228+
assert_instance_of RDoc::AnyMethod, method
229+
assert_equal "bar", method.name
230+
assert_instance_of RDoc::AnyMethod, method.is_alias_for
231+
assert_equal "foo", method.is_alias_for.name
232+
assert_nil method.is_alias_for.is_alias_for
233+
end
234+
235+
def test_method_alias_decl_2
236+
parser = parser(<<~RBS)
237+
class A
238+
def foo: () -> void
239+
alias bar foo
240+
alias baz bar
241+
alias foo dam
242+
end
243+
RBS
244+
245+
parser.scan()
246+
247+
top_level = parser.top_level
248+
249+
klass = top_level.find_class_or_module("A")
250+
method = klass.method_list.find { |m| m.name == "baz" }
251+
252+
assert_instance_of RDoc::AnyMethod, method
253+
assert_equal "baz", method.name
254+
assert_instance_of RDoc::AnyMethod, method.is_alias_for
255+
assert_equal "bar", method.is_alias_for.name
256+
assert_instance_of RDoc::AnyMethod, method.is_alias_for.is_alias_for
257+
assert_equal "foo", method.is_alias_for.is_alias_for.name
258+
259+
assert_instance_of RDoc::Alias, klass.external_aliases.first
260+
assert_equal "foo", klass.external_aliases.first.name
261+
assert_equal "dam", klass.external_aliases.first.old_name
262+
end
263+
264+
def test_attr_decl_1
265+
parser = parser(<<~RBS)
266+
class A
267+
attr_reader foo: Integer
268+
attr_writer bar: Float
269+
attr_accessor dam: String
270+
end
271+
RBS
272+
273+
parser.scan()
274+
275+
top_level = parser.top_level
276+
277+
klass = top_level.find_class_or_module("A")
278+
attr_r = klass.attributes[0]
279+
attr_w = klass.attributes[1]
280+
attr_a = klass.attributes[2]
281+
282+
assert_instance_of RDoc::Attr, attr_r
283+
assert_equal "foo", attr_r.name
284+
assert_equal "R", attr_r.rw
285+
assert_equal "", attr_r.comment
286+
287+
assert_instance_of RDoc::Attr, attr_w
288+
assert_equal "bar", attr_w.name
289+
assert_equal "W", attr_w.rw
290+
assert_equal "", attr_w.comment
291+
292+
assert_instance_of RDoc::Attr, attr_a
293+
assert_equal "dam", attr_a.name
294+
assert_equal "RW", attr_a.rw
295+
assert_equal "", attr_a.comment
296+
end
297+
298+
def test_attr_decl_2
299+
parser = parser(<<~RBS)
300+
class A
301+
# Comment 1
302+
attr_reader foo: Integer
303+
# Comment 2
304+
attr_writer bar: Float
305+
# Comment 3
306+
attr_accessor dam: String
307+
end
308+
RBS
309+
310+
parser.scan()
311+
312+
top_level = parser.top_level
313+
314+
klass = top_level.find_class_or_module("A")
315+
attr_r = klass.attributes[0]
316+
attr_w = klass.attributes[1]
317+
attr_a = klass.attributes[2]
318+
319+
assert_instance_of RDoc::Attr, attr_r
320+
assert_equal "foo", attr_r.name
321+
assert_equal "R", attr_r.rw
322+
assert_equal "Comment 1", attr_r.comment.text
323+
324+
assert_instance_of RDoc::Attr, attr_w
325+
assert_equal "bar", attr_w.name
326+
assert_equal "W", attr_w.rw
327+
assert_equal "Comment 2", attr_w.comment.text
328+
329+
assert_instance_of RDoc::Attr, attr_a
330+
assert_equal "dam", attr_a.name
331+
assert_equal "RW", attr_a.rw
332+
assert_equal "Comment 3", attr_a.comment.text
333+
end
334+
335+
def test_include_decl_1
336+
parser = parser(<<~RBS)
337+
module D
338+
end
339+
class A
340+
module B
341+
end
342+
class C
343+
include B
344+
# Test comment
345+
include D
346+
end
347+
end
348+
RBS
349+
350+
parser.scan()
351+
352+
top_level = parser.top_level
353+
354+
klass = top_level.find_class_or_module("A::C")
355+
rdoc_include = klass.includes.first
356+
assert_instance_of RDoc::Include, rdoc_include
357+
assert_equal "A::B", rdoc_include.name
358+
assert_equal "", rdoc_include.comment
359+
360+
rdoc_include = klass.includes[1]
361+
assert_instance_of RDoc::Include, rdoc_include
362+
assert_equal "D", rdoc_include.name
363+
assert_equal "Test comment", rdoc_include.comment.text
364+
end
365+
366+
def test_extend_decl_1
367+
parser = parser(<<~RBS)
368+
module D
369+
end
370+
class A
371+
module B
372+
end
373+
class C
374+
extend B
375+
# Test comment
376+
extend D
377+
end
378+
end
379+
RBS
380+
381+
parser.scan()
382+
383+
top_level = parser.top_level
384+
385+
klass = top_level.find_class_or_module("A::C")
386+
rdoc_extend = klass.extends.first
387+
assert_instance_of RDoc::Extend, rdoc_extend
388+
assert_equal "A::B", rdoc_extend.name
389+
assert_equal "", rdoc_extend.comment
390+
391+
rdoc_extend = klass.extends[1]
392+
assert_instance_of RDoc::Extend, rdoc_extend
393+
assert_equal "D", rdoc_extend.name
394+
assert_equal "Test comment", rdoc_extend.comment.text
395+
end
150396
end

0 commit comments

Comments
 (0)