forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
language.rb
156 lines (125 loc) · 3.57 KB
/
language.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# frozen_string_literal: true
module RuboCop
module RSpec
# RSpec public API methods that are commonly used in cops
module Language
RSPEC = '{(const {nil? cbase} :RSpec) nil?}'
module Patterns
BLOCK_PATTERN = <<-PATTERN
(block (send {(const {nil? cbase} :RSpec) nil?} $_ ...) ...)
PATTERN
SEND_PATTERN = <<-PATTERN
(send {(const {nil? cbase} :RSpec) nil?} $_ ...)
PATTERN
end
# Set of method selectors
class SelectorSet
def initialize(selectors)
@selectors = selectors
end
def ==(other)
selectors.eql?(other.selectors)
end
def +(other)
self.class.new(selectors + other.selectors)
end
def include?(selector)
selectors.include?(selector)
end
def block_pattern
"(block #{send_pattern} ...)"
end
def block_pass_pattern
"(send #{RSPEC} #{node_pattern_union} _ block_pass)"
end
def block_or_block_pass_pattern
"{#{block_pattern} #{block_pass_pattern}}"
end
def send_pattern
"(send #{RSPEC} #{node_pattern_union} ...)"
end
def node_pattern_union
"{#{node_pattern}}"
end
def node_pattern
selectors.map(&:inspect).join(' ')
end
protected
attr_reader :selectors
end
module ExampleGroups
GROUPS = SelectorSet.new(%i[describe context feature example_group])
SKIPPED = SelectorSet.new(%i[xdescribe xcontext xfeature])
FOCUSED = SelectorSet.new(%i[fdescribe fcontext ffeature])
ALL = GROUPS + SKIPPED + FOCUSED
end
module SharedGroups
EXAMPLES = SelectorSet.new(%i[shared_examples shared_examples_for])
CONTEXT = SelectorSet.new(%i[shared_context])
ALL = EXAMPLES + CONTEXT
end
module Includes
EXAMPLES = SelectorSet.new(
%i[
it_behaves_like
it_should_behave_like
include_examples
]
)
CONTEXT = SelectorSet.new(%i[include_context])
ALL = EXAMPLES + CONTEXT
end
module Examples
EXAMPLES = SelectorSet.new(%i[it specify example scenario its])
FOCUSED = SelectorSet.new(%i[fit fspecify fexample fscenario focus])
SKIPPED = SelectorSet.new(%i[xit xspecify xexample xscenario skip])
PENDING = SelectorSet.new(%i[pending])
ALL = EXAMPLES + FOCUSED + SKIPPED + PENDING
end
module Hooks
NAMES = %i[
prepend_before
before
append_before
around
prepend_after
after
append_after
].freeze
ALL = SelectorSet.new(NAMES)
module Scopes
ALL = SelectorSet.new(
%i[
each
example
context
all
suite
]
)
end
end
module Helpers
ALL = SelectorSet.new(%i[let let!])
end
module Subject
ALL = SelectorSet.new(%i[subject subject!])
end
module Expectations
ALL = SelectorSet.new(%i[expect is_expected expect_any_instance_of])
end
module Runners
ALL = SelectorSet.new(%i[to to_not not_to])
end
ALL =
ExampleGroups::ALL +
SharedGroups::ALL +
Examples::ALL +
Hooks::ALL +
Helpers::ALL +
Subject::ALL +
Expectations::ALL +
Runners::ALL
end
end
end