Sha256: 39862114c38bad01758ac3ff1415a1c7d0df803959514d32bb33a3c1e57f3f0d

Contents?: true

Size: 1.56 KB

Versions: 14

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require 'rast/assert'
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
    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

14 entries across 14 versions & 1 rubygems

Version Path
rast-0.11.1 lib/rast/rules/rule_validator.rb
rast-0.11.0 lib/rast/rules/rule_validator.rb
rast-0.10.0 lib/rast/rules/rule_validator.rb
rast-0.9.0 lib/rast/rules/rule_validator.rb
rast-0.9.0.pre lib/rast/rules/rule_validator.rb
rast-0.8.1.pre lib/rast/rules/rule_validator.rb
rast-0.8.0.pre lib/rast/rules/rule_validator.rb
rast-0.6.2.pre lib/rast/rules/rule_validator.rb
rast-0.6.1.pre lib/rast/rules/rule_validator.rb
rast-0.6.0.pre lib/rast/rules/rule_validator.rb
rast-0.4.2.pre lib/rast/rules/rule_validator.rb
rast-0.4.1.pre lib/rast/rules/rule_validator.rb
rast-0.4.0.pre lib/rast/rules/rule_validator.rb
rast-0.3.0.pre lib/rast/rules/rule_validator.rb