Sha256: 4be893ffdab9456e3117f5f78e48ec2381dedc4a11bc04c684ce22b99afdecd9

Contents?: true

Size: 1.15 KB

Versions: 20

Compression:

Stored size: 1.15 KB

Contents

require 'gherkin/native'

module Gherkin
  module Parser
    class TagExpression
      native_impl('gherkin')

      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

20 entries across 20 versions & 1 rubygems

Version Path
gherkin-2.0.2 lib/gherkin/parser/tag_expression.rb
gherkin-2.0.2-i386-mswin32 lib/gherkin/parser/tag_expression.rb
gherkin-2.0.2-i386-mingw32 lib/gherkin/parser/tag_expression.rb
gherkin-2.0.2-universal-dotnet lib/gherkin/parser/tag_expression.rb
gherkin-2.0.2-java lib/gherkin/parser/tag_expression.rb
gherkin-2.0.1 lib/gherkin/parser/tag_expression.rb
gherkin-2.0.1-i386-mswin32 lib/gherkin/parser/tag_expression.rb
gherkin-2.0.1-i386-mingw32 lib/gherkin/parser/tag_expression.rb
gherkin-2.0.1-universal-dotnet lib/gherkin/parser/tag_expression.rb
gherkin-2.0.1-java lib/gherkin/parser/tag_expression.rb
gherkin-2.0.0 lib/gherkin/parser/tag_expression.rb
gherkin-2.0.0-i386-mswin32 lib/gherkin/parser/tag_expression.rb
gherkin-2.0.0-i386-mingw32 lib/gherkin/parser/tag_expression.rb
gherkin-2.0.0-universal-dotnet lib/gherkin/parser/tag_expression.rb
gherkin-2.0.0-java lib/gherkin/parser/tag_expression.rb
gherkin-1.0.30 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.30-i386-mswin32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.30-i386-mingw32 lib/gherkin/parser/tag_expression.rb
gherkin-1.0.30-universal-dotnet lib/gherkin/parser/tag_expression.rb
gherkin-1.0.30-java lib/gherkin/parser/tag_expression.rb