Sha256: 7f856ef03dbc0f63f94982705cf336ad342394e5d948a9da29aa08b85b978b9f

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require 'rast/rules/rule_processor'

# Validates rules
class RuleValidator
  def validate(scenario: [], fixture: {})
    rule_result = RuleProcessor.new.evaluate(
      scenario: scenario,
      fixture: fixture
    )

    spec = fixture[:spec]
    validate_results(scenario, rule_result, spec)
  end

  private

  def validate_results(scenario, rule_result, spec)
    rule = spec.rule
    single_result = rule.size == 1
    if single_result
      next_result = rule_result.first
      outcome = rule.outcomes.first
      binary_outcome(outcome: outcome, spec: spec, expected: next_result)
    else
      validate_multi(scenario: scenario, spec: spec, rule_result: rule_result)
    end
  end

  def validate_multi(scenario: [], spec: nil, rule_result: [])
    matched_outputs = []
    match_count = 0

    rule_result.map { |result| result.to_s == 'true' }.each_with_index do |result, i|
      next unless result

      match_count += 1
      matched_outputs << spec.rule.outcomes[i]
    end

    verify_results(spec, scenario, matched_outputs, match_count)

    matched_outputs.first || spec.default_outcome
  end

  def verify_results(spec, scenario, matched_outputs, match_count)
    Rast.assert("#{spec.description} #{scenario} must fall into a unique rule" \
      " outcome/clause, matched: #{matched_outputs}") do
      match_count == 1 || match_count.zero? && !spec.default_outcome.nil?
    end
  end

  def binary_outcome(outcome: '', spec: nil, expected: false)
    if expected == 'true'
      outcome
    else
      spec.pair[outcome]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rast-0.19.1 lib/rast/rules/rule_validator.rb
rast-0.19.0 lib/rast/rules/rule_validator.rb