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 EmptyLines do
|
|
8
|
-
let
|
|
8
|
+
let(:empty_lines) { EmptyLines.new }
|
|
9
9
|
|
|
10
10
|
it 'finds offences in inner classes' do
|
|
11
11
|
inspect_source(empty_lines, '', ['class K',
|
|
@@ -21,8 +21,8 @@ module Rubocop
|
|
|
21
21
|
' def p',
|
|
22
22
|
' end',
|
|
23
23
|
'end'])
|
|
24
|
-
empty_lines.offences.size.
|
|
25
|
-
empty_lines.offences.map(&:line_number).sort.
|
|
24
|
+
expect(empty_lines.offences.size).to eq(2)
|
|
25
|
+
expect(empty_lines.offences.map(&:line_number).sort).to eq([7, 11])
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
# Only one def, so rule about empty line *between* defs does not
|
|
@@ -31,7 +31,7 @@ module Rubocop
|
|
|
31
31
|
inspect_source(empty_lines, '', ['x = 0',
|
|
32
32
|
'def m',
|
|
33
33
|
'end'])
|
|
34
|
-
empty_lines.offences.
|
|
34
|
+
expect(empty_lines.offences).to be_empty
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
# Only one def, so rule about empty line *between* defs does not
|
|
@@ -41,7 +41,7 @@ module Rubocop
|
|
|
41
41
|
' # 123',
|
|
42
42
|
' def m',
|
|
43
43
|
' end'])
|
|
44
|
-
empty_lines.offences.
|
|
44
|
+
expect(empty_lines.offences).to be_empty
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
it 'accepts the first def without leading empty line in a class' do
|
|
@@ -49,7 +49,7 @@ module Rubocop
|
|
|
49
49
|
' def m',
|
|
50
50
|
' end',
|
|
51
51
|
'end'])
|
|
52
|
-
empty_lines.offences.
|
|
52
|
+
expect(empty_lines.offences).to be_empty
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
it 'accepts a def that follows an empty line and then a comment' do
|
|
@@ -64,7 +64,7 @@ module Rubocop
|
|
|
64
64
|
' end',
|
|
65
65
|
'end',
|
|
66
66
|
])
|
|
67
|
-
empty_lines.offences.
|
|
67
|
+
expect(empty_lines.offences).to be_empty
|
|
68
68
|
end
|
|
69
69
|
|
|
70
70
|
it 'accepts a def that is the first of a module' do
|
|
@@ -76,7 +76,7 @@ module Rubocop
|
|
|
76
76
|
'end',
|
|
77
77
|
]
|
|
78
78
|
inspect_source(empty_lines, '', source)
|
|
79
|
-
empty_lines.offences.map(&:message).
|
|
79
|
+
expect(empty_lines.offences.map(&:message)).to be_empty
|
|
80
80
|
end
|
|
81
81
|
end
|
|
82
82
|
end
|
|
@@ -5,36 +5,42 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe Encoding do
|
|
8
|
-
let
|
|
8
|
+
let(:encoding) { Encoding.new }
|
|
9
9
|
|
|
10
|
-
it 'registers an offence when no encoding present' do
|
|
10
|
+
it 'registers an offence when no encoding present', ruby: 1.9 do
|
|
11
11
|
inspect_source(encoding, 'file.rb', ['def foo() end'])
|
|
12
12
|
|
|
13
|
-
encoding.offences.map(&:message).
|
|
14
|
-
['Missing encoding comment.']
|
|
13
|
+
expect(encoding.offences.map(&:message)).to eq(
|
|
14
|
+
['Missing encoding comment.'])
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
it 'accepts encoding on first line' do
|
|
17
|
+
it 'accepts encoding on first line', ruby: 1.9 do
|
|
18
18
|
inspect_source(encoding, 'file.rb', ['# encoding: utf-8',
|
|
19
19
|
'def foo() end'])
|
|
20
20
|
|
|
21
|
-
encoding.offences.
|
|
21
|
+
expect(encoding.offences).to be_empty
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
it 'accepts encoding on second line when shebang present' do
|
|
24
|
+
it 'accepts encoding on second line when shebang present', ruby: 1.9 do
|
|
25
25
|
inspect_source(encoding, 'file.rb', ['#!/usr/bin/env ruby',
|
|
26
26
|
'# encoding: utf-8',
|
|
27
27
|
'def foo() end'])
|
|
28
28
|
|
|
29
|
-
encoding.offences.map(&:message).
|
|
29
|
+
expect(encoding.offences.map(&:message)).to be_empty
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
it '
|
|
32
|
+
it 'books an offence when encoding is in the wrong place', ruby: 1.9 do
|
|
33
33
|
inspect_source(encoding, 'file.rb', ['def foo() end',
|
|
34
34
|
'# encoding: utf-8'])
|
|
35
35
|
|
|
36
|
-
encoding.offences.map(&:message).
|
|
37
|
-
['Missing encoding comment.']
|
|
36
|
+
expect(encoding.offences.map(&:message)).to eq(
|
|
37
|
+
['Missing encoding comment.'])
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it 'does not register an offence on Ruby 2.0', ruby: 2.0 do
|
|
41
|
+
inspect_source(encoding, 'file.rb', ['def foo() end'])
|
|
42
|
+
|
|
43
|
+
expect(encoding.offences).to be_empty
|
|
38
44
|
end
|
|
39
45
|
end
|
|
40
46
|
end
|
|
@@ -5,18 +5,18 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe EndOfLine do
|
|
8
|
-
let
|
|
8
|
+
let(:eol) { EndOfLine.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for CR+LF' do
|
|
11
11
|
inspect_source(eol, 'file.rb', ["x=0\r", ''])
|
|
12
|
-
eol.offences.map(&:message).
|
|
13
|
-
['Carriage return character detected.']
|
|
12
|
+
expect(eol.offences.map(&:message)).to eq(
|
|
13
|
+
['Carriage return character detected.'])
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it 'registers an offence for CR at end of file' do
|
|
17
17
|
inspect_source(eol, 'file.rb', ["x=0\r"])
|
|
18
|
-
eol.offences.map(&:message).
|
|
19
|
-
['Carriage return character detected.']
|
|
18
|
+
expect(eol.offences.map(&:message)).to eq(
|
|
19
|
+
['Carriage return character detected.'])
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe FavorModifier do
|
|
8
|
+
let(:if_until) { IfUnlessModifier.new }
|
|
9
|
+
let(:while_until) { WhileUntilModifier.new }
|
|
10
|
+
|
|
11
|
+
it 'registers an offence for multiline if that fits on one line' do
|
|
12
|
+
# This if statement fits exactly on one line if written as a modifier.
|
|
13
|
+
inspect_source(if_until, 'file.rb',
|
|
14
|
+
['if a_condition_that_is_just_short_enough',
|
|
15
|
+
' some_long_metod_name(followed_by_args)',
|
|
16
|
+
'end'])
|
|
17
|
+
expect(if_until.offences.map(&:message)).to eq(
|
|
18
|
+
['Favor modifier if/unless usage when you have a single-line body.' +
|
|
19
|
+
' Another good alternative is the usage of control flow and/or.'])
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "accepts multiline if that doesn't fit on one line" do
|
|
23
|
+
check_too_long(if_until, 'if')
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it 'accepts multiline if whose body is more than one line' do
|
|
27
|
+
check_short_multiline(if_until, 'if')
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'registers an offence for multiline unless that fits on one line' do
|
|
31
|
+
inspect_source(if_until, 'file.rb', ['unless a',
|
|
32
|
+
' b',
|
|
33
|
+
'end'])
|
|
34
|
+
expect(if_until.offences.map(&:message)).to eq(
|
|
35
|
+
['Favor modifier if/unless usage when you have a single-line body.' +
|
|
36
|
+
' Another good alternative is the usage of control flow and/or.'])
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it 'accepts code with EOL comment since user might want to keep it' do
|
|
40
|
+
inspect_source(if_until, 'file.rb', ['unless a',
|
|
41
|
+
' b # A comment',
|
|
42
|
+
'end'])
|
|
43
|
+
expect(if_until.offences.map(&:message)).to be_empty
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'accepts if-else-end' do
|
|
47
|
+
inspect_source(if_until, 'file.rb',
|
|
48
|
+
['if args.last.is_a? Hash then args.pop else ' +
|
|
49
|
+
'Hash.new end'])
|
|
50
|
+
expect(if_until.offences.map(&:message)).to be_empty
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "accepts multiline unless that doesn't fit on one line" do
|
|
54
|
+
check_too_long(while_until, 'unless')
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'accepts multiline unless whose body is more than one line' do
|
|
58
|
+
check_short_multiline(while_until, 'unless')
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it 'registers an offence for multiline while that fits on one line' do
|
|
62
|
+
check_really_short(while_until, 'while')
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "accepts multiline while that doesn't fit on one line" do
|
|
66
|
+
check_too_long(while_until, 'while')
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it 'accepts multiline while whose body is more than one line' do
|
|
70
|
+
check_short_multiline(while_until, 'while')
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'registers an offence for multiline until that fits on one line' do
|
|
74
|
+
check_really_short(while_until, 'until')
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "accepts multiline until that doesn't fit on one line" do
|
|
78
|
+
check_too_long(while_until, 'until')
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'accepts multiline until whose body is more than one line' do
|
|
82
|
+
check_short_multiline(while_until, 'until')
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def check_really_short(cop, keyword)
|
|
86
|
+
inspect_source(cop, 'file.rb', ["#{keyword} a",
|
|
87
|
+
' b',
|
|
88
|
+
'end'])
|
|
89
|
+
expect(cop.offences.map(&:message)).to eq(
|
|
90
|
+
['Favor modifier while/until usage when you have a single-line ' +
|
|
91
|
+
'body.'])
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def check_too_long(cop, keyword)
|
|
95
|
+
inspect_source(cop, 'file.rb',
|
|
96
|
+
[" #{keyword} a_lengthy_condition_that_goes_on_and_on",
|
|
97
|
+
' some_long_metod_name(followed_by_args)',
|
|
98
|
+
' end'])
|
|
99
|
+
expect(cop.offences.map(&:message)).to be_empty
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def check_short_multiline(cop, keyword)
|
|
103
|
+
inspect_source(cop, 'file.rb',
|
|
104
|
+
["#{keyword} ENV['COVERAGE']",
|
|
105
|
+
" require 'simplecov'",
|
|
106
|
+
' SimpleCov.start',
|
|
107
|
+
'end'])
|
|
108
|
+
expect(cop.offences.map(&:message)).to be_empty
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe FavorUnlessOverNegatedIf do
|
|
8
|
+
let(:fav_unless) { FavorUnlessOverNegatedIf.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for if with exclamation point condition' do
|
|
11
|
+
inspect_source(fav_unless, 'file.rb',
|
|
12
|
+
['if !a_condition',
|
|
13
|
+
' some_method',
|
|
14
|
+
'end',
|
|
15
|
+
'some_method if !a_condition',
|
|
16
|
+
])
|
|
17
|
+
expect(fav_unless.offences.map(&:message)).to eq(
|
|
18
|
+
['Favor unless (or control flow or) over if for negative ' +
|
|
19
|
+
'conditions.'] * 2)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'registers an offence for if with "not" condition' do
|
|
23
|
+
inspect_source(fav_unless, 'file.rb',
|
|
24
|
+
['if not a_condition',
|
|
25
|
+
' some_method',
|
|
26
|
+
'end',
|
|
27
|
+
'some_method if not a_condition'])
|
|
28
|
+
expect(fav_unless.offences.map(&:message)).to eq(
|
|
29
|
+
['Favor unless (or control flow or) over if for negative ' +
|
|
30
|
+
'conditions.'] * 2)
|
|
31
|
+
expect(fav_unless.offences.map(&:line_number)).to eq([1, 4])
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it 'accepts an if/else with negative condition' do
|
|
35
|
+
inspect_source(fav_unless, 'file.rb',
|
|
36
|
+
['if !a_condition',
|
|
37
|
+
' some_method',
|
|
38
|
+
'else',
|
|
39
|
+
' something_else',
|
|
40
|
+
'end',
|
|
41
|
+
'if not a_condition',
|
|
42
|
+
' some_method',
|
|
43
|
+
'elsif other_condition',
|
|
44
|
+
' something_else',
|
|
45
|
+
'end'])
|
|
46
|
+
expect(fav_unless.offences.map(&:message)).to be_empty
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'accepts an if where only part of the contition is negated' do
|
|
50
|
+
inspect_source(fav_unless, 'file.rb',
|
|
51
|
+
['if !a_condition && another_condition',
|
|
52
|
+
' some_method',
|
|
53
|
+
'end',
|
|
54
|
+
'if not a_condition or another_condition',
|
|
55
|
+
' some_method',
|
|
56
|
+
'end',
|
|
57
|
+
'some_method if not a_condition or another_condition'])
|
|
58
|
+
expect(fav_unless.offences.map(&:message)).to be_empty
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe FavorUntilOverNegatedWhile do
|
|
8
|
+
let(:fav_until) { FavorUntilOverNegatedWhile.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for while with exclamation point condition' do
|
|
11
|
+
inspect_source(fav_until, 'file.rb',
|
|
12
|
+
['while !a_condition',
|
|
13
|
+
' some_method',
|
|
14
|
+
'end',
|
|
15
|
+
'some_method while !a_condition',
|
|
16
|
+
])
|
|
17
|
+
expect(fav_until.offences.map(&:message)).to eq(
|
|
18
|
+
['Favor until over while for negative conditions.'] * 2)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'registers an offence for while with "not" condition' do
|
|
22
|
+
inspect_source(fav_until, 'file.rb',
|
|
23
|
+
['while (not a_condition)',
|
|
24
|
+
' some_method',
|
|
25
|
+
'end',
|
|
26
|
+
'some_method while not a_condition'])
|
|
27
|
+
expect(fav_until.offences.map(&:message)).to eq(
|
|
28
|
+
['Favor until over while for negative conditions.'] * 2)
|
|
29
|
+
expect(fav_until.offences.map(&:line_number)).to eq([1, 4])
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it 'accepts an while where only part of the contition is negated' do
|
|
33
|
+
inspect_source(fav_until, 'file.rb',
|
|
34
|
+
['while !a_condition && another_condition',
|
|
35
|
+
' some_method',
|
|
36
|
+
'end',
|
|
37
|
+
'while not a_condition or another_condition',
|
|
38
|
+
' some_method',
|
|
39
|
+
'end',
|
|
40
|
+
'some_method while not a_condition or other_cond'])
|
|
41
|
+
expect(fav_until.offences.map(&:message)).to be_empty
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -7,13 +7,12 @@ module Rubocop
|
|
|
7
7
|
describe Grammar do
|
|
8
8
|
EXAMPLE = '3.times { |i| x = "#{y}#{z}}" }'
|
|
9
9
|
tokens = Ripper.lex(EXAMPLE).map { |t| Token.new(*t) }
|
|
10
|
-
let
|
|
10
|
+
let(:grammar) { Grammar.new(tokens) }
|
|
11
11
|
|
|
12
|
-
it 'correlates token indices to grammar paths' do
|
|
12
|
+
it 'correlates token indices to grammar paths', ruby: 2.0 do
|
|
13
13
|
method_block = [:program, :method_add_block]
|
|
14
14
|
brace_block = method_block + [:brace_block]
|
|
15
|
-
|
|
16
|
-
[[[1, 0], :on_int, '3'],
|
|
15
|
+
test_2_0 = [[[1, 0], :on_int, '3'],
|
|
17
16
|
[[1, 1], :on_period, '.'],
|
|
18
17
|
[[1, 2], :on_ident, 'times'],
|
|
19
18
|
[[1, 7], :on_sp, ' '],
|
|
@@ -30,20 +29,21 @@ module Rubocop
|
|
|
30
29
|
[[1, 18], :on_tstring_beg, '"'],
|
|
31
30
|
[[1, 19], :on_embexpr_beg, '#{'], # 15
|
|
32
31
|
[[1, 21], :on_ident, 'y'],
|
|
33
|
-
[[1, 22], :on_rbrace, '}'],
|
|
32
|
+
[[1, 22], :on_embexpr_end, '}'], # [[1, 22], :on_rbrace, '}'],
|
|
34
33
|
[[1, 23], :on_embexpr_beg, '#{'],
|
|
35
34
|
[[1, 25], :on_ident, 'z'],
|
|
36
|
-
[[1, 26], :on_rbrace, '}'], # 20
|
|
35
|
+
[[1, 26], :on_embexpr_end, '}'], # [[1, 26], :on_rbrace, '}'], # 20
|
|
37
36
|
[[1, 27], :on_tstring_content, '}'],
|
|
38
37
|
[[1, 28], :on_tstring_end, '"'],
|
|
39
38
|
[[1, 29], :on_sp, ' '],
|
|
40
39
|
[[1, 30], :on_rbrace, '}']]
|
|
41
|
-
|
|
40
|
+
expect(Ripper.lex(EXAMPLE)).to eq(test_2_0) if RUBY_VERSION >= '2.0'
|
|
42
41
|
sexp = Ripper.sexp(EXAMPLE)
|
|
43
42
|
Position.make_position_objects(sexp)
|
|
44
43
|
|
|
45
44
|
varref = (RUBY_VERSION == '1.9.2') ? :var_ref : :vcall
|
|
46
|
-
|
|
45
|
+
|
|
46
|
+
test = {
|
|
47
47
|
0 => method_block + [:call, :@int], # 3
|
|
48
48
|
2 => method_block + [:call, :@ident], # times
|
|
49
49
|
4 => brace_block, # {
|
|
@@ -58,8 +58,13 @@ module Rubocop
|
|
|
58
58
|
:string_embexpr, varref, :@ident], # z
|
|
59
59
|
21 => brace_block + [:assign, :string_literal, :string_content,
|
|
60
60
|
:@tstring_content], # }
|
|
61
|
-
24 => brace_block, # }
|
|
62
61
|
}
|
|
62
|
+
if RUBY_VERSION >= '2.0'
|
|
63
|
+
expect(grammar.correlate(sexp)).to eq(test)
|
|
64
|
+
else
|
|
65
|
+
test = (test[24] = brace_block)
|
|
66
|
+
expect(grammar.correlate(sexp)).to eq(test)
|
|
67
|
+
end
|
|
63
68
|
end
|
|
64
69
|
end
|
|
65
70
|
end
|
|
@@ -5,39 +5,39 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe HashSyntax do
|
|
8
|
-
let
|
|
8
|
+
let(:hash_syntax) { HashSyntax.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for hash rocket syntax when new is possible' do
|
|
11
11
|
inspect_source(hash_syntax, '', ['x = { :a => 0 }'])
|
|
12
|
-
hash_syntax.offences.map(&:message).
|
|
13
|
-
['Ruby 1.8 hash syntax detected']
|
|
12
|
+
expect(hash_syntax.offences.map(&:message)).to eq(
|
|
13
|
+
['Ruby 1.8 hash syntax detected'])
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it 'registers an offence for mixed syntax when new is possible' do
|
|
17
17
|
inspect_source(hash_syntax, '', ['x = { :a => 0, b: 1 }'])
|
|
18
|
-
hash_syntax.offences.map(&:message).
|
|
19
|
-
['Ruby 1.8 hash syntax detected']
|
|
18
|
+
expect(hash_syntax.offences.map(&:message)).to eq(
|
|
19
|
+
['Ruby 1.8 hash syntax detected'])
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it 'registers an offence for hash rockets in method calls' do
|
|
23
23
|
inspect_source(hash_syntax, '', ['func(3, :a => 0)'])
|
|
24
|
-
hash_syntax.offences.map(&:message).
|
|
25
|
-
['Ruby 1.8 hash syntax detected']
|
|
24
|
+
expect(hash_syntax.offences.map(&:message)).to eq(
|
|
25
|
+
['Ruby 1.8 hash syntax detected'])
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
it 'accepts hash rockets when keys have different types' do
|
|
29
29
|
inspect_source(hash_syntax, '', ['x = { :a => 0, "b" => 1 }'])
|
|
30
|
-
hash_syntax.offences.map(&:message).
|
|
30
|
+
expect(hash_syntax.offences.map(&:message)).to be_empty
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
it 'accepts new syntax in a hash literal' do
|
|
34
34
|
inspect_source(hash_syntax, '', ['x = { a: 0, b: 1 }'])
|
|
35
|
-
hash_syntax.offences.map(&:message).
|
|
35
|
+
expect(hash_syntax.offences.map(&:message)).to be_empty
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
it 'accepts new syntax in method calls' do
|
|
39
39
|
inspect_source(hash_syntax, '', ['func(3, a: 0)'])
|
|
40
|
-
hash_syntax.offences.map(&:message).
|
|
40
|
+
expect(hash_syntax.offences.map(&:message)).to be_empty
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
end
|
|
@@ -5,12 +5,12 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe IfWithSemicolon do
|
|
8
|
-
let
|
|
8
|
+
let(:iws) { IfWithSemicolon.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for one line if/;/end' do
|
|
11
11
|
inspect_source(iws, '', ['if cond; run else dont end'])
|
|
12
|
-
iws.offences.map(&:message).
|
|
13
|
-
['Never use if x; Use the ternary operator instead.']
|
|
12
|
+
expect(iws.offences.map(&:message)).to eq(
|
|
13
|
+
['Never use if x; Use the ternary operator instead.'])
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
end
|
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe Indentation do
|
|
8
|
-
let
|
|
8
|
+
let(:ind) { Indentation.new }
|
|
9
9
|
|
|
10
10
|
it "registers an offence for a when clause that's deeper than case" do
|
|
11
11
|
source = ['case a',
|
|
@@ -15,8 +15,8 @@ module Rubocop
|
|
|
15
15
|
' end',
|
|
16
16
|
'end']
|
|
17
17
|
inspect_source(ind, 'file.rb', source)
|
|
18
|
-
ind.offences.map(&:message).
|
|
19
|
-
['Indent when as deep as case.'] * 2
|
|
18
|
+
expect(ind.offences.map(&:message)).to eq(
|
|
19
|
+
['Indent when as deep as case.'] * 2)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it "accepts a when clause that's equally indented with case" do
|
|
@@ -33,7 +33,7 @@ module Rubocop
|
|
|
33
33
|
'end',
|
|
34
34
|
'']
|
|
35
35
|
inspect_source(ind, 'file.rb', source)
|
|
36
|
-
ind.offences.
|
|
36
|
+
expect(ind.offences).to be_empty
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
it "doesn't get confused by strings with case in them" do
|
|
@@ -43,7 +43,7 @@ module Rubocop
|
|
|
43
43
|
'end',
|
|
44
44
|
'']
|
|
45
45
|
inspect_source(ind, 'file.rb', source)
|
|
46
|
-
ind.offences.map(&:message).
|
|
46
|
+
expect(ind.offences.map(&:message)).to be_empty
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
it "doesn't get confused by symbols named case or when" do
|
|
@@ -56,7 +56,7 @@ module Rubocop
|
|
|
56
56
|
'end',
|
|
57
57
|
'']
|
|
58
58
|
inspect_source(ind, 'file.rb', source)
|
|
59
|
-
ind.offences.map(&:message).
|
|
59
|
+
expect(ind.offences.map(&:message)).to be_empty
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
it 'accepts correctly indented whens in complex combinations' do
|
|
@@ -79,7 +79,7 @@ module Rubocop
|
|
|
79
79
|
'end',
|
|
80
80
|
'']
|
|
81
81
|
inspect_source(ind, 'file.rb', source)
|
|
82
|
-
ind.offences.map(&:message).
|
|
82
|
+
expect(ind.offences.map(&:message)).to be_empty
|
|
83
83
|
end
|
|
84
84
|
end
|
|
85
85
|
end
|
|
@@ -5,17 +5,17 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe LineLength do
|
|
8
|
-
let
|
|
8
|
+
let(:ll) { LineLength.new }
|
|
9
9
|
|
|
10
10
|
it "registers an offence for a line that's 80 characters wide" do
|
|
11
11
|
ll.inspect('file.rb', ['#' * 80], nil, nil)
|
|
12
|
-
ll.offences.size.
|
|
13
|
-
ll.offences.first.message.
|
|
12
|
+
expect(ll.offences.size).to eq(1)
|
|
13
|
+
expect(ll.offences.first.message).to eq('Line is too long. [80/79]')
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it "accepts a line that's 79 characters wide" do
|
|
17
17
|
ll.inspect('file.rb', ['#' * 79], nil, nil)
|
|
18
|
-
ll.offences.
|
|
18
|
+
expect(ll.offences).to be_empty
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe MethodAndVariableSnakeCase do
|
|
8
|
+
let(:snake_case) { MethodAndVariableSnakeCase.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for camel case in names' do
|
|
11
|
+
inspect_source(snake_case, 'file.rb',
|
|
12
|
+
['def myMethod',
|
|
13
|
+
' myLocal = 1',
|
|
14
|
+
' self.mySetter = 2',
|
|
15
|
+
' @myAttribute = 3',
|
|
16
|
+
'end',
|
|
17
|
+
])
|
|
18
|
+
expect(snake_case.offences.map(&:message)).to eq(
|
|
19
|
+
['Use snake_case for methods and variables.'] * 4)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'accepts snake case in names' do
|
|
23
|
+
inspect_source(snake_case, 'file.rb',
|
|
24
|
+
['def my_method',
|
|
25
|
+
' my_local_html = 1',
|
|
26
|
+
' self.my_setter = 2',
|
|
27
|
+
' @my_attribute = 3',
|
|
28
|
+
'end',
|
|
29
|
+
])
|
|
30
|
+
expect(snake_case.offences.map(&:message)).to be_empty
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it 'registers an offence for mixed snake case and camel case' do
|
|
34
|
+
inspect_source(snake_case, 'file.rb',
|
|
35
|
+
['def visit_Arel_Nodes_SelectStatement',
|
|
36
|
+
'end'])
|
|
37
|
+
expect(snake_case.offences.map(&:message)).to eq(
|
|
38
|
+
['Use snake_case for methods and variables.'])
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it 'accepts screaming snake case globals' do
|
|
42
|
+
inspect_source(snake_case, 'file.rb', ['$MY_GLOBAL = 0'])
|
|
43
|
+
expect(snake_case.offences.map(&:message)).to be_empty
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -5,19 +5,19 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe MultilineBlocks do
|
|
8
|
-
let
|
|
8
|
+
let(:blocks) { MultilineBlocks.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for a multiline block with braces' do
|
|
11
11
|
inspect_source(blocks, '', ['each { |x|',
|
|
12
12
|
'}'])
|
|
13
|
-
blocks.offences.map(&:message).
|
|
14
|
-
['Avoid using {...} for multi-line blocks.']
|
|
13
|
+
expect(blocks.offences.map(&:message)).to eq(
|
|
14
|
+
['Avoid using {...} for multi-line blocks.'])
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it 'accepts a multiline block with do-end' do
|
|
18
18
|
inspect_source(blocks, '', ['each do |x|',
|
|
19
19
|
'end'])
|
|
20
|
-
blocks.offences.map(&:message).
|
|
20
|
+
expect(blocks.offences.map(&:message)).to be_empty
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
end
|