rubocop 0.23.0 → 0.24.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 (115) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +35 -1
  4. data/CONTRIBUTING.md +1 -1
  5. data/README.md +6 -6
  6. data/config/default.yml +25 -6
  7. data/config/enabled.yml +20 -0
  8. data/lib/rubocop.rb +10 -13
  9. data/lib/rubocop/cli.rb +23 -20
  10. data/lib/rubocop/cop/lint/def_end_alignment.rb +47 -0
  11. data/lib/rubocop/cop/lint/end_alignment.rb +18 -65
  12. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  13. data/lib/rubocop/cop/lint/syntax.rb +28 -4
  14. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -1
  15. data/lib/rubocop/cop/lint/unused_block_argument.rb +13 -1
  16. data/lib/rubocop/cop/lint/useless_access_modifier.rb +3 -2
  17. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -9
  18. data/lib/rubocop/cop/lint/useless_setter_call.rb +28 -20
  19. data/lib/rubocop/cop/mixin/access_modifier_node.rb +18 -0
  20. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +4 -2
  21. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +42 -0
  22. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +32 -0
  23. data/lib/rubocop/cop/mixin/surrounding_space.rb +7 -8
  24. data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
  25. data/lib/rubocop/cop/offense.rb +27 -14
  26. data/lib/rubocop/cop/style/access_modifier_indentation.rb +2 -9
  27. data/lib/rubocop/cop/style/attr.rb +3 -1
  28. data/lib/rubocop/cop/style/class_check.rb +42 -0
  29. data/lib/rubocop/cop/style/each_with_object.rb +5 -1
  30. data/lib/rubocop/cop/style/empty_lines.rb +1 -4
  31. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +2 -8
  32. data/lib/rubocop/cop/style/empty_lines_around_body.rb +1 -4
  33. data/lib/rubocop/cop/style/encoding.rb +3 -5
  34. data/lib/rubocop/cop/style/end_of_line.rb +2 -5
  35. data/lib/rubocop/cop/style/file_name.rb +2 -4
  36. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -1
  37. data/lib/rubocop/cop/style/indentation_consistency.rb +2 -1
  38. data/lib/rubocop/cop/style/indentation_width.rb +25 -5
  39. data/lib/rubocop/cop/style/line_length.rb +59 -5
  40. data/lib/rubocop/cop/style/next.rb +18 -18
  41. data/lib/rubocop/cop/style/numeric_literals.rb +22 -9
  42. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
  43. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  44. data/lib/rubocop/cop/style/semicolon.rb +1 -3
  45. data/lib/rubocop/cop/style/space_after_colon.rb +7 -3
  46. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +3 -1
  47. data/lib/rubocop/cop/style/space_before_comma.rb +16 -0
  48. data/lib/rubocop/cop/style/space_before_semicolon.rb +16 -0
  49. data/lib/rubocop/cop/style/tab.rb +6 -5
  50. data/lib/rubocop/cop/style/trailing_comma.rb +33 -6
  51. data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
  52. data/lib/rubocop/cop/style/unneeded_capital_w.rb +6 -0
  53. data/lib/rubocop/cop/style/unneeded_percent_q.rb +45 -0
  54. data/lib/rubocop/cop/style/unneeded_percent_x.rb +2 -3
  55. data/lib/rubocop/cop/style/word_array.rb +1 -1
  56. data/lib/rubocop/cop/team.rb +6 -12
  57. data/lib/rubocop/cop/util.rb +26 -8
  58. data/lib/rubocop/cop/variable_force.rb +3 -6
  59. data/lib/rubocop/cop/variable_force/variable.rb +7 -3
  60. data/lib/rubocop/processed_source.rb +52 -12
  61. data/lib/rubocop/{file_inspector.rb → runner.rb} +50 -59
  62. data/lib/rubocop/version.rb +1 -1
  63. data/relnotes/v0.24.0.md +77 -0
  64. data/rubocop.gemspec +4 -4
  65. data/spec/rubocop/cli_spec.rb +38 -21
  66. data/spec/rubocop/config_loader_spec.rb +7 -6
  67. data/spec/rubocop/config_spec.rb +8 -8
  68. data/spec/rubocop/cop/cop_spec.rb +1 -1
  69. data/spec/rubocop/cop/lint/def_end_alignment_spec.rb +108 -0
  70. data/spec/rubocop/cop/lint/end_alignment_spec.rb +0 -47
  71. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +6 -7
  72. data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +19 -0
  73. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +8 -18
  74. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +99 -51
  75. data/spec/rubocop/cop/offense_spec.rb +3 -3
  76. data/spec/rubocop/cop/rails/delegate_spec.rb +1 -1
  77. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +12 -0
  78. data/spec/rubocop/cop/style/align_hash_spec.rb +4 -4
  79. data/spec/rubocop/cop/style/align_parameters_spec.rb +1 -1
  80. data/spec/rubocop/cop/style/attr_spec.rb +12 -2
  81. data/spec/rubocop/cop/style/class_check_spec.rb +41 -0
  82. data/spec/rubocop/cop/style/each_with_object_spec.rb +5 -0
  83. data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +5 -0
  84. data/spec/rubocop/cop/style/indentation_width_spec.rb +95 -0
  85. data/spec/rubocop/cop/style/line_length_spec.rb +75 -0
  86. data/spec/rubocop/cop/style/next_spec.rb +28 -0
  87. data/spec/rubocop/cop/style/numeric_literals_spec.rb +10 -0
  88. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +5 -0
  89. data/spec/rubocop/cop/style/space_after_colon_spec.rb +17 -0
  90. data/spec/rubocop/cop/style/space_around_equals_in_parameter_default_spec.rb +11 -0
  91. data/spec/rubocop/cop/style/space_around_operators_spec.rb +1 -0
  92. data/spec/rubocop/cop/style/space_before_comma_spec.rb +42 -0
  93. data/spec/rubocop/cop/style/space_before_semicolon_spec.rb +28 -0
  94. data/spec/rubocop/cop/style/trailing_comma_spec.rb +37 -15
  95. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +8 -10
  96. data/spec/rubocop/cop/style/unneeded_percent_q_spec.rb +72 -0
  97. data/spec/rubocop/cop/style/word_array_spec.rb +6 -0
  98. data/spec/rubocop/cop/team_spec.rb +8 -8
  99. data/spec/rubocop/cop/util_spec.rb +10 -0
  100. data/spec/rubocop/cop/variable_force/assignment_spec.rb +1 -1
  101. data/spec/rubocop/cop/variable_force/locatable_spec.rb +1 -1
  102. data/spec/rubocop/cop/variable_force/scope_spec.rb +1 -1
  103. data/spec/rubocop/cop/variable_force/variable_spec.rb +4 -4
  104. data/spec/rubocop/formatter/base_formatter_spec.rb +5 -5
  105. data/spec/rubocop/formatter/colorizable_spec.rb +2 -2
  106. data/spec/rubocop/formatter/json_formatter_spec.rb +1 -1
  107. data/spec/rubocop/path_util_spec.rb +15 -15
  108. data/spec/rubocop/processed_source_spec.rb +104 -50
  109. data/spec/rubocop/runner_spec.rb +64 -0
  110. data/spec/spec_helper.rb +8 -10
  111. data/spec/support/shared_examples.rb +22 -0
  112. metadata +39 -15
  113. data/lib/rubocop/source_parser.rb +0 -47
  114. data/spec/rubocop/file_inspector_spec.rb +0 -84
  115. data/spec/rubocop/source_parser_spec.rb +0 -85
