rspec-tap-formatters 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,407 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ RSpec.describe RSpec::TAP::Formatters::Default do
6
+ subject(:formatter) { described_class.new(report_output) }
7
+
8
+ let(:report_output) { StringIO.new }
9
+ let(:report_printer) { RSpec::TAP::Formatters::Printer.new(report_output) }
10
+ let(:report_test_stats) { RSpec::TAP::Formatters::TestStats.new }
11
+
12
+ before do
13
+ formatter.instance_variable_set(:@printer, report_printer)
14
+ formatter.instance_variable_set(:@test_stats, report_test_stats)
15
+ formatter.instance_variable_set(:@seed, nil)
16
+ formatter.instance_variable_set(:@level, 0)
17
+ formatter.instance_variable_set(:@example_number, 0)
18
+ end
19
+
20
+ describe '#seed' do
21
+ let(:seed) { SecureRandom.random_number(10_000) }
22
+
23
+ context 'with seed used' do
24
+ let(:notification) { OpenStruct.new(seed: seed, seed_used?: true) }
25
+
26
+ it 'updates instance variable' do
27
+ expect { formatter.seed(notification) }
28
+ .to change { formatter.instance_variable_get(:@seed) }
29
+ .from(nil)
30
+ .to(seed)
31
+ end
32
+ end
33
+
34
+ context 'without seed used' do
35
+ let(:notification) { OpenStruct.new(seed: seed, seed_used?: false) }
36
+
37
+ it 'does not update instance variable' do
38
+ expect { formatter.seed(notification) }
39
+ .not_to change { formatter.instance_variable_get(:@seed) }
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#start' do
45
+ let(:count) { 1 + SecureRandom.random_number(5) }
46
+ let(:notification) { OpenStruct.new(count: count) }
47
+
48
+ it 'delegates to printer' do
49
+ allow(report_printer).to receive(:start_output)
50
+
51
+ formatter.start(notification)
52
+
53
+ expect(report_printer).to have_received(:start_output).with(no_args)
54
+ end
55
+ end
56
+
57
+ describe '#start_dump' do
58
+ it 'delegates to printer' do
59
+ allow(report_printer).to receive(:example_progress_dump)
60
+
61
+ formatter.start_dump(OpenStruct.new)
62
+
63
+ expect(report_printer).to have_received(:example_progress_dump)
64
+ .with(no_args)
65
+ end
66
+ end
67
+
68
+ describe '#example_group_started' do
69
+ let(:level) { 1 + SecureRandom.random_number(5) }
70
+ let(:example_number) { 1 + SecureRandom.random_number(5) }
71
+
72
+ let(:description) { 'test-or-group-foo' }
73
+ let(:group) { OpenStruct.new(description: description) }
74
+ let(:notification) { OpenStruct.new(group: group) }
75
+
76
+ before do
77
+ formatter.instance_variable_set(:@level, level)
78
+ formatter.instance_variable_set(:@example_number, example_number)
79
+ end
80
+
81
+ it 'delegates to printer' do
82
+ allow(report_printer).to receive(:group_start_output)
83
+
84
+ formatter.example_group_started(notification)
85
+
86
+ expect(report_printer).to have_received(:group_start_output)
87
+ .with(notification, level)
88
+ end
89
+
90
+ it 'increments level by one' do
91
+ expect { formatter.example_group_started(notification) }
92
+ .to change { formatter.instance_variable_get(:@level) }
93
+ .by(1)
94
+ end
95
+
96
+ it 'resets example number to zero' do
97
+ expect { formatter.example_group_started(notification) }
98
+ .to change { formatter.instance_variable_get(:@example_number) }
99
+ .from(example_number)
100
+ .to(0)
101
+ end
102
+ end
103
+
104
+ describe '#example_group_finished' do
105
+ let(:passed) { 1 + SecureRandom.random_number(5) }
106
+ let(:failed) { 1 + SecureRandom.random_number(5) }
107
+ let(:pending) { 1 + SecureRandom.random_number(5) }
108
+ let(:tests) { passed + failed + pending }
109
+ let(:stats_data) { { line_number => [tests, passed, failed, pending] } }
110
+
111
+ let(:group) { OpenStruct.new(metadata: { line_number: line_number }) }
112
+ let(:notification) { OpenStruct.new(group: group) }
113
+
114
+ before do
115
+ formatter.instance_variable_set(:@level, level)
116
+ formatter.instance_variable_get(:@test_stats)
117
+ .instance_variable_set(:@data, stats_data)
118
+ end
119
+
120
+ context 'when root level test' do
121
+ let(:level) { 0 }
122
+ let(:line_number) { 1 + SecureRandom.random_number(5) }
123
+
124
+ it 'delegates to printer' do
125
+ allow(report_printer).to receive(:group_finished_output)
126
+
127
+ formatter.example_group_finished(notification)
128
+
129
+ expect(report_printer).to have_received(:group_finished_output)
130
+ .with(stats_data[line_number], level)
131
+ end
132
+
133
+ it 'does not decrement level' do
134
+ expect { formatter.example_group_finished(notification) }
135
+ .not_to change { formatter.instance_variable_get(:@level) }
136
+ end
137
+
138
+ it 'instantiates test stats object' do
139
+ expect { formatter.example_group_finished(notification) }
140
+ .to change {
141
+ formatter.instance_variable_get(:@test_stats).object_id
142
+ }
143
+ end
144
+ end
145
+
146
+ context 'when non-root level test' do
147
+ let(:level) { 2 }
148
+ let(:line_number) { 1 + SecureRandom.random_number(5) }
149
+
150
+ it 'delegates to printer' do
151
+ allow(report_printer).to receive(:group_finished_output)
152
+
153
+ formatter.example_group_finished(notification)
154
+
155
+ expect(report_printer).to have_received(:group_finished_output)
156
+ .with(stats_data[line_number], level)
157
+ end
158
+
159
+ it 'decrements level' do
160
+ expect { formatter.example_group_finished(notification) }
161
+ .to change { formatter.instance_variable_get(:@level) }
162
+ .by(-1)
163
+ end
164
+
165
+ it 'does not instantiate test stats object' do
166
+ expect { formatter.example_group_finished(notification) }
167
+ .not_to change {
168
+ formatter.instance_variable_get(:@test_stats).object_id
169
+ }
170
+ end
171
+ end
172
+ end
173
+
174
+ describe '#example_started' do
175
+ let(:example_number) { 1 + SecureRandom.random_number(5) }
176
+
177
+ it 'increments example number by one' do
178
+ expect { formatter.example_started(OpenStruct.new) }
179
+ .to change { formatter.instance_variable_get(:@example_number) }
180
+ .by(1)
181
+ end
182
+ end
183
+
184
+ describe '#example_passed' do
185
+ let(:level) { 1 + SecureRandom.random_number(5) }
186
+ let(:example_number) { 1 + SecureRandom.random_number(5) }
187
+ let(:example_status) { :success }
188
+ let(:example_status_index) { 1 }
189
+
190
+ let(:description) { 'example-foo' }
191
+ let(:example) { OpenStruct.new(description: description) }
192
+ let(:notification) { OpenStruct.new(example: example) }
193
+
194
+ before do
195
+ formatter.instance_variable_set(:@level, level)
196
+ formatter.instance_variable_set(:@example_number, example_number)
197
+
198
+ allow(report_test_stats).to receive(:populate)
199
+ end
200
+
201
+ it 'populates test stats' do
202
+ formatter.example_passed(notification)
203
+
204
+ expect(report_test_stats).to have_received(:populate)
205
+ .with(notification, example_status_index)
206
+ end
207
+
208
+ it 'delegates progress report to printer' do
209
+ allow(report_printer).to receive(:example_progress_output)
210
+
211
+ formatter.example_passed(notification)
212
+
213
+ expect(report_printer).to have_received(:example_progress_output)
214
+ .with(example_status)
215
+ end
216
+
217
+ it 'delegates status report to printer' do
218
+ allow(report_printer).to receive(:success_output)
219
+
220
+ formatter.example_passed(notification)
221
+
222
+ expect(report_printer).to have_received(:success_output)
223
+ .with(description, example_number, level)
224
+ end
225
+ end
226
+
227
+ describe '#example_failed' do
228
+ let(:level) { 1 + SecureRandom.random_number(5) }
229
+ let(:example_number) { 1 + SecureRandom.random_number(5) }
230
+ let(:example_status) { :failure }
231
+ let(:example_status_index) { 2 }
232
+
233
+ let(:description) { 'example-foo' }
234
+ let(:example) { OpenStruct.new(description: description) }
235
+ let(:notification) { OpenStruct.new(example: example) }
236
+
237
+ before do
238
+ formatter.instance_variable_set(:@level, level)
239
+ formatter.instance_variable_set(:@example_number, example_number)
240
+
241
+ allow(report_test_stats).to receive(:populate)
242
+ allow(report_printer).to receive(:failure_reason_output)
243
+ end
244
+
245
+ it 'populates test stats' do
246
+ formatter.example_failed(notification)
247
+
248
+ expect(report_test_stats).to have_received(:populate)
249
+ .with(notification, example_status_index)
250
+ end
251
+
252
+ it 'delegates progress report to printer' do
253
+ allow(report_printer).to receive(:example_progress_output)
254
+
255
+ formatter.example_failed(notification)
256
+
257
+ expect(report_printer).to have_received(:example_progress_output)
258
+ .with(example_status)
259
+ end
260
+
261
+ it 'delegates status report to printer' do
262
+ allow(report_printer).to receive(:failure_output)
263
+
264
+ formatter.example_failed(notification)
265
+
266
+ expect(report_printer).to have_received(:failure_output)
267
+ .with(description, example_number, level)
268
+ end
269
+
270
+ it 'delegates reason to printer' do
271
+ formatter.example_failed(notification)
272
+
273
+ expect(report_printer).to have_received(:failure_reason_output)
274
+ .with(notification, level + 1)
275
+ end
276
+ end
277
+
278
+ describe '#example_pending' do
279
+ let(:level) { 1 + SecureRandom.random_number(5) }
280
+ let(:example_number) { 1 + SecureRandom.random_number(5) }
281
+ let(:example_status) { :pending }
282
+ let(:example_status_index) { 3 }
283
+
284
+ let(:description) { 'example-foo' }
285
+ let(:example) do
286
+ OpenStruct.new(
287
+ description: description,
288
+ execution_result: execution_result
289
+ )
290
+ end
291
+ let(:notification) { OpenStruct.new(example: example) }
292
+
293
+ before do
294
+ formatter.instance_variable_set(:@level, level)
295
+ formatter.instance_variable_set(:@example_number, example_number)
296
+
297
+ allow(report_test_stats).to receive(:populate)
298
+ end
299
+
300
+ shared_examples_for 'pending example' do
301
+ it 'populates test stats' do
302
+ formatter.example_pending(notification)
303
+
304
+ expect(report_test_stats).to have_received(:populate)
305
+ .with(notification, example_status_index)
306
+ end
307
+
308
+ it 'delegates progress report to printer' do
309
+ allow(report_printer).to receive(:example_progress_output)
310
+
311
+ formatter.example_pending(notification)
312
+
313
+ expect(report_printer).to have_received(:example_progress_output)
314
+ .with(example_status)
315
+ end
316
+
317
+ it 'delegates status report to printer' do
318
+ allow(report_printer).to receive(:pending_output)
319
+
320
+ formatter.example_pending(notification)
321
+
322
+ expect(report_printer).to have_received(:pending_output)
323
+ .with(notification, description, example_number, level)
324
+ end
325
+ end
326
+
327
+ context 'with pending' do
328
+ let(:pending_message) { "pending-#{SecureRandom.hex}" }
329
+ let(:directive) { "TODO: #{pending_message}" }
330
+ let(:execution_result) do
331
+ OpenStruct.new(
332
+ pending_message: pending_message,
333
+ example_skipped?: false
334
+ )
335
+ end
336
+
337
+ include_examples('pending example')
338
+ end
339
+
340
+ context 'with skipped' do
341
+ let(:pending_message) { "skip-#{SecureRandom.hex}" }
342
+ let(:directive) { "SKIP: #{pending_message}" }
343
+ let(:execution_result) do
344
+ OpenStruct.new(
345
+ pending_message: pending_message,
346
+ example_skipped?: true
347
+ )
348
+ end
349
+
350
+ include_examples('pending example')
351
+ end
352
+ end
353
+
354
+ describe '#message' do
355
+ let(:notification) { OpenStruct.new }
356
+
357
+ it 'delegates to printer' do
358
+ allow(report_printer).to receive(:message_output)
359
+
360
+ formatter.message(notification)
361
+
362
+ expect(report_printer).to have_received(:message_output)
363
+ .with(notification)
364
+ end
365
+ end
366
+
367
+ describe '#dump_failures' do
368
+ let(:notification) { OpenStruct.new }
369
+
370
+ it 'delegates to printer' do
371
+ allow(report_printer).to receive(:store_failed_examples_summary)
372
+
373
+ formatter.dump_failures(notification)
374
+
375
+ expect(report_printer).to have_received(:store_failed_examples_summary)
376
+ .with(notification)
377
+ end
378
+ end
379
+
380
+ describe '#dump_pending' do
381
+ let(:notification) { OpenStruct.new }
382
+
383
+ it 'delegates to printer' do
384
+ allow(report_printer).to receive(:store_pending_examples_summary)
385
+
386
+ formatter.dump_pending(notification)
387
+
388
+ expect(report_printer).to have_received(:store_pending_examples_summary)
389
+ .with(notification)
390
+ end
391
+ end
392
+
393
+ describe '#dump_summary' do
394
+ let(:seed) { 1 + SecureRandom.random_number(10_000) }
395
+ let(:notification) { OpenStruct.new }
396
+
397
+ it 'delegates to printer' do
398
+ allow(report_printer).to receive(:summary_output)
399
+
400
+ formatter.instance_variable_set(:@seed, seed)
401
+ formatter.dump_summary(notification)
402
+
403
+ expect(report_printer).to have_received(:summary_output)
404
+ .with(notification, seed)
405
+ end
406
+ end
407
+ end
@@ -0,0 +1,257 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ RSpec.describe RSpec::TAP::Formatters::FlatCompact do
6
+ subject(:formatter) { described_class.new(report_output) }
7
+
8
+ let(:report_output) { StringIO.new }
9
+ let(:report_printer) { RSpec::TAP::Formatters::Printer.new(report_output) }
10
+
11
+ before do
12
+ formatter.instance_variable_set(:@printer, report_printer)
13
+ formatter.instance_variable_set(:@seed, nil)
14
+ formatter.instance_variable_set(:@example_number, 0)
15
+ end
16
+
17
+ describe '#seed' do
18
+ let(:seed) { SecureRandom.random_number(10_000) }
19
+
20
+ context 'with seed used' do
21
+ let(:notification) { OpenStruct.new(seed: seed, seed_used?: true) }
22
+
23
+ it 'updates instance variable' do
24
+ expect { formatter.seed(notification) }
25
+ .to change { formatter.instance_variable_get(:@seed) }
26
+ .from(nil)
27
+ .to(seed)
28
+ end
29
+ end
30
+
31
+ context 'without seed used' do
32
+ let(:notification) { OpenStruct.new(seed: seed, seed_used?: false) }
33
+
34
+ it 'does not update instance variable' do
35
+ expect { formatter.seed(notification) }
36
+ .not_to change { formatter.instance_variable_get(:@seed) }
37
+ end
38
+ end
39
+ end
40
+
41
+ describe '#start' do
42
+ let(:count) { 1 + SecureRandom.random_number(5) }
43
+ let(:notification) { OpenStruct.new(count: count) }
44
+
45
+ it 'delegates to printer' do
46
+ allow(report_printer).to receive(:start_output)
47
+
48
+ formatter.start(notification)
49
+
50
+ expect(report_printer).to have_received(:start_output).with(no_args)
51
+ end
52
+ end
53
+
54
+ describe '#start_dump' do
55
+ it 'delegates to printer' do
56
+ allow(report_printer).to receive(:example_progress_dump)
57
+
58
+ formatter.start_dump(OpenStruct.new)
59
+
60
+ expect(report_printer).to have_received(:example_progress_dump)
61
+ .with(no_args)
62
+ end
63
+ end
64
+
65
+ describe '#example_started' do
66
+ let(:example_number) { 1 + SecureRandom.random_number(5) }
67
+
68
+ it 'increments example number by one' do
69
+ expect { formatter.example_started(OpenStruct.new) }
70
+ .to change { formatter.instance_variable_get(:@example_number) }
71
+ .by(1)
72
+ end
73
+ end
74
+
75
+ describe '#example_passed' do
76
+ let(:example_number) { 1 + SecureRandom.random_number(5) }
77
+ let(:example_status) { :success }
78
+ let(:example_status_index) { 1 }
79
+
80
+ let(:description) { 'example-foo' }
81
+ let(:example) { OpenStruct.new(full_description: description) }
82
+ let(:notification) { OpenStruct.new(example: example) }
83
+
84
+ before do
85
+ formatter.instance_variable_set(:@example_number, example_number)
86
+ end
87
+
88
+ it 'delegates progress report to printer' do
89
+ allow(report_printer).to receive(:example_progress_output)
90
+
91
+ formatter.example_passed(notification)
92
+
93
+ expect(report_printer).to have_received(:example_progress_output)
94
+ .with(example_status)
95
+ end
96
+
97
+ it 'delegates status report to printer' do
98
+ allow(report_printer).to receive(:success_output)
99
+
100
+ formatter.example_passed(notification)
101
+
102
+ expect(report_printer).to have_received(:success_output)
103
+ .with(description, example_number, 0)
104
+ end
105
+ end
106
+
107
+ describe '#example_failed' do
108
+ let(:example_number) { 1 + SecureRandom.random_number(5) }
109
+ let(:example_status) { :failure }
110
+ let(:example_status_index) { 2 }
111
+
112
+ let(:description) { 'example-foo' }
113
+ let(:example) { OpenStruct.new(full_description: description) }
114
+ let(:notification) { OpenStruct.new(example: example) }
115
+
116
+ before do
117
+ formatter.instance_variable_set(:@example_number, example_number)
118
+ end
119
+
120
+ it 'delegates progress report to printer' do
121
+ allow(report_printer).to receive(:example_progress_output)
122
+
123
+ formatter.example_failed(notification)
124
+
125
+ expect(report_printer).to have_received(:example_progress_output)
126
+ .with(example_status)
127
+ end
128
+
129
+ it 'delegates status report to printer' do
130
+ allow(report_printer).to receive(:failure_output)
131
+
132
+ formatter.example_failed(notification)
133
+
134
+ expect(report_printer).to have_received(:failure_output)
135
+ .with(description, example_number, 0)
136
+ end
137
+ end
138
+
139
+ describe '#example_pending' do
140
+ let(:example_number) { 1 + SecureRandom.random_number(5) }
141
+ let(:example_status) { :pending }
142
+ let(:example_status_index) { 3 }
143
+
144
+ let(:description) { 'example-foo' }
145
+ let(:example) do
146
+ OpenStruct.new(
147
+ full_description: description,
148
+ execution_result: execution_result
149
+ )
150
+ end
151
+ let(:notification) { OpenStruct.new(example: example) }
152
+
153
+ before do
154
+ formatter.instance_variable_set(:@example_number, example_number)
155
+ end
156
+
157
+ shared_examples_for 'pending example' do
158
+ it 'delegates progress report to printer' do
159
+ allow(report_printer).to receive(:example_progress_output)
160
+
161
+ formatter.example_pending(notification)
162
+
163
+ expect(report_printer).to have_received(:example_progress_output)
164
+ .with(example_status)
165
+ end
166
+
167
+ it 'delegates status report to printer' do
168
+ allow(report_printer).to receive(:pending_output)
169
+
170
+ formatter.example_pending(notification)
171
+
172
+ expect(report_printer).to have_received(:pending_output)
173
+ .with(notification, description, example_number, 0)
174
+ end
175
+ end
176
+
177
+ context 'with pending' do
178
+ let(:pending_message) { "pending-#{SecureRandom.hex}" }
179
+ let(:directive) { "TODO: #{pending_message}" }
180
+ let(:execution_result) do
181
+ OpenStruct.new(
182
+ pending_message: pending_message,
183
+ example_skipped?: false
184
+ )
185
+ end
186
+
187
+ include_examples('pending example')
188
+ end
189
+
190
+ context 'with skipped' do
191
+ let(:pending_message) { "skip-#{SecureRandom.hex}" }
192
+ let(:directive) { "SKIP: #{pending_message}" }
193
+ let(:execution_result) do
194
+ OpenStruct.new(
195
+ pending_message: pending_message,
196
+ example_skipped?: true
197
+ )
198
+ end
199
+
200
+ include_examples('pending example')
201
+ end
202
+ end
203
+
204
+ describe '#message' do
205
+ let(:notification) { OpenStruct.new }
206
+
207
+ it 'delegates to printer' do
208
+ allow(report_printer).to receive(:message_output)
209
+
210
+ formatter.message(notification)
211
+
212
+ expect(report_printer).to have_received(:message_output)
213
+ .with(notification)
214
+ end
215
+ end
216
+
217
+ describe '#dump_failures' do
218
+ let(:notification) { OpenStruct.new }
219
+
220
+ it 'delegates to printer' do
221
+ allow(report_printer).to receive(:store_failed_examples_summary)
222
+
223
+ formatter.dump_failures(notification)
224
+
225
+ expect(report_printer).to have_received(:store_failed_examples_summary)
226
+ .with(notification)
227
+ end
228
+ end
229
+
230
+ describe '#dump_pending' do
231
+ let(:notification) { OpenStruct.new }
232
+
233
+ it 'delegates to printer' do
234
+ allow(report_printer).to receive(:store_pending_examples_summary)
235
+
236
+ formatter.dump_pending(notification)
237
+
238
+ expect(report_printer).to have_received(:store_pending_examples_summary)
239
+ .with(notification)
240
+ end
241
+ end
242
+
243
+ describe '#dump_summary' do
244
+ let(:seed) { 1 + SecureRandom.random_number(10_000) }
245
+ let(:notification) { OpenStruct.new }
246
+
247
+ it 'delegates to printer' do
248
+ allow(report_printer).to receive(:summary_output)
249
+
250
+ formatter.instance_variable_set(:@seed, seed)
251
+ formatter.dump_summary(notification)
252
+
253
+ expect(report_printer).to have_received(:summary_output)
254
+ .with(notification, seed)
255
+ end
256
+ end
257
+ end