rubocop 0.4.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (190) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +50 -0
  3. data/.rubocop.yml +5 -127
  4. data/.travis.yml +7 -1
  5. data/CHANGELOG.md +157 -0
  6. data/CONTRIBUTING.md +13 -6
  7. data/Gemfile +3 -8
  8. data/README.md +160 -9
  9. data/Rakefile +3 -17
  10. data/bin/rubocop +16 -10
  11. data/config/default.yml +46 -0
  12. data/config/disabled.yml +5 -0
  13. data/config/enabled.yml +322 -0
  14. data/lib/rubocop/cli.rb +248 -93
  15. data/lib/rubocop/config.rb +205 -0
  16. data/lib/rubocop/config_store.rb +37 -0
  17. data/lib/rubocop/cop/access_control.rb +41 -0
  18. data/lib/rubocop/cop/alias.rb +17 -0
  19. data/lib/rubocop/cop/align_parameters.rb +20 -95
  20. data/lib/rubocop/cop/and_or.rb +26 -0
  21. data/lib/rubocop/cop/ascii_comments.rb +13 -0
  22. data/lib/rubocop/cop/ascii_identifiers.rb +19 -0
  23. data/lib/rubocop/cop/avoid_class_vars.rb +15 -0
  24. data/lib/rubocop/cop/avoid_for.rb +17 -0
  25. data/lib/rubocop/cop/avoid_global_vars.rb +61 -0
  26. data/lib/rubocop/cop/avoid_perl_backrefs.rb +17 -0
  27. data/lib/rubocop/cop/avoid_perlisms.rb +47 -0
  28. data/lib/rubocop/cop/block_comments.rb +15 -0
  29. data/lib/rubocop/cop/blocks.rb +11 -47
  30. data/lib/rubocop/cop/case_indentation.rb +22 -0
  31. data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -11
  32. data/lib/rubocop/cop/class_methods.rb +15 -0
  33. data/lib/rubocop/cop/collection_methods.rb +16 -16
  34. data/lib/rubocop/cop/colon_method_call.rb +20 -0
  35. data/lib/rubocop/cop/constant_name.rb +24 -0
  36. data/lib/rubocop/cop/cop.rb +34 -47
  37. data/lib/rubocop/cop/def_parentheses.rb +43 -35
  38. data/lib/rubocop/cop/empty_line_between_defs.rb +22 -0
  39. data/lib/rubocop/cop/empty_lines.rb +21 -13
  40. data/lib/rubocop/cop/empty_literal.rb +47 -0
  41. data/lib/rubocop/cop/encoding.rb +3 -3
  42. data/lib/rubocop/cop/end_of_line.rb +3 -3
  43. data/lib/rubocop/cop/ensure_return.rb +19 -0
  44. data/lib/rubocop/cop/eval.rb +19 -0
  45. data/lib/rubocop/cop/favor_join.rb +22 -0
  46. data/lib/rubocop/cop/favor_modifier.rb +38 -48
  47. data/lib/rubocop/cop/favor_percent_r.rb +19 -0
  48. data/lib/rubocop/cop/favor_sprintf.rb +21 -0
  49. data/lib/rubocop/cop/favor_unless_over_negated_if.rb +19 -17
  50. data/lib/rubocop/cop/handle_exceptions.rb +17 -0
  51. data/lib/rubocop/cop/hash_syntax.rb +29 -14
  52. data/lib/rubocop/cop/if_then_else.rb +32 -29
  53. data/lib/rubocop/cop/leading_comment_space.rb +17 -0
  54. data/lib/rubocop/cop/line_continuation.rb +15 -0
  55. data/lib/rubocop/cop/line_length.rb +4 -4
  56. data/lib/rubocop/cop/loop.rb +33 -0
  57. data/lib/rubocop/cop/method_and_variable_snake_case.rb +41 -17
  58. data/lib/rubocop/cop/method_length.rb +52 -0
  59. data/lib/rubocop/cop/new_lambda_literal.rb +8 -6
  60. data/lib/rubocop/cop/not.rb +21 -0
  61. data/lib/rubocop/cop/numeric_literals.rb +9 -7
  62. data/lib/rubocop/cop/offence.rb +12 -1
  63. data/lib/rubocop/cop/op_method.rb +26 -0
  64. data/lib/rubocop/cop/parameter_lists.rb +12 -6
  65. data/lib/rubocop/cop/parentheses_around_condition.rb +11 -11
  66. data/lib/rubocop/cop/percent_r.rb +19 -0
  67. data/lib/rubocop/cop/reduce_arguments.rb +29 -0
  68. data/lib/rubocop/cop/rescue_exception.rb +26 -0
  69. data/lib/rubocop/cop/rescue_modifier.rb +17 -0
  70. data/lib/rubocop/cop/semicolon.rb +31 -0
  71. data/lib/rubocop/cop/single_line_methods.rb +44 -0
  72. data/lib/rubocop/cop/space_after_comma_etc.rb +30 -10
  73. data/lib/rubocop/cop/space_after_control_keyword.rb +29 -0
  74. data/lib/rubocop/cop/string_literals.rb +9 -23
  75. data/lib/rubocop/cop/surrounding_space.rb +223 -83
  76. data/lib/rubocop/cop/symbol_array.rb +31 -0
  77. data/lib/rubocop/cop/symbol_name.rb +23 -0
  78. data/lib/rubocop/cop/syntax.rb +35 -5
  79. data/lib/rubocop/cop/tab.rb +3 -3
  80. data/lib/rubocop/cop/ternary_operator.rb +26 -24
  81. data/lib/rubocop/cop/trailing_whitespace.rb +3 -5
  82. data/lib/rubocop/cop/trivial_accessors.rb +26 -0
  83. data/lib/rubocop/cop/unless_else.rb +11 -7
  84. data/lib/rubocop/cop/util.rb +26 -0
  85. data/lib/rubocop/cop/variable_interpolation.rb +29 -0
  86. data/lib/rubocop/cop/when_then.rb +6 -14
  87. data/lib/rubocop/cop/word_array.rb +37 -0
  88. data/lib/rubocop/report/emacs_style.rb +2 -2
  89. data/lib/rubocop/report/plain_text.rb +1 -1
  90. data/lib/rubocop/version.rb +3 -1
  91. data/lib/rubocop.rb +48 -8
  92. data/rubocop.gemspec +32 -151
  93. data/spec/project_spec.rb +27 -0
  94. data/spec/rubocop/cli_spec.rb +573 -200
  95. data/spec/rubocop/config_spec.rb +409 -0
  96. data/spec/rubocop/config_store_spec.rb +66 -0
  97. data/spec/rubocop/cops/access_control_spec.rb +129 -0
  98. data/spec/rubocop/cops/alias_spec.rb +39 -0
  99. data/spec/rubocop/cops/align_parameters_spec.rb +66 -70
  100. data/spec/rubocop/cops/and_or_spec.rb +37 -0
  101. data/spec/rubocop/cops/ascii_comments_spec.rb +26 -0
  102. data/spec/rubocop/cops/ascii_identifiers_spec.rb +26 -0
  103. data/spec/rubocop/cops/avoid_class_vars_spec.rb +25 -0
  104. data/spec/rubocop/cops/avoid_for_spec.rb +35 -0
  105. data/spec/rubocop/cops/avoid_global_vars_spec.rb +32 -0
  106. data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +18 -0
  107. data/spec/rubocop/cops/avoid_perlisms_spec.rb +44 -0
  108. data/spec/rubocop/cops/block_comments_spec.rb +25 -0
  109. data/spec/rubocop/cops/blocks_spec.rb +33 -0
  110. data/spec/rubocop/cops/{indentation_spec.rb → case_indentation_spec.rb} +7 -7
  111. data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +15 -5
  112. data/spec/rubocop/cops/class_methods_spec.rb +49 -0
  113. data/spec/rubocop/cops/collection_methods_spec.rb +9 -4
  114. data/spec/rubocop/cops/colon_method_call_spec.rb +53 -0
  115. data/spec/rubocop/cops/constant_name_spec.rb +42 -0
  116. data/spec/rubocop/cops/def_with_parentheses_spec.rb +13 -8
  117. data/spec/rubocop/cops/def_without_parentheses_spec.rb +11 -5
  118. data/spec/rubocop/cops/empty_line_between_defs_spec.rb +83 -0
  119. data/spec/rubocop/cops/empty_lines_spec.rb +14 -59
  120. data/spec/rubocop/cops/empty_literal_spec.rb +90 -0
  121. data/spec/rubocop/cops/encoding_spec.rb +11 -11
  122. data/spec/rubocop/cops/end_of_line_spec.rb +2 -2
  123. data/spec/rubocop/cops/ensure_return_spec.rb +35 -0
  124. data/spec/rubocop/cops/eval_spec.rb +39 -0
  125. data/spec/rubocop/cops/favor_join_spec.rb +35 -0
  126. data/spec/rubocop/cops/favor_modifier_spec.rb +16 -14
  127. data/spec/rubocop/cops/favor_percent_r_spec.rb +29 -0
  128. data/spec/rubocop/cops/favor_sprintf_spec.rb +51 -0
  129. data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +4 -4
  130. data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +3 -3
  131. data/spec/rubocop/cops/handle_exceptions_spec.rb +34 -0
  132. data/spec/rubocop/cops/hash_syntax_spec.rb +11 -6
  133. data/spec/rubocop/cops/if_with_semicolon_spec.rb +7 -1
  134. data/spec/rubocop/cops/leading_comment_space_spec.rb +54 -0
  135. data/spec/rubocop/cops/line_continuation_spec.rb +24 -0
  136. data/spec/rubocop/cops/line_length_spec.rb +3 -2
  137. data/spec/rubocop/cops/loop_spec.rb +31 -0
  138. data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +55 -9
  139. data/spec/rubocop/cops/method_length_spec.rb +147 -0
  140. data/spec/rubocop/cops/multiline_if_then_spec.rb +15 -15
  141. data/spec/rubocop/cops/new_lambda_literal_spec.rb +5 -6
  142. data/spec/rubocop/cops/not_spec.rb +31 -0
  143. data/spec/rubocop/cops/numeric_literals_spec.rb +13 -13
  144. data/spec/rubocop/cops/offence_spec.rb +13 -0
  145. data/spec/rubocop/cops/one_line_conditional_spec.rb +1 -1
  146. data/spec/rubocop/cops/op_method_spec.rb +78 -0
  147. data/spec/rubocop/cops/parameter_lists_spec.rb +7 -7
  148. data/spec/rubocop/cops/parentheses_around_condition_spec.rb +41 -44
  149. data/spec/rubocop/cops/percent_r_spec.rb +29 -0
  150. data/spec/rubocop/cops/reduce_arguments_spec.rb +57 -0
  151. data/spec/rubocop/cops/rescue_exception_spec.rb +125 -0
  152. data/spec/rubocop/cops/rescue_modifier_spec.rb +37 -0
  153. data/spec/rubocop/cops/semicolon_spec.rb +88 -0
  154. data/spec/rubocop/cops/single_line_methods_spec.rb +50 -0
  155. data/spec/rubocop/cops/space_after_colon_spec.rb +3 -3
  156. data/spec/rubocop/cops/space_after_comma_spec.rb +14 -2
  157. data/spec/rubocop/cops/space_after_control_keyword_spec.rb +67 -0
  158. data/spec/rubocop/cops/space_after_semicolon_spec.rb +6 -1
  159. data/spec/rubocop/cops/space_around_braces_spec.rb +18 -3
  160. data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +12 -2
  161. data/spec/rubocop/cops/space_around_operators_spec.rb +88 -26
  162. data/spec/rubocop/cops/space_inside_brackets_spec.rb +13 -7
  163. data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +79 -0
  164. data/spec/rubocop/cops/space_inside_parens_spec.rb +7 -3
  165. data/spec/rubocop/cops/string_literals_spec.rb +21 -6
  166. data/spec/rubocop/cops/symbol_array_spec.rb +41 -0
  167. data/spec/rubocop/cops/symbol_name_spec.rb +119 -0
  168. data/spec/rubocop/cops/syntax_spec.rb +28 -5
  169. data/spec/rubocop/cops/tab_spec.rb +2 -2
  170. data/spec/rubocop/cops/ternary_operator_spec.rb +13 -17
  171. data/spec/rubocop/cops/trailing_whitespace_spec.rb +3 -3
  172. data/spec/rubocop/cops/trivial_accessors_spec.rb +329 -0
  173. data/spec/rubocop/cops/unless_else_spec.rb +8 -8
  174. data/spec/rubocop/cops/variable_interpolation_spec.rb +49 -0
  175. data/spec/rubocop/cops/when_then_spec.rb +14 -14
  176. data/spec/rubocop/cops/word_array_spec.rb +47 -0
  177. data/spec/spec_helper.rb +30 -9
  178. data/spec/support/file_helper.rb +21 -0
  179. data/spec/support/isolated_environment.rb +27 -0
  180. metadata +235 -76
  181. data/.document +0 -5
  182. data/Gemfile.lock +0 -41
  183. data/VERSION +0 -1
  184. data/lib/rubocop/cop/ampersands_pipes_vs_and_or.rb +0 -25
  185. data/lib/rubocop/cop/grammar.rb +0 -135
  186. data/lib/rubocop/cop/indentation.rb +0 -44
  187. data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +0 -57
  188. data/spec/rubocop/cops/grammar_spec.rb +0 -71
  189. data/spec/rubocop/cops/multiline_blocks_spec.rb +0 -24
  190. data/spec/rubocop/cops/single_line_blocks_spec.rb +0 -22
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe FavorJoin do
8
+ let(:fj) { FavorJoin.new }
9
+
10
+ it 'registers an offence for an array followed by string' do
11
+ inspect_source(fj,
12
+ ['%w(one two three) * ", "'])
13
+ expect(fj.offences.size).to eq(1)
14
+ expect(fj.offences.map(&:message))
15
+ .to eq([FavorJoin::MSG])
16
+ end
17
+
18
+ it 'does not register an offence for numbers' do
19
+ inspect_source(fj,
20
+ ['%w(one two three) * 4'])
21
+ expect(fj.offences).to be_empty
22
+ end
23
+
24
+ it 'does not register an offence for ambiguous cases' do
25
+ inspect_source(fj,
26
+ ['test * ", "'])
27
+ expect(fj.offences).to be_empty
28
+
29
+ inspect_source(fj,
30
+ ['%w(one two three) * test'])
31
+ expect(fj.offences).to be_empty
32
+ end
33
+ end
34
+ end
35
+ end
@@ -4,13 +4,14 @@ require 'spec_helper'
4
4
 