@@ -19,6 +19,12 @@ describe RuboCop::Cop::Style::WordArray, :config do
19
19
  expect(cop.offenses.size).to eq(1)
20
20
  end
21
21
 
22
+ it 'registers an offense for arrays of unicode word characters' do
23
+ inspect_source(cop,
24
+ ['["ВУЗ", "вуз", "中文网"]'])
25
+ expect(cop.offenses.size).to eq(1)
26
+ end
27
+
22
28
  it 'registers an offense for arrays with character constants' do
23
29
  inspect_source(cop,
24
30
  ['["one", ?\n]'])
@@ -13,12 +13,12 @@ describe RuboCop::Cop::Team do
13
13
 
14
14
  context 'when the option argument of .new is omitted' do
15
15
  subject { described_class.new(cop_classes, config).autocorrect? }
16
- it { should be_false }
16
+ it { should be_falsey }
17
17
  end
18
18
 
19
19
  context 'when { auto_correct: true } is passed to .new' do
20
20
  let(:options) { { auto_correct: true } }
21
- it { should be_true }
21
+ it { should be_truthy }
22
22
  end
23
23
  end
24
24
 
@@ -27,12 +27,12 @@ describe RuboCop::Cop::Team do
27
27
 
28
28
  context 'when the option argument of .new is omitted' do
