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.
Files changed (139) hide show
  1. data/.rubocop.yml +176 -0
  2. data/.travis.yml +1 -0
  3. data/CHANGELOG.md +60 -0
  4. data/CONTRIBUTING.md +13 -6
  5. data/Gemfile +1 -13
  6. data/README.md +131 -6
  7. data/Rakefile +1 -15
  8. data/bin/rubocop +2 -1
  9. data/lib/rubocop/cli.rb +133 -21
  10. data/lib/rubocop/cop/alias.rb +18 -0
  11. data/lib/rubocop/cop/align_parameters.rb +5 -4
  12. data/lib/rubocop/cop/array_literal.rb +22 -0
  13. data/lib/rubocop/cop/ascii_identifiers_and_comments.rb +18 -0
  14. data/lib/rubocop/cop/avoid_class_vars.rb +20 -0
  15. data/lib/rubocop/cop/avoid_for.rb +17 -0
  16. data/lib/rubocop/cop/avoid_perl_backrefs.rb +20 -0
  17. data/lib/rubocop/cop/avoid_perlisms.rb +46 -0
  18. data/lib/rubocop/cop/blocks.rb +1 -1
  19. data/lib/rubocop/cop/brace_after_percent.rb +28 -0
  20. data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -0
  21. data/lib/rubocop/cop/collection_methods.rb +29 -0
  22. data/lib/rubocop/cop/cop.rb +16 -4
  23. data/lib/rubocop/cop/def_parentheses.rb +4 -1
  24. data/lib/rubocop/cop/encoding.rb +7 -5
  25. data/lib/rubocop/cop/end_of_line.rb +1 -3
  26. data/lib/rubocop/cop/ensure_return.rb +36 -0
  27. data/lib/rubocop/cop/favor_modifier.rb +82 -0
  28. data/lib/rubocop/cop/favor_percent_r.rb +19 -0
  29. data/lib/rubocop/cop/favor_sprintf.rb +37 -0
  30. data/lib/rubocop/cop/favor_unless_over_negated_if.rb +47 -0
  31. data/lib/rubocop/cop/grammar.rb +12 -8
  32. data/lib/rubocop/cop/handle_exceptions.rb +21 -0
  33. data/lib/rubocop/cop/hash_literal.rb +22 -0
  34. data/lib/rubocop/cop/indentation.rb +1 -1
  35. data/lib/rubocop/cop/line_length.rb +7 -3
  36. data/lib/rubocop/cop/method_and_variable_snake_case.rb +31 -0
  37. data/lib/rubocop/cop/method_length.rb +66 -0
  38. data/lib/rubocop/cop/new_lambda_literal.rb +17 -0
  39. data/lib/rubocop/cop/offence.rb +3 -1
  40. data/lib/rubocop/cop/op_method.rb +23 -0
  41. data/lib/rubocop/cop/parentheses_around_condition.rb +24 -0
  42. data/lib/rubocop/cop/percent_literals.rb +25 -0
  43. data/lib/rubocop/cop/percent_r.rb +19 -0
  44. data/lib/rubocop/cop/reduce_arguments.rb +67 -0
  45. data/lib/rubocop/cop/rescue_exception.rb +39 -0
  46. data/lib/rubocop/cop/rescue_modifier.rb +20 -0
  47. data/lib/rubocop/cop/semicolon.rb +18 -0
  48. data/lib/rubocop/cop/space_after_comma_etc.rb +2 -1
  49. data/lib/rubocop/cop/string_literals.rb +12 -0
  50. data/lib/rubocop/cop/surrounding_space.rb +48 -47
  51. data/lib/rubocop/cop/symbol_snake_case.rb +27 -0
  52. data/lib/rubocop/cop/syntax.rb +42 -0
  53. data/lib/rubocop/cop/tab.rb +1 -3
  54. data/lib/rubocop/cop/variable_interpolation.rb +20 -0
  55. data/lib/rubocop/cop/when_then.rb +3 -0
  56. data/lib/rubocop/report/emacs_style.rb +2 -2
  57. data/lib/rubocop/report/plain_text.rb +1 -1
  58. data/lib/rubocop/report/report.rb +5 -2
  59. data/lib/rubocop/version.rb +3 -1
  60. data/lib/rubocop.rb +37 -0
  61. data/rubocop.gemspec +36 -134
  62. data/spec/rubocop/cli_spec.rb +326 -48
  63. data/spec/rubocop/cops/alias_spec.rb +36 -0
  64. data/spec/rubocop/cops/align_parameters_spec.rb +43 -34
  65. data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
  66. data/spec/rubocop/cops/array_literal_spec.rb +29 -0
  67. data/spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb +38 -0
  68. data/spec/rubocop/cops/avoid_class_vars_spec.rb +20 -0
  69. data/spec/rubocop/cops/avoid_for_spec.rb +22 -0
  70. data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +18 -0
  71. data/spec/rubocop/cops/avoid_perlisms_spec.rb +44 -0
  72. data/spec/rubocop/cops/brace_after_percent_spec.rb +27 -0
  73. data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +34 -0
  74. data/spec/rubocop/cops/collection_methods_spec.rb +39 -0
  75. data/spec/rubocop/cops/cop_spec.rb +5 -5
  76. data/spec/rubocop/cops/def_with_parentheses_spec.rb +5 -5
  77. data/spec/rubocop/cops/def_without_parentheses_spec.rb +4 -4
  78. data/spec/rubocop/cops/empty_lines_spec.rb +8 -8
  79. data/spec/rubocop/cops/encoding_spec.rb +17 -11
  80. data/spec/rubocop/cops/end_of_line_spec.rb +5 -5
  81. data/spec/rubocop/cops/ensure_return_spec.rb +37 -0
  82. data/spec/rubocop/cops/favor_modifier_spec.rb +112 -0
  83. data/spec/rubocop/cops/favor_percent_r.rb +29 -0
  84. data/spec/rubocop/cops/favor_sprintf_spec.rb +57 -0
  85. data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +62 -0
  86. data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +45 -0
  87. data/spec/rubocop/cops/grammar_spec.rb +59 -44
  88. data/spec/rubocop/cops/handle_exceptions_spec.rb +36 -0
  89. data/spec/rubocop/cops/hash_literal_spec.rb +29 -0
  90. data/spec/rubocop/cops/hash_syntax_spec.rb +10 -10
  91. data/spec/rubocop/cops/if_with_semicolon_spec.rb +3 -3
  92. data/spec/rubocop/cops/indentation_spec.rb +7 -7
  93. data/spec/rubocop/cops/line_length_spec.rb +4 -4
  94. data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +47 -0
  95. data/spec/rubocop/cops/method_length_spec.rb +150 -0
  96. data/spec/rubocop/cops/multiline_blocks_spec.rb +4 -4
  97. data/spec/rubocop/cops/multiline_if_then_spec.rb +7 -7
  98. data/spec/rubocop/cops/new_lambda_literal_spec.rb +23 -0
  99. data/spec/rubocop/cops/numeric_literals_spec.rb +10 -10
  100. data/spec/rubocop/cops/offence_spec.rb +10 -4
  101. data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
  102. data/spec/rubocop/cops/op_method_spec.rb +58 -0
  103. data/spec/rubocop/cops/parameter_lists_spec.rb +4 -4
  104. data/spec/rubocop/cops/parentheses_around_condition_spec.rb +64 -0
  105. data/spec/rubocop/cops/percent_literals_spec.rb +47 -0
  106. data/spec/rubocop/cops/percent_r_spec.rb +29 -0
  107. data/spec/rubocop/cops/reduce_arguments_spec.rb +57 -0
  108. data/spec/rubocop/cops/rescue_exception_spec.rb +73 -0
  109. data/spec/rubocop/cops/rescue_modifier.rb +40 -0
  110. data/spec/rubocop/cops/semicolon_spec.rb +29 -0
  111. data/spec/rubocop/cops/single_line_blocks_spec.rb +4 -4
  112. data/spec/rubocop/cops/space_after_colon_spec.rb +5 -5
  113. data/spec/rubocop/cops/space_after_comma_spec.rb +3 -3
  114. data/spec/rubocop/cops/space_after_semicolon_spec.rb +3 -3
  115. data/spec/rubocop/cops/space_around_braces_spec.rb +6 -6
  116. data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +22 -0
  117. data/spec/rubocop/cops/space_around_operators_spec.rb +57 -25
  118. data/spec/rubocop/cops/space_inside_brackets_spec.rb +8 -8
  119. data/spec/rubocop/cops/space_inside_parens_spec.rb +4 -4
  120. data/spec/rubocop/cops/string_literals_spec.rb +9 -6
  121. data/spec/rubocop/cops/symbol_snake_case_spec.rb +74 -0
  122. data/spec/rubocop/cops/syntax_spec.rb +34 -0
  123. data/spec/rubocop/cops/tab_spec.rb +4 -4
  124. data/spec/rubocop/cops/ternary_operator_spec.rb +8 -8
  125. data/spec/rubocop/cops/trailing_whitespace_spec.rb +4 -4
  126. data/spec/rubocop/cops/unless_else_spec.rb +4 -4
  127. data/spec/rubocop/cops/variable_interpolation_spec.rb +45 -0
  128. data/spec/rubocop/cops/when_then_spec.rb +12 -5
  129. data/spec/rubocop/reports/emacs_style_spec.rb +3 -3
  130. data/spec/rubocop/reports/report_spec.rb +5 -5
  131. data/spec/spec_helper.rb +6 -0
  132. metadata +165 -36
  133. data/.rbenv-version +0 -1
  134. data/.rvmrc +0 -1
  135. data/Gemfile.lock +0 -39
  136. data/VERSION +0 -1
  137. data/features/rubocop.feature +0 -9
  138. data/features/step_definitions/rubocop_steps.rb +0 -1
  139. data/features/support/env.rb +0 -15
