Class: TestBench::Output::Raw

Inherits:
Object
  • Object
show all
Includes:
Fixture::Output, PrintError, Writer::Dependency
Defined in:
lib/test_bench/output/raw.rb

Defined Under Namespace

Modules: Defaults

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Attributes included from PrintError

#omit_backtrace_pattern, #reverse_backtraces

Attributes included from Writer::Dependency

#writer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PrintError

call, error, error_backtrace, error_cause, error_message, #print_error

Instance Attribute Details

#current_batchObject

Returns the value of attribute current_batch.



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

def current_batch
  @current_batch
end

#detail_settingObject



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

def detail_setting
  @detail_setting ||= Defaults.detail
end

#previous_byte_offsetObject



38
39
40
# File 'lib/test_bench/output/raw.rb', line 38

def previous_byte_offset
  @previous_byte_offset ||= 0
end

#verboseObject Also known as: verbose?



11
12
13
14
15
# File 'lib/test_bench/output/raw.rb', line 11

def verbose
  instance_variable_defined?(:@verbose) ?
    @verbose :
    @verbose = Defaults.verbose
end

Class Method Details

.assure_detail_setting(detail_setting) ⇒ Object



318
319
320
321
322
# File 'lib/test_bench/output/raw.rb', line 318

def self.assure_detail_setting(detail_setting)
  unless detail_settings.include?(detail_setting)
    raise Error, "Invalid detail setting #{detail_setting.inspect} (Valid values: #{detail_settings.map(&:inspect).join(', ')})"
  end
end

.build(verbose: nil, detail: nil, omit_backtrace_pattern: nil, reverse_backtraces: nil, writer: nil, device: nil, styling: nil) ⇒ Object



57
58
59
60
61
# File 'lib/test_bench/output/raw.rb', line 57

def self.build(verbose: nil, detail: nil, omit_backtrace_pattern: nil, reverse_backtraces: nil, writer: nil, device: nil, styling: nil)
  instance = new
  instance.configure(verbose: verbose, detail: detail, omit_backtrace_pattern: omit_backtrace_pattern, reverse_backtraces: reverse_backtraces, writer: writer, device: device, styling: styling)
  instance
end

.configure(receiver, attr_name: nil, **args) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/test_bench/output/raw.rb', line 63

def self.configure(receiver, attr_name: nil, **args)
  attr_name ||= :raw_output

  instance = build(**args)
  receiver.public_send(:"#{attr_name}=", instance)
  instance
end

.default_detail_settingObject



332
333
334
# File 'lib/test_bench/output/raw.rb', line 332

def self.default_detail_setting
  detail_settings.fetch(0)
end

.detail_settingsObject



324
325
326
327
328
329
330
# File 'lib/test_bench/output/raw.rb', line 324

def self.detail_settings
  [
    :failure,
    :on,
    :off
  ]
end

Instance Method Details

#batch_finished(batch_data) ⇒ Object



32
33
34
35
36
# File 'lib/test_bench/output/raw.rb', line 32

def batch_finished(batch_data)
  if batch_data.depth.zero?
    self.current_batch = nil
  end
end

#batch_starting(batch_data) ⇒ Object



26
27
28
29
30
# File 'lib/test_bench/output/raw.rb', line 26

def batch_starting(batch_data)
  if batch_data.depth.zero?
    self.current_batch = batch_data
  end
end

#comment(text) ⇒ Object



270
271
272
273
274
275
# File 'lib/test_bench/output/raw.rb', line 270

def comment(text)
  writer
    .indent
    .text(text)
    .newline
end

#configure(verbose: nil, detail: nil, omit_backtrace_pattern: nil, reverse_backtraces: nil, writer: nil, device: nil, styling: nil) ⇒ Object



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

def configure(verbose: nil, detail: nil, omit_backtrace_pattern: nil, reverse_backtraces: nil, writer: nil, device: nil, styling: nil)
  unless detail.nil?
    self.class.assure_detail_setting(detail)
  end

  self.detail_setting = detail unless detail.nil?
  self.verbose = verbose unless verbose.nil?

  self.omit_backtrace_pattern = omit_backtrace_pattern unless omit_backtrace_pattern.nil?
  self.reverse_backtraces = reverse_backtraces unless reverse_backtraces.nil?

  Writer.configure(self, writer: writer, styling: styling, device: device)
end

#default_test_titleObject



314
315
316
# File 'lib/test_bench/output/raw.rb', line 314

def default_test_title
  'Test'
end

#detail(text) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/test_bench/output/raw.rb', line 277

def detail(text)
  return unless verbose? || detail?

  if verbose? && !detail?
    text = "(detail omitted: #{text})"
  end

  writer
    .indent
    .text(text)
    .newline
end

#detail?(result = nil) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/test_bench/output/raw.rb', line 71

def detail?(result=nil)
  result ||= current_batch&.result

  case detail_setting
  when :failure
    result != true
  when :on
    true
  when :off
    false
  end
end

#enter_context(title, batch_data: nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/test_bench/output/raw.rb', line 84