29
29
  subject { described_class.new(cop_classes, config).debug? }
30
- it { should be_false }
30
+ it { should be_falsey }
31
31
  end
32
32
 
33
33
  context 'when { debug: true } is passed to .new' do
34
34
  let(:options) { { debug: true } }
35
- it { should be_true }
35
+ it { should be_truthy }
36
36
  end
37
37
  end
38
38
 
@@ -41,7 +41,7 @@ describe RuboCop::Cop::Team do
41
41
 
42
42
  let(:file_path) { '/tmp/example.rb' }
43
43
  let(:offenses) do
44
- team.inspect_file(RuboCop::SourceParser.parse_file(file_path))
44
+ team.inspect_file(RuboCop::ProcessedSource.from_file(file_path))
45
45
  end
46
46
 
47
47
  before do
@@ -53,7 +53,7 @@ describe RuboCop::Cop::Team do
53
53
 
54
54
  it 'returns offenses' do
55
55
  expect(offenses).not_to be_empty
56
- expect(offenses.all? { |o| o.is_a?(RuboCop::Cop::Offense) }).to be_true
56
+ expect(offenses.all? { |o| o.is_a?(RuboCop::Cop::Offense) }).to be_truthy
57
57
  end
58
58
 
59
59
  context 'when Parser reports non-fatal warning for the file' do
@@ -87,7 +87,7 @@ describe RuboCop::Cop::Team do
87
87
  end
88
88
 
89
89
  it 'does autocorrection' do
90
- team.inspect_file(RuboCop::SourceParser.parse_file(file_path))
90
+ team.inspect_file(RuboCop::ProcessedSource.from_file(file_path))
91
91
  corrected_source = File.read(file_path)
