forked from rubocop/rubocop
/
binary_operator_parameter_name_spec.rb
108 lines (94 loc) · 2.54 KB
/
binary_operator_parameter_name_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Naming::BinaryOperatorParameterName do
subject(:cop) { described_class.new }
it 'registers an offense for `#+` when argument is not named other' do
expect_offense(<<~RUBY)
def +(foo); end
^^^ When defining the `+` operator, name its argument `other`.
RUBY
end
it 'registers an offense for `#eql?` when argument is not named other' do
expect_offense(<<~RUBY)
def eql?(foo); end
^^^ When defining the `eql?` operator, name its argument `other`.
RUBY
end
it 'registers an offense for `#equal?` when argument is not named other' do
expect_offense(<<~RUBY)
def equal?(foo); end
^^^ When defining the `equal?` operator, name its argument `other`.
RUBY
end
it 'works properly even if the argument not surrounded with braces' do
expect_offense(<<~RUBY)
def + another
^^^^^^^ When defining the `+` operator, name its argument `other`.
another
end
RUBY
end
it 'does not register an offense for arg named other' do
expect_no_offenses(<<~RUBY)
def +(other)
other
end
RUBY
end
it 'does not register an offense for arg named _other' do
expect_no_offenses(<<~RUBY)
def <=>(_other)
0
end
RUBY
end
it 'does not register an offense for []' do
expect_no_offenses(<<~RUBY)
def [](index)
other
end
RUBY
end
it 'does not register an offense for []=' do
expect_no_offenses(<<~RUBY)
def []=(index, value)
other
end
RUBY
end
it 'does not register an offense for <<' do
expect_no_offenses(<<~RUBY)
def <<(cop)
other
end
RUBY
end
it 'does not register an offense for ===' do
expect_no_offenses(<<~RUBY)
def ===(string)
string
end
RUBY
end
it 'does not register an offense for multibyte character method name' do
expect_no_offenses(<<~RUBY)
def do_something(string)
string
end
RUBY
end
it 'does not register an offense for non binary operators' do
expect_no_offenses(<<~RUBY)
def -@; end
# This + is not a unary operator. It can only be
# called with dot notation.
def +; end
def *(a, b); end # Quite strange, but legal ruby.
def `(cmd); end
RUBY
end
it 'does not register an offense for the match operator' do
expect_no_offenses(<<~RUBY)
def =~(regexp); end
RUBY
end
end