@@ -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
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe PercentLiterals do
8
+ let(:pl) { PercentLiterals.new }
9
+
10
+ it 'registers an offence for %q' do
11
+ inspect_source(pl,
12
+ 'file.rb',
13
+ ['puts %q(test)'])
14
+ expect(pl.offences.size).to eq(1)
15
+ expect(pl.offences.map(&:message))
16
+ .to eq(['The use of %q is discouraged.'])
17
+ end
18
+
19
+ it 'registers an offence for %Q' do
20
+ inspect_source(pl,
21
+ 'file.rb',
22
+ ['puts %Q(test)'])
23
+ expect(pl.offences.size).to eq(1)
24
+ expect(pl.offences.map(&:message))
25
+ .to eq(['The use of %Q is discouraged.'])
26
+ end
27
+
28
+ it 'registers an offence for %x' do
29
+ inspect_source(pl,
30
+ 'file.rb',
31
+ ['puts %x(test)'])
32
+ expect(pl.offences.size).to eq(1)
33
+ expect(pl.offences.map(&:message))
34
+ .to eq(['The use of %x is discouraged.'])
35
+ end
36
+
37
+ it 'registers an offence for %s' do
38
+ inspect_source(pl,
39
+ 'file.rb',
40
+ ['puts %s(test)'])
41
+ expect(pl.offences.size).to eq(1)
42
+ expect(pl.offences.map(&:message))
43
+ .to eq(['The use of %s is discouraged.'])
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe PercentR do
8
+ let(:apr) { PercentR.new }
9
+
10
+ it 'registers an offence for %r with zero or one slash in regexp' do
11
+ inspect_source(apr, 'file.rb', ['x =~ %r(/home)',
12
+ 'y =~ %r(etc)'])
13
+ expect(apr.offences.map(&:message))
14
+ .to eq([PercentR::ERROR_MESSAGE] * 2)
15
+ end
16
+
17
+ it 'accepts %r with at least two slashes in regexp' do
18
+ inspect_source(apr, 'file.rb', ['x =~ %r(/home/)',
19
+ 'y =~ %r(/////)'])
20
+ expect(apr.offences.map(&:message)).to be_empty
21
+ end
22
+
23
+ it 'accepts slash delimiters for regexp' do
24
+ inspect_source(apr, 'file.rb', ['x =~ /\/home/'])
25
+ expect(apr.offences.map(&:message)).to be_empty
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe ReduceArguments do
8
+ let(:reduce_arguments) { ReduceArguments.new }
9
+
10
+ it 'find wrong argument names in calls with different syntax' do
11
+ inspect_source(reduce_arguments, '',
12
+ ['def m',
13
+ ' [0, 1].reduce { |c, d| c + d }',
14
+ ' [0, 1].reduce{ |c, d| c + d }',
15
+ ' [0, 1].reduce(5) { |c, d| c + d }',
16
+ ' [0, 1].reduce(5){ |c, d| c + d }',
17
+ ' [0, 1].reduce (5) { |c, d| c + d }',
18
+ ' [0, 1].reduce(5) { |c, d| c + d }',
19
+ 'end'])
20
+ expect(reduce_arguments.offences.size).to eq(6)
21
+ expect(reduce_arguments.offences
22
+ .map(&:line_number).sort).to eq((2..7).to_a)
23
+ end
24
+
25
+ it 'allows calls with proper argument names' do
26
+ inspect_source(reduce_arguments, '',
27
+ ['def m',
28
+ ' [0, 1].reduce { |a, e| a + e }',
29
+ ' [0, 1].reduce{ |a, e| a + e }',
30
+ ' [0, 1].reduce(5) { |a, e| a + e }',
31
+ ' [0, 1].reduce(5){ |a, e| a + e }',
32
+ ' [0, 1].reduce (5) { |a, e| a + e }',
33
+ ' [0, 1].reduce(5) { |a, e| a + e }',
34
+ 'end'])
35
+ expect(reduce_arguments.offences).to be_empty
36
+ end
37
+
38
+ it 'ignores do..end blocks' do
39
+ inspect_source(reduce_arguments, '',
40
+ ['def m',
41
+ ' [0, 1].reduce do |c, d|',
42
+ ' c + d',
43
+ ' end',
44
+ 'end'])
45
+ expect(reduce_arguments.offences).to be_empty
46
+ end
47
+
48
+ it 'ignores :reduce symbols' do
49
+ inspect_source(reduce_arguments, '',
50
+ ['def m',
51
+ ' call_method(:reduce) { |a, b| a + b}',
52
+ 'end'])
53
+ expect(reduce_arguments.offences).to be_empty
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,73 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe RescueException do
8
+ let(:re) { RescueException.new }
9
+
10
+ it 'registers an offence for rescue from Exception' do
11
+ inspect_source(re,
12
+ 'file.rb',
13
+ ['begin',
14
+ ' something',
15
+ 'rescue Exception',
16
+ ' #do nothing',
17
+ 'end'])
18
+ expect(re.offences.size).to eq(1)
19
+ expect(re.offences.map(&:message))
20
+ .to eq([RescueException::ERROR_MESSAGE])
21
+ end
22
+
23
+ it 'registers an offence for rescue with Exception => e' do
24
+ inspect_source(re,
25
+ 'file.rb',
26
+ ['begin',
27
+ ' something',
28
+ 'rescue Exception => e',
29
+ ' #do nothing',
30
+ 'end'])
31
+ expect(re.offences.size).to eq(1)
32
+ expect(re.offences.map(&:message))
33
+ .to eq([RescueException::ERROR_MESSAGE])
34
+ end
35
+
36
+ it 'does not register an offence for rescue with other class' do
37
+ inspect_source(re,
38
+ 'file.rb',
39
+ ['begin',
40
+ ' something',
41
+ ' return',
42
+ 'rescue ArgumentError => e',
43
+ ' file.close',
44
+ 'end'])
45
+ expect(re.offences).to be_empty
46
+ end
47
+
48
+ it 'does not register an offence for rescue with other classes' do
49
+ inspect_source(re,
50
+ 'file.rb',
51
+ ['begin',
52
+ ' something',
53
+ ' return',
54
+ 'rescue EOFError, ArgumentError => e',
55
+ ' file.close',
56
+ 'end'])
57
+ expect(re.offences).to be_empty
58
+ end
59
+
60
+ it 'does not register an offence for rescue with a module prefix' do
61
+ inspect_source(re,
62
+ 'file.rb',
63
+ ['begin',
64
+ ' something',
65
+ ' return',
66
+ 'rescue Test::Exception => e',
67
+ ' file.close',
68
+ 'end'])
69
+ expect(re.offences).to be_empty
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe RescueModifier do
8
+ let(:rm) { RescueModifier.new }
9
+
10
+ it 'registers an offence for modifier rescue' do
11
+ inspect_source(rm,
12
+ 'file.rb',
13
+ ['method rescue handle'])
14
+ expect(rm.offences.size).to eq(1)
15
+ expect(rm.offences.map(&:message))
16
+ .to eq([RescueModifier::ERROR_MESSAGE])
17
+ end
18
+
19
+ it 'handles more complex expression with modifier rescue' do
20
+ inspect_source(rm,
21
+ 'file.rb',
22
+ ['method1 or method2 rescue handle'])
23
+ expect(rm.offences.size).to eq(1)
24
+ expect(rm.offences.map(&:message))
25
+ .to eq([RescueModifier::ERROR_MESSAGE])
26
+ end
27
+
28
+ it 'does not register an offence for normal rescue' do
29
+ inspect_source(rm,
30
+ 'file.rb',
31
+ ['begin',
32
+ 'test',
33
+ 'rescue',
34
+ 'handle',
35
+ 'end'])
36
+ expect(rm.offences).to be_empty
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe Semicolon do
8
+ let(:s) { Semicolon.new }
9
+
10
+ it 'registers an offence for a single expression' do
11
+ inspect_source(s,
12
+ 'file.rb',
13
+ ['puts "this is a test";'])
14
+ expect(s.offences.size).to eq(1)
15
+ expect(s.offences.map(&:message))
16
+ .to eq([Semicolon::ERROR_MESSAGE])
17
+ end
18
+
19
+ it 'registers an offence for several expressions' do
20
+ inspect_source(s,
21
+ 'file.rb',
22
+ ['puts "this is a test"; puts "So is this"'])
23
+ expect(s.offences.size).to eq(1)
24
+ expect(s.offences.map(&:message))
25
+ .to eq([Semicolon::ERROR_MESSAGE])
26
+ end
27
+ end
28
+ end
29
+ end
@@ -5,17 +5,17 @@ require 'spec_helper'
5
5
  module Rubocop
