/
string_identifier_argument_spec.rb
153 lines (134 loc) · 5.63 KB
/
string_identifier_argument_spec.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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Performance::StringIdentifierArgument, :config do
described_class::RESTRICT_ON_SEND.each do |method|
if method == described_class::TWO_ARGUMENTS_METHOD
it 'registers an offense when using string two arguments for `alias_method`' do
expect_offense(<<~RUBY)
alias_method 'new', 'original'
^^^^^ Use `:new` instead of `'new'`.
^^^^^^^^^^ Use `:original` instead of `'original'`.
RUBY
expect_correction(<<~RUBY)
alias_method :new, :original
RUBY
end
it 'does not register an offense when using symbol two arguments for `alias_method`' do
expect_no_offenses(<<~RUBY)
alias_method :new, :original
RUBY
end
it 'does not register an offense when using symbol single argument for `alias_method`' do
expect_no_offenses(<<~RUBY)
alias_method :new
RUBY
end
else
# FIXME: `undefined method `[]' for nil` occurs Prism 0.24.0. It has been resolved in
# the development line. This will be resolved in Prism > 0.24.0 and higher releases.
it "registers an offense when using string argument for `#{method}` method", broken_on: :prism do
expect_offense(<<~RUBY, method: method)
#{method}('do_something')
_{method} ^^^^^^^^^^^^^^ Use `:do_something` instead of `'do_something'`.
RUBY
expect_correction(<<~RUBY)
#{method}(:do_something)
RUBY
end
# FIXME: `undefined method `[]' for nil` occurs Prism 0.24.0. It has been resolved in
# the development line. This will be resolved in Prism > 0.24.0 and higher releases.
it "does not register an offense when using symbol argument for `#{method}` method", broken_on: :prism do
expect_no_offenses(<<~RUBY)
#{method}(:do_something)
RUBY
end
if described_class::INTERPOLATION_IGNORE_METHODS.include?(method)
# FIXME: `undefined method `[]' for nil` occurs Prism 0.24.0. It has been resolved in
# the development line. This will be resolved in Prism > 0.24.0 and higher releases.
it 'does not register an offense when using string interpolation for `#{method}` method', broken_on: :prism do
# NOTE: These methods don't support `::` when passing a symbol. const_get('A::B') is valid
# but const_get(:'A::B') isn't. Since interpolated arguments may contain any content these
# cases are not detected as an offense to prevent false positives.
expect_no_offenses(<<~RUBY)
#{method}("\#{module_name}class_name")
RUBY
end
else
# FIXME: `undefined method `[]' for nil` occurs Prism 0.24.0. It has been resolved in
# the development line. This will be resolved in Prism > 0.24.0 and higher releases.
it 'registers an offense when using interpolated string argument', broken_on: :prism do
expect_offense(<<~RUBY, method: method)
#{method}("do_something_\#{var}")
_{method} ^^^^^^^^^^^^^^^^^^^^^ Use `:"do_something_\#{var}"` instead of `"do_something_\#{var}"`.
RUBY
expect_correction(<<~RUBY)
#{method}(:"do_something_\#{var}")
RUBY
end
end
end
end
described_class::MULTIPLE_ARGUMENTS_METHODS.each do |method|
it "registers an offense when using string multiple arguments for `#{method}` method" do
expect_offense(<<~RUBY, method: method)
#{method} 'one', 'two', 'three'
_{method} ^^^^^ Use `:one` instead of `'one'`.
_{method} ^^^^^ Use `:two` instead of `'two'`.
_{method} ^^^^^^^ Use `:three` instead of `'three'`.
RUBY
expect_correction(<<~RUBY)
#{method} :one, :two, :three
RUBY
end
it "does not register an offense when using symbol multiple arguments for `#{method}`" do
expect_no_offenses(<<~RUBY)
#{method} :one, :two, :three
RUBY
end
end
described_class::COMMAND_METHODS.each do |method|
it "does not register an offense when using string argument for `#{method}` method with receiver" do
expect_no_offenses(<<~RUBY)
obj.#{method}('do_something')
RUBY
end
end
it 'does not register an offense when no arguments' do
expect_no_offenses(<<~RUBY)
send
RUBY
end
it 'does not register an offense when using integer argument' do
expect_no_offenses(<<~RUBY)
send(42)
RUBY
end
it 'does not register an offense when using cbase class string argument' do
expect_no_offenses(<<~RUBY)
Object.const_defined?('::Foo')
RUBY
end
it 'does not register an offense when using namespaced class string argument' do
expect_no_offenses(<<~RUBY)
Object.const_defined?('Foo::Bar')
RUBY
end
it 'does not register an offense when using symbol argument for no identifier argument' do
expect_no_offenses(<<~RUBY)
foo('do_something')
RUBY
end
# e.g. Trunip https://github.com/jnicklas/turnip#calling-steps-from-other-steps
it 'does not register an offense when using string argument includes spaces' do
expect_no_offenses(<<~RUBY)
send(':foo is :bar', foo, bar)
RUBY
end
# NOTE: `attr` method is not included in this list as it can cause false positives in Nokogiri API.
# And `attr` may not be used because `Style/Attr` registers an offense.
# https://github.com/rubocop/rubocop-performance/issues/278
it 'does not register an offense when using string argument for `attr` method' do
expect_no_offenses(<<~RUBY)
attr('foo')
RUBY
end
end