Sha256: 383abcdc18fc0164775615c74dd97b2cb7e1697ae2377fc8affd4b2bdb4484fc

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

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)
        raise_argument_error unless actual.is_a?(RSpecHTML::Element)
        @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

      def raise_argument_error
        raise ArumentError, 'Expected RSpecHTML::Element with `match_text` matcher.'
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-html-0.2.14 lib/rspec_html/matchers/match_text.rb