5
5
  module Rubocop
6
6
  module Cop
7
- describe FavorModifier do
7
+ describe FavorModifier, broken: true do
8
8
  let(:if_until) { IfUnlessModifier.new }
9
9
  let(:while_until) { WhileUntilModifier.new }
10
+ before { LineLength.config = { 'Max' => 79 } }
10
11
 
11
12
  it 'registers an offence for multiline if that fits on one line' do
12
13
  # This if statement fits exactly on one line if written as a modifier.
13
- inspect_source(if_until, 'file.rb',
14
+ inspect_source(if_until,
14
15
  ['if a_condition_that_is_just_short_enough',
15
16
  ' some_long_metod_name(followed_by_args)',
16
17
  'end'])
@@ -28,23 +29,24 @@ module Rubocop
28
29
  end
29
30
 
30
31
  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'])
32
+ inspect_source(if_until, ['unless a',
33
+ ' b',
34
+ 'end'])
34
35
  expect(if_until.offences.map(&:message)).to eq(
35
36
  ['Favor modifier if/unless usage when you have a single-line body.' +
36
37
  ' Another good alternative is the usage of control flow and/or.'])
37
38
  end
38
39
 
39
40
  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'])
41
+ pending
42
+ inspect_source(if_until, ['unless a',
43
+ ' b # A comment',
44
+ 'end'])
43
45
  expect(if_until.offences.map(&:message)).to be_empty
44
46
  end
45
47
 
46
48
  it 'accepts if-else-end' do
47
- inspect_source(if_until, 'file.rb',
49
+ inspect_source(if_until,
48
50
  ['if args.last.is_a? Hash then args.pop else ' +
49
51
  'Hash.new end'])
50
52
  expect(if_until.offences.map(&:message)).to be_empty
@@ -83,16 +85,16 @@ module Rubocop
83
85
  end
84
86
 
85
87
  def check_really_short(cop, keyword)
86
- inspect_source(cop, 'file.rb', ["#{keyword} a",
87
- ' b',
88
- 'end'])
88
+ inspect_source(cop, ["#{keyword} a",
89
+ ' b',
90
+ 'end'])
89
91
  expect(cop.offences.map(&:message)).to eq(
90
92
  ['Favor modifier while/until usage when you have a single-line ' +
91
93
  'body.'])
92
94
  end
93
95
 
94
96
  def check_too_long(cop, keyword)
95
- inspect_source(cop, 'file.rb',
97
+ inspect_source(cop,
96
98
  [" #{keyword} a_lengthy_condition_that_goes_on_and_on",
97
99
  ' some_long_metod_name(followed_by_args)',
98
100
  ' end'])
@@ -100,7 +102,7 @@ module Rubocop
100
102
  end
101
103
 
102
104
  def check_short_multiline(cop, keyword)
103
- inspect_source(cop, 'file.rb',
105
+ inspect_source(cop,
104
106
  ["#{keyword} ENV['COVERAGE']",
105
107
  " require 'simplecov'",
106
108
  ' SimpleCov.start',
@@ -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, ['x =~ /home\/\//',
12
+ 'y =~ /etc\/top\//'])
13
+ expect(fpr.offences.map(&:message))
14
+ .to eq([FavorPercentR::MSG] * 2)
15
+ end
16
+
17
+ it 'accepts // with only one slash in regexp' do
18
+ inspect_source(fpr, ['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, ['x =~ %r(/home/)'])
25
+ expect(fpr.offences.map(&:message)).to be_empty
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,51 @@
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
+ ['puts "%d" % 10'])
13
+ expect(fs.offences.size).to eq(1)
14
+ expect(fs.offences.map(&:message))
15
+ .to eq([FavorSprintf::MSG])
16
+ end
17
+
18
+ it 'registers an offence for something followed by an array' do
19
+ inspect_source(fs,
20
+ ['puts x % [10, 11]'])
21
+ expect(fs.offences.size).to eq(1)
22
+ expect(fs.offences.map(&:message))
23
+ .to eq([FavorSprintf::MSG])
24
+ end
25
+
26
+ it 'does not register an offence for numbers' do
27
+ inspect_source(fs,
28
+ ['puts 10 % 4'])
29
+ expect(fs.offences).to be_empty
30
+ end
31
+
32
+ it 'does not register an offence for ambiguous cases' do
33
+ inspect_source(fs,
34
+ ['puts x % 4'])
35
+ expect(fs.offences).to be_empty
36
+
37
+ inspect_source(fs,
38
+ ['puts x % Y'])
39
+ expect(fs.offences).to be_empty
40
+ end
41
+
42
+ it 'works if the first operand contains embedded expressions' do
43
+ inspect_source(fs,
44
+ ['puts "#{x * 5} %d #{@test}" % 10'])
45
+ expect(fs.offences.size).to eq(1)
46
+ expect(fs.offences.map(&:message))
47
+ .to eq([FavorSprintf::MSG])
48
+ end
49
+ end
50
+ end
51
+ end
@@ -8,7 +8,7 @@ module Rubocop
8
8
  let(:fav_unless) { FavorUnlessOverNegatedIf.new }
9
9
 
10
10
  it 'registers an offence for if with exclamation point condition' do
11
- inspect_source(fav_unless, 'file.rb',
11
+ inspect_source(fav_unless,
12
12
  ['if !a_condition',
13
13
  ' some_method',
14
14
  'end',
@@ -20,7 +20,7 @@ module Rubocop
20
20
  end
21
21
 
22
22
  it 'registers an offence for if with "not" condition' do
23
- inspect_source(fav_unless, 'file.rb',
23
+ inspect_source(fav_unless,
24
24
  ['if not a_condition',
25
25
  ' some_method',
26
26
  'end',
@@ -32,7 +32,7 @@ module Rubocop
32
32
  end
33
33
 
34
34
  it 'accepts an if/else with negative condition' do
35
- inspect_source(fav_unless, 'file.rb',
35
+ inspect_source(fav_unless,
36
36
  ['if !a_condition',
37
37
  ' some_method',
38
38
  'else',
@@ -47,7 +47,7 @@ module Rubocop
47
47
  end
48
48
 
49
49
  it 'accepts an if where only part of the contition is negated' do
50
- inspect_source(fav_unless, 'file.rb',
50
+ inspect_source(fav_unless,
51
51
  ['if !a_condition && another_condition',
52
52
  ' some_method',
53
53
  'end',
@@ -8,7 +8,7 @@ module Rubocop
8
8
  let(:fav_until) { FavorUntilOverNegatedWhile.new }
9
9
 
10
10
  it 'registers an offence for while with exclamation point condition' do
11
- inspect_source(fav_until, 'file.rb',
11
+ inspect_source(fav_until,
12
12
  ['while !a_condition',
13
13
  ' some_method',
14
14
  'end',
@@ -19,7 +19,7 @@ module Rubocop
19
19
  end
20
20
 
21
21
  it 'registers an offence for while with "not" condition' do
22
- inspect_source(fav_until, 'file.rb',
22
+ inspect_source(fav_until,
23
23
  ['while (not a_condition)',
24
24
  ' some_method',
25
25
  'end',
@@ -30,7 +30,7 @@ module Rubocop
30
30
  end
31
31
 
32
32
  it 'accepts an while where only part of the contition is negated' do
33
- inspect_source(fav_until, 'file.rb',
33
+ inspect_source(fav_until,
34
34
  ['while !a_condition && another_condition',
35
35
  ' some_method',
36
36
  'end',
@@ -0,0 +1,34 @@
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
+ ['begin',
13
+ ' something',
14
+ 'rescue',
15
+ ' #do nothing',
16
+ 'end'])
17
+ expect(he.offences.size).to eq(1)
18
+ expect(he.offences.map(&:message))
19
+ .to eq([HandleExceptions::MSG])
20
+ end
21
+
22
+ it 'does not register an offence for rescue with body' do
23
+ inspect_source(he,
24
+ ['begin',
25
+ ' something',
26
+ ' return',
27
+ 'rescue',
28
+ ' file.close',
29
+ 'end'])
30
+ expect(he.offences).to be_empty
31
+ end
32
+ end
33
+ end
34
+ end
@@ -8,35 +8,40 @@ module Rubocop
8
8
  let(:hash_syntax) { HashSyntax.new }
9
9
 
10
10
  it 'registers an offence for hash rocket syntax when new is possible' do
11
- inspect_source(hash_syntax, '', ['x = { :a => 0 }'])
11
+ inspect_source(hash_syntax, ['x = { :a => 0 }'])
12
12
  expect(hash_syntax.offences.map(&:message)).to eq(
13
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
- inspect_source(hash_syntax, '', ['x = { :a => 0, b: 1 }'])
17
+ inspect_source(hash_syntax, ['x = { :a => 0, b: 1 }'])
18
18
  expect(hash_syntax.offences.map(&:message)).to eq(
19
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
- inspect_source(hash_syntax, '', ['func(3, :a => 0)'])
23
+ inspect_source(hash_syntax, ['func(3, :a => 0)'])
24
24
  expect(hash_syntax.offences.map(&:message)).to eq(
25
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
- inspect_source(hash_syntax, '', ['x = { :a => 0, "b" => 1 }'])
29
+ inspect_source(hash_syntax, ['x = { :a => 0, "b" => 1 }'])
30
+ expect(hash_syntax.offences.map(&:message)).to be_empty
31
+ end
32
+
33
+ it 'accepts hash rockets when keys special symbols in them' do
34
+ inspect_source(hash_syntax, ['x = { :"t o" => 0, :"\xab" => 1 }'])
30
35
  expect(hash_syntax.offences.map(&:message)).to be_empty
31
36
  end
32
37
 
33
38
  it 'accepts new syntax in a hash literal' do
34
- inspect_source(hash_syntax, '', ['x = { a: 0, b: 1 }'])
39
+ inspect_source(hash_syntax, ['x = { a: 0, b: 1 }'])
35
40
  expect(hash_syntax.offences.map(&:message)).to be_empty
36
41
  end
37
42
 
38
43
  it 'accepts new syntax in method calls' do
39
- inspect_source(hash_syntax, '', ['func(3, a: 0)'])
44
+ inspect_source(hash_syntax, ['func(3, a: 0)'])
40
45
  expect(hash_syntax.offences.map(&:message)).to be_empty
41
46
  end
42
47
  end
@@ -8,10 +8,16 @@ module Rubocop
8
8
  let(:iws) { IfWithSemicolon.new }
9
9
 
10
10
  it 'registers an offence for one line if/;/end' do
11
- inspect_source(iws, '', ['if cond; run else dont end'])
11
+ inspect_source(iws, ['if cond; run else dont end'])
12
12
  expect(iws.offences.map(&:message)).to eq(
13
13
  ['Never use if x; Use the ternary operator instead.'])
14
14
  end
15
+
16
+ it 'can handle modifier conditionals' do
17
+ inspect_source(iws, ['class Hash',
18
+ 'end if RUBY_VERSION < "1.8.7"'])
19
+ expect(iws.offences.map(&:message)).to be_empty
20
+ end
15
21
  end
16
22
  end
17
23
  end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe LeadingCommentSpace do
8
+ let(:lcs) { LeadingCommentSpace.new }
9
+
10
+ it 'registers an offence for comment without leading space' do
11
+ inspect_source(lcs,
12
+ ['#missing space'])
13
+ expect(lcs.offences.size).to eq(1)
14
+ end
15
+
16
+ it 'does not register an offence for # followed by no text' do
17
+ inspect_source(lcs,
18
+ ['#'])
19
+ expect(lcs.offences).to be_empty
20
+ end
21
+
22
+ it 'does not register an offence for more than one space' do
23
+ inspect_source(lcs,
24
+ ['# heavily indented'])
25
+ expect(lcs.offences).to be_empty
26
+ end
27
+
28
+ it 'does not register an offence for more than one #' do
29
+ inspect_source(lcs,
30
+ ['###### heavily indented'])
31
+ expect(lcs.offences).to be_empty
32
+ end
33
+
34
+ it 'does not register an offence for only #s' do
35
+ inspect_source(lcs,
36
+ ['######'])
37
+ expect(lcs.offences).to be_empty
38
+ end
39
+
40
+ it 'does not register an offence for #! on first line' do
41
+ inspect_source(lcs,
42
+ ['#!/usr/bin/ruby',
43
+ 'test'])
44
+ expect(lcs.offences).to be_empty
45
+ end
46
+
47
+ it 'registers an offence for #! after the first line' do
48
+ inspect_source(lcs,
49
+ ['test', '#!/usr/bin/ruby'])
50
+ expect(lcs.offences.size).to eq(1)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe LineContinuation do
8
+ let(:lc) { LineContinuation.new }
9
+
10
+ it 'registers an offence for line continuation char' do
11
+ inspect_source(lc,
12
+ ['test = 5 \\', '+ 5'])
13
+ expect(lc.offences.size).to eq(1)
14
+ end
15
+
16
+ it 'does not register an offence for cont char in a string' do
17
+ inspect_source(lc,
18
+ ['result = "test string\\\n"',
19
+ 'more'])
20
+ expect(lc.offences).to be_empty
21
+ end
22
+ end
23
+ end
24
+ end
@@ -6,15 +6,16 @@ module Rubocop
6
6
  module Cop
7
7
  describe LineLength do
8
8
  let(:ll) { LineLength.new }
9
+ before { LineLength.config = { 'Max' => 79 } }
9
10
 
10
11
  it "registers an offence for a line that's 80 characters wide" do
11
- ll.inspect('file.rb', ['#' * 80], nil, nil)
12
+ ll.inspect(['#' * 80], nil, nil, nil)
12
13
  expect(ll.offences.size).to eq(1)
13
14
  expect(ll.offences.first.message).to eq('Line is too long. [80/79]')
14
15
  end
15
16
 
16
17
  it "accepts a line that's 79 characters wide" do
17
- ll.inspect('file.rb', ['#' * 79], nil, nil)
18
+ ll.inspect(['#' * 79], nil, nil, nil)
18
19
  expect(ll.offences).to be_empty
19
20
  end
20
21
  end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe Loop do
8
+ let(:loop) { Loop.new }
9
+
10
+ it 'registers an offence for begin/end/while' do
11
+ inspect_source(loop, ['begin something; top; end while test'])
12
+ expect(loop.offences.size).to eq(1)
13
+ end
14
+
15
+ it 'registers an offence for begin/end/until' do
16
+ inspect_source(loop, ['begin something; top; end until test'])
17
+ expect(loop.offences.size).to eq(1)
18
+ end
19
+
20
+ it 'accepts normal while' do
21
+ inspect_source(loop, ['while test; one; two; end'])
22
+ expect(loop.offences).to be_empty
23
+ end
24
+
25
+ it 'accepts normal until' do
26
+ inspect_source(loop, ['until test; one; two; end'])
27
+ expect(loop.offences).to be_empty
28
+ end
29
+ end
30
+ end
31
+ end
@@ -7,20 +7,55 @@ module Rubocop
7
7
  describe MethodAndVariableSnakeCase do
8
8
  let(:snake_case) { MethodAndVariableSnakeCase.new }
9
9
 
10
- it 'registers an offence for camel case in names' do
11
- inspect_source(snake_case, 'file.rb',
10
+ it 'registers an offence for camel case in instance method name' do
11
+ inspect_source(snake_case,
12
12
  ['def myMethod',
13
- ' myLocal = 1',
14
- ' self.mySetter = 2',
15
- ' @myAttribute = 3',
13
+ ' # ...',
16
14
  'end',
17
15
  ])
18
16
  expect(snake_case.offences.map(&:message)).to eq(
19
- ['Use snake_case for methods and variables.'] * 4)
17
+ ['Use snake_case for methods and variables.'])
18
+ end
19
+
20
+ it 'registers an offence for camel case in singleton method name' do
21
+ inspect_source(snake_case,
22
+ ['def self.myMethod',
23
+ ' # ...',
24
+ 'end',
25
+ ])
26
+ expect(snake_case.offences.map(&:message)).to eq(
27
+ ['Use snake_case for methods and variables.'])
28
+ end
29
+
30
+ it 'registers an offence for camel case in local variable name' do
31
+ inspect_source(snake_case, ['myLocal = 1'])
32
+ expect(snake_case.offences.map(&:message)).to eq(
33
+ ['Use snake_case for methods and variables.'])
34
+ end
35
+
36
+ it 'registers an offence for camel case in instance variable name' do
37
+ inspect_source(snake_case, ['@myAttribute = 3'])
38
+ expect(snake_case.offences.map(&:message)).to eq(
39
+ ['Use snake_case for methods and variables.'])
40
+ end
41
+
42
+ it 'registers an offence for camel case in setter name' do
43
+ inspect_source(snake_case, ['self.mySetter = 2'])
44
+ expect(snake_case.offences.map(&:message)).to eq(
45
+ ['Use snake_case for methods and variables.'])
46
+ end
47
+
48
+ it 'registers an offence for capitalized camel case' do
49
+ inspect_source(snake_case,
50
+ ['def MyMethod',
51
+ 'end',
52
+ ])
53
+ expect(snake_case.offences.map(&:message)).to eq(
54
+ ['Use snake_case for methods and variables.'])
20
55
  end
21
56
 
22
57
  it 'accepts snake case in names' do
23
- inspect_source(snake_case, 'file.rb',
58
+ inspect_source(snake_case,
24
59
  ['def my_method',
25
60
  ' my_local_html = 1',
26
61
  ' self.my_setter = 2',
@@ -31,7 +66,7 @@ module Rubocop
31
66
  end
32
67
 
33
68
  it 'registers an offence for mixed snake case and camel case' do
34
- inspect_source(snake_case, 'file.rb',
69
+ inspect_source(snake_case,
35
70
  ['def visit_Arel_Nodes_SelectStatement',
36
71
  'end'])
37
72
  expect(snake_case.offences.map(&:message)).to eq(
@@ -39,7 +74,18 @@ module Rubocop
39
74
  end
40
75
 
41
76
  it 'accepts screaming snake case globals' do
42
- inspect_source(snake_case, 'file.rb', ['$MY_GLOBAL = 0'])
77
+ inspect_source(snake_case, ['$MY_GLOBAL = 0'])
78
+ expect(snake_case.offences.map(&:message)).to be_empty
79
+ end
80
+
81
+ it 'accepts screaming snake case constants' do
82
+ inspect_source(snake_case, ['MY_CONSTANT = 0'])
83
+ expect(snake_case.offences.map(&:message)).to be_empty
84
+ end
85
+
86
+ it 'accepts assigning to camel case constant' do
87
+ inspect_source(snake_case,
88
+ ['Paren = Struct.new :left, :right, :kind'])
43
89
  expect(snake_case.offences.map(&:message)).to be_empty
44
90
  end
45
91
  end