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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/CHANGELOG.md +35 -1
- data/CONTRIBUTING.md +1 -1
- data/README.md +6 -6
- data/config/default.yml +25 -6
- data/config/enabled.yml +20 -0
- data/lib/rubocop.rb +10 -13
- data/lib/rubocop/cli.rb +23 -20
- data/lib/rubocop/cop/lint/def_end_alignment.rb +47 -0
- data/lib/rubocop/cop/lint/end_alignment.rb +18 -65
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/syntax.rb +28 -4
- data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -1
- data/lib/rubocop/cop/lint/unused_block_argument.rb +13 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +3 -2
- data/lib/rubocop/cop/lint/useless_assignment.rb +1 -9
- data/lib/rubocop/cop/lint/useless_setter_call.rb +28 -20
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +18 -0
- data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +4 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +42 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +32 -0
- data/lib/rubocop/cop/mixin/surrounding_space.rb +7 -8
- data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
- data/lib/rubocop/cop/offense.rb +27 -14
- data/lib/rubocop/cop/style/access_modifier_indentation.rb +2 -9
- data/lib/rubocop/cop/style/attr.rb +3 -1
- data/lib/rubocop/cop/style/class_check.rb +42 -0
- data/lib/rubocop/cop/style/each_with_object.rb +5 -1
- data/lib/rubocop/cop/style/empty_lines.rb +1 -4
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +2 -8
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +1 -4
- data/lib/rubocop/cop/style/encoding.rb +3 -5
- data/lib/rubocop/cop/style/end_of_line.rb +2 -5
- data/lib/rubocop/cop/style/file_name.rb +2 -4
- data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -1
- data/lib/rubocop/cop/style/indentation_consistency.rb +2 -1
- data/lib/rubocop/cop/style/indentation_width.rb +25 -5
- data/lib/rubocop/cop/style/line_length.rb +59 -5
- data/lib/rubocop/cop/style/next.rb +18 -18
- data/lib/rubocop/cop/style/numeric_literals.rb +22 -9
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -3
- data/lib/rubocop/cop/style/space_after_colon.rb +7 -3
- data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +3 -1
- data/lib/rubocop/cop/style/space_before_comma.rb +16 -0
- data/lib/rubocop/cop/style/space_before_semicolon.rb +16 -0
- data/lib/rubocop/cop/style/tab.rb +6 -5
- data/lib/rubocop/cop/style/trailing_comma.rb +33 -6
- data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +6 -0
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +45 -0
- data/lib/rubocop/cop/style/unneeded_percent_x.rb +2 -3
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/team.rb +6 -12
- data/lib/rubocop/cop/util.rb +26 -8
- data/lib/rubocop/cop/variable_force.rb +3 -6
- data/lib/rubocop/cop/variable_force/variable.rb +7 -3
- data/lib/rubocop/processed_source.rb +52 -12
- data/lib/rubocop/{file_inspector.rb → runner.rb} +50 -59
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/v0.24.0.md +77 -0
- data/rubocop.gemspec +4 -4
- data/spec/rubocop/cli_spec.rb +38 -21
- data/spec/rubocop/config_loader_spec.rb +7 -6
- data/spec/rubocop/config_spec.rb +8 -8
- data/spec/rubocop/cop/cop_spec.rb +1 -1
- data/spec/rubocop/cop/lint/def_end_alignment_spec.rb +108 -0
- data/spec/rubocop/cop/lint/end_alignment_spec.rb +0 -47
- data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +6 -7
- data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +19 -0
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +8 -18
- data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +99 -51
- data/spec/rubocop/cop/offense_spec.rb +3 -3
- data/spec/rubocop/cop/rails/delegate_spec.rb +1 -1
- data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +12 -0
- data/spec/rubocop/cop/style/align_hash_spec.rb +4 -4
- data/spec/rubocop/cop/style/align_parameters_spec.rb +1 -1
- data/spec/rubocop/cop/style/attr_spec.rb +12 -2
- data/spec/rubocop/cop/style/class_check_spec.rb +41 -0
- data/spec/rubocop/cop/style/each_with_object_spec.rb +5 -0
- data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +5 -0
- data/spec/rubocop/cop/style/indentation_width_spec.rb +95 -0
- data/spec/rubocop/cop/style/line_length_spec.rb +75 -0
- data/spec/rubocop/cop/style/next_spec.rb +28 -0
- data/spec/rubocop/cop/style/numeric_literals_spec.rb +10 -0
- data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +5 -0
- data/spec/rubocop/cop/style/space_after_colon_spec.rb +17 -0
- data/spec/rubocop/cop/style/space_around_equals_in_parameter_default_spec.rb +11 -0
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +1 -0
- data/spec/rubocop/cop/style/space_before_comma_spec.rb +42 -0
- data/spec/rubocop/cop/style/space_before_semicolon_spec.rb +28 -0
- data/spec/rubocop/cop/style/trailing_comma_spec.rb +37 -15
- data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +8 -10
- data/spec/rubocop/cop/style/unneeded_percent_q_spec.rb +72 -0
- data/spec/rubocop/cop/style/word_array_spec.rb +6 -0
- data/spec/rubocop/cop/team_spec.rb +8 -8
- data/spec/rubocop/cop/util_spec.rb +10 -0
- data/spec/rubocop/cop/variable_force/assignment_spec.rb +1 -1
- data/spec/rubocop/cop/variable_force/locatable_spec.rb +1 -1
- data/spec/rubocop/cop/variable_force/scope_spec.rb +1 -1
- data/spec/rubocop/cop/variable_force/variable_spec.rb +4 -4
- data/spec/rubocop/formatter/base_formatter_spec.rb +5 -5
- data/spec/rubocop/formatter/colorizable_spec.rb +2 -2
- data/spec/rubocop/formatter/json_formatter_spec.rb +1 -1
- data/spec/rubocop/path_util_spec.rb +15 -15
- data/spec/rubocop/processed_source_spec.rb +104 -50
- data/spec/rubocop/runner_spec.rb +64 -0
- data/spec/spec_helper.rb +8 -10
- data/spec/support/shared_examples.rb +22 -0
- metadata +39 -15
- data/lib/rubocop/source_parser.rb +0 -47
- data/spec/rubocop/file_inspector_spec.rb +0 -84
- 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
|
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
|
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
|
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
|
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::
|
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
|
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::
|
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
|
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
|
@@ -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
|
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
|
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
|
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
|
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(:
|
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
|
-
|
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 '
|
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(
|
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
|
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
|
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
|
52
|
+
should eq("\e[31mfoo\e[0m")
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -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
|
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
|
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
|
48
|
-
expect(subject.match_path?('dir', 'dir/file', '')).to
|
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
|
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
|
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
|
61
|
-
expect(subject.match_path?('dir/*/*', 'dir/sub/file', '')).to
|
62
|
-
expect(subject.match_path?('dir/**/*', 'dir/sub/file', '')).to
|
63
|
-
expect(subject.match_path?('dir/**/*', 'dir/file', '')).to
|
64
|
-
expect(subject.match_path?('**/*', 'dir/sub/file', '')).to
|
65
|
-
expect(subject.match_path?('**/file', 'file', '')).to
|
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
|
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
|
72
|
-
expect(subject.match_path?(/^d.*e$/, 'dir/filez', '')).to
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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(
|
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('
|
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[
|
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[
|
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[
|
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
|
-
|
70
|
-
Parser::Diagnostic.new(level, :odd_hash, [], double('location'))
|
71
|
-
end
|
131
|
+
subject { processed_source.valid_syntax? }
|
72
132
|
|
73
|
-
|
74
|
-
|
75
|
-
end
|
133
|
+
context 'when the source is completely valid' do
|
134
|
+
let(:source) { 'def valid_code; end' }
|
76
135
|
|
77
|
-
|
78
|
-
|
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
|
86
|
-
let(:
|
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
|
146
|
+
expect(processed_source).not_to be_valid_syntax
|
90
147
|
end
|
91
148
|
end
|
92
149
|
|
93
|
-
context 'when the source has
|
94
|
-
let(:
|
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.
|
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
|
102
|
-
|
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
|
164
|
+
expect(processed_source).not_to be_valid_syntax
|
111
165
|
end
|
112
166
|
end
|
113
167
|
end
|