Module: Igniter::Embed::Contractable::Acceptance

Defined in:
lib/igniter/embed/contractable/acceptance.rb

Class Method Summary collapse

Class Method Details

.evaluate(policy:, report:, candidate:, options:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/igniter/embed/contractable/acceptance.rb', line 9

def evaluate(policy:, report:, candidate:, options:)
  case policy.to_sym
  when :exact
    result(policy: :exact, failures: report.match? ? [] : ["differential mismatch"])
  when :completed
    completed = candidate.fetch(:status) != :error && candidate.fetch(:error).nil?
    result(policy: :completed, failures: completed ? [] : ["candidate did not complete"])
  when :shape
    failures = shape_failures(candidate.fetch(:outputs), options.fetch(:outputs, {}))
    result(policy: :shape, failures: failures)
  else
    raise ArgumentError, "unknown contractable acceptance policy #{policy}"
  end
end

.result(policy:, failures:) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/igniter/embed/contractable/acceptance.rb', line 24

def result(policy:, failures:)
  {
    policy: policy,
    accepted: failures.empty?,
    failures: failures
  }
end

.shape_failures(outputs, expected, path = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/igniter/embed/contractable/acceptance.rb', line 32

def shape_failures(outputs, expected, path = [])
  expected.flat_map do |key, matcher|
    current_path = path + [key]
    next ["#{current_path.join(".")} is missing"] unless outputs.key?(key)

    value = outputs.fetch(key)
    if matcher.is_a?(Hash)
      value.is_a?(Hash) ? shape_failures(value, matcher, current_path) : ["#{current_path.join(".")} is not a hash"]
    elsif matcher.is_a?(Class) || matcher.is_a?(Module)
      value.is_a?(matcher) ? [] : ["#{current_path.join(".")} is not a #{matcher}"]
    elsif matcher.respond_to?(:call)
      matcher.call(value) ? [] : ["#{current_path.join(".")} did not satisfy predicate"]
    else
      value == matcher ? [] : ["#{current_path.join(".")} did not equal #{matcher.inspect}"]
    end
  end
end