92
92
  expect(corrected_source).to eq([
93
93
  '# encoding: utf-8',
@@ -107,7 +107,7 @@ describe RuboCop::Cop::Team do
107
107
 
108
108
  it 'returns cop instances' do
109
109
  expect(cops).not_to be_empty
110
- expect(cops.all? { |c| c.is_a?(RuboCop::Cop::Cop) }).to be_true
110
+ expect(cops.all? { |c| c.is_a?(RuboCop::Cop::Cop) }).to be_truthy
111
111
  end
112
112
 
113
113
  context 'when only some cop classes are passed to .new' do
@@ -46,4 +46,14 @@ describe RuboCop::Cop::Util do
46
46
  end
47
47
  end
48
48
  end
49
+
50
+ # Test compatibility with Range#size in Ruby 2.0.
51
+ describe '#numeric_range_size', ruby: 2 do
52
+ [1..1, 1...1, 1..2, 1...2, 1..3, 1...3, 1..-1, 1...-1].each do |range|
53
+ context "with range #{range}" do
54
+ subject { described_class.numeric_range_size(range) }
55
+ it { should eq(range.size) }
56
+ end
57
+ end
58
+ end
49
59
  end
@@ -7,7 +7,7 @@ describe RuboCop::Cop::VariableForce::Assignment do
7
7
  include AST::Sexp
8
8
 
9
9
  let(:ast) do
10
- processed_source = RuboCop::SourceParser.parse(source)
10
+ processed_source = RuboCop::ProcessedSource.new(source)
11
11
  processed_source.ast
12
12
  end
13
13
 
@@ -18,7 +18,7 @@ describe RuboCop::Cop::VariableForce::Locatable do
18
18
  end
19
19
 
20
20
  let(:ast) do
21
- processed_source = RuboCop::SourceParser.parse(source)
21
+ processed_source = RuboCop::ProcessedSource.new(source)
22
22
  processed_source.ast
23
23
  end
24
24
 
@@ -23,7 +23,7 @@ describe RuboCop::Cop::VariableForce::Scope do
23
23
  end
24
24
 
25
25
  let(:ast) do
26
- ast = RuboCop::SourceParser.parse(source).ast
26
+ ast = RuboCop::ProcessedSource.new(source).ast
27
27
  RuboCop::Cop::VariableForce.wrap_with_top_level_node(ast)
28
28
  end
29
29
 
@@ -26,14 +26,14 @@ describe RuboCop::Cop::VariableForce::Variable do
26
26
  subject { variable.referenced? }
27
27
 
28
28
  context 'when the variable is not assigned' do
29
- it { should be_false }
29
+ it { should be_falsey }
30
30
 
31
31
  context 'and the variable is referenced' do
32
32
  before do
33
33
  variable.reference!(s(:lvar, name))
34
34
  end
35
35
 
36
- it { should be_true }
36
+ it { should be_truthy }
37
37
  end
38
38
  end
39
39
 
@@ -43,7 +43,7 @@ describe RuboCop::Cop::VariableForce::Variable do
43
43
  end
44
44
 
45
45
  context 'and the variable is not yet referenced' do
46
- it { should be_false }
46
+ it { should be_falsey }
47
47
  end
48
48
 
49
49
  context 'and the variable is referenced' do
@@ -51,7 +51,7 @@ describe RuboCop::Cop::VariableForce::Variable do
51
51
  variable.reference!(s(:lvar, name))
52
52
  end
53
53
 
54
- it { should be_true }
54
+ it { should be_truthy }
55
55
  end
56
56
  end
57
57
  end
@@ -9,7 +9,7 @@ module RuboCop
9
9
 
10
10
  describe 'how the API methods are invoked', :isolated_environment do
11
11
  subject(:formatter) { double('formatter').as_null_object }
12
- let(:cli) { CLI.new }
12
+ let(:runner) { Runner.new({}, ConfigStore.new) }
13
13
  let(:output) { $stdout.string }
14
14
 
15
15
  before do
@@ -39,7 +39,7 @@ module RuboCop
39
39
  end
40
40
 
41
41
  def run
42
- cli.run([])
42
+ runner.run([])
43
43
  end
44
44
 
45
45
  describe 'invocation order' do
@@ -105,7 +105,7 @@ module RuboCop
105
105
  end
106
106
 
107
107
  context 'when RuboCop is interrupted by user' do
108
- it 'received processed file paths' do
108
+ it 'receives only processed file paths' do
109
109
  class << formatter
110
110
  attr_reader :processed_file_count
111
111
 
@@ -115,7 +115,7 @@ module RuboCop
115
115
  end
116
116
  end
117
117
 
118
- allow(cli).to receive(:wants_to_quit?) do
118
+ allow(runner).to receive(:aborting?) do
119
119
  formatter.processed_file_count == 2
120
120
  end
121
121
 
@@ -180,7 +180,7 @@ module RuboCop
180
180
  fail
181
181
  end
182
182
  expect(offenses.all? { |o| o.is_a?(RuboCop::Cop::Offense) })
183
- .to be_true
183
+ .to be_truthy
184
184
  end
185
185
  run
186
186
  end
@@ -34,7 +34,7 @@ module RuboCop
34
34
 
35
35
  shared_examples 'does nothing' do
36
36
  it 'does nothing' do
37
- should == 'foo'
37
+ should eq('foo')
38
38
  end
39
39
  end
40
40
 
@@ -49,7 +49,7 @@ module RuboCop
49
49
  end
50
50
 
51
51
  it 'colorize the passed string' do
52
- should == "\e[31mfoo\e[0m"
52
+ should eq("\e[31mfoo\e[0m")
53
53
  end
54
54
  end
55
55
 
@@ -118,7 +118,7 @@ module RuboCop
118
118
  end
119
119
 
120
120
  it 'sets Offense#corrected? value for :corrected key' do
121
- expect(hash[:corrected]).to be_true
121
+ expect(hash[:corrected]).to be_truthy
122
122
  end
123
123
 
124
124
  before do
@@ -31,7 +31,7 @@ describe RuboCop::PathUtil do
31
31
  context 'with deprecated patterns' do
32
32
  it 'matches dir/** and prints warning' do
33
33
  expect(subject.match_path?('dir/**', 'dir/sub/file', '.rubocop.yml'))
34
- .to be_true
34
+ .to be_truthy
35
35
  expect($stderr.string)
36
36
  .to eq("Warning: Deprecated pattern style 'dir/**' in " \
37
37
  ".rubocop.yml. Change to 'dir/**/*'.\n")
@@ -39,37 +39,37 @@ describe RuboCop::PathUtil do
39
39
 
40
40
  it 'matches strings to the basename and prints warning' do
41
41
  expect(subject.match_path?('file', 'dir/file', '.rubocop.yml'))
42
- .to be_true
42
+ .to be_truthy
43
43
  expect($stderr.string)
44
44
  .to eq("Warning: Deprecated pattern style 'file' in .rubocop.yml. " \
45
45
  "Change to '**/file'.\n")
46
46
 
47
- expect(subject.match_path?('file', 'dir/files', '')).to be_false
48
- expect(subject.match_path?('dir', 'dir/file', '')).to be_false
47
+ expect(subject.match_path?('file', 'dir/files', '')).to be_falsey
48
+ expect(subject.match_path?('dir', 'dir/file', '')).to be_falsey
49
49
  end
50
50
  end
51
51
 
52
52
  it 'matches strings to the full path' do
53
53
  expect(subject.match_path?("#{Dir.pwd}/dir/file",
54
- "#{Dir.pwd}/dir/file", '')).to be_true
54
+ "#{Dir.pwd}/dir/file", '')).to be_truthy
55
55
  expect(subject.match_path?("#{Dir.pwd}/dir/file",
56
- "#{Dir.pwd}/dir/dir/file", '')).to be_false
56
+ "#{Dir.pwd}/dir/dir/file", '')).to be_falsey
57
57
  end
