File: current_path_query.rb

package info (click to toggle)
ruby-capybara 3.40.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,368 kB
  • sloc: ruby: 23,988; javascript: 752; makefile: 11
file content (61 lines) | stat: -rw-r--r-- 1,644 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

require 'addressable/uri'

module Capybara
  # @api private
  module Queries
    class CurrentPathQuery < BaseQuery
      def initialize(expected_path, **options, &optional_filter_block)
        super(options)
        @expected_path = expected_path
        @options = {
          url: !@expected_path.is_a?(Regexp) && !::Addressable::URI.parse(@expected_path || '').hostname.nil?,
          ignore_query: false
        }.merge(options)
        @filter_block = optional_filter_block
        assert_valid_keys
      end

      def resolves_for?(session)
        uri = ::Addressable::URI.parse(session.current_url)
        @actual_path = (options[:ignore_query] ? uri&.omit(:query) : uri).then do |u|
          options[:url] ? u&.to_s : u&.request_uri
        end

        res = if @expected_path.is_a? Regexp
          @actual_path.to_s.match?(@expected_path)
        else
          ::Addressable::URI.parse(@expected_path) == ::Addressable::URI.parse(@actual_path)
        end

        res && matches_filter_block?(uri)
      end

      def failure_message
        failure_message_helper
      end

      def negative_failure_message
        failure_message_helper(' not')
      end

    private

      def matches_filter_block?(url)
        return true unless @filter_block

        @filter_block.call(url)
      end

      def failure_message_helper(negated = '')
        verb = @expected_path.is_a?(Regexp) ? 'match' : 'equal'
        "expected #{@actual_path.inspect}#{negated} to #{verb} #{@expected_path.inspect}"
      end

      def valid_keys
        %i[wait url ignore_query]
      end
    end
  end
end