rubocop 0.3.0 → 0.6.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 +176 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +60 -0
- data/CONTRIBUTING.md +13 -6
- data/Gemfile +1 -13
- data/README.md +131 -6
- data/Rakefile +1 -15
- data/bin/rubocop +2 -1
- data/lib/rubocop/cli.rb +133 -21
- data/lib/rubocop/cop/alias.rb +18 -0
- data/lib/rubocop/cop/align_parameters.rb +5 -4
- data/lib/rubocop/cop/array_literal.rb +22 -0
- data/lib/rubocop/cop/ascii_identifiers_and_comments.rb +18 -0
- data/lib/rubocop/cop/avoid_class_vars.rb +20 -0
- data/lib/rubocop/cop/avoid_for.rb +17 -0
- data/lib/rubocop/cop/avoid_perl_backrefs.rb +20 -0
- data/lib/rubocop/cop/avoid_perlisms.rb +46 -0
- data/lib/rubocop/cop/blocks.rb +1 -1
- data/lib/rubocop/cop/brace_after_percent.rb +28 -0
- 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 +16 -4
- data/lib/rubocop/cop/def_parentheses.rb +4 -1
- data/lib/rubocop/cop/encoding.rb +7 -5
- data/lib/rubocop/cop/end_of_line.rb +1 -3
- data/lib/rubocop/cop/ensure_return.rb +36 -0
- data/lib/rubocop/cop/favor_modifier.rb +82 -0
- data/lib/rubocop/cop/favor_percent_r.rb +19 -0
- data/lib/rubocop/cop/favor_sprintf.rb +37 -0
- data/lib/rubocop/cop/favor_unless_over_negated_if.rb +47 -0
- data/lib/rubocop/cop/grammar.rb +12 -8
- data/lib/rubocop/cop/handle_exceptions.rb +21 -0
- data/lib/rubocop/cop/hash_literal.rb +22 -0
- 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/method_length.rb +66 -0
- data/lib/rubocop/cop/new_lambda_literal.rb +17 -0
- data/lib/rubocop/cop/offence.rb +3 -1
- data/lib/rubocop/cop/op_method.rb +23 -0
- data/lib/rubocop/cop/parentheses_around_condition.rb +24 -0
- data/lib/rubocop/cop/percent_literals.rb +25 -0
- data/lib/rubocop/cop/percent_r.rb +19 -0
- data/lib/rubocop/cop/reduce_arguments.rb +67 -0
- data/lib/rubocop/cop/rescue_exception.rb +39 -0
- data/lib/rubocop/cop/rescue_modifier.rb +20 -0
- data/lib/rubocop/cop/semicolon.rb +18 -0
- data/lib/rubocop/cop/space_after_comma_etc.rb +2 -1
- data/lib/rubocop/cop/string_literals.rb +12 -0
- data/lib/rubocop/cop/surrounding_space.rb +48 -47
- data/lib/rubocop/cop/symbol_snake_case.rb +27 -0
- data/lib/rubocop/cop/syntax.rb +42 -0
- data/lib/rubocop/cop/tab.rb +1 -3
- data/lib/rubocop/cop/variable_interpolation.rb +20 -0
- data/lib/rubocop/cop/when_then.rb +3 -0
- data/lib/rubocop/report/emacs_style.rb +2 -2
- data/lib/rubocop/report/plain_text.rb +1 -1
- data/lib/rubocop/report/report.rb +5 -2
- data/lib/rubocop/version.rb +3 -1
- data/lib/rubocop.rb +37 -0
- data/rubocop.gemspec +36 -134
- data/spec/rubocop/cli_spec.rb +326 -48
- data/spec/rubocop/cops/alias_spec.rb +36 -0
- data/spec/rubocop/cops/align_parameters_spec.rb +43 -34
- data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
- data/spec/rubocop/cops/array_literal_spec.rb +29 -0
- data/spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb +38 -0
- data/spec/rubocop/cops/avoid_class_vars_spec.rb +20 -0
- data/spec/rubocop/cops/avoid_for_spec.rb +22 -0
- data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +18 -0
- data/spec/rubocop/cops/avoid_perlisms_spec.rb +44 -0
- data/spec/rubocop/cops/brace_after_percent_spec.rb +27 -0
- 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/ensure_return_spec.rb +37 -0
- data/spec/rubocop/cops/favor_modifier_spec.rb +112 -0
- data/spec/rubocop/cops/favor_percent_r.rb +29 -0
- data/spec/rubocop/cops/favor_sprintf_spec.rb +57 -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 +59 -44
- data/spec/rubocop/cops/handle_exceptions_spec.rb +36 -0
- data/spec/rubocop/cops/hash_literal_spec.rb +29 -0
- 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/method_length_spec.rb +150 -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 +10 -4
- data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
- data/spec/rubocop/cops/op_method_spec.rb +58 -0
- 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/percent_literals_spec.rb +47 -0
- data/spec/rubocop/cops/percent_r_spec.rb +29 -0
- data/spec/rubocop/cops/reduce_arguments_spec.rb +57 -0
- data/spec/rubocop/cops/rescue_exception_spec.rb +73 -0
- data/spec/rubocop/cops/rescue_modifier.rb +40 -0
- data/spec/rubocop/cops/semicolon_spec.rb +29 -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 +57 -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 +9 -6
- data/spec/rubocop/cops/symbol_snake_case_spec.rb +74 -0
- data/spec/rubocop/cops/syntax_spec.rb +34 -0
- data/spec/rubocop/cops/tab_spec.rb +4 -4
- 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/variable_interpolation_spec.rb +45 -0
- data/spec/rubocop/cops/when_then_spec.rb +12 -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 +165 -36
- data/.rbenv-version +0 -1
- data/.rvmrc +0 -1
- data/Gemfile.lock +0 -39
- data/VERSION +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,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
|
|
@@ -5,27 +5,30 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe StringLiterals do
|
|
8
|
-
let
|
|
8
|
+
let(:sl) { StringLiterals.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for double quotes when single quotes suffice' do
|
|
11
11
|
inspect_source(sl, 'file.rb', ['s = "abc"'])
|
|
12
|
-
sl.offences.map(&:message).
|
|
12
|
+
expect(sl.offences.map(&:message)).to eq(
|
|
13
13
|
["Prefer single-quoted strings when you don't need string " +
|
|
14
|
-
'interpolation or special symbols.']
|
|
14
|
+
'interpolation or special symbols.'])
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it 'accepts double quotes when they are needed' do
|
|
18
18
|
src = ['a = "\n"',
|
|
19
19
|
'b = "#{encode_severity}:#{sprintf("%3d", line_number)}: #{m}"',
|
|
20
|
-
'c = "\'"'
|
|
20
|
+
'c = "\'"',
|
|
21
|
+
'd = "#@test"',
|
|
22
|
+
'e = "#$test"',
|
|
23
|
+
'f = "#@@test"']
|
|
21
24
|
inspect_source(sl, 'file.rb', src)
|
|
22
|
-
sl.offences.map(&:message).
|
|
25
|
+
expect(sl.offences.map(&:message)).to be_empty
|
|
23
26
|
end
|
|
24
27
|
|
|
25
28
|
it 'can handle double quotes within embedded expression' do
|
|
26
29
|
src = ['"#{"A"}"']
|
|
27
30
|
inspect_source(sl, 'file.rb', src)
|
|
28
|
-
sl.offences.map(&:message).
|
|
31
|
+
expect(sl.offences.map(&:message)).to be_empty
|
|
29
32
|
end
|
|
30
33
|
end
|
|
31
34
|
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe SymbolSnakeCase do
|
|
8
|
+
let(:snake_case) { SymbolSnakeCase.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for camel case in names' do
|
|
11
|
+
inspect_source(snake_case, 'file.rb',
|
|
12
|
+
['test = :BadIdea'])
|
|
13
|
+
expect(snake_case.offences.map(&:message)).to eq(
|
|
14
|
+
['Use snake_case for symbols.'])
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it 'accepts snake case in names' do
|
|
18
|
+
inspect_source(snake_case, 'file.rb',
|
|
19
|
+
['test = :good_idea'])
|
|
20
|
+
expect(snake_case.offences).to be_empty
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'accepts snake case with a prefix @ in names' do
|
|
24
|
+
inspect_source(snake_case, 'file.rb',
|
|
25
|
+
['test = :@good_idea'])
|
|
26
|
+
expect(snake_case.offences).to be_empty
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it 'accepts snake case with ? suffix' do
|
|
30
|
+
inspect_source(snake_case, 'file.rb',
|
|
31
|
+
['test = :good_idea?'])
|
|
32
|
+
expect(snake_case.offences).to be_empty
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it 'accepts snake case with ! suffix' do
|
|
36
|
+
inspect_source(snake_case, 'file.rb',
|
|
37
|
+
['test = :good_idea!'])
|
|
38
|
+
expect(snake_case.offences).to be_empty
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it 'accepts snake case with = suffix' do
|
|
42
|
+
inspect_source(snake_case, 'file.rb',
|
|
43
|
+
['test = :good_idea='])
|
|
44
|
+
expect(snake_case.offences).to be_empty
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it 'accepts special cases - !, [] and **' do
|
|
48
|
+
inspect_source(snake_case, 'file.rb',
|
|
49
|
+
['test = :**',
|
|
50
|
+
'test = :!',
|
|
51
|
+
'test = :[]',
|
|
52
|
+
'test = :[]='])
|
|
53
|
+
expect(snake_case.offences).to be_empty
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it 'accepts special cases - ==, <=>, >, <, >=, <=' do
|
|
57
|
+
inspect_source(snake_case, 'file.rb',
|
|
58
|
+
['test = :==',
|
|
59
|
+
'test = :<=>',
|
|
60
|
+
'test = :>',
|
|
61
|
+
'test = :<',
|
|
62
|
+
'test = :>=',
|
|
63
|
+
'test = :<='])
|
|
64
|
+
expect(snake_case.offences).to be_empty
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it 'registers an offence for SCREAMING_SNAKE_CASE' do
|
|
68
|
+
inspect_source(snake_case, 'file.rb',
|
|
69
|
+
['test = :BAD_IDEA'])
|
|
70
|
+
expect(snake_case.offences.size).to eq(1)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe Syntax do
|
|
8
|
+
let(:sc) { Syntax.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for unused variables', ruby: 2.0 do
|
|
11
|
+
sc.inspect('file.rb', ['x = 5', 'puts 10'], nil, nil)
|
|
12
|
+
expect(sc.offences.size).to eq(1)
|
|
13
|
+
expect(sc.offences.first.message)
|
|
14
|
+
.to eq('Assigned but unused variable - x')
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe '#process_line' do
|
|
18
|
+
it 'processes warnings correctly' do
|
|
19
|
+
l, s, m = sc.process_line('admin.rb:1: warning: possibly useless')
|
|
20
|
+
expect(l).to eq(1)
|
|
21
|
+
expect(s).to eq(:warning)
|
|
22
|
+
expect(m).to eq('Possibly useless')
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it 'processes errors correctly' do
|
|
26
|
+
l, s, m = sc.process_line('admin.rb:1: unterminated string meets')
|
|
27
|
+
expect(l).to eq(1)
|
|
28
|
+
expect(s).to eq(:error)
|
|
29
|
+
expect(m).to eq('Unterminated string meets')
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -5,16 +5,16 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe Tab do
|
|
8
|
-
let
|
|
8
|
+
let(:tab) { Tab.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for a line indented with tab' do
|
|
11
11
|
tab.inspect('file.rb', ["\tx = 0"], nil, nil)
|
|
12
|
-
tab.offences.size.
|
|
12
|
+
expect(tab.offences.size).to eq(1)
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
it 'accepts a line with tab in a string' do
|
|
16
|
-
tab.inspect('file.rb', [
|
|
17
|
-
tab.offences.
|
|
16
|
+
tab.inspect('file.rb', ["(x = \"\t\")"], nil, nil)
|
|
17
|
+
expect(tab.offences).to be_empty
|
|
18
18
|
end
|
|
19
19
|
end
|
|
20
20
|
end
|
|
@@ -5,30 +5,30 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe MultilineTernaryOperator do
|
|
8
|
-
let
|
|
8
|
+
let(:op) { MultilineTernaryOperator.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for a multiline ternary operator expression' do
|
|
11
11
|
inspect_source(op, 'file.rb', ['a = cond ?',
|
|
12
12
|
' b : c'])
|
|
13
|
-
op.offences.map(&:message).
|
|
13
|
+
expect(op.offences.map(&:message)).to eq(
|
|
14
14
|
['Avoid multi-line ?: (the ternary operator); use if/unless ' +
|
|
15
|
-
'instead.']
|
|
15
|
+
'instead.'])
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
it 'accepts a single line ternary operator expression' do
|
|
19
19
|
inspect_source(op, 'file.rb', ['a = cond ? b : c'])
|
|
20
|
-
op.offences.map(&:message).
|
|
20
|
+
expect(op.offences.map(&:message)).to be_empty
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
describe NestedTernaryOperator do
|
|
25
|
-
let
|
|
25
|
+
let(:op) { NestedTernaryOperator.new }
|
|
26
26
|
|
|
27
27
|
it 'registers an offence for a nested ternary operator expression' do
|
|
28
28
|
inspect_source(op, 'file.rb', ['a ? (b ? b1 : b2) : a2'])
|
|
29
|
-
op.offences.map(&:message).
|
|
29
|
+
expect(op.offences.map(&:message)).to eq(
|
|
30
30
|
['Ternary operators must not be nested. Prefer if/else constructs ' +
|
|
31
|
-
'instead.']
|
|
31
|
+
'instead.'])
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
it 'accepts a non-nested ternary operator within an if' do
|
|
@@ -37,7 +37,7 @@ module Rubocop
|
|
|
37
37
|
'else',
|
|
38
38
|
' d',
|
|
39
39
|
'end'])
|
|
40
|
-
op.offences.map(&:message).
|
|
40
|
+
expect(op.offences.map(&:message)).to be_empty
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
end
|
|
@@ -5,22 +5,22 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe TrailingWhitespace do
|
|
8
|
-
let
|
|
8
|
+
let(:tws) { TrailingWhitespace.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for a line ending with space' do
|
|
11
11
|
source = ['x = 0 ']
|
|
12
12
|
tws.inspect('file.rb', source, nil, nil)
|
|
13
|
-
tws.offences.size.
|
|
13
|
+
expect(tws.offences.size).to eq(1)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it 'registers an offence for a line ending with tab' do
|
|
17
17
|
tws.inspect('file.rb', ["x = 0\t"], nil, nil)
|
|
18
|
-
tws.offences.size.
|
|
18
|
+
expect(tws.offences.size).to eq(1)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
it 'accepts a line without trailing whitespace' do
|
|
22
22
|
tws.inspect('file.rb', ["x = 0\n"], nil, nil)
|
|
23
|
-
tws.offences.
|
|
23
|
+
expect(tws.offences).to be_empty
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
26
|
end
|
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe UnlessElse do
|
|
8
|
-
let
|
|
8
|
+
let(:ue) { UnlessElse.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for an unless with else' do
|
|
11
11
|
inspect_source(ue, 'file.rb', ['unless x',
|
|
@@ -13,16 +13,16 @@ module Rubocop
|
|
|
13
13
|
'else',
|
|
14
14
|
' a = 0',
|
|
15
15
|
'end'])
|
|
16
|
-
ue.offences.map(&:message).
|
|
16
|
+
expect(ue.offences.map(&:message)).to eq(
|
|
17
17
|
['Never use unless with else. Rewrite these with the ' +
|
|
18
|
-
'positive case first.']
|
|
18
|
+
'positive case first.'])
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
it 'accepts an unless without else' do
|
|
22
22
|
inspect_source(ue, 'file.rb', ['unless x',
|
|
23
23
|
' a = 1',
|
|
24
24
|
'end'])
|
|
25
|
-
ue.offences.map(&:message).
|
|
25
|
+
expect(ue.offences.map(&:message)).to be_empty
|
|
26
26
|
end
|
|
27
27
|
end
|
|
28
28
|
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe VariableInterpolation do
|
|
8
|
+
let(:vi) { VariableInterpolation.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for interpolated global variables' do
|
|
11
|
+
inspect_source(vi,
|
|
12
|
+
'file.rb',
|
|
13
|
+
['puts "this is a #$test"'])
|
|
14
|
+
expect(vi.offences.size).to eq(1)
|
|
15
|
+
expect(vi.offences.map(&:message))
|
|
16
|
+
.to eq(['Replace interpolated var $test with expression #{$test}.'])
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'registers an offence for interpolated instance variables' do
|
|
20
|
+
inspect_source(vi,
|
|
21
|
+
'file.rb',
|
|
22
|
+
['puts "this is a #@test"'])
|
|
23
|
+
expect(vi.offences.size).to eq(1)
|
|
24
|
+
expect(vi.offences.map(&:message))
|
|
25
|
+
.to eq(['Replace interpolated var @test with expression #{@test}.'])
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'registers an offence for interpolated class variables' do
|
|
29
|
+
inspect_source(vi,
|
|
30
|
+
'file.rb',
|
|
31
|
+
['puts "this is a #@@t"'])
|
|
32
|
+
expect(vi.offences.size).to eq(1)
|
|
33
|
+
expect(vi.offences.map(&:message))
|
|
34
|
+
.to eq(['Replace interpolated var @@t with expression #{@@t}.'])
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'does not register an offence for variables in expressions' do
|
|
38
|
+
inspect_source(vi,
|
|
39
|
+
'file.rb',
|
|
40
|
+
['puts "this is a #{@test}"'])
|
|
41
|
+
expect(vi.offences).to be_empty
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -5,21 +5,28 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe WhenThen do
|
|
8
|
-
let
|
|
8
|
+
let(:wt) { WhenThen.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for when x;' do
|
|
11
11
|
inspect_source(wt, 'file.rb', ['case a',
|
|
12
12
|
'when b; c',
|
|
13
13
|
'end'])
|
|
14
|
-
wt.offences.map(&:message).
|
|
15
|
-
['Never use "when x;". Use "when x then" instead.']
|
|
14
|
+
expect(wt.offences.map(&:message)).to eq(
|
|
15
|
+
['Never use "when x;". Use "when x then" instead.'])
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'misses semicolon but does not crash when there are no tokens' do
|
|
19
|
+
inspect_source(wt, 'file.rb', ['case a',
|
|
20
|
+
'when []; {}',
|
|
21
|
+
'end'])
|
|
22
|
+
expect(wt.offences.map(&:message)).to eq([])
|
|
16
23
|
end
|
|
17
24
|
|
|
18
25
|
it 'accepts when x then' do
|
|
19
26
|
inspect_source(wt, 'file.rb', ['case a',
|
|
20
27
|
'when b then c',
|
|
21
28
|
'end'])
|
|
22
|
-
wt.offences.map(&:message).
|
|
29
|
+
expect(wt.offences.map(&:message)).to be_empty
|
|
23
30
|
end
|
|
24
31
|
|
|
25
32
|
it 'accepts ; separating statements in the body of when' do
|
|
@@ -31,7 +38,7 @@ module Rubocop
|
|
|
31
38
|
'when f',
|
|
32
39
|
' g; h',
|
|
33
40
|
'end'])
|
|
34
|
-
wt.offences.map(&:message).
|
|
41
|
+
expect(wt.offences.map(&:message)).to be_empty
|
|
35
42
|
end
|
|
36
43
|
end
|
|
37
44
|
end
|
|
@@ -6,7 +6,7 @@ require 'stringio'
|
|
|
6
6
|
module Rubocop
|
|
7
7
|
module Report
|
|
8
8
|
describe EmacsStyle do
|
|
9
|
-
let
|
|
9
|
+
let(:emacs_style) { Rubocop::Report.create('test', :emacs_style) }
|
|
10
10
|
|
|
11
11
|
it 'displays parsable text' do
|
|
12
12
|
cop = Cop::Cop.new
|
|
@@ -17,8 +17,8 @@ module Rubocop
|
|
|
17
17
|
|
|
18
18
|
s = StringIO.new
|
|
19
19
|
emacs_style.display(s)
|
|
20
|
-
s.string.
|
|
21
|
-
|
|
20
|
+
expect(s.string).to eq ['test:1: C: message 1',
|
|
21
|
+
"test:11: F: message 2\n"].join("\n")
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
end
|
|
@@ -5,14 +5,14 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Report
|
|
7
7
|
describe Report do
|
|
8
|
-
let
|
|
8
|
+
let(:report) { Report.new('test') }
|
|
9
9
|
|
|
10
10
|
it 'initially has 0 entries' do
|
|
11
|
-
report.entries.size.
|
|
11
|
+
expect(report.entries.size).to be_zero
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
it 'initially has nothing to report' do
|
|
15
|
-
report.empty
|
|
15
|
+
expect(report.empty?).to be_true
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
it 'keeps track of offences' do
|
|
@@ -21,8 +21,8 @@ module Rubocop
|
|
|
21
21
|
|
|
22
22
|
report << cop
|
|
23
23
|
|
|
24
|
-
report.empty
|
|
25
|
-
report.entries.size.
|
|
24
|
+
expect(report.empty?).to be_false
|
|
25
|
+
expect(report.entries.size).to eq(1)
|
|
26
26
|
end
|
|
27
27
|
end
|
|
28
28
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -40,6 +40,12 @@ module ExitCodeMatchers
|
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
RSpec.configure do |config|
|
|
43
|
+
config.filter_run_excluding ruby: ->(v) { !RUBY_VERSION.start_with?(v.to_s) }
|
|
44
|
+
|
|
45
|
+
config.expect_with :rspec do |c|
|
|
46
|
+
c.syntax = :expect # disables `should`
|
|
47
|
+
end
|
|
48
|
+
|
|
43
49
|
config.include(ExitCodeMatchers)
|
|
44
50
|
end
|
|
45
51
|
|