6
6
  module Cop
7
7
  describe SingleLineBlocks do
8
- let (:blocks) { SingleLineBlocks.new }
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).should ==
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).should == []
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 (:space) { SpaceAfterColon.new }
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).should ==
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).should == []
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).should == []
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 (:space) { SpaceAfterComma.new }
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).should ==
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 (:space) { SpaceAfterSemicolon.new }
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).should ==
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 (:space) { SpaceAroundBraces.new }
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).should ==
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).should ==
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).should == []
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,94 @@ require 'spec_helper'
5
5
  module Rubocop
6
6
  module Cop
7
7
  describe SpaceAroundOperators do
8
- let (:space) { SpaceAroundOperators.new }
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).should ==
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 for ternary operator without space' do
17
+ inspect_source(space, 'file.rb', ['x == 0?1:2'])
18
+ expect(space.offences.map(&:message)).to eq(
19
+ ["Surrounding space missing for operator '?'.",
20
+ "Surrounding space missing for operator ':'."])
21
+ end
22
+
23
+ it 'registers an offence in presence of modifier if statement' do
24
+ check_modifier('if')
25
+ end
26
+
27
+ it 'registers an offence in presence of modifier unless statement' do
28
+ check_modifier('unless')
29
+ end
30
+
31
+ it 'registers an offence in presence of modifier while statement' do
32
+ check_modifier('unless')
33
+ end
34
+
35
+ it 'registers an offence in presence of modifier until statement' do
36
+ check_modifier('unless')
37
+ end
38
+
39
+ def check_modifier(keyword)
40
+ src = ["a=1 #{keyword} condition",
41
+ 'c=2']
42
+ inspect_source(space, 'file.rb', src)
43
+ expect(space.offences.map(&:line_number)).to eq([1, 2])
44
+ expect(space.offences.map(&:message)).to eq(
45
+ ["Surrounding space missing for operator '='."] * 2)
14
46
  end
