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
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  module Rubocop
6
6
  module Cop
7
7
  describe EmptyLines do
8
- let (:empty_lines) { EmptyLines.new }
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.should == 2
25
- empty_lines.offences.map(&:line_number).sort.should == [7, 11]
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.size.should == 0
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.size.should == 0
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.size.should == 0
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.size.should == 0
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).should == []
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 (:encoding) { Encoding.new }
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).should ==
14
- ['Missing encoding comment.']
13
+ expect(encoding.offences.map(&:message)).to eq(
14
+ ['Missing utf-8 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.should == []
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).should == []
29
+ expect(encoding.offences.map(&:message)).to be_empty
30
30
  end
31
31
 
32
- it 'registers an offence when encoding is in the wrong place' do
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).should ==
37
- ['Missing encoding comment.']
36
+ expect(encoding.offences.map(&:message)).to eq(
37
+ ['Missing utf-8 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 (:eol) { EndOfLine.new }
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).should ==
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).should ==
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,37 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe EnsureReturn do
8
+ let(:er) { EnsureReturn.new }
9
+
10
+ it 'registers an offence for return in ensure' do
11
+ inspect_source(er,
12
+ 'file.rb',
13
+ ['begin',
14
+ ' something',
15
+ 'ensure',
16
+ ' file.close',
17
+ ' return',
18
+ 'end'])
19
+ expect(er.offences.size).to eq(1)
20
+ expect(er.offences.map(&:message))
21
+ .to eq([EnsureReturn::ERROR_MESSAGE])
22
+ end
23
+
24
+ it 'does not register an offence for return outside ensure' do
25
+ inspect_source(er,
26
+ 'file.rb',
27
+ ['begin',
28
+ ' something',
29
+ ' return',
30
+ 'ensure',
31
+ ' file.close',
32
+ 'end'])
33
+ expect(er.offences).to be_empty
34
+ end
35
+ end
36
+ end
37
+ 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,29 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe FavorPercentR do
8
+ let(:fpr) { FavorPercentR.new }
9
+
10
+ it 'registers an offence for // with two slashes in regexp' do
11
+ inspect_source(fpr, 'file.rb', ['x =~ /home\/\//',
12
+ 'y =~ /etc\/top\//'])
13
+ expect(fpr.offences.map(&:message))
14
+ .to eq([FavorPercentR::ERROR_MESSAGE] * 2)
15
+ end
16
+
17
+ it 'accepts // with only one slash in regexp' do
18
+ inspect_source(fpr, 'file.rb', ['x =~ /\/home/',
19
+ 'y =~ /\//)'])
20
+ expect(fpr.offences.map(&:message)).to be_empty
21
+ end
22
+
23
+ it 'accepts %r delimiters for regexp with two or more slashes' do
24
+ inspect_source(fpr, 'file.rb', ['x =~ %r(/home/)'])
25
+ expect(fpr.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 FavorSprintf do
8
+ let(:fs) { FavorSprintf.new }
9
+
10
+ it 'registers an offence for a string followed by something' do
11
+ inspect_source(fs,
12
+ 'file.rb',
13
+ ['puts "%d" % 10'])
14
+ expect(fs.offences.size).to eq(1)
15
+ expect(fs.offences.map(&:message))
16
+ .to eq([FavorSprintf::ERROR_MESSAGE])
17
+ end
18
+
19
+ it 'registers an offence for something followed by an array' do
20
+ inspect_source(fs,
21
+ 'file.rb',
22
+ ['puts x % [10, 11]'])
23
+ expect(fs.offences.size).to eq(1)
24
+ expect(fs.offences.map(&:message))
25
+ .to eq([FavorSprintf::ERROR_MESSAGE])
26
+ end
27
+
28
+ it 'does not register an offence for numbers' do
29
+ inspect_source(fs,
30
+ 'file.rb',
31
+ ['puts 10 % 4'])
32
+ expect(fs.offences).to be_empty
33
+ end
34
+
35
+ it 'does not register an offence for ambiguous cases' do
36
+ inspect_source(fs,
37
+ 'file.rb',
38
+ ['puts x % 4'])
39
+ expect(fs.offences).to be_empty
40
+
41
+ inspect_source(fs,
42
+ 'file.rb',
43
+ ['puts x % Y'])
44
+ expect(fs.offences).to be_empty
45
+ end
46
+
47
+ it 'works if the first operand contains embedded expressions' do
48
+ inspect_source(fs,
49
+ 'file.rb',
50
+ ['puts "#{x * 5} %d #{@test}" % 10'])
51
+ expect(fs.offences.size).to eq(1)
52
+ expect(fs.offences.map(&:message))
53
+ .to eq([FavorSprintf::ERROR_MESSAGE])
54
+ end
55
+ end
56
+ end
57
+ 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
@@ -5,61 +5,76 @@ require 'spec_helper'
5
5
  module Rubocop
6
6
  module Cop
7
7
  describe Grammar do
8
- EXAMPLE = '3.times { |i| x = "#{y}#{z}}" }'
8
+ EXAMPLE = '3.times { |i| x = "#{c ? y : a}#{z}}" }'
9
9
  tokens = Ripper.lex(EXAMPLE).map { |t| Token.new(*t) }
10
- let (:grammar) { Grammar.new(tokens) }
10
+ let(:grammar) { Grammar.new(tokens) }
11
11
 
12
12
  it 'correlates token indices to grammar paths' do
13
13
  method_block = [:program, :method_add_block]
14
14
  brace_block = method_block + [:brace_block]
15
- Ripper.lex(EXAMPLE).should ==
16
- [[[1, 0], :on_int, '3'],
17
- [[1, 1], :on_period, '.'],
18
- [[1, 2], :on_ident, 'times'],
19
- [[1, 7], :on_sp, ' '],
20
- [[1, 8], :on_lbrace, '{'],
21
- [[1, 9], :on_sp, ' '], # 5
22
- [[1, 10], :on_op, '|'],
23
- [[1, 11], :on_ident, 'i'],
24
- [[1, 12], :on_op, '|'],
25
- [[1, 13], :on_sp, ' '],
26
- [[1, 14], :on_ident, 'x'], # 10
27
- [[1, 15], :on_sp, ' '],
28
- [[1, 16], :on_op, '='],
29
- [[1, 17], :on_sp, ' '],
30
- [[1, 18], :on_tstring_beg, '"'],
31
- [[1, 19], :on_embexpr_beg, '#{'], # 15
32
- [[1, 21], :on_ident, 'y'],
33
- [[1, 22], :on_rbrace, '}'],
34
- [[1, 23], :on_embexpr_beg, '#{'],
35
- [[1, 25], :on_ident, 'z'],
36
- [[1, 26], :on_rbrace, '}'], # 20
37
- [[1, 27], :on_tstring_content, '}'],
38
- [[1, 28], :on_tstring_end, '"'],
39
- [[1, 29], :on_sp, ' '],
40
- [[1, 30], :on_rbrace, '}']]
15
+ string_embexpr = brace_block + [:assign, :string_literal,
16
+ :string_content,
17
+ :string_embexpr]
41
18
 
19
+ test_2_0 = [[[1, 0], :on_int, '3'],
20
+ [[1, 1], :on_period, '.'],
21
+ [[1, 2], :on_ident, 'times'],
22
+ [[1, 7], :on_sp, ' '],
23
+ [[1, 8], :on_lbrace, '{'],
24
+ [[1, 9], :on_sp, ' '], # 5
25
+ [[1, 10], :on_op, '|'],
26
+ [[1, 11], :on_ident, 'i'],
27
+ [[1, 12], :on_op, '|'],
28
+ [[1, 13], :on_sp, ' '],
29
+ [[1, 14], :on_ident, 'x'],
30
+ [[1, 15], :on_sp, ' '],
31
+ [[1, 16], :on_op, '='],
32
+ [[1, 17], :on_sp, ' '],
33
+ [[1, 18], :on_tstring_beg, '"'],
34
+ [[1, 19], :on_embexpr_beg, '#{'],
35
+ [[1, 21], :on_ident, 'c'],
36
+ [[1, 22], :on_sp, ' '],
37
+ [[1, 23], :on_op, '?'],
38
+ [[1, 24], :on_sp, ' '],
39
+ [[1, 25], :on_ident, 'y'],
40
+ [[1, 26], :on_sp, ' '],
41
+ [[1, 27], :on_op, ':'],
42
+ [[1, 28], :on_sp, ' '],
43
+ [[1, 29], :on_ident, 'a'],
44
+ [[1, 30], :on_embexpr_end, '}'],
45
+ [[1, 31], :on_embexpr_beg, '#{'],
46
+ [[1, 33], :on_ident, 'z'],
47
+ [[1, 34], :on_embexpr_end, '}'],
48
+ [[1, 35], :on_tstring_content, '}'],
49
+ [[1, 36], :on_tstring_end, '"'],
50
+ [[1, 37], :on_sp, ' '],
51
+ [[1, 38], :on_rbrace, '}']]
52
+ expect(Ripper.lex(EXAMPLE)).to eq(test_2_0) if RUBY_VERSION >= '2.0'
42
53
  sexp = Ripper.sexp(EXAMPLE)
43
54
  Position.make_position_objects(sexp)
44
55
 
45
56
  varref = (RUBY_VERSION == '1.9.2') ? :var_ref : :vcall
46
- grammar.correlate(sexp).should == {
47
- 0 => method_block + [:call, :@int], # 3
48
- 2 => method_block + [:call, :@ident], # times
49
- 4 => brace_block, # {
50
- 6 => brace_block + [:block_var], # |
51
- 7 => brace_block + [:block_var, :params, :@ident], # i
52
- 8 => brace_block + [:block_var], # |
53
- 10 => brace_block + [:assign, :var_field, :@ident], # x
54
- 12 => brace_block + [:assign], # =
55
- 16 => brace_block + [:assign, :string_literal, :string_content,
56
- :string_embexpr, varref, :@ident], # y
57
- 19 => brace_block + [:assign, :string_literal, :string_content,
58
- :string_embexpr, varref, :@ident], # z
59
- 21 => brace_block + [:assign, :string_literal, :string_content,
60
- :@tstring_content], # }
61
- 24 => brace_block, # }
57
+
58
+ test = {
59
+ 0 => method_block + [:call, :@int], # 3
60
+ 2 => method_block + [:call, :@ident], # times
61
+ 4 => brace_block, # {
62
+ 6 => brace_block + [:block_var], # |
63
+ 7 => brace_block + [:block_var, :params, :@ident], # i
64
+ 8 => brace_block + [:block_var], # |
65
+ 10 => brace_block + [:assign, :var_field, :@ident], # x
66
+ 12 => brace_block + [:assign], # =
67
+ 16 => string_embexpr + [:ifop, varref, :@ident], # c
68
+ 18 => string_embexpr + [:ifop], # ?
69
+ 20 => string_embexpr + [:ifop, varref, :@ident], # y
70
+ 22 => string_embexpr + [:ifop], # :
71
+ 24 => string_embexpr + [:ifop, varref, :@ident], # a
72
+ 27 => string_embexpr + [:vcall, :@ident], # z
73
+ 29 => brace_block + [:assign, :string_literal,
74
+ :string_content, :@tstring_content], # }
75
+ 32 => brace_block,
62
76
  }
77
+ expect(grammar.correlate(sexp)).to eq(test)
63
78
  end
64
79
  end
65
80
  end
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe HandleExceptions do
8
+ let(:he) { HandleExceptions.new }
9
+
10
+ it 'registers an offence for empty rescue block' do
11
+ inspect_source(he,
12
+ 'file.rb',
13
+ ['begin',
14
+ ' something',
15
+ 'rescue',
16
+ ' #do nothing',
17
+ 'end'])
18
+ expect(he.offences.size).to eq(1)
19
+ expect(he.offences.map(&:message))
20
+ .to eq([HandleExceptions::ERROR_MESSAGE])
21
+ end
22
+
23
+ it 'does not register an offence for rescue with body' do
24
+ inspect_source(he,
25
+ 'file.rb',
26
+ ['begin',
27
+ ' something',
28
+ ' return',
29
+ 'rescue',
30
+ ' file.close',
31
+ 'end'])
32
+ expect(he.offences).to be_empty
33
+ end
34
+ end
35
+ end
36
+ end