module Rails::Dom::Testing::Assertions::DomAssertions
def assert_dom_equal(expected, actual, message = nil, strict: false, html_version: nil)
assert_dom_equal expected, actual, html_version: :html5
html_version: :html4 or html_version: :html5 keyword arguments:
If you want to specify the HTML parser just for a particular assertion, pass
+Rails.application.config.dom_testing_default_html_version+.
When testing in a Rails application, the parser default can also be set by setting
Rails::Dom::Testing.default_html_version (either :html4 or :html5).
The DOMs are created using an HTML parser specified by
assert_dom_not_equal "
\nfoo\n\
", "foo
", strict: trueassert_dom_equal "
\nfoo\n\
", "foo
", strict: false# these assertions will both pass
strict: true:
and newlines). If you want stricter matching with exact matching for whitespace, pass
By default, the matcher will not pay attention to whitespace in text nodes (e.g., spaces
)
link_to("Apples", "http://www.example.com"),
'Apples',
assert_dom_equal(
# assert that the referenced method generates the appropriate HTML string
\Test two HTML strings for equivalency (e.g., equal even when attributes are in another order)
def assert_dom_equal(expected, actual, message = nil, strict: false, html_version: nil) expected_dom, actual_dom = fragment(expected, html_version: html_version), fragment(actual, html_version: html_version) message ||= "Expected: #{expected}\nActual: #{actual}" assert compare_doms(expected_dom, actual_dom, strict), message end
def assert_dom_not_equal(expected, actual, message = nil, strict: false, html_version: nil)
assert_dom_not_equal expected, actual, html_version: :html5
html_version: :html4 or html_version: :html5 keyword arguments:
If you want to specify the HTML parser just for a particular assertion, pass
+Rails.application.config.dom_testing_default_html_version+.
When testing in a Rails application, the parser default can also be set by setting
Rails::Dom::Testing.default_html_version (either :html4 or :html5).
The DOMs are created using an HTML parser specified by
assert_dom_not_equal "
\nfoo\n\
", "foo
", strict: trueassert_dom_equal "
\nfoo\n\
", "foo
", strict: false# these assertions will both pass
strict: true:
and newlines). If you want stricter matching with exact matching for whitespace, pass
By default, the matcher will not pay attention to whitespace in text nodes (e.g., spaces
)
link_to("Oranges", "http://www.example.com"),
'Apples',
assert_dom_not_equal(
# assert that the referenced method does not generate the specified HTML string
The negated form of +assert_dom_equal+.
def assert_dom_not_equal(expected, actual, message = nil, strict: false, html_version: nil) expected_dom, actual_dom = fragment(expected, html_version: html_version), fragment(actual, html_version: html_version) message ||= "Expected: #{expected}\nActual: #{actual}" assert_not compare_doms(expected_dom, actual_dom, strict), message end
def compare_doms(expected, actual, strict)
def compare_doms(expected, actual, strict) expected_children = extract_children(expected, strict) actual_children = extract_children(actual, strict) return false unless expected_children.size == actual_children.size expected_children.each_with_index do |child, i| return false unless equal_children?(child, actual_children[i], strict) end true end
def equal_attribute?(attr, other_attr)
def equal_attribute?(attr, other_attr) attr.name == other_attr.name && attr.value == other_attr.value end
def equal_attribute_nodes?(nodes, other_nodes)
def equal_attribute_nodes?(nodes, other_nodes) return false unless nodes.size == other_nodes.size nodes = nodes.sort_by(&:name) other_nodes = other_nodes.sort_by(&:name) nodes.each_with_index do |attr, i| return false unless equal_attribute?(attr, other_nodes[i]) end true end
def equal_child?(child, other_child, strict)
def equal_child?(child, other_child, strict) if strict child.to_s == other_child.to_s else child.to_s.split == other_child.to_s.split end end
def equal_children?(child, other_child, strict)
def equal_children?(child, other_child, strict) return false unless child.type == other_child.type if child.element? child.name == other_child.name && equal_attribute_nodes?(child.attribute_nodes, other_child.attribute_nodes) && compare_doms(child, other_child, strict) else equal_child?(child, other_child, strict) end end
def extract_children(node, strict)
def extract_children(node, strict) if strict node.children else node.children.reject { |n| n.text? && n.text.blank? } end end
def fragment(text, html_version: nil)
def fragment(text, html_version: nil) Rails::Dom::Testing.html_document_fragment(html_version: html_version).parse(text) end