15
47
 
16
48
  it 'registers an offence for binary operators that could be unary' do
17
49
  inspect_source(space, 'file.rb', ['a-3', 'x&0xff', 'z+0'])
18
- space.offences.map(&:message).should ==
50
+ expect(space.offences.map(&:message)).to eq(
19
51
  ["Surrounding space missing for operator '-'.",
20
52
  "Surrounding space missing for operator '&'.",
21
- "Surrounding space missing for operator '+'."]
53
+ "Surrounding space missing for operator '+'."])
22
54
  end
23
55
 
24
56
  it 'registers an offence for arguments to a method' do
25
57
  inspect_source(space, 'file.rb', ['puts 1+2'])
26
- space.offences.map(&:message).should ==
27
- ["Surrounding space missing for operator '+'."]
58
+ expect(space.offences.map(&:message)).to eq(
59
+ ["Surrounding space missing for operator '+'."])
28
60
  end
29
61
 
30
62
  it 'accepts operator symbols' do
31
63
  inspect_source(space, 'file.rb', ['func(:-)'])
32
- space.offences.map(&:message).should == []
64
+ expect(space.offences.map(&:message)).to be_empty
33
65
  end
34
66
 
35
67
  it 'accepts ranges' do
36
68
  inspect_source(space, 'file.rb', ['a, b = (1..2), (1...3)'])