def enter_context(title, batch_data: nil)
  batch_starting(batch_data) unless batch_data.nil?

  return if title.nil?

  writer
    .indent
    .escape_code(:green)
    .text(title)
    .escape_code(:reset_fg)
    .newline

  writer.increase_indentation
end

#enter_file(path) ⇒ Object



290
291
292
293
294
295
296
# File 'lib/test_bench/output/raw.rb', line 290

def enter_file(path)
  text = "Running #{path}"

  writer.text(text).newline

  self.previous_byte_offset = writer.byte_offset
end

#error(error) ⇒ Object



266
267
268
# File 'lib/test_bench/output/raw.rb', line 266

def error(error)
  print_error(error)
end

#exit_context(title, result, batch_data: nil) ⇒ Object



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

def exit_context(title, result, batch_data: nil)
  return if title.nil?

  writer.decrease_indentation

  if verbose?
    text = "Finished context #{title.inspect} (Result: #{result_text(result)})"

    color = result ? :green : :red

    writer
      .indent
      .escape_code(:faint).escape_code(:italic).escape_code(color)
      .text(text)
      .escape_code(:reset_fg).escape_code(:reset_italic).escape_code(:reset_intensity)
      .newline
  end

  if writer.indentation_depth.zero?
    writer.newline
  end

ensure
  batch_finished(batch_data) unless batch_data.nil?
end

#exit_file(path, result) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/test_bench/output/raw.rb', line 298

def exit_file(path, result)
  unless writer.byte_offset > previous_byte_offset
    writer
      .escape_code(:faint)
      .text("(Nothing written)")
      .escape_code(:reset_intensity)
      .newline

    writer.newline
  end
end

#finish_fixture(fixture, result, batch_data: nil) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/test_bench/output/raw.rb', line 246

def finish_fixture(fixture, result, batch_data: nil)
  return unless verbose?

  fixture_class = fixture.class.inspect

  text = "Finished fixture (Fixture: #{fixture_class}, Result: #{result_text(result)})"

  writer.decrease_indentation

  writer
    .indent
    .escape_code(:magenta)
    .text(text)
    .escape_code(:reset_fg)
    .newline

ensure
  batch_finished(batch_data) unless batch_data.nil?
end

#finish_test(title, result, batch_data: nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/test_bench/output/raw.rb', line 172

def finish_test(title, result, batch_data: nil)
  batch_data = nil if verbose?

  if batch_data&.passed? && title.nil?
    return
  end

  writer.decrease_indentation

  if batch_data.nil?
    print_test_result(title, result)
  end

ensure
  batch_finished(batch_data) unless batch_data.nil?
end


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/test_bench/output/raw.rb', line 189

def print_test_result(title, result)
  title ||= default_test_title

  if writer.styling?
    text = title
  elsif result
    text = title
  else
    text = "#{title} (failed)"
  end

  color = result ? :green : :red

  writer
    .indent
    .escape_code(color)
    .text(text)
    .escape_code(:reset_fg)
    .newline
end

#result_text(result) ⇒ Object



310
311
312
# File 'lib/test_bench/output/raw.rb', line 310

def result_text(result)
  result ? 'pass' : 'failure'
end

#skip_context(title) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/test_bench/output/raw.rb', line 125

def skip_context(title)
  return if title.nil?

  writer
    .indent
    .escape_code(:yellow)
    .text(title)
    .escape_code(:reset_fg)
    .newline
end

#skip_test(title) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/test_bench/output/raw.rb', line 210

def skip_test(title)
  title ||= default_test_title

  if writer.styling?
    text = title
  else
    text = "#{title} (skipped)"
  end

  writer
    .indent
    .escape_code(:yellow)
    .text(text)
    .escape_code(:reset_fg)
    .newline
end

#start_fixture(fixture, batch_data: nil) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/test_bench/output/raw.rb', line 227

def start_fixture(fixture, batch_data: nil)
  batch_starting(batch_data) unless batch_data.nil?

  return unless verbose?

  fixture_class = fixture.class.inspect

  text = "Starting fixture (Fixture: #{fixture_class})"

  writer
    .indent
    .escape_code(:blue)
    .text(text)
    .escape_code(:reset_fg)
    .newline

  writer.increase_indentation
end

#start_test(title, batch_data: nil) ⇒ Object



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

def start_test(title, batch_data: nil)
  batch_starting(batch_data) unless batch_data.nil?

  batch_data = nil if verbose

  if batch_data&.passed? && title.nil?
    return
  end

  if batch_data.nil?
    if title.nil?
      text = "Starting test"
    else
      text = "Starting test #{title.inspect}"
    end

    writer
      .indent
      .escape_code(:faint)
      .escape_code(:italic)
      .escape_code(:blue)
      .text(text)
      .escape_code(:reset_fg)
      .escape_code(:reset_italic)
      .escape_code(:reset_intensity)
      .newline

  else
    result = batch_data.result

    print_test_result(title, result)
  end

  writer.increase_indentation
end