58
58
 
59
59
  it 'matches glob expressions' do
60
- expect(subject.match_path?('dir/*', 'dir/file', '')).to be_true
61
- expect(subject.match_path?('dir/*/*', 'dir/sub/file', '')).to be_true
62
- expect(subject.match_path?('dir/**/*', 'dir/sub/file', '')).to be_true
63
- expect(subject.match_path?('dir/**/*', 'dir/file', '')).to be_true
64
- expect(subject.match_path?('**/*', 'dir/sub/file', '')).to be_true
65
- expect(subject.match_path?('**/file', 'file', '')).to be_true
60
+ expect(subject.match_path?('dir/*', 'dir/file', '')).to be_truthy
61
+ expect(subject.match_path?('dir/*/*', 'dir/sub/file', '')).to be_truthy
62
+ expect(subject.match_path?('dir/**/*', 'dir/sub/file', '')).to be_truthy
63
+ expect(subject.match_path?('dir/**/*', 'dir/file', '')).to be_truthy
64
+ expect(subject.match_path?('**/*', 'dir/sub/file', '')).to be_truthy
65
+ expect(subject.match_path?('**/file', 'file', '')).to be_truthy
66
66
 
67
- expect(subject.match_path?('sub/*', 'dir/sub/file', '')).to be_false
67
+ expect(subject.match_path?('sub/*', 'dir/sub/file', '')).to be_falsey
68
68
  end
