Sha256: d259a002e8fe42b2124fc3eb1d0d300cc85f395409e089a6488fe7442e96088c

Contents?: true

Size: 1.16 KB

Versions: 80

Compression:

Stored size: 1.16 KB

Contents

module Gherkin
  module Parser
    class TagExpression
      require 'gherkin/java_impl'
      java_impl('gherkin.jar')

      attr_reader :limits

      def initialize(tag_expressions)
        @ands = []
        @limits = {}
        tag_expressions.each do |expr|
          add(expr.strip.split(/\s*,\s*/))
        end
      end

      def empty?
        @ands.empty?
      end

      def eval(tags)
        return true if @ands.flatten.empty?
        vars = Hash[*tags.map{|tag| [tag, true]}.flatten]
        !!Kernel.eval(ruby_expression)
      end

    private

      def add(tags)
        negatives, positives = tags.partition{|tag| tag =~ /^~/}
        positive_limits = Hash[*positives.map{|positive| tag, limit = positive.split(':'); [tag, limit ? limit.to_i : nil]}.flatten]
        @limits.merge!(positive_limits)
        @ands << (negatives + positive_limits.keys)
      end

      def ruby_expression
        "(" + @ands.map do |ors|
          ors.map do |tag|
            if tag =~ /^~(.*)/
              "!vars['#{$1}']"
            else
              "vars['#{tag}']"
            end
          end.join("||")
        end.join(")&&(") + ")"
      end
    end
  end
end

Version data entries

80 entries across 80 versions & 1 rubygems

Version Path
gherkin-1.0.29 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.29-i386-mswin32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.29-i386-mingw32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.29-java lib/gherkin/parser/tag_expression.rb
gherkin-1.0.28 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.28-i386-mswin32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.28-i386-mingw32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.28-java lib/gherkin/parser/tag_expression.rb
gherkin-1.0.27 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.27-i386-mswin32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.27-i386-mingw32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.27-java lib/gherkin/parser/tag_expression.rb
gherkin-1.0.26 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.26-i386-mswin32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.26-i386-mingw32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.26-java lib/gherkin/parser/tag_expression.rb
gherkin-1.0.25 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.25-i386-mswin32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.25-i386-mingw32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.25-java lib/gherkin/parser/tag_expression.rb