Sha256: 447a1e26b7ad27512b551b22aa5d6fdd9ef96dd97f4bb6df77af32e995796c70

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 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]
    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

  private

  def validate_multi(scenario: [], spec: nil, rule_result: [])
    # binding.pry

    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
    Rast.assert("Scenario must fall into a unique rule output/clause:
     #{scenario} , matched: #{matched_outputs}") { match_count == 1 }

    matched_outputs.first
  end

  def binary_outcome(outcome: '', spec: nil, expected: false)
    is_positive = spec.pair.keys.include?(outcome)
    if is_positive
      expected == 'true' ? outcome : opposite(outcome: outcome, spec: spec)
    else
      expected == 'true' ? opposite(outcome: outcome, spec: spec) : outcome
    end
  end

  def opposite(outcome: '', spec: nil)
    if spec.pair.keys.include? outcome
      spec.pair[outcome]
    else
      spec.pair_reversed[outcome]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rast-0.15.1 lib/rast/rules/rule_validator.rb
rast-0.14.0 lib/rast/rules/rule_validator.rb
rast-0.11.4 lib/rast/rules/rule_validator.rb
rast-0.11.3 lib/rast/rules/rule_validator.rb