forked from rubocop/rubocop
/
emacs_style_formatter_spec.rb
107 lines (88 loc) · 3.12 KB
/
emacs_style_formatter_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true
RSpec.describe RuboCop::Formatter::EmacsStyleFormatter, :config do
subject(:formatter) { described_class.new(output) }
let(:source) { %w[a b cdefghi].join("\n") }
let(:output) { StringIO.new }
describe '#file_finished' do
it 'displays parsable text' do
cop.add_offense(
nil,
location: Parser::Source::Range.new(source_buffer, 0, 1),
message: 'message 1'
)
cop.add_offense(
nil,
location: Parser::Source::Range.new(source_buffer, 9, 10),
message: 'message 2'
)
formatter.file_finished('test', cop.offenses)
expect(output.string).to eq <<~OUTPUT
test:1:1: C: message 1
test:3:6: C: message 2
OUTPUT
end
context 'when the offense is automatically corrected' do
let(:file) { '/path/to/file' }
let(:offense) do
RuboCop::Cop::Offense.new(:convention, location,
'This is a message.', 'CopName', status)
end
let(:location) do
source_buffer = Parser::Source::Buffer.new('test', 1)
source_buffer.source = "a\n"
Parser::Source::Range.new(source_buffer, 0, 1)
end
let(:status) { :corrected }
it 'prints [Corrected] along with message' do
formatter.file_finished(file, [offense])
expect(output.string)
.to include(': [Corrected] This is a message.')
end
end
context 'when the offense is marked as todo' do
let(:file) { '/path/to/file' }
let(:offense) do
RuboCop::Cop::Offense.new(:convention, location,
'This is a message.', 'CopName', status)
end
let(:location) do
source_buffer = Parser::Source::Buffer.new('test', 1)
source_buffer.source = "a\n"
Parser::Source::Range.new(source_buffer, 0, 1)
end
let(:status) { :corrected_with_todo }
it 'prints [Todo] along with message' do
formatter.file_finished(file, [offense])
expect(output.string)
.to include(': [Todo] This is a message.')
end
end
context 'when the offense message contains a newline' do
let(:file) { '/path/to/file' }
let(:offense) do
RuboCop::Cop::Offense.new(:error, location,
"unmatched close parenthesis: /\n world " \
"# Some comment containing a )\n/",
'CopName', :uncorrected)
end
let(:location) do
source_buffer = Parser::Source::Buffer.new('test', 1)
source_buffer.source = "a\n"
Parser::Source::Range.new(source_buffer, 0, 1)
end
it 'strips newlines out of the error message' do
formatter.file_finished(file, [offense])
expect(output.string).to eq(
'/path/to/file:1:1: E: unmatched close parenthesis: / ' \
"world # Some comment containing a ) /\n"
)
end
end
end
describe '#finished' do
it 'does not report summary' do
formatter.finished(['/path/to/file'])
expect(output.string.empty?).to be(true)
end
end
end