Class: TestBench::Output

Inherits:
Object
  • Object
show all
Includes:
ImportConstants, Telemetry::Sink::Handler
Defined in:
lib/test_bench/output/get.rb,
lib/test_bench/output/level.rb,
lib/test_bench/output/device.rb,
lib/test_bench/output/output.rb,
lib/test_bench/output/writer.rb,
lib/test_bench/output/device/null.rb,
lib/test_bench/output/writer/style.rb,
lib/test_bench/output/comment_style.rb,
lib/test_bench/output/controls/text.rb,
lib/test_bench/output/detail_policy.rb,
lib/test_bench/output/controls/event.rb,
lib/test_bench/output/controls/style.rb,
lib/test_bench/output/controls/random.rb,
lib/test_bench/output/controls/status.rb,
lib/test_bench/output/controls/session.rb,
lib/test_bench/output/device/substitute.rb,
lib/test_bench/output/writer/substitute.rb,
lib/test_bench/output/controls/comment_style.rb,
lib/test_bench/output/controls/events/failed.rb,
lib/test_bench/output/controls/events/aborted.rb,
lib/test_bench/output/controls/events/skipped.rb,
lib/test_bench/output/controls/events/detailed.rb,
lib/test_bench/output/controls/events/commented.rb,
lib/test_bench/output/controls/events/file_queued.rb,
lib/test_bench/output/controls/events/test_started.rb,
lib/test_bench/output/controls/events/file_executed.rb,
lib/test_bench/output/controls/events/test_finished.rb,
lib/test_bench/output/controls/events/file_not_found.rb,
lib/test_bench/output/controls/events/context_started.rb,
lib/test_bench/output/controls/events/context_finished.rb

Defined Under Namespace

Modules: CommentStyle, Controls, Defaults, DetailPolicy, Get, Level Classes: Branch, Device, Writer

Constant Summary collapse

Result =
Session::Result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#branchesObject



25
26
27
# File 'lib/test_bench/output/output.rb', line 25

def branches
  @branches ||= []
end

#detail_policyObject



15
16
17
# File 'lib/test_bench/output/output.rb', line 15

def detail_policy
  @detail_policy ||= DetailPolicy.on
end

#output_levelObject



20
21
22
# File 'lib/test_bench/output/output.rb', line 20

def output_level
  @output_level ||= Level.all
end

#statusObject



30
31
32
# File 'lib/test_bench/output/output.rb', line 30

def status
  @status ||= Session::Status.initial
end

#writerObject



10
11
12
# File 'lib/test_bench/output/output.rb', line 10

def writer
  @writer ||= Writer::Substitute.build
end

Class Method Details

