Class: Backspin::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/backspin/recorder.rb

Overview

Handles capture-mode recording and verification

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode: :record, record: nil, matcher: nil, filter: nil, filter_on: :both) ⇒ Recorder

Returns a new instance of Recorder.



11
12
13
14
15
16
17
# File 'lib/backspin/recorder.rb', line 11

def initialize(mode: :record, record: nil, matcher: nil, filter: nil, filter_on: :both)
  @mode = mode
  @record = record
  @matcher = matcher
  @filter = filter
  @filter_on = filter_on
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



9
10
11
# File 'lib/backspin/recorder.rb', line 9

def filter
  @filter
end

#filter_onObject (readonly)

Returns the value of attribute filter_on.



9
10
11
# File 'lib/backspin/recorder.rb', line 9

def filter_on
  @filter_on
end

#matcherObject (readonly)

Returns the value of attribute matcher.



9
10
11
# File 'lib/backspin/recorder.rb', line 9

def matcher
  @matcher
end

#modeObject (readonly)

Returns the value of attribute mode.



9
10
11
# File 'lib/backspin/recorder.rb', line 9

def mode
  @mode
end

#recordObject (readonly)

Returns the value of attribute record.



9
10
11
# File 'lib/backspin/recorder.rb', line 9

def record
  @record
end

Instance Method Details

#perform_capture_recordingObject

Performs capture recording by intercepting all stdout/stderr output



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/backspin/recorder.rb', line 20

def perform_capture_recording
  captured_stdout, captured_stderr, output = capture_output { yield }

  actual_snapshot = Snapshot.new(
    command_type: Backspin::Capturer,
    args: ["<captured block>"],
    stdout: captured_stdout,
    stderr: captured_stderr,
    status: 0,
    recorded_at: Time.now.utc.iso8601
  )

  record.set_snapshot(actual_snapshot)
  record.save(filter: @filter)

  BackspinResult.new(
    mode: :record,
    record_path: record.path,
    actual: actual_snapshot,
    output: output
  )
end

#perform_capture_verificationObject

Performs capture verification by capturing output and comparing with recorded values



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
# File 'lib/backspin/recorder.rb', line 44

def perform_capture_verification
  raise RecordNotFoundError, "Record not found: #{record.path}" unless record.exists?
  raise RecordNotFoundError, "No snapshot found in record #{record.path}" if record.empty?

  expected_snapshot = record.snapshot
  unless expected_snapshot.command_type == Backspin::Capturer
    raise RecordFormatError, "Invalid record format: expected Backspin::Capturer for capture"
  end

  captured_stdout, captured_stderr, output = capture_output { yield }

  actual_snapshot = Snapshot.new(
    command_type: Backspin::Capturer,
    args: ["<captured block>"],
    stdout: captured_stdout,
    stderr: captured_stderr,
    status: 0
  )

  command_diff = CommandDiff.new(
    expected: expected_snapshot,
    actual: actual_snapshot,
    matcher: @matcher,
    filter: @filter,
    filter_on: @filter_on
  )

  BackspinResult.new(
    mode: :verify,
    record_path: record.path,
    actual: actual_snapshot,
    expected: expected_snapshot,
    verified: command_diff.verified?,
    command_diff: command_diff,
    output: output
  )
end