37
- space.offences.map(&:message).should == []
69
+ expect(space.offences.map(&:message)).to be_empty
38
70
  end
39
71
 
40
72
  it 'accepts scope operator' do
41
73
  source = ['@io.class == Zlib::GzipWriter']
42
74
  inspect_source(space, 'file.rb', source)
43
- space.offences.map(&:message).should == []
75
+ expect(space.offences.map(&:message)).to be_empty
44
76
  end
45
77
 
46
78
  it 'accepts ::Kernel::raise' do
47
79
  source = ['::Kernel::raise IllegalBlockError.new']
48
80
  inspect_source(space, 'file.rb', source)
49
- space.offences.map(&:message).should == []
81
+ expect(space.offences.map(&:message)).to be_empty
50
82
  end
51
83
 
52
84
  it 'accepts exclamation point negation' do
53
85
  inspect_source(space, 'file.rb', ['x = !a&&!b'])
54
- space.offences.map(&:message).should ==
55
- ["Surrounding space missing for operator '&&'."]
86
+ expect(space.offences.map(&:message)).to eq(
87
+ ["Surrounding space missing for operator '&&'."])
56
88
  end
57
89
 
58
90
  it 'accepts exclamation point definition' do
59
91
  inspect_source(space, 'file.rb', [' def !',
60
92
  ' !__getobj__',
61
93
  ' end'])