69
69
 
70
70
  it 'matches regexps' do
71
- expect(subject.match_path?(/^d.*e$/, 'dir/file', '')).to be_true
72
- expect(subject.match_path?(/^d.*e$/, 'dir/filez', '')).to be_false
71
+ expect(subject.match_path?(/^d.*e$/, 'dir/file', '')).to be_truthy
72
+ expect(subject.match_path?(/^d.*e$/, 'dir/filez', '')).to be_falsey
73
73
  end
74
74
  end
75
75
  end
@@ -3,31 +3,93 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe RuboCop::ProcessedSource do
6
- subject(:processed_source) do
7
- described_class.new(
8
- buffer,
9
- double('ast'),
10
- double('comments'),
11
- double('tokens'),
12
- diagnostics
13
- )
6
+ include FileHelper
7
+
8
+ subject(:processed_source) { described_class.new(source, path) }
9
+
10
+ let(:source) { <<-END.strip_indent }
11
+ # encoding: utf-8
12
+ def some_method
13
+ puts 'foo'
14
+ end
15
+ some_method
16
+ END
17
+
18
+ let(:path) { 'path/to/file.rb' }
19
+
20
+ describe '.from_file', :isolated_environment do
21
+ before do
22
+ create_file(path, 'foo')
23
+ end
24
+
25
+ let(:processed_source) { described_class.from_file(path) }
26
+
27
+ it 'returns an instance of ProcessedSource' do
28
+ expect(processed_source).to be_a(described_class)
29
+ end
30
+
31
+ it "sets the file path to the instance's #path" do
32
+ expect(processed_source.path).to eq(path)
33
+ end
34
+ end
35
+
36
+ describe '#path' do
37
+ it 'is the path passed to .new' do
38
+ expect(processed_source.path).to eq(path)
39
+ end
40
+ end
41
+
42
+ describe '#buffer' do
43
+ it 'is a source buffer' do
44
+ expect(processed_source.buffer).to be_a(Parser::Source::Buffer)
45
+ end
46
+ end
47
+
48
+ describe '#ast' do
49
+ it 'is the root node of AST' do
50
+ expect(processed_source.ast).to be_a(Parser::AST::Node)
51
+ end
14
52
  end
15
53
 
16
- let(:diagnostics) { double('diagnostics') }
54
+ describe '#comments' do
55
+ it 'is an array of comments' do
56
+ expect(processed_source.comments).to be_a(Array)
57
+ expect(processed_source.comments.first).to be_a(Parser::Source::Comment)
58
+ end
59
+ end
17
60
 
18
- let(:source) do
19
- [
20
- 'def some_method',
21
- " puts 'foo'",
22
- 'end',
23
- 'some_method'
24
- ].join("\n")
61
+ describe '#tokens' do
62
+ it 'has an array of tokens' do
63
+ expect(processed_source.tokens).to be_a(Array)
64
+ expect(processed_source.tokens.first).to be_a(RuboCop::Token)
65
+ end
25
66
  end
26
67
 
27
- let(:buffer) do
28
- buffer = Parser::Source::Buffer.new('(string)', 1)
29
- buffer.source = source
30
- buffer
68
+ shared_context 'invalid encoding source' do
69
+ let(:source) do
70
+ [
71
+ '# encoding: utf-8',
72
+ "# \xf9"
73
+ ].join("\n")
74
+ end
75
+ end
76
+
77
+ describe '#parser_error' do
78
+ context 'when the source was properly parsed' do
79
+ it 'is nil' do
80
+ expect(processed_source.parser_error).to be_nil
81
+ end
82
+ end
83
+
84
+ context 'when the source could not be parsed due to encoding error' do
85
+ include_context 'invalid encoding source'
86
+
87
+ it 'returns the error' do
88
+ expect(processed_source.parser_error).to be_a(Exception)
89
+ expect(processed_source.parser_error.message)
90
+ .to include('invalid byte sequence')
91
+ end
92
+ end
31
93
  end
