-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathvisitor_test.rb
73 lines (60 loc) · 1.71 KB
/
visitor_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true
require_relative "test_helper"
module SyntaxTree
class VisitorTest < Minitest::Test
def test_visit_tree
parsed_tree = SyntaxTree.parse(<<~RUBY)
class Foo
def foo; end
class Bar
def bar; end
end
end
def baz; end
RUBY
visitor = DummyVisitor.new
visitor.visit(parsed_tree)
assert_equal(%w[Foo foo Bar bar baz], visitor.visited_nodes)
end
class DummyVisitor < Visitor
attr_reader :visited_nodes
def initialize
super
@visited_nodes = []
end
visit_methods do
def visit_class(node)
@visited_nodes << node.constant.constant.value
super
end
def visit_def(node)
@visited_nodes << node.name.value
end
end
end
if defined?(DidYouMean.correct_error)
def test_visit_method_correction
error = assert_raises { Visitor.visit_method(:visit_binar) }
message =
if Exception.method_defined?(:detailed_message)
error.detailed_message
else
error.message
end
assert_match(/visit_binary/, message)
end
end
class VisitMethodsTestVisitor < BasicVisitor
end
def test_visit_methods
VisitMethodsTestVisitor.visit_methods do
assert_raises(BasicVisitor::VisitMethodError) do
# In reality, this would be a method defined using the def keyword,
# but we're using method_added here to trigger the checker so that we
# aren't defining methods dynamically in the test suite.
VisitMethodsTestVisitor.method_added(:visit_foo)
end
end
end
end
end