rubocop 0.7.2 → 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 (184) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +7 -1
  3. data/CHANGELOG.md +19 -0
  4. data/README.md +4 -8
  5. data/bin/rubocop +2 -2
  6. data/config/default.yml +8 -0
  7. data/config/enabled.yml +21 -24
  8. data/lib/rubocop.rb +9 -7
  9. data/lib/rubocop/cli.rb +73 -52
  10. data/lib/rubocop/config.rb +8 -5
  11. data/lib/rubocop/cop/access_control.rb +41 -0
  12. data/lib/rubocop/cop/alias.rb +7 -5
  13. data/lib/rubocop/cop/align_parameters.rb +20 -96
  14. data/lib/rubocop/cop/and_or.rb +26 -0
  15. data/lib/rubocop/cop/ascii_comments.rb +3 -8
  16. data/lib/rubocop/cop/ascii_identifiers.rb +6 -5
  17. data/lib/rubocop/cop/avoid_class_vars.rb +5 -10
  18. data/lib/rubocop/cop/avoid_for.rb +7 -5
  19. data/lib/rubocop/cop/avoid_global_vars.rb +19 -7
  20. data/lib/rubocop/cop/avoid_perl_backrefs.rb +7 -10
  21. data/lib/rubocop/cop/avoid_perlisms.rb +11 -10
  22. data/lib/rubocop/cop/block_comments.rb +4 -6
  23. data/lib/rubocop/cop/blocks.rb +11 -47
  24. data/lib/rubocop/cop/case_indentation.rb +9 -31
  25. data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -11
  26. data/lib/rubocop/cop/class_methods.rb +5 -10
  27. data/lib/rubocop/cop/collection_methods.rb +16 -16
  28. data/lib/rubocop/cop/colon_method_call.rb +8 -32
  29. data/lib/rubocop/cop/constant_name.rb +24 -0
  30. data/lib/rubocop/cop/cop.rb +20 -78
  31. data/lib/rubocop/cop/def_parentheses.rb +43 -35
  32. data/lib/rubocop/cop/empty_line_between_defs.rb +11 -15
  33. data/lib/rubocop/cop/empty_lines.rb +20 -9
  34. data/lib/rubocop/cop/empty_literal.rb +47 -0
  35. data/lib/rubocop/cop/encoding.rb +3 -3
  36. data/lib/rubocop/cop/end_of_line.rb +3 -3
  37. data/lib/rubocop/cop/ensure_return.rb +6 -23
  38. data/lib/rubocop/cop/eval.rb +7 -10
  39. data/lib/rubocop/cop/favor_join.rb +9 -24
  40. data/lib/rubocop/cop/favor_modifier.rb +38 -48
  41. data/lib/rubocop/cop/favor_percent_r.rb +7 -7
  42. data/lib/rubocop/cop/favor_sprintf.rb +8 -24
  43. data/lib/rubocop/cop/favor_unless_over_negated_if.rb +19 -17
  44. data/lib/rubocop/cop/handle_exceptions.rb +7 -11
  45. data/lib/rubocop/cop/hash_syntax.rb +29 -14
  46. data/lib/rubocop/cop/if_then_else.rb +32 -29
  47. data/lib/rubocop/cop/leading_comment_space.rb +5 -8
  48. data/lib/rubocop/cop/line_continuation.rb +4 -7
  49. data/lib/rubocop/cop/line_length.rb +3 -3
  50. data/lib/rubocop/cop/loop.rb +33 -0
  51. data/lib/rubocop/cop/method_and_variable_snake_case.rb +42 -19
  52. data/lib/rubocop/cop/method_length.rb +34 -37
  53. data/lib/rubocop/cop/new_lambda_literal.rb +8 -6
  54. data/lib/rubocop/cop/not.rb +10 -4
  55. data/lib/rubocop/cop/numeric_literals.rb +9 -7
  56. data/lib/rubocop/cop/offence.rb +1 -1
  57. data/lib/rubocop/cop/op_method.rb +12 -22
  58. data/lib/rubocop/cop/parameter_lists.rb +12 -6
  59. data/lib/rubocop/cop/parentheses_around_condition.rb +11 -11
  60. data/lib/rubocop/cop/percent_r.rb +7 -7
  61. data/lib/rubocop/cop/reduce_arguments.rb +13 -51
  62. data/lib/rubocop/cop/rescue_exception.rb +13 -29
  63. data/lib/rubocop/cop/rescue_modifier.rb +5 -8
  64. data/lib/rubocop/cop/semicolon.rb +15 -74
  65. data/lib/rubocop/cop/single_line_methods.rb +28 -44
  66. data/lib/rubocop/cop/space_after_comma_etc.rb +29 -9
  67. data/lib/rubocop/cop/space_after_control_keyword.rb +16 -15
  68. data/lib/rubocop/cop/string_literals.rb +9 -35
  69. data/lib/rubocop/cop/surrounding_space.rb +213 -112
  70. data/lib/rubocop/cop/symbol_array.rb +9 -7
  71. data/lib/rubocop/cop/symbol_name.rb +23 -0
  72. data/lib/rubocop/cop/syntax.rb +14 -7
  73. data/lib/rubocop/cop/tab.rb +3 -3
  74. data/lib/rubocop/cop/ternary_operator.rb +26 -24
  75. data/lib/rubocop/cop/trailing_whitespace.rb +3 -5
  76. data/lib/rubocop/cop/trivial_accessors.rb +18 -95
  77. data/lib/rubocop/cop/unless_else.rb +11 -7
  78. data/lib/rubocop/cop/util.rb +26 -0
  79. data/lib/rubocop/cop/variable_interpolation.rb +18 -10
  80. data/lib/rubocop/cop/when_then.rb +6 -17
  81. data/lib/rubocop/cop/word_array.rb +18 -19
  82. data/lib/rubocop/version.rb +1 -1
  83. data/rubocop.gemspec +1 -0
  84. data/spec/project_spec.rb +1 -1
  85. data/spec/rubocop/cli_spec.rb +16 -9
  86. data/spec/rubocop/config_spec.rb +13 -3
  87. data/spec/rubocop/cops/access_control_spec.rb +129 -0
  88. data/spec/rubocop/cops/alias_spec.rb +2 -6
  89. data/spec/rubocop/cops/align_parameters_spec.rb +58 -71
  90. data/spec/rubocop/cops/and_or_spec.rb +37 -0
  91. data/spec/rubocop/cops/ascii_comments_spec.rb +3 -4
  92. data/spec/rubocop/cops/ascii_identifiers_spec.rb +3 -4
  93. data/spec/rubocop/cops/avoid_class_vars_spec.rb +7 -2
  94. data/spec/rubocop/cops/avoid_for_spec.rb +1 -4
  95. data/spec/rubocop/cops/{avoid_global_vars.rb → avoid_global_vars_spec.rb} +4 -4
  96. data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +1 -1
  97. data/spec/rubocop/cops/avoid_perlisms_spec.rb +5 -5
  98. data/spec/rubocop/cops/block_comments_spec.rb +0 -4
  99. data/spec/rubocop/cops/blocks_spec.rb +33 -0
  100. data/spec/rubocop/cops/case_indentation_spec.rb +5 -5
  101. data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +15 -5
  102. data/spec/rubocop/cops/class_methods_spec.rb +4 -4
  103. data/spec/rubocop/cops/collection_methods_spec.rb +9 -4
  104. data/spec/rubocop/cops/colon_method_call_spec.rb +11 -5
  105. data/spec/rubocop/cops/constant_name_spec.rb +42 -0
  106. data/spec/rubocop/cops/def_with_parentheses_spec.rb +13 -8
  107. data/spec/rubocop/cops/def_without_parentheses_spec.rb +11 -5
  108. data/spec/rubocop/cops/empty_line_between_defs_spec.rb +38 -38
  109. data/spec/rubocop/cops/empty_lines_spec.rb +15 -3
  110. data/spec/rubocop/cops/empty_literal_spec.rb +90 -0
  111. data/spec/rubocop/cops/encoding_spec.rb +9 -9
  112. data/spec/rubocop/cops/end_of_line_spec.rb +2 -2
  113. data/spec/rubocop/cops/ensure_return_spec.rb +1 -3
  114. data/spec/rubocop/cops/eval_spec.rb +8 -5
  115. data/spec/rubocop/cops/favor_join_spec.rb +1 -5
  116. data/spec/rubocop/cops/favor_modifier_spec.rb +16 -14
  117. data/spec/rubocop/cops/{favor_percent_r.rb → favor_percent_r_spec.rb} +6 -6
  118. data/spec/rubocop/cops/favor_sprintf_spec.rb +3 -9
  119. data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +4 -4
  120. data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +3 -3
  121. data/spec/rubocop/cops/handle_exceptions_spec.rb +1 -3
  122. data/spec/rubocop/cops/hash_syntax_spec.rb +11 -6
  123. data/spec/rubocop/cops/if_with_semicolon_spec.rb +7 -1
  124. data/spec/rubocop/cops/leading_comment_space_spec.rb +0 -7
  125. data/spec/rubocop/cops/line_continuation_spec.rb +2 -2
  126. data/spec/rubocop/cops/line_length_spec.rb +2 -2
  127. data/spec/rubocop/cops/loop_spec.rb +31 -0
  128. data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +38 -12
  129. data/spec/rubocop/cops/method_length_spec.rb +85 -85
  130. data/spec/rubocop/cops/multiline_if_then_spec.rb +15 -15
  131. data/spec/rubocop/cops/new_lambda_literal_spec.rb +3 -3
  132. data/spec/rubocop/cops/not_spec.rb +1 -4
  133. data/spec/rubocop/cops/numeric_literals_spec.rb +13 -13
  134. data/spec/rubocop/cops/one_line_conditional_spec.rb +1 -1
  135. data/spec/rubocop/cops/op_method_spec.rb +2 -9
  136. data/spec/rubocop/cops/parameter_lists_spec.rb +7 -7
  137. data/spec/rubocop/cops/parentheses_around_condition_spec.rb +41 -44
  138. data/spec/rubocop/cops/percent_r_spec.rb +6 -6
  139. data/spec/rubocop/cops/reduce_arguments_spec.rb +4 -4
  140. data/spec/rubocop/cops/rescue_exception_spec.rb +48 -8
  141. data/spec/rubocop/cops/rescue_modifier_spec.rb +2 -5
  142. data/spec/rubocop/cops/semicolon_spec.rb +2 -30
  143. data/spec/rubocop/cops/single_line_methods_spec.rb +13 -13
  144. data/spec/rubocop/cops/space_after_colon_spec.rb +3 -3
  145. data/spec/rubocop/cops/space_after_comma_spec.rb +14 -2
  146. data/spec/rubocop/cops/space_after_control_keyword_spec.rb +42 -3
  147. data/spec/rubocop/cops/space_after_semicolon_spec.rb +2 -2
  148. data/spec/rubocop/cops/space_around_braces_spec.rb +18 -3
  149. data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +4 -4
  150. data/spec/rubocop/cops/space_around_operators_spec.rb +82 -27
  151. data/spec/rubocop/cops/space_inside_brackets_spec.rb +13 -7
  152. data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +14 -9
  153. data/spec/rubocop/cops/space_inside_parens_spec.rb +7 -3
  154. data/spec/rubocop/cops/string_literals_spec.rb +17 -5
  155. data/spec/rubocop/cops/symbol_array_spec.rb +18 -2
  156. data/spec/rubocop/cops/symbol_name_spec.rb +119 -0
  157. data/spec/rubocop/cops/syntax_spec.rb +25 -18
  158. data/spec/rubocop/cops/tab_spec.rb +2 -2
  159. data/spec/rubocop/cops/ternary_operator_spec.rb +13 -17
  160. data/spec/rubocop/cops/trailing_whitespace_spec.rb +3 -3
  161. data/spec/rubocop/cops/trivial_accessors_spec.rb +17 -20
  162. data/spec/rubocop/cops/unless_else_spec.rb +8 -8
  163. data/spec/rubocop/cops/variable_interpolation_spec.rb +0 -5
  164. data/spec/rubocop/cops/when_then_spec.rb +14 -21
  165. data/spec/rubocop/cops/word_array_spec.rb +12 -4
  166. data/spec/spec_helper.rb +12 -4
  167. metadata +40 -31
  168. data/.document +0 -5
  169. data/lib/rubocop/cop/ampersands_pipes_vs_and_or.rb +0 -25
  170. data/lib/rubocop/cop/array_literal.rb +0 -61
  171. data/lib/rubocop/cop/brace_after_percent.rb +0 -32
  172. data/lib/rubocop/cop/grammar.rb +0 -138
  173. data/lib/rubocop/cop/hash_literal.rb +0 -61
  174. data/lib/rubocop/cop/percent_literals.rb +0 -25
  175. data/lib/rubocop/cop/symbol_snake_case.rb +0 -47
  176. data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +0 -57
  177. data/spec/rubocop/cops/array_literal_spec.rb +0 -46
  178. data/spec/rubocop/cops/brace_after_percent_spec.rb +0 -33
  179. data/spec/rubocop/cops/grammar_spec.rb +0 -81
  180. data/spec/rubocop/cops/hash_literal_spec.rb +0 -46
  181. data/spec/rubocop/cops/multiline_blocks_spec.rb +0 -24
  182. data/spec/rubocop/cops/percent_literals_spec.rb +0 -47
  183. data/spec/rubocop/cops/single_line_blocks_spec.rb +0 -22
  184. data/spec/rubocop/cops/symbol_snake_case_spec.rb +0 -93
