rubocop 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rubocop.yml +40 -0
- data/.travis.yml +1 -0
- data/Gemfile +7 -10
- data/Gemfile.lock +21 -19
- data/README.md +62 -5
- data/VERSION +1 -1
- data/bin/rubocop +1 -1
- data/lib/rubocop/cli.rb +35 -15
- data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -0
- data/lib/rubocop/cop/collection_methods.rb +29 -0
- data/lib/rubocop/cop/cop.rb +1 -1
- data/lib/rubocop/cop/def_parentheses.rb +4 -1
- data/lib/rubocop/cop/encoding.rb +6 -4
- data/lib/rubocop/cop/end_of_line.rb +1 -3
- data/lib/rubocop/cop/favor_modifier.rb +82 -0
- data/lib/rubocop/cop/favor_unless_over_negated_if.rb +47 -0
- data/lib/rubocop/cop/grammar.rb +3 -2
- data/lib/rubocop/cop/indentation.rb +1 -1
- data/lib/rubocop/cop/line_length.rb +7 -3
- data/lib/rubocop/cop/method_and_variable_snake_case.rb +31 -0
- data/lib/rubocop/cop/new_lambda_literal.rb +17 -0
- data/lib/rubocop/cop/parentheses_around_condition.rb +24 -0
- data/lib/rubocop/cop/surrounding_space.rb +48 -47
- data/lib/rubocop/cop/syntax.rb +19 -0
- data/lib/rubocop/cop/tab.rb +1 -3
- data/lib/rubocop/report/plain_text.rb +1 -1
- data/lib/rubocop/report/report.rb +5 -2
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +14 -0
- data/rubocop.gemspec +38 -22
- data/spec/rubocop/cli_spec.rb +120 -40
- data/spec/rubocop/cops/align_parameters_spec.rb +29 -29
- data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
- data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +34 -0
- data/spec/rubocop/cops/collection_methods_spec.rb +39 -0
- data/spec/rubocop/cops/cop_spec.rb +5 -5
- data/spec/rubocop/cops/def_with_parentheses_spec.rb +5 -5
- data/spec/rubocop/cops/def_without_parentheses_spec.rb +4 -4
- data/spec/rubocop/cops/empty_lines_spec.rb +8 -8
- data/spec/rubocop/cops/encoding_spec.rb +17 -11
- data/spec/rubocop/cops/end_of_line_spec.rb +5 -5
- data/spec/rubocop/cops/favor_modifier_spec.rb +112 -0
- data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +62 -0
- data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +45 -0
- data/spec/rubocop/cops/grammar_spec.rb +14 -9
- data/spec/rubocop/cops/hash_syntax_spec.rb +10 -10
- data/spec/rubocop/cops/if_with_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/indentation_spec.rb +7 -7
- data/spec/rubocop/cops/line_length_spec.rb +4 -4
- data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +47 -0
- data/spec/rubocop/cops/multiline_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/multiline_if_then_spec.rb +7 -7
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +23 -0
- data/spec/rubocop/cops/numeric_literals_spec.rb +10 -10
- data/spec/rubocop/cops/offence_spec.rb +4 -4
- data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
- data/spec/rubocop/cops/parameter_lists_spec.rb +4 -4
- data/spec/rubocop/cops/parentheses_around_condition_spec.rb +64 -0
- data/spec/rubocop/cops/single_line_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/space_after_colon_spec.rb +5 -5
- data/spec/rubocop/cops/space_after_comma_spec.rb +3 -3
- data/spec/rubocop/cops/space_after_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/space_around_braces_spec.rb +6 -6
- data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +22 -0
- data/spec/rubocop/cops/space_around_operators_spec.rb +50 -25
- data/spec/rubocop/cops/space_inside_brackets_spec.rb +8 -8
- data/spec/rubocop/cops/space_inside_parens_spec.rb +4 -4
- data/spec/rubocop/cops/string_literals_spec.rb +5 -5
- data/spec/rubocop/cops/syntax_spec.rb +18 -0
- data/spec/rubocop/cops/tab_spec.rb +3 -3
- data/spec/rubocop/cops/ternary_operator_spec.rb +8 -8
- data/spec/rubocop/cops/trailing_whitespace_spec.rb +4 -4
- data/spec/rubocop/cops/unless_else_spec.rb +4 -4
- data/spec/rubocop/cops/when_then_spec.rb +5 -5
- data/spec/rubocop/reports/emacs_style_spec.rb +3 -3
- data/spec/rubocop/reports/report_spec.rb +5 -5
- data/spec/spec_helper.rb +6 -0
- metadata +57 -28
- data/.rbenv-version +0 -1
- data/.rvmrc +0 -1
- data/features/rubocop.feature +0 -9
- data/features/step_definitions/rubocop_steps.rb +0 -1
- data/features/support/env.rb +0 -15
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe MultilineIfThen do
|
|
8
|
-
let
|
|
8
|
+
let(:mit) { MultilineIfThen.new }
|
|
9
9
|
|
|
10
10
|
# if
|
|
11
11
|
|
|
@@ -18,13 +18,13 @@ module Rubocop
|
|
|
18
18
|
'end',
|
|
19
19
|
'if cond then # bad',
|
|
20
20
|
'end'])
|
|
21
|
-
mit.offences.map(&:line_number).
|
|
21
|
+
expect(mit.offences.map(&:line_number)).to eq([1, 3, 5, 7])
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
it 'accepts multiline if without then' do
|
|
25
25
|
inspect_source(mit, '', ['if cond',
|
|
26
26
|
'end'])
|
|
27
|
-
mit.offences.map(&:message).
|
|
27
|
+
expect(mit.offences.map(&:message)).to be_empty
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
it 'accepts table style if/then/elsif/ends' do
|
|
@@ -34,7 +34,7 @@ module Rubocop
|
|
|
34
34
|
'elsif @io == $stderr then str << "$stderr"',
|
|
35
35
|
'else str << @io.class.to_s',
|
|
36
36
|
'end'])
|
|
37
|
-
mit.offences.map(&:message).
|
|
37
|
+
expect(mit.offences.map(&:message)).to be_empty
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
# unless
|
|
@@ -42,14 +42,14 @@ module Rubocop
|
|
|
42
42
|
it 'registers an offence for then in multiline unless' do
|
|
43
43
|
inspect_source(mit, '', ['unless cond then',
|
|
44
44
|
'end'])
|
|
45
|
-
mit.offences.map(&:message).
|
|
46
|
-
['Never use then for multi-line if/unless.']
|
|
45
|
+
expect(mit.offences.map(&:message)).to eq(
|
|
46
|
+
['Never use then for multi-line if/unless.'])
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
it 'accepts multiline unless without then' do
|
|
50
50
|
inspect_source(mit, '', ['unless cond',
|
|
51
51
|
'end'])
|
|
52
|
-
mit.offences.map(&:message).
|
|
52
|
+
expect(mit.offences.map(&:message)).to be_empty
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
55
|
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe NewLambdaLiteral do
|
|
8
|
+
let(:lambda_literal) { NewLambdaLiteral.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for an old lambda call' do
|
|
11
|
+
inspect_source(lambda_literal, 'file.rb', ['f = lambda { |x| x }'])
|
|
12
|
+
expect(lambda_literal.offences.map(&:message)).to eq(
|
|
13
|
+
['The new lambda literal syntax is preferred in Ruby 1.9.'])
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'accepts the new lambda literal' do
|
|
17
|
+
inspect_source(lambda_literal, 'file.rb', ['lambda = ->(x) { x }',
|
|
18
|
+
'lambda.(1)'])
|
|
19
|
+
expect(lambda_literal.offences.map(&:message)).to be_empty
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -5,44 +5,44 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe NumericLiterals do
|
|
8
|
-
let
|
|
8
|
+
let(:num) { NumericLiterals.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for a long integer without underscores' do
|
|
11
11
|
inspect_source(num, 'file.rb', ['a = 123456'])
|
|
12
|
-
num.offences.map(&:message).
|
|
12
|
+
expect(num.offences.map(&:message)).to eq(
|
|
13
13
|
['Add underscores to large numeric literals to improve their ' +
|
|
14
|
-
'readability.']
|
|
14
|
+
'readability.'])
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it 'registers an offence for an integer with not enough underscores' do
|
|
18
18
|
inspect_source(num, 'file.rb', ['a = 123456_789000'])
|
|
19
|
-
num.offences.map(&:message).
|
|
19
|
+
expect(num.offences.map(&:message)).to eq(
|
|
20
20
|
['Add underscores to large numeric literals to improve their ' +
|
|
21
|
-
'readability.']
|
|
21
|
+
'readability.'])
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
it 'registers an offence for a long float without underscores' do
|
|
25
25
|
inspect_source(num, 'file.rb', ['a = 1.234567'])
|
|
26
|
-
num.offences.map(&:message).
|
|
26
|
+
expect(num.offences.map(&:message)).to eq(
|
|
27
27
|
['Add underscores to large numeric literals to improve their ' +
|
|
28
|
-
'readability.']
|
|
28
|
+
'readability.'])
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it 'accepts long numbers with underscore' do
|
|
32
32
|
inspect_source(num, 'file.rb', ['a = 123_456',
|
|
33
33
|
'b = 1.234_56'])
|
|
34
|
-
num.offences.map(&:message).
|
|
34
|
+
expect(num.offences.map(&:message)).to be_empty
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
it 'accepts a short integer without underscore' do
|
|
38
38
|
inspect_source(num, 'file.rb', ['a = 123'])
|
|
39
|
-
num.offences.map(&:message).
|
|
39
|
+
expect(num.offences.map(&:message)).to be_empty
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
it 'accepts short numbers without underscore' do
|
|
43
43
|
inspect_source(num, 'file.rb', ['a = 123',
|
|
44
44
|
'b = 123.456'])
|
|
45
|
-
num.offences.map(&:message).
|
|
45
|
+
expect(num.offences.map(&:message)).to be_empty
|
|
46
46
|
end
|
|
47
47
|
end
|
|
48
48
|
end
|
|
@@ -8,15 +8,15 @@ module Rubocop
|
|
|
8
8
|
it 'has a few required attributes' do
|
|
9
9
|
offence = Offence.new(:convention, 1, 'message')
|
|
10
10
|
|
|
11
|
-
offence.severity.
|
|
12
|
-
offence.line_number.
|
|
13
|
-
offence.message.
|
|
11
|
+
expect(offence.severity).to eq(:convention)
|
|
12
|
+
expect(offence.line_number).to eq(1)
|
|
13
|
+
expect(offence.message).to eq('message')
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it 'overrides #to_s' do
|
|
17
17
|
offence = Offence.new(:convention, 1, 'message')
|
|
18
18
|
|
|
19
|
-
offence.to_s.
|
|
19
|
+
expect(offence.to_s).to eq('C: 1: message')
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
end
|
|
@@ -5,12 +5,11 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe OneLineConditional do
|
|
8
|
-
let
|
|
8
|
+
let(:olc) { OneLineConditional.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for one line if/then/end' do
|
|
11
11
|
inspect_source(olc, '', ['if cond then run else dont end'])
|
|
12
|
-
olc.offences.map(&:message).
|
|
13
|
-
['Favor the ternary operator (?:) over if/then/else/end constructs.']
|
|
12
|
+
expect(olc.offences.map(&:message)).to eq([olc.error_message])
|
|
14
13
|
end
|
|
15
14
|
end
|
|
16
15
|
end
|
|
@@ -5,19 +5,19 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe ParameterLists do
|
|
8
|
-
let
|
|
8
|
+
let(:list) { ParameterLists.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for a method def with 5 parameters' do
|
|
11
11
|
inspect_source(list, 'file.rb', ['def meth(a, b, c, d, e)',
|
|
12
12
|
'end'])
|
|
13
|
-
list.offences.map(&:message).
|
|
14
|
-
['Avoid parameter lists longer than four parameters.']
|
|
13
|
+
expect(list.offences.map(&:message)).to eq(
|
|
14
|
+
['Avoid parameter lists longer than four parameters.'])
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it 'accepts a method def with 4 parameters' do
|
|
18
18
|
inspect_source(list, 'file.rb', ['def meth(a, b, c, d)',
|
|
19
19
|
'end'])
|
|
20
|
-
list.offences.map(&:message).
|
|
20
|
+
expect(list.offences.map(&:message)).to be_empty
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe ParenthesesAroundCondition do
|
|
8
|
+
let(:pac) { ParenthesesAroundCondition.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for parentheses around condition' do
|
|
11
|
+
inspect_source(pac, 'file.rb', ['if (x > 10)',
|
|
12
|
+
'elsif (x < 3)',
|
|
13
|
+
'end',
|
|
14
|
+
'unless (x > 10)',
|
|
15
|
+
'end',
|
|
16
|
+
'while (x > 10)',
|
|
17
|
+
'end',
|
|
18
|
+
'until (x > 10)',
|
|
19
|
+
'end',
|
|
20
|
+
'x += 1 if (x < 10)',
|
|
21
|
+
'x += 1 unless (x < 10)',
|
|
22
|
+
'x += 1 while (x < 10)',
|
|
23
|
+
'x += 1 until (x < 10)',
|
|
24
|
+
])
|
|
25
|
+
expect(pac.offences.map(&:message)).to eq(
|
|
26
|
+
["Don't use parentheses around the condition of an if/unless/" +
|
|
27
|
+
'while/until, unless the condition contains an assignment.'] * 9)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'accepts condition without parentheses' do
|
|
31
|
+
inspect_source(pac, 'file.rb', ['if x > 10',
|
|
32
|
+
'end',
|
|
33
|
+
'unless x > 10',
|
|
34
|
+
'end',
|
|
35
|
+
'while x > 10',
|
|
36
|
+
'end',
|
|
37
|
+
'until x > 10',
|
|
38
|
+
'end',
|
|
39
|
+
'x += 1 if x < 10',
|
|
40
|
+
'x += 1 unless x < 10',
|
|
41
|
+
'x += 1 while x < 10',
|
|
42
|
+
'x += 1 until x < 10',
|
|
43
|
+
])
|
|
44
|
+
expect(pac.offences.map(&:message)).to be_empty
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Parentheses are sometimes used to help the editor make nice
|
|
48
|
+
# indentation of conditions spanning several lines.
|
|
49
|
+
it 'accepts parentheses around multiline conditions' do
|
|
50
|
+
inspect_source(pac, 'file.rb', ['if (@lex_state != EXPR_BEG &&',
|
|
51
|
+
' @lex_state != EXPR_FNAME &&',
|
|
52
|
+
' trans[1])',
|
|
53
|
+
'end'])
|
|
54
|
+
expect(pac.offences.map(&:message)).to be_empty
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'accepts parentheses around assignment' do
|
|
58
|
+
inspect_source(pac, 'file.rb', ['if (x = self.next_value)',
|
|
59
|
+
'end'])
|
|
60
|
+
expect(pac.offences.map(&:message)).to be_empty
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -5,17 +5,17 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe SingleLineBlocks do
|
|
8
|
-
let
|
|
8
|
+
let(:blocks) { SingleLineBlocks.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for a single line block with do-end' do
|
|
11
11
|
inspect_source(blocks, '', ['each do |x| end'])
|
|
12
|
-
blocks.offences.map(&:message).
|
|
13
|
-
['Prefer {...} over do...end for single-line blocks.']
|
|
12
|
+
expect(blocks.offences.map(&:message)).to eq(
|
|
13
|
+
['Prefer {...} over do...end for single-line blocks.'])
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it 'accepts a single line block with braces' do
|
|
17
17
|
inspect_source(blocks, '', ['each { |x| }'])
|
|
18
|
-
blocks.offences.map(&:message).
|
|
18
|
+
expect(blocks.offences.map(&:message)).to be_empty
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
end
|
|
@@ -5,22 +5,22 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe SpaceAfterColon do
|
|
8
|
-
let
|
|
8
|
+
let(:space) { SpaceAfterColon.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for colon without space after it' do
|
|
11
11
|
inspect_source(space, 'file.rb', ['x = w ? {a:3}:4'])
|
|
12
|
-
space.offences.map(&:message).
|
|
13
|
-
['Space missing after colon.'] * 2
|
|
12
|
+
expect(space.offences.map(&:message)).to eq(
|
|
13
|
+
['Space missing after colon.'] * 2)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it 'allows the colons in symbols' do
|
|
17
17
|
inspect_source(space, 'file.rb', ['x = :a'])
|
|
18
|
-
space.offences.map(&:message).
|
|
18
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
it 'allows colons in strings' do
|
|
22
22
|
inspect_source(space, 'file.rb', ["str << ':'"])
|
|
23
|
-
space.offences.map(&:message).
|
|
23
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
26
|
end
|
|
@@ -5,12 +5,12 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe SpaceAfterComma do
|
|
8
|
-
let
|
|
8
|
+
let(:space) { SpaceAfterComma.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for block argument commas' do
|
|
11
11
|
inspect_source(space, 'file.rb', ['each { |s,t| }'])
|
|
12
|
-
space.offences.map(&:message).
|
|
13
|
-
['Space missing after comma.']
|
|
12
|
+
expect(space.offences.map(&:message)).to eq(
|
|
13
|
+
['Space missing after comma.'])
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
end
|
|
@@ -5,12 +5,12 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe SpaceAfterSemicolon do
|
|
8
|
-
let
|
|
8
|
+
let(:space) { SpaceAfterSemicolon.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for semicolon without space after it' do
|
|
11
11
|
inspect_source(space, 'file.rb', ['x = 1;y = 2'])
|
|
12
|
-
space.offences.map(&:message).
|
|
13
|
-
['Space missing after semicolon.']
|
|
12
|
+
expect(space.offences.map(&:message)).to eq(
|
|
13
|
+
['Space missing after semicolon.'])
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
end
|
|
@@ -5,18 +5,18 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe SpaceAroundBraces do
|
|
8
|
-
let
|
|
8
|
+
let(:space) { SpaceAroundBraces.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for left brace without spaces' do
|
|
11
11
|
inspect_source(space, 'file.rb', ['each{ puts }'])
|
|
12
|
-
space.offences.map(&:message).
|
|
13
|
-
["Surrounding space missing for '{'."]
|
|
12
|
+
expect(space.offences.map(&:message)).to eq(
|
|
13
|
+
["Surrounding space missing for '{'."])
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it 'registers an offence for right brace without inner space' do
|
|
17
17
|
inspect_source(space, 'file.rb', ['each { puts}'])
|
|
18
|
-
space.offences.map(&:message).
|
|
19
|
-
["Space missing to the left of '}'."]
|
|
18
|
+
expect(space.offences.map(&:message)).to eq(
|
|
19
|
+
["Space missing to the left of '}'."])
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it 'accepts an empty hash literal with no space inside' do
|
|
@@ -25,7 +25,7 @@ module Rubocop
|
|
|
25
25
|
'end',
|
|
26
26
|
'@views = {}',
|
|
27
27
|
''])
|
|
28
|
-
space.offences.map(&:message).
|
|
28
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe SpaceAroundEqualsInParameterDefault do
|
|
8
|
+
let(:space) { SpaceAroundEqualsInParameterDefault.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for default value assignment without space' do
|
|
11
|
+
inspect_source(space, 'file.rb', ['def f(x, y=0, z=1)', 'end'])
|
|
12
|
+
expect(space.offences.map(&:message)).to eq(
|
|
13
|
+
['Surrounding space missing in default value assignment.'] * 2)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'accepts default value assignment with space' do
|
|
17
|
+
inspect_source(space, 'file.rb', ['def f(x, y = 0, z = 1)', 'end'])
|
|
18
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -5,62 +5,87 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe SpaceAroundOperators do
|
|
8
|
-
let
|
|
8
|
+
let(:space) { SpaceAroundOperators.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for assignment without space on both sides' do
|
|
11
11
|
inspect_source(space, 'file.rb', ['x=0', 'y= 0', 'z =0'])
|
|
12
|
-
space.offences.map(&:message).
|
|
13
|
-
["Surrounding space missing for operator '='."] * 3
|
|
12
|
+
expect(space.offences.map(&:message)).to eq(
|
|
13
|
+
["Surrounding space missing for operator '='."] * 3)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'registers an offence in presence of modifier if statement' do
|
|
17
|
+
check_modifier('if')
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'registers an offence in presence of modifier unless statement' do
|
|
21
|
+
check_modifier('unless')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'registers an offence in presence of modifier while statement' do
|
|
25
|
+
check_modifier('unless')
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'registers an offence in presence of modifier until statement' do
|
|
29
|
+
check_modifier('unless')
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def check_modifier(keyword)
|
|
33
|
+
src = ["a=1 #{keyword} condition",
|
|
34
|
+
'c=2']
|
|
35
|
+
inspect_source(space, 'file.rb', src)
|
|
36
|
+
expect(space.offences.map(&:line_number)).to eq([1, 2])
|
|
37
|
+
expect(space.offences.map(&:message)).to eq(
|
|
38
|
+
["Surrounding space missing for operator '='."] * 2)
|
|
14
39
|
end
|
|
15
40
|
|
|
16
41
|
it 'registers an offence for binary operators that could be unary' do
|
|
17
42
|
inspect_source(space, 'file.rb', ['a-3', 'x&0xff', 'z+0'])
|
|
18
|
-
space.offences.map(&:message).
|
|
43
|
+
expect(space.offences.map(&:message)).to eq(
|
|
19
44
|
["Surrounding space missing for operator '-'.",
|
|
20
45
|
"Surrounding space missing for operator '&'.",
|
|
21
|
-
"Surrounding space missing for operator '+'."]
|
|
46
|
+
"Surrounding space missing for operator '+'."])
|
|
22
47
|
end
|
|
23
48
|
|
|
24
49
|
it 'registers an offence for arguments to a method' do
|
|
25
50
|
inspect_source(space, 'file.rb', ['puts 1+2'])
|
|
26
|
-
space.offences.map(&:message).
|
|
27
|
-
["Surrounding space missing for operator '+'."]
|
|
51
|
+
expect(space.offences.map(&:message)).to eq(
|
|
52
|
+
["Surrounding space missing for operator '+'."])
|
|
28
53
|
end
|
|
29
54
|
|
|
30
55
|
it 'accepts operator symbols' do
|
|
31
56
|
inspect_source(space, 'file.rb', ['func(:-)'])
|
|
32
|
-
space.offences.map(&:message).
|
|
57
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
33
58
|
end
|
|
34
59
|
|
|
35
60
|
it 'accepts ranges' do
|
|
36
61
|
inspect_source(space, 'file.rb', ['a, b = (1..2), (1...3)'])
|
|
37
|
-
space.offences.map(&:message).
|
|
62
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
38
63
|
end
|
|
39
64
|
|
|
40
65
|
it 'accepts scope operator' do
|
|
41
66
|
source = ['@io.class == Zlib::GzipWriter']
|
|
42
67
|
inspect_source(space, 'file.rb', source)
|
|
43
|
-
space.offences.map(&:message).
|
|
68
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
44
69
|
end
|
|
45
70
|
|
|
46
71
|
it 'accepts ::Kernel::raise' do
|
|
47
72
|
source = ['::Kernel::raise IllegalBlockError.new']
|
|
48
73
|
inspect_source(space, 'file.rb', source)
|
|
49
|
-
space.offences.map(&:message).
|
|
74
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
50
75
|
end
|
|
51
76
|
|
|
52
77
|
it 'accepts exclamation point negation' do
|
|
53
78
|
inspect_source(space, 'file.rb', ['x = !a&&!b'])
|
|
54
|
-
space.offences.map(&:message).
|
|
55
|
-
["Surrounding space missing for operator '&&'."]
|
|
79
|
+
expect(space.offences.map(&:message)).to eq(
|
|
80
|
+
["Surrounding space missing for operator '&&'."])
|
|
56
81
|
end
|
|
57
82
|
|
|
58
83
|
it 'accepts exclamation point definition' do
|
|
59
84
|
inspect_source(space, 'file.rb', [' def !',
|
|
60
85
|
' !__getobj__',
|
|
61
86
|
' end'])
|
|
62
|
-
space.offences.
|
|
63
|
-
space.offences.map(&:message).
|
|
87
|
+
expect(space.offences).to be_empty
|
|
88
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
64
89
|
end
|
|
65
90
|
|
|
66
91
|
it 'accepts a unary' do
|
|
@@ -70,52 +95,52 @@ module Rubocop
|
|
|
70
95
|
' *labels, &blk)',
|
|
71
96
|
' end',
|
|
72
97
|
''])
|
|
73
|
-
space.offences.map(&:message).
|
|
98
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
74
99
|
end
|
|
75
100
|
|
|
76
101
|
it 'accepts splat operator' do
|
|
77
102
|
inspect_source(space, 'file.rb', ['return *list if options'])
|
|
78
|
-
space.offences.map(&:message).
|
|
103
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
79
104
|
end
|
|
80
105
|
|
|
81
106
|
it 'accepts def of operator' do
|
|
82
107
|
inspect_source(space, 'file.rb', ['def +(other); end'])
|
|
83
|
-
space.offences.map(&:message).
|
|
108
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
84
109
|
end
|
|
85
110
|
|
|
86
111
|
it 'accepts an assignment with spaces' do
|
|
87
112
|
inspect_source(space, 'file.rb', ['x = 0'])
|
|
88
|
-
space.offences.
|
|
113
|
+
expect(space.offences).to be_empty
|
|
89
114
|
end
|
|
90
115
|
|
|
91
116
|
it "accepts some operators that are exceptions and don't need spaces" do
|
|
92
117
|
inspect_source(space, 'file.rb', ['(1..3)',
|
|
93
118
|
'ActionController::Base',
|
|
94
119
|
'each { |s, t| }'])
|
|
95
|
-
space.offences.map(&:message).
|
|
120
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
96
121
|
end
|
|
97
122
|
|
|
98
123
|
it 'accepts an assignment followed by newline' do
|
|
99
124
|
inspect_source(space, 'file.rb', ['x =\n 0'])
|
|
100
|
-
space.offences.
|
|
125
|
+
expect(space.offences).to be_empty
|
|
101
126
|
end
|
|
102
127
|
|
|
103
128
|
it 'registers an offences for exponent operator with spaces' do
|
|
104
129
|
inspect_source(space, 'file.rb', ['x = a * b ** 2'])
|
|
105
|
-
space.offences.map(&:message).
|
|
106
|
-
['Space around operator ** detected.']
|
|
130
|
+
expect(space.offences.map(&:message)).to eq(
|
|
131
|
+
['Space around operator ** detected.'])
|
|
107
132
|
end
|
|
108
133
|
|
|
109
134
|
it 'accepts exponent operator without spaces' do
|
|
110
135
|
inspect_source(space, 'file.rb', ['x = a * b**2'])
|
|
111
|
-
space.offences.
|
|
136
|
+
expect(space.offences).to be_empty
|
|
112
137
|
end
|
|
113
138
|
|
|
114
139
|
it 'accepts unary operators without space' do
|
|
115
140
|
inspect_source(space, 'file.rb', ['[].map(&:size)',
|
|
116
141
|
'-3',
|
|
117
142
|
'x = +2'])
|
|
118
|
-
space.offences.map(&:message).
|
|
143
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
119
144
|
end
|
|
120
145
|
end
|
|
121
146
|
end
|
|
@@ -5,38 +5,38 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe SpaceInsideBrackets do
|
|
8
|
-
let
|
|
8
|
+
let(:space) { SpaceInsideBrackets.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for an array literal with spaces inside' do
|
|
11
11
|
inspect_source(space, 'file.rb', ['a = [1, 2 ]',
|
|
12
12
|
'b = [ 1, 2]'])
|
|
13
|
-
space.offences.map(&:message).
|
|
13
|
+
expect(space.offences.map(&:message)).to eq(
|
|
14
14
|
['Space inside square brackets detected.',
|
|
15
|
-
'Space inside square brackets detected.']
|
|
15
|
+
'Space inside square brackets detected.'])
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
it 'accepts space inside square brackets if on its own row' do
|
|
19
19
|
inspect_source(space, 'file.rb', ['a = [',
|
|
20
20
|
' 1, 2',
|
|
21
21
|
' ]'])
|
|
22
|
-
space.offences.map(&:message).
|
|
22
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
it 'accepts square brackets as method name' do
|
|
26
26
|
inspect_source(space, 'file.rb', ['def Vector.[](*array)',
|
|
27
27
|
'end'])
|
|
28
|
-
space.offences.map(&:message).
|
|
28
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it 'accepts square brackets called with method call syntax' do
|
|
32
32
|
inspect_source(space, 'file.rb', ['subject.[](0)'])
|
|
33
|
-
space.offences.map(&:message).
|
|
33
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
it 'only reports a single space once' do
|
|
37
37
|
inspect_source(space, 'file.rb', ['[ ]'])
|
|
38
|
-
space.offences.map(&:message).
|
|
39
|
-
['Space inside square brackets detected.']
|
|
38
|
+
expect(space.offences.map(&:message)).to eq(
|
|
39
|
+
['Space inside square brackets detected.'])
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
end
|
|
@@ -5,21 +5,21 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe SpaceInsideParens do
|
|
8
|
-
let
|
|
8
|
+
let(:space) { SpaceInsideParens.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for spaces inside parens' do
|
|
11
11
|
inspect_source(space, 'file.rb', ['f( 3)',
|
|
12
12
|
'g(3 )'])
|
|
13
|
-
space.offences.map(&:message).
|
|
13
|
+
expect(space.offences.map(&:message)).to eq(
|
|
14
14
|
['Space inside parentheses detected.',
|
|
15
|
-
'Space inside parentheses detected.']
|
|
15
|
+
'Space inside parentheses detected.'])
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
it 'accepts parentheses in block parameter list' do
|
|
19
19
|
inspect_source(space, 'file.rb',
|
|
20
20
|
['list.inject(Tms.new) { |sum, (label, item)|',
|
|
21
21
|
'}'])
|
|
22
|
-
space.offences.map(&:message).
|
|
22
|
+
expect(space.offences.map(&:message)).to be_empty
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
end
|