62
- space.offences.should == []
63
- space.offences.map(&:message).should == []
94
+ expect(space.offences).to be_empty
95
+ expect(space.offences.map(&:message)).to be_empty
64
96
  end
65
97
 
66
98
  it 'accepts a unary' do
@@ -70,52 +102,52 @@ module Rubocop
70
102
  ' *labels, &blk)',
71
103
  ' end',
72
104
  ''])
73
- space.offences.map(&:message).should == []
105
+ expect(space.offences.map(&:message)).to be_empty
74
106
  end
75
107
 
76
108
  it 'accepts splat operator' do
77
109
  inspect_source(space, 'file.rb', ['return *list if options'])
78
- space.offences.map(&:message).should == []
110
+ expect(space.offences.map(&:message)).to be_empty
79
111
  end
80
112
 
81
113
  it 'accepts def of operator' do
82
114
  inspect_source(space, 'file.rb', ['def +(other); end'])
83
- space.offences.map(&:message).should == []
115
+ expect(space.offences.map(&:message)).to be_empty
84
116
  end
85
117
 
86
118
  it 'accepts an assignment with spaces' do
87
119
  inspect_source(space, 'file.rb', ['x = 0'])
88
- space.offences.size.should == 0
120
+ expect(space.offences).to be_empty
89
121
  end
90
122
 
91
123
  it "accepts some operators that are exceptions and don't need spaces" do
92
124
  inspect_source(space, 'file.rb', ['(1..3)',
93
125
  'ActionController::Base',
94
126
  'each { |s, t| }'])
95
- space.offences.map(&:message).should == []
127
+ expect(space.offences.map(&:message)).to be_empty
96
128
  end
97
129
 
98
130
  it 'accepts an assignment followed by newline' do
99
131
  inspect_source(space, 'file.rb', ['x =\n 0'])
100
- space.offences.size.should == 0
132
+ expect(space.offences).to be_empty
101
133
  end
102
134
 
103
135
  it 'registers an offences for exponent operator with spaces' do
104
136
  inspect_source(space, 'file.rb', ['x = a * b ** 2'])
105
- space.offences.map(&:message).should ==
106
- ['Space around operator ** detected.']
137
+ expect(space.offences.map(&:message)).to eq(
138
+ ['Space around operator ** detected.'])
107
139
  end
108
140
 
109
141
  it 'accepts exponent operator without spaces' do
110
142
  inspect_source(space, 'file.rb', ['x = a * b**2'])
111
- space.offences.size.should == 0
143
+ expect(space.offences).to be_empty
112
144
  end
113
145
 
114
146
  it 'accepts unary operators without space' do
115
147
  inspect_source(space, 'file.rb', ['[].map(&:size)',
116
148
  '-3',
117
149
  'x = +2'])
118
- space.offences.map(&:message).should == []
150
+ expect(space.offences.map(&:message)).to be_empty
119
151
  end
120
152
  end
121
153
  end