-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathsearch_test.rb
127 lines (90 loc) · 3.17 KB
/
search_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# frozen_string_literal: true
require_relative "test_helper"
module SyntaxTree
class SearchTest < Minitest::Test
def test_search_invalid_syntax
assert_raises(Pattern::CompilationError) { search("", "<>") }
end
def test_search_invalid_constant
assert_raises(Pattern::CompilationError) { search("", "Foo") }
end
def test_search_invalid_nested_constant
assert_raises(Pattern::CompilationError) { search("", "Foo::Bar") }
end
def test_search_regexp_with_interpolation
assert_raises(Pattern::CompilationError) { search("", "/\#{foo}/") }
end
def test_search_string_with_interpolation
assert_raises(Pattern::CompilationError) { search("", '"#{foo}"') }
end
def test_search_symbol_with_interpolation
assert_raises(Pattern::CompilationError) { search("", ":\"\#{foo}\"") }
end
def test_search_invalid_node
assert_raises(Pattern::CompilationError) { search("", "Int[^foo]") }
end
def test_search_self
assert_raises(Pattern::CompilationError) { search("", "self") }
end
def test_search_array_pattern_no_constant
results = search("1 + 2", "[Int, Int]")
assert_equal 1, results.length
end
def test_search_array_pattern
results = search("1 + 2", "Binary[Int, Int]")
assert_equal 1, results.length
end
def test_search_binary_or
results = search("Foo + Bar + 1", "VarRef | Int")
assert_equal 3, results.length
assert_equal "1", results.min_by { |node| node.class.name }.value
end
def test_search_const
results = search("Foo + Bar + Baz", "VarRef")
assert_equal 3, results.length
assert_equal %w[Bar Baz Foo], results.map { |node| node.value.value }.sort
end
def test_search_object_const
results = search("1 + 2 + 3", "Int[value: String]")
assert_equal 3, results.length
end
def test_search_syntax_tree_const
results = search("Foo + Bar + Baz", "SyntaxTree::VarRef")
assert_equal 3, results.length
end
def test_search_hash_pattern_no_constant
results = search("Foo + Bar + Baz", "{ value: Const }")
assert_equal 3, results.length
end
def test_search_hash_pattern_string
results = search("Foo + Bar + Baz", "VarRef[value: Const[value: 'Foo']]")
assert_equal 1, results.length
assert_equal "Foo", results.first.value.value
end
def test_search_hash_pattern_regexp
results = search("Foo + Bar + Baz", "VarRef[value: Const[value: /^Ba/]]")
assert_equal 2, results.length
assert_equal %w[Bar Baz], results.map { |node| node.value.value }.sort
end
def test_search_string_empty
results = search("", "''")
assert_empty results
end
def test_search_symbol_empty
results = search("", ":''")
assert_empty results
end
def test_search_symbol_plain
results = search("1 + 2", "Binary[operator: :'+']")
assert_equal 1, results.length
end
def test_search_symbol
results = search("1 + 2", "Binary[operator: :+]")
assert_equal 1, results.length
end
private
def search(source, query)
SyntaxTree.search(source, query).to_a
end
end
end