Sha256: e81a66ede3d8df7f28d39cc8ea54bdd3f25c1af5edf4ccbd94a8a0ef130a4899

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Cucumber
  module CucumberExpressions
    class ParameterTypeMatcher
      attr_reader :parameter_type

      def initialize(parameter_type, regexp, text, match_position = 0)
        @parameter_type, @regexp, @text = parameter_type, regexp, text
        @match = @regexp.match(@text, match_position)
      end

      def advance_to(new_match_position)
        (new_match_position...@text.length).each do |advancedPos|
          matcher = self.class.new(parameter_type, @regexp, @text, advancedPos)
          return matcher if matcher.find && matcher.full_word?
        end

        self.class.new(parameter_type, @regexp, @text, @text.length)
      end

      def find
        !@match.nil? && !group.empty?
      end

      def full_word?
        space_before_match_or_sentence_start? && space_after_match_or_sentence_end?
      end

      def start
        @match.begin(0)
      end

      def group
        @match.captures[0]
      end

      def <=>(other)
        pos_comparison = start <=> other.start
        return pos_comparison if pos_comparison != 0

        length_comparison = other.group.length <=> group.length
        return length_comparison if length_comparison != 0

        0
      end

      private

      def space_before_match_or_sentence_start?
        match_begin = @match.begin(0)
        match_begin == 0 || @text[match_begin - 1].match(/\p{Z}|\p{P}|\p{S}/)
      end

      def space_after_match_or_sentence_end?
        match_end = @match.end(0)
        match_end == @text.length || @text[match_end].match(/\p{Z}|\p{P}|\p{S}/)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/cucumber-cucumber-expressions-17.0.1/lib/cucumber/cucumber_expressions/parameter_type_matcher.rb
cucumber-cucumber-expressions-17.0.1 lib/cucumber/cucumber_expressions/parameter_type_matcher.rb
cucumber-cucumber-expressions-17.0.0 lib/cucumber/cucumber_expressions/parameter_type_matcher.rb