File: have_node_matcher.rb

package info (click to toggle)
ruby-roadie 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 512 kB
  • sloc: ruby: 3,418; makefile: 5
file content (21 lines) | stat: -rw-r--r-- 670 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
# frozen_string_literal: true

RSpec::Matchers.define :have_node do |selector|
  chain(:with_attributes) { |attributes| @attributes = attributes }
  match do |document|
    node = document.at_css(selector)
    if @attributes
      node && match_attributes(node.attributes)
    else
      node
    end
  end

  failure_message { "expected document to #{name_to_sentence}#{expected_to_sentence}" }
  failure_message_when_negated { "expected document to not #{name_to_sentence}#{expected_to_sentence}" }

  def match_attributes(node_attributes)
    attributes = node_attributes.map { |name, attribute| [name, attribute.value] }.to_h
    @attributes == attributes
  end
end