Class: Backspin::Matcher

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

Overview

Handles matching logic between recorded and actual commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, recorded_command:, actual_command:) ⇒ Matcher

Returns a new instance of Matcher.



8
9
10
11
12
# File 'lib/backspin/matcher.rb', line 8

def initialize(config:, recorded_command:, actual_command:)
  @config = normalize_config(config)
  @recorded_command = recorded_command
  @actual_command = actual_command
end

Instance Attribute Details

#actual_commandObject (readonly)

Returns the value of attribute actual_command.



6
7
8
# File 'lib/backspin/matcher.rb', line 6

def actual_command
  @actual_command
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/backspin/matcher.rb', line 6

def config
  @config
end

#recorded_commandObject (readonly)

Returns the value of attribute recorded_command.



6
7
8
# File 'lib/backspin/matcher.rb', line 6

def recorded_command
  @recorded_command
end

Instance Method Details

#failure_reasonString

Returns reason why matching failed.

Returns:

  • (String)

    reason why matching failed



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

def failure_reason
  reasons = []

  if config.nil?
    # Default matcher checks all fields
    recorded_hash = recorded_command.to_h
    actual_hash = actual_command.to_h

    reasons << "stdout differs" if recorded_hash["stdout"] != actual_hash["stdout"]
    reasons << "stderr differs" if recorded_hash["stderr"] != actual_hash["stderr"]
    reasons << "exit status differs" if recorded_hash["status"] != actual_hash["status"]
  elsif config.is_a?(Hash)
    recorded_hash = recorded_command.to_h
    actual_hash = actual_command.to_h

    # Only check matchers that were provided
    config.each do |field, matcher_proc|
      case field
      when :all
        reasons << ":all matcher failed" unless matcher_proc.call(recorded_hash, actual_hash)
      when :stdout, :stderr, :status
        unless matcher_proc.call(recorded_hash[field.to_s], actual_hash[field.to_s])
          reasons << "#{field} custom matcher failed"
        end
      end
    end
  else
    # Proc matcher
    reasons << "custom matcher failed"
  end

  reasons.join(", ")
end

#match?Boolean

Returns true if commands match according to the configured matcher.

Returns:

  • (Boolean)

    true if commands match according to the configured matcher



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/backspin/matcher.rb', line 15

def match?
  if config.nil?
    # Default behavior: check all fields for equality
    default_matcher.call(recorded_command.to_h, actual_command.to_h)
  elsif config.is_a?(Proc)
    config.call(recorded_command.to_h, actual_command.to_h)
  elsif config.is_a?(Hash)
    verify_with_hash_matcher
  else
    raise ArgumentError, "Invalid matcher type: #{config.class}"
  end
end