Sha256: 87ea4ee1987298591deb6edbafd3e129f766b96fd5b52f67312494262e89472c

Contents?: true

Size: 953 Bytes

Versions: 3

Compression:

Stored size: 953 Bytes

Contents

# frozen_string_literal: true

module RSpecHTML
  module Matchers
    # Matches text or regex within a given DOM element (strips HTML tags and compares text content only).
    class MatchText
      include Base

      diffable

      def match(actual)
        @rspec_actual = actual&.text
        return regexp_match?(actual) || regexp_siblings_match?(actual) if @expected.is_a?(Regexp)

        string_match?(actual) || string_siblings_match?(actual)
      end

      private

      def regexp_match?(actual)
        @expected.match(actual&.text || '')
      end

      def regexp_siblings_match?(actual)
        actual.siblings.any? { |sibling| @expected.match(sibling&.text || '') }
      end

      def string_match?(actual)
        (actual&.text || '').include?(@expected.to_s)
      end

      def string_siblings_match?(actual)
        actual.siblings.any? { |sibling| (sibling&.text || '').include?(@expected.to_s) }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rspec-html-0.2.13 lib/rspec_html/matchers/match_text.rb
rspec-html-0.2.12 lib/rspec_html/matchers/match_text.rb
rspec-html-0.2.11 lib/rspec_html/matchers/match_text.rb