forked from rubocop/rubocop
/
json_formatter_spec.rb
177 lines (145 loc) · 5.06 KB
/
json_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# frozen_string_literal: true
RSpec.describe RuboCop::Formatter::JSONFormatter do
subject(:formatter) { described_class.new(output) }
let(:output) { StringIO.new }
let(:files) { %w[/path/to/file1 /path/to/file2] }
let(:location) do
source_buffer = Parser::Source::Buffer.new('test', 1)
source_buffer.source = %w[a b cdefghi].join("\n")
Parser::Source::Range.new(source_buffer, 2, 10)
end
let(:offense) do
RuboCop::Cop::Offense.new(:convention, location,
'This is message', 'CopName', :corrected)
end
describe '#started' do
let(:summary) { formatter.output_hash[:summary] }
it 'sets target file count in summary' do
expect(summary[:target_file_count].nil?).to be(true)
formatter.started(%w[/path/to/file1 /path/to/file2])
expect(summary[:target_file_count]).to eq(2)
end
end
describe '#file_finished' do
before do
count = 0
allow(formatter).to receive(:hash_for_file) do
count += 1
end
end
let(:summary) { formatter.output_hash[:summary] }
it 'adds detected offense count in summary' do
expect(summary[:offense_count]).to eq(0)
formatter.file_started(files[0], {})
expect(summary[:offense_count]).to eq(0)
formatter.file_finished(files[0], [
instance_double(RuboCop::Cop::Offense),
instance_double(RuboCop::Cop::Offense)
])
expect(summary[:offense_count]).to eq(2)
end
it 'adds value of #hash_for_file to #output_hash[:files]' do
expect(formatter.output_hash[:files].empty?).to be(true)
formatter.file_started(files[0], {})
expect(formatter.output_hash[:files].empty?).to be(true)
formatter.file_finished(files[0], [])
expect(formatter.output_hash[:files]).to eq([1])
formatter.file_started(files[1], {})
expect(formatter.output_hash[:files]).to eq([1])
formatter.file_finished(files[1], [])
expect(formatter.output_hash[:files]).to eq([1, 2])
end
end
describe '#finished' do
let(:summary) { formatter.output_hash[:summary] }
it 'sets inspected file count in summary' do
expect(summary[:inspected_file_count].nil?).to be(true)
formatter.finished(%w[/path/to/file1 /path/to/file2])
expect(summary[:inspected_file_count]).to eq(2)
end
it 'outputs #output_hash as JSON' do
formatter.finished(files)
json = output.string
restored_hash = JSON.parse(json, symbolize_names: true)
expect(restored_hash).to eq(formatter.output_hash)
end
end
describe '#hash_for_file' do
subject(:hash) { formatter.hash_for_file(file, offenses) }
let(:file) { File.expand_path('spec/spec_helper.rb') }
let(:offenses) do
[
instance_double(RuboCop::Cop::Offense),
instance_double(RuboCop::Cop::Offense)
]
end
before do
count = 0
allow(formatter).to receive(:hash_for_offense) do
count += 1
end
end
it 'sets relative file path for :path key' do
expect(hash[:path]).to eq('spec/spec_helper.rb')
end
it 'sets an array of #hash_for_offense values for :offenses key' do
expect(hash[:offenses]).to eq([1, 2])
end
end
describe '#hash_for_offense' do
subject(:hash) { formatter.hash_for_offense(offense) }
it 'sets Offense#severity value for :severity key' do
expect(hash[:severity]).to eq(:convention)
end
it 'sets Offense#message value for :message key' do
expect(hash[:message]).to eq('This is message')
end
it 'sets Offense#cop_name value for :cop_name key' do
expect(hash[:cop_name]).to eq('CopName')
end
it 'sets Offense#correctable? value for :correctable key' do
expect(hash[:correctable]).to be_truthy
end
it 'sets Offense#corrected? value for :corrected key' do
expect(hash[:corrected]).to be_truthy
end
it 'sets value of #hash_for_location for :location key' do
location_hash = {
start_line: 2,
start_column: 1,
last_line: 3,
last_column: 6,
length: 8,
line: 2,
column: 1
}
expect(hash[:location]).to eq(location_hash)
end
end
describe '#hash_for_location' do
subject(:hash) { formatter.hash_for_location(offense) }
it 'sets line value for :line key' do
expect(hash[:line]).to eq(2)
end
it 'sets column value for :column key' do
expect(hash[:column]).to eq(1)
end
it 'sets length value for :length key' do
expect(hash[:length]).to eq(8)
end
context 'when the location is pseudo' do
let(:location) { RuboCop::Cop::Offense::NO_LOCATION }
it 'returns a valid hash' do
expect(hash).to eq({
start_line: 1,
start_column: 1,
last_line: 1,
last_column: 0,
length: 0,
line: 1,
column: 1
})
end
end
end
end