Sha256: 2647aa4e98b7a291ecb0e17881a5d27c1d79f18ddb474aa42f5b3d43fc4ac7af

Contents?: true

Size: 1.2 KB

Versions: 10

Compression:

Stored size: 1.2 KB

Contents

module Lopata
  # @private
  class Condition
    attr_reader :condition, :positive, :dynamic
    def initialize(condition, positive: true)
      @condition, @positive = condition, positive
      @dynamic = @condition.is_a?(Proc)
    end

    alias positive? positive
    alias dynamic? dynamic

    # Match scenario on build-time. 
    def match?(scenario)
      # dynamic steps match scenario in build-time: will be verified later
      return true if dynamic?  
      matched = match_metadata?(scenario)
      positive? ? matched : !matched
    end

    def match_metadata?(scenario)
      metadata = scenario.metadata
      case condition
      when Hash
        condition.keys.all? do |k| 
          if condition[k].is_a? Array
            condition[k].include?(metadata[k])
          else
            metadata[k] == condition[k]
          end
        end
      when Array
        condition.map { |key| metadata[key] }.all?
      when TrueClass, FalseClass
        condition
      else
        metadata[condition]
      end
    end

    def match_dynamic?(scenario_runtime)
      return false unless dynamic?
      matched = scenario_runtime.instance_exec(&condition)
      positive? ? matched : !matched
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lopata-0.1.32 lib/lopata/condition.rb
lopata-0.1.31 lib/lopata/condition.rb
lopata-0.1.30 lib/lopata/condition.rb
lopata-0.1.29 lib/lopata/condition.rb
lopata-0.1.28 lib/lopata/condition.rb
lopata-0.1.27 lib/lopata/condition.rb
lopata-0.1.26 lib/lopata/condition.rb
lopata-0.1.25 lib/lopata/condition.rb
lopata-0.1.24 lib/lopata/condition.rb
lopata-0.1.23 lib/lopata/condition.rb