.build(styling: nil, device: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/test_bench/output/output.rb', line 35

def self.build(styling: nil, device: nil)
  instance = new

  instance.detail_policy = Defaults.detail_policy
  instance.output_level = Defaults.output_level

  Writer.configure(instance, styling:, device:)

  instance
end

.register_telemetry_sink(session = nil, styling: nil, device: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/test_bench/output/output.rb', line 46

def self.register_telemetry_sink(session=nil, styling: nil, device: nil)
  session ||= Session.instance

  output = build(styling:, device:)

  session.register_telemetry_sink(output)

  output
end

Instance Method Details

#branch(event) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/test_bench/output/output.rb', line 122

def branch(event)
  branch = Branch.new

  branches << branch

  pend(event)

  branch
end

#current_branchObject



142
143
144
# File 'lib/test_bench/output/output.rb', line 142

def current_branch
  branches.last
end

#merge(event) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/test_bench/output/output.rb', line 104

def merge(event)
  pend(event)

  merge_branch = branches.pop

  if root?
    if not Level.output?(output_level, merge_branch.status.result)
      return
    end

    merge_branch.each do |event, status|
      resolve(event, status)
    end
  else
    current_branch.merge(merge_branch)
  end
end

#pend(event) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/test_bench/output/output.rb', line 132

def pend(event)
  status.update(event)

  if root?
    resolve(event, status)
  else
    current_branch.pend(event)
  end
end

#resolve(event, status) ⇒ Object



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
178
# File 'lib/test_bench/output/output.rb', line 150

def resolve(event, status)
  case event
  in Failed => failed
    resolve_failed(failed)
  in Aborted => aborted
    resolve_aborted(aborted)
  in Skipped => skipped
    resolve_skipped(skipped)
  in Commented => commented
    resolve_commented(commented)
  in Detailed => detailed
    resolve_detailed(detailed, status)
  in TestStarted => test_started
    resolve_test_started(test_started, status)
  in TestFinished => test_finished
    resolve_test_finished(test_finished)
  in ContextStarted => context_started
    resolve_context_started(context_started, status)
  in ContextFinished => context_finished
    resolve_context_finished(context_finished, status)
  in FileQueued => file_queued
    resolve_file_queued(file_queued, status)
  in FileExecuted => file_executed
    resolve_file_executed(file_executed)
  in FileNotFound => file_not_found
    resolve_file_not_found(file_not_found)
  else
  end
end

#resolve_aborted(aborted) ⇒ Object



189
190
191
192
193
194
195
196
197
198
# File 'lib/test_bench/output/output.rb', line 189

def resolve_aborted(aborted)
  message = aborted.message

  writer.
    indent.
    style(:bold, :red).
    print("Aborted: ").
    style(:reset_intensity).
    puts(message)
end

#resolve_commented(commented) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/test_bench/output/output.rb', line 220

def resolve_commented(commented)
  text = commented.text

  disposition = commented.disposition
  style = CommentStyle.fetch(disposition)

  case style
  when CommentStyle.detect
    if text.end_with?("\n")
      style = CommentStyle.block
    else
      style = CommentStyle.normal
    end

  when CommentStyle.raw
    writer.
      print(text).
      print("\n")

    return
  end

  if text.empty?
    writer.
      indent.
      style(:faint, :italic)

    if style == CommentStyle.heading
      writer.puts('(no heading)')

      if not writer.styling?
        writer.
          indent.
          puts("- - -")
      end
    else
      writer.puts('(empty)')
    end

    return
  end

  case style
  when CommentStyle.normal
    writer.
      indent.
      puts(text)

  when CommentStyle.heading
    writer.
      indent.
      style(:bold).
      puts(text)

    if not writer.styling?
      writer.
        indent.
        puts("- - -")
    end

  when CommentStyle.block
    text.each_line do |line|
      writer.
        indent.
        style(:reverse_video)

      if writer.styling?
        writer.print(' ')
      else
        writer.print('>')
      end

      writer.
        style(:reset_reverse_video).
        print(' ').
        puts(line)
    end

  when CommentStyle.line_number
    lines = text.each_line

    final_line_number_width = lines.count.to_s.length
    marker_width = final_line_number_width + 2

    lines.with_index(1) do |line, line_number|
      line_marker = "#{line_number}.".ljust(marker_width)

      writer.
        indent.
        style(:faint).
        print(line_marker).
        style(:reset_intensity).
        puts(line)
    end
  end
end

#resolve_context_finished(context_finished, status) ⇒ Object



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/test_bench/output/output.rb', line 402

def resolve_context_finished(context_finished, status)
  title = context_finished.title

  if title.nil?
    return
  end

  writer.decrease_indentation

  if not writer.indentation_depth.zero?
    return
  end

  error_count = status.error_sequence
  failure_count = status.failure_sequence
  skip_count = status.skip_sequence

  if error_count > 0 || failure_count > 0 || skip_count > 0
    writer.puts
  end

  if not error_count.zero?
    writer.
      style(:red).
      print("Errors: ").
      style(:bold).
      puts("#{status.error_sequence}")
  end

  if not failure_count.zero?
    writer.
      style(:red).
      print("Failures: ").
      style(:bold).
      puts("#{status.failure_sequence}")
  end

  if not skip_count.zero?
    writer.
      style(:yellow).
      print("Skipped: ").
      style(:bold).
      puts("#{status.skip_sequence}")
  end
end

#resolve_context_started(context_started, status) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/test_bench/output/output.rb', line 371

def resolve_context_started(context_started, status)
  title = context_started.title

  if title.nil?
    return
  end

  writer.indent

  result = status.result

  case result
  when Result.aborted, Result.failed
    writer.style(:red)
  when Result.passed, Result.incomplete
    writer.style(:green)
  end

  writer.print(title)

  if not writer.styling?
    if result == Result.aborted
      writer.print(" (aborted)")
    end
  end

  writer.puts

  writer.increase_indentation
end

#resolve_detailed(detailed, status) ⇒ Object



317
318
319
320
321
322
323
324
325
# File 'lib/test_bench/output/output.rb', line 317

def resolve_detailed(detailed, status)
  result = status.result

  print_detail = DetailPolicy.detail?(detail_policy, result)

  if print_detail
    resolve_commented(detailed)
  end
end

#resolve_failed(failed) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/test_bench/output/output.rb', line 180

def resolve_failed(failed)
  message = failed.message

  writer.
    indent.
    style(:red).
    puts(message)
end

#resolve_file_executed(file_executed) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/test_bench/output/output.rb', line 465

def resolve_file_executed(file_executed)
  indented = writer.indentation_depth > 0

  result = file_executed.result

  if indented
    file = file_executed.file

    writer.
      indent.
      style(:italic).
      print("Ran #{file}")

    if result == Result.none
      writer.
        print(' ').
        style(:faint).
        print("(no tests)")
    end

    writer.puts
  else
    if result == Result.none
      writer.
        style(:faint, :italic).
        puts("(no tests)")
    end

    writer.puts
  end
end

#resolve_file_not_found(file_not_found) ⇒ Object



497
498
499
500
501
502
503
504
505
506
# File 'lib/test_bench/output/output.rb', line 497

def resolve_file_not_found(file_not_found)
  file = file_not_found.file

  writer.
    style(:red).
    print("File not found: ").
    style(:bold).
    puts(file).
    puts
end

#resolve_file_queued(file_queued, status) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/test_bench/output/output.rb', line 448

def resolve_file_queued(file_queued, status)
  writer.indent

  if status.result == Session::Result.aborted
    writer.style(:bold, :red)
  end

  file = file_queued.file
  writer.print("Running #{file}")

  if !writer.styling? && status.result == Session::Result.aborted
    writer.print(" (aborted)")
  end

  writer.puts
end

#resolve_skipped(skipped) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/test_bench/output/output.rb', line 200

def resolve_skipped(skipped)
  message = skipped.message

  writer.
    indent.
    style(:yellow)

  if not message.nil?
    writer.print(message)

    if not writer.styling?
      writer.print(" (skipped)")
    end
  else
    writer.print("Skipped")
  end

  writer.puts
end

#resolve_test_finished(test_finished) ⇒ Object



363
364
365
366
367
368
369
# File 'lib/test_bench/output/output.rb', line 363

def resolve_test_finished(test_finished)
  title = test_finished.title

  if not title.nil?
    writer.decrease_indentation
  end
end

#resolve_test_started(test_started, status) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/test_bench/output/output.rb', line 327

def resolve_test_started(test_started, status)
  title = test_started.title

  if title.nil?
    return
  end

  writer.indent

  result = status.result

  case result
  when Result.passed
    writer.style(:green)
  when Result.failed, Result.aborted, Result.incomplete, Result.none
    writer.style(:bold, :red)
  end

  writer.print(title)

  if not writer.styling?
    case result
    when Result.aborted
      writer.print(" (aborted)")
    when Result.failed
      writer.print(" (failed)")
    when Result.incomplete, Result.none
      writer.print(" (inconclusive)")
    end
  end

  writer.puts

  writer.increase_indentation
end

#root?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/test_bench/output/output.rb', line 146

def root?
  current_branch.nil?
end