@@ -8,33 +8,39 @@ module Rubocop
8
8
  let(:space) { SpaceInsideBrackets.new }
9
9
 
10
10
  it 'registers an offence for an array literal with spaces inside' do
11
- inspect_source(space, 'file.rb', ['a = [1, 2 ]',
11
+ inspect_source(space, ['a = [1, 2 ]',
12
12
  'b = [ 1, 2]'])
13
13
  expect(space.offences.map(&:message)).to eq(
14
14
  ['Space inside square brackets detected.',
15
15
  'Space inside square brackets detected.'])
16
16
  end
17
17
 
18
+ it 'accepts space inside strings within square brackets' do
19
+ inspect_source(space, ["['Encoding:',",
20
+ " ' Enabled: false']"])
21
+ expect(space.offences.map(&:message)).to be_empty
22
+ end
23
+
18
24
  it 'accepts space inside square brackets if on its own row' do
19
- inspect_source(space, 'file.rb', ['a = [',
20
- ' 1, 2',
21
- ' ]'])
25
+ inspect_source(space, ['a = [',
26
+ ' 1, 2',
27
+ ' ]'])
22
28
  expect(space.offences.map(&:message)).to be_empty
23
29
  end
24
30
 
25
31
  it 'accepts square brackets as method name' do
26
- inspect_source(space, 'file.rb', ['def Vector.[](*array)',
32
+ inspect_source(space, ['def Vector.[](*array)',
27
33
  'end'])
28
34
  expect(space.offences.map(&:message)).to be_empty
29
35
  end
30
36
 
31
37
  it 'accepts square brackets called with method call syntax' do
32
- inspect_source(space, 'file.rb', ['subject.[](0)'])
38
+ inspect_source(space, ['subject.[](0)'])
33
39
  expect(space.offences.map(&:message)).to be_empty
34
40
  end
35
41
 
36
42
  it 'only reports a single space once' do
37
- inspect_source(space, 'file.rb', ['[ ]'])
43
+ inspect_source(space, ['[ ]'])
38
44
  expect(space.offences.map(&:message)).to eq(
39
45
  ['Space inside square brackets detected.'])
40
46
  end
@@ -13,15 +13,15 @@ module Rubocop
13
13
  end
14
14
 
15
15
  it 'registers an offence for hashes with no spaces by default' do
16
- inspect_source(sihlb, '',
17
- ['h = {a: 1, b: 2}',
16
+ inspect_source(sihlb,
17
+ ['h = {a: 1, b: :two}',
18
18
  'h = {a => 1 }'])
19
19
  expect(sihlb.offences.map(&:message)).to eq(
20
20
  ['Space inside hash literal braces missing.'] * 3)
21
21
  end
22
22
 
23
23
  it 'registers an offence for hashes with no spaces if so configured' do
24
- inspect_source(sihlb, '',
24
+ inspect_source(sihlb,
25
25
  ['h = {a: 1, b: 2}',
26
26
  'h = {a => 1 }'])
27
27
  expect(sihlb.offences.map(&:message)).to eq(
@@ -31,14 +31,14 @@ module Rubocop
31
31
  it 'registers an offence for hashes with spaces if so configured' do
32
32
  SpaceInsideHashLiteralBraces.config['EnforcedStyleIsWithSpaces'] =
33
33
  false
34
- inspect_source(sihlb, '',
34
+ inspect_source(sihlb,
35
35
  ['h = { a: 1, b: 2 }'])
36
36
  expect(sihlb.offences.map(&:message)).to eq(
37
37
  ['Space inside hash literal braces detected.'] * 2)
38
38
  end
39
39
 
40
40
  it 'accepts hashes with spaces by default' do
41
- inspect_source(sihlb, '',
41
+ inspect_source(sihlb,
42
42
  ['h = { a: 1, b: 2 }',
43
43
  'h = { a => 1 }'])
44
44
  expect(sihlb.offences.map(&:message)).to be_empty
@@ -47,26 +47,31 @@ module Rubocop
47
47
  it 'accepts hashes with no spaces if so configured' do
48
48
  SpaceInsideHashLiteralBraces.config['EnforcedStyleIsWithSpaces'] =
49
49
  false
50
- inspect_source(sihlb, '',
50
+ inspect_source(sihlb,
51
51
  ['h = {a: 1, b: 2}',
52
52
  'h = {a => 1}'])
53
53
  expect(sihlb.offences.map(&:message)).to be_empty
54
54
  end
55
55
 
56
56
  it 'accepts empty hashes without spaces by default' do
57
- inspect_source(sihlb, '', ['h = {}'])
57
+ inspect_source(sihlb, ['h = {}'])
58
58
  expect(sihlb.offences).to be_empty
59
59
  end
60
60
 
61
61
  it 'accepts empty hashes without spaces if configured false' do
62
62
  SpaceInsideHashLiteralBraces.config['EnforcedStyleIsWithSpaces'] =
63
63
  false
64
- inspect_source(sihlb, '', ['h = {}'])
64
+ inspect_source(sihlb, ['h = {}'])
65
65
  expect(sihlb.offences).to be_empty
66
66
  end
67
67
 
68
68
  it 'accepts empty hashes without spaces even if configured true' do
69
- inspect_source(sihlb, '', ['h = {}'])
69
+ inspect_source(sihlb, ['h = {}'])
70
+ expect(sihlb.offences).to be_empty
71
+ end
72
+
73
+ it 'accepts hash literals with no braces' do
74
+ inspect_source(sihlb, ['x(a: b.c)'])
70
75
  expect(sihlb.offences).to be_empty
71
76
  end
72
77
  end
@@ -8,20 +8,24 @@ module Rubocop
8
8
  let(:space) { SpaceInsideParens.new }
9
9
 
10
10
  it 'registers an offence for spaces inside parens' do
11
- inspect_source(space, 'file.rb', ['f( 3)',
12
- 'g(3 )'])
11
+ inspect_source(space, ['f( 3)',
12
+ 'g(3 )'])
13
13
  expect(space.offences.map(&:message)).to eq(
14
14
  ['Space inside parentheses detected.',
15
15
  'Space inside parentheses detected.'])
16
16
  end
17
17
 
18
18
  it 'accepts parentheses in block parameter list' do
19
- inspect_source(space, 'file.rb',
19
+ inspect_source(space,
20
20
  ['list.inject(Tms.new) { |sum, (label, item)|',
21
21
  '}'])
22
22
  expect(space.offences.map(&:message)).to be_empty
23
23
  end
24
24
 
25
+ it 'accepts parentheses with no spaces' do
26
+ inspect_source(space, ['split("\n")'])
27
+ expect(space.offences.map(&:message)).to be_empty
28
+ end
25
29
  end
26
30
  end
27
31
  end
@@ -8,7 +8,7 @@ module Rubocop
8
8
  let(:sl) { StringLiterals.new }
9
9
 
10
10
  it 'registers an offence for double quotes when single quotes suffice' do
11
- inspect_source(sl, 'file.rb', ['s = "abc"'])
11
+ inspect_source(sl, ['s = "abc"'])
12
12
  expect(sl.offences.map(&:message)).to eq(
13
13
  ["Prefer single-quoted strings when you don't need string " +
14
14
  'interpolation or special symbols.'])
@@ -21,15 +21,27 @@ module Rubocop
21
21
  'd = "#@test"',
22
22
  'e = "#$test"',
23
23
  'f = "#@@test"']
24
- inspect_source(sl, 'file.rb', src)
24
+ inspect_source(sl, src)
25
25
  expect(sl.offences.map(&:message)).to be_empty
26
26
  end
27
27
 
28
- it 'can handle double quotes within embedded expression' do
29
- src = ['"#{"A"}"']
30
- inspect_source(sl, 'file.rb', src)
28
+ it 'accepts double quotes with some other special symbols' do
29
+ pending
30
+ # "Substitutions in double-quoted strings"
31
+ # http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
32
+ src = ['g = "\xf9"']
33
+ inspect_source(sl, src)
31
34
  expect(sl.offences.map(&:message)).to be_empty
32
35
  end
36
+
37
+ it 'can handle double quotes within embedded expression' do
38
+ # This seems to be a Parser bug
39
+ pending do
40
+ src = ['"#{"A"}"']
41
+ inspect_source(sl, src)
42
+ expect(sl.offences.map(&:message)).to be_empty
43
+ end
44
+ end
33
45
  end
34
46
  end
35
47
  end
@@ -9,14 +9,30 @@ module Rubocop
9
9
 
10
10
  it 'registers an offence for arrays of symbols', { ruby: 2.0 } do
11
11
  inspect_source(sa,
12
- 'file.rb',
13
12
  ['[:one, :two, :three]'])
14
13
  expect(sa.offences.size).to eq(1)
15
14
  end
16
15
 
16
+ it 'does not reg an offence for array with non-syms', { ruby: 2.0 } do
17
+ inspect_source(sa,
18
+ ['[:one, :two, "three"]'])
19
+ expect(sa.offences).to be_empty
20
+ end
21
+
22
+ it 'does not reg an offence for array starting with %i', { ruby: 2.0 } do
23
+ inspect_source(sa,
24
+ ['%i(one two three)'])
25
+ expect(sa.offences).to be_empty
26
+ end
27
+
28
+ it 'does not reg an offence for array with one element', { ruby: 2.0 } do
29
+ inspect_source(sa,
30
+ ['[:three]'])
31
+ expect(sa.offences).to be_empty
32
+ end
33
+
17
34
  it 'does nothing on Ruby 1.9', { ruby: 1.9 } do
18
35
  inspect_source(sa,
19
- 'file.rb',
20
36
  ['[:one, :two, :three]'])
21
37
  expect(sa.offences).to be_empty
22
38
  end
@@ -0,0 +1,119 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe SymbolName do
8
+ let(:symbol_name) { SymbolName.new }
9
+
10
+ before do
11
+ SymbolName.config = Config.default_configuration.for_cop('SymbolName')
12
+ end
13
+
14
+ context 'when AllowCamelCase is true' do
15
+ before do
16
+ SymbolName.config = {
17
+ 'AllowCamelCase' => true
18
+ }
19
+ end
20
+
21
+ it 'does not register an offence for camel case in names' do
22
+ inspect_source(symbol_name,
23
+ ['test = :BadIdea'])
24
+ expect(symbol_name.offences).to be_empty
25
+ end
26
+ end
27
+
28
+ context 'when AllowCamelCase is false' do
29
+ before do
30
+ SymbolName.config = {
31
+ 'AllowCamelCase' => false
32
+ }
33
+ end
34
+
35
+ it 'registers an offence for camel case in names' do
36
+ inspect_source(symbol_name,
37
+ ['test = :BadIdea'])
38
+ expect(symbol_name.offences.map(&:message)).to eq(
39
+ ['Use snake_case for symbols.'])
40
+ end
41
+ end
42
+
43
+ it 'registers an offence for symbol used as hash label' do
44
+ inspect_source(symbol_name,
45
+ ['{ KEY_ONE: 1, KEY_TWO: 2 }'])
46
+ expect(symbol_name.offences.map(&:message)).to eq(
47
+ ['Use snake_case for symbols.'] * 2)
48
+ end
49
+
50
+ it 'accepts snake case in names' do
51
+ inspect_source(symbol_name,
52
+ ['test = :good_idea'])
53
+ expect(symbol_name.offences).to be_empty
54
+ end
55
+
56
+ it 'accepts snake case in hash label names' do
57
+ inspect_source(symbol_name,
58
+ ['{ one: 1, one_more_3: 2 }'])
59
+ expect(symbol_name.offences).to be_empty
60
+ end
61
+
62
+ it 'accepts snake case with a prefix @ in names' do
63
+ inspect_source(symbol_name,
64
+ ['test = :@good_idea'])
65
+ expect(symbol_name.offences).to be_empty
66
+ end
67
+
68
+ it 'accepts snake case with ? suffix' do
69
+ inspect_source(symbol_name,
70
+ ['test = :good_idea?'])
71
+ expect(symbol_name.offences).to be_empty
72
+ end
73
+
74
+ it 'accepts snake case with ! suffix' do
75
+ inspect_source(symbol_name,
76
+ ['test = :good_idea!'])
77
+ expect(symbol_name.offences).to be_empty
78
+ end
79
+
80
+ it 'accepts snake case with = suffix' do
81
+ inspect_source(symbol_name,
82
+ ['test = :good_idea='])
83
+ expect(symbol_name.offences).to be_empty
84
+ end
85
+
86
+ it 'accepts special cases - !, [] and **' do
87
+ inspect_source(symbol_name,
88
+ ['test = :**',
89
+ 'test = :!',
90
+ 'test = :[]',
91
+ 'test = :[]='])
92
+ expect(symbol_name.offences).to be_empty
93
+ end
94
+
95
+ it 'accepts special cases - ==, <=>, >, <, >=, <=' do
96
+ inspect_source(symbol_name,
97
+ ['test = :==',
98
+ 'test = :<=>',
99
+ 'test = :>',
100
+ 'test = :<',
101
+ 'test = :>=',
102
+ 'test = :<='])
103
+ expect(symbol_name.offences).to be_empty
104
+ end
105
+
106
+ it 'can handle an alias of and operator without crashing' do
107
+ inspect_source(symbol_name,
108
+ ['alias + add'])
109
+ expect(symbol_name.offences).to be_empty
110
+ end
111
+
112
+ it 'registers an offence for SCREAMING_symbol_name' do
113
+ inspect_source(symbol_name,
114
+ ['test = :BAD_IDEA'])
115
+ expect(symbol_name.offences.size).to eq(1)
116
+ end
117
+ end
118
+ end
119
+ end
@@ -7,26 +7,33 @@ module Rubocop
7
7
  describe Syntax do
8
8
  let(:sc) { Syntax.new }
9
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')
10
+ if RUBY_ENGINE == 'ruby'
11
+ it 'registers an offence for unused variables', ruby: 2.0 do
12
+ source = "x = 5\nputs 10"
13
+ Dir.mktmpdir do |tmpdir|
14
+ path = File.join(tmpdir, 'file.rb')
15
+ File.open(path, 'w') { |f| f.write(source) }
16
+ sc.inspect_file(path)
17
+ end
18
+ expect(sc.offences.size).to eq(1)
19
+ expect(sc.offences.first.message)
20
+ .to eq('Assigned but unused variable - x')
23
21
  end
24
22
 
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')
23
+ describe '#process_line' do
24
+ it 'processes warnings correctly' do
25
+ l, s, m = sc.process_line('admin.rb:1: warning: possibly useless')
26
+ expect(l).to eq(1)
27
+ expect(s).to eq(:warning)
28
+ expect(m).to eq('Possibly useless')
29
+ end
30
+
31
+ it 'processes errors correctly' do
32
+ l, s, m = sc.process_line('admin.rb:1: unterminated string meets')
33
+ expect(l).to eq(1)
34
+ expect(s).to eq(:error)
35
+ expect(m).to eq('Unterminated string meets')
36
+ end
30
37
  end
31
38
  end
32
39
  end
@@ -8,12 +8,12 @@ module Rubocop
8
8
  let(:tab) { Tab.new }
9
9
 
10
10
  it 'registers an offence for a line indented with tab' do
11
- tab.inspect('file.rb', ["\tx = 0"], nil, nil)
11
+ tab.inspect(["\tx = 0"], nil, nil, nil)
12
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', ["(x = \"\t\")"], nil, nil)
16
+ tab.inspect(["(x = \"\t\")"], nil, nil, nil)
17
17
  expect(tab.offences).to be_empty
18
18
  end
19
19
  end
@@ -8,16 +8,14 @@ module Rubocop
8
8
  let(:op) { MultilineTernaryOperator.new }
9
9
 
10
10
  it 'registers an offence for a multiline ternary operator expression' do
11
- inspect_source(op, 'file.rb', ['a = cond ?',
12
- ' b : c'])
13
- expect(op.offences.map(&:message)).to eq(
14
- ['Avoid multi-line ?: (the ternary operator); use if/unless ' +
15
- 'instead.'])
11
+ inspect_source(op, ['a = cond ?',
12
+ ' b : c'])
13
+ expect(op.offences.size).to eq(1)
16
14
  end
17
15
 
18
16
  it 'accepts a single line ternary operator expression' do
19
- inspect_source(op, 'file.rb', ['a = cond ? b : c'])
20
- expect(op.offences.map(&:message)).to be_empty
17
+ inspect_source(op, ['a = cond ? b : c'])
18
+ expect(op.offences).to be_empty
21
19
  end
22
20
  end
23
21
 
@@ -25,19 +23,17 @@ module Rubocop
25
23
  let(:op) { NestedTernaryOperator.new }
26
24
 
27
25
  it 'registers an offence for a nested ternary operator expression' do
28
- inspect_source(op, 'file.rb', ['a ? (b ? b1 : b2) : a2'])
29
- expect(op.offences.map(&:message)).to eq(
30
- ['Ternary operators must not be nested. Prefer if/else constructs ' +
31
- 'instead.'])
26
+ inspect_source(op, ['a ? (b ? b1 : b2) : a2'])
27
+ expect(op.offences.size).to eq(1)
32
28
  end
33
29
 
34
30
  it 'accepts a non-nested ternary operator within an if' do
35
- inspect_source(op, 'file.rb', ['a = if x',
36
- ' cond ? b : c',
37
- 'else',
38
- ' d',
39
- 'end'])
40
- expect(op.offences.map(&:message)).to be_empty
31
+ inspect_source(op, ['a = if x',
32
+ ' cond ? b : c',
33
+ 'else',
34
+ ' d',
35
+ 'end'])
36
+ expect(op.offences).to be_empty
41
37
  end
42
38
  end
43
39
  end