32
94
 
33
95
  describe '#lines' do
@@ -36,78 +98,70 @@ describe RuboCop::ProcessedSource do
36
98
  end
37
99
 
38
100
  it 'has same number of elements as line count' do
39
- expect(processed_source.lines.size).to eq(4)
101
+ expect(processed_source.lines.size).to eq(5)
40
102
  end
41
103
 
42
104
  it 'contains lines as string without linefeed' do
43
105
  first_line = processed_source.lines.first
44
- expect(first_line).to eq('def some_method')
106
+ expect(first_line).to eq('# encoding: utf-8')
45
107
  end
46
108
  end
47
109
 
48
110
  describe '#[]' do
49
111
  context 'when an index is passed' do
50
112
  it 'returns the line' do
51
- expect(processed_source[2]).to eq('end')
113
+ expect(processed_source[3]).to eq('end')
52
114
  end
53
115
  end
54
116
 
55
117
  context 'when a range is passed' do
56
118
  it 'returns the array of lines' do
57
- expect(processed_source[2..3]).to eq(%w(end some_method))
119
+ expect(processed_source[3..4]).to eq(%w(end some_method))
58
120
  end
59
121
  end
60
122
 
61
123
  context 'when start index and length are passed' do
62
124
  it 'returns the array of lines' do
63
- expect(processed_source[2, 2]).to eq(%w(end some_method))
125
+ expect(processed_source[3, 2]).to eq(%w(end some_method))
64
126
  end
65
127
  end
66
128
  end
67
129
 
68
130
  describe 'valid_syntax?' do
69
- def create_diagnostics(level)
70
- Parser::Diagnostic.new(level, :odd_hash, [], double('location'))
71
- end
131
+ subject { processed_source.valid_syntax? }
72
132
 
73
- let(:diagnostics) do
74
- [create_diagnostics(level)]
75
- end
133
+ context 'when the source is completely valid' do
134
+ let(:source) { 'def valid_code; end' }
76
135
 
77
- context 'when the source has diagnostic with error level' do
78
- let(:level) { :error }
79
-
80
- it 'returns false' do
81
- expect(processed_source.valid_syntax?).to be_false
136
+ it 'returns true' do
137
+ expect(processed_source.diagnostics).to be_empty
138
+ expect(processed_source).to be_valid_syntax
82
139
  end
83
140
  end
84
141
 
85
- context 'when the source has diagnostic with fatal level' do
86
- let(:level) { :fatal }
142
+ context 'when the source is invalid' do
143
+ let(:source) { 'def invalid_code; en' }
87
144
 
88
145
  it 'returns false' do
89
- expect(processed_source.valid_syntax?).to be_false
146
+ expect(processed_source).not_to be_valid_syntax
90
147
  end
91
148
  end
92
149
 
93
- context 'when the source has diagnostic with warning level' do
94
- let(:level) { :warning }
150
+ context 'when the source is valid but has some warning diagnostics' do
151
+ let(:source) { 'do_something *array' }
95
152
 
96
153
  it 'returns true' do
97
- expect(processed_source.valid_syntax?).to be_true
154
+ expect(processed_source.diagnostics).not_to be_empty
155
+ expect(processed_source.diagnostics.first.level).to eq(:warning)
156
+ expect(processed_source).to be_valid_syntax
98
157
  end
99
158
  end
100
159
 
101
- context 'when the source has diagnostics with error and warning level' do
102
- let(:diagnostics) do
103
- [
104
- create_diagnostics(:error),
105
- create_diagnostics(:warning)
106
- ]
107
- end
160
+ context 'when the source could not be parsed due to encoding error' do
161
+ include_context 'invalid encoding source'
108
162
 
109
163
  it 'returns false' do
110
- expect(processed_source.valid_syntax?).to be_false
164
+ expect(processed_source).not_to be_valid_syntax
111
165
  end
112
166
  end
113
167
  end