Skip to content

Commit

Permalink
Moved Dom and Selector assertions from ActionDispatch to ActionView.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspth committed Jun 16, 2014
1 parent 9f73f9f commit 95c517b
Show file tree
Hide file tree
Showing 11 changed files with 557 additions and 545 deletions.
1 change: 1 addition & 0 deletions actionpack/lib/action_controller/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ def build_response(klass)
included do
include ActionController::TemplateAssertions
include ActionDispatch::Assertions
include ActionView::Assertions
class_attribute :_controller_class
setup :setup_controller_request_and_response
end
Expand Down
4 changes: 0 additions & 4 deletions actionpack/lib/action_dispatch/testing/assertions.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
module ActionDispatch
module Assertions
autoload :DomAssertions, 'action_dispatch/testing/assertions/dom'
autoload :ResponseAssertions, 'action_dispatch/testing/assertions/response'
autoload :RoutingAssertions, 'action_dispatch/testing/assertions/routing'
autoload :SelectorAssertions, 'action_dispatch/testing/assertions/selector'

extend ActiveSupport::Concern

include DomAssertions
include ResponseAssertions
include RoutingAssertions
include SelectorAssertions
end
end

75 changes: 2 additions & 73 deletions actionpack/lib/action_dispatch/testing/assertions/dom.rb
Original file line number Diff line number Diff line change
@@ -1,74 +1,3 @@
require 'loofah'
require 'active_support/deprecation'

module ActionDispatch
module Assertions
module DomAssertions
# \Test two HTML strings for equivalency (e.g., equal even when attributes are in another order)
#
# # assert that the referenced method generates the appropriate HTML string
# assert_dom_equal '<a href="http://www.example.com">Apples</a>', link_to("Apples", "http://www.example.com")
def assert_dom_equal(expected, actual, message = nil)
expected_dom, actual_dom = doms_from_strings(expected, actual)
message ||= "Expected: #{expected_dom}\nActual: #{actual_dom}"
assert compare_doms(expected_dom, actual_dom), message
end

# The negated form of +assert_dom_equal+.
#
# # assert that the referenced method does not generate the specified HTML string
# assert_dom_not_equal '<a href="http://www.example.com">Apples</a>', link_to("Oranges", "http://www.example.com")
def assert_dom_not_equal(expected, actual, message = nil)
expected_dom, actual_dom = doms_from_strings(expected, actual)
message ||= "Expected: #{expected_dom}\nActual: #{actual_dom}"
assert_not compare_doms(expected_dom, actual_dom), message
end

protected
# +doms_from_strings+ creates a Loofah::HTML::DocumentFragment for every string in strings
def doms_from_strings(*strings)
strings.map { |str| Loofah.fragment(str) }
end

# +compare_doms+ takes two doms loops over all their children and compares each child via +equal_children?+
def compare_doms(expected, actual)
expected.children.each_with_index do |child, i|
return false unless equal_children?(child, actual.children[i])
end
true
end

# +equal_children?+ compares children according to their type
# Determines further comparison via said type
# i.e. element node children with equal names has their attributes compared using +attributes_are_equal?+
def equal_children?(child, other_child)
return false unless child.type == other_child.type

case child.type
when Nokogiri::XML::Node::ELEMENT_NODE
child.name == other_child.name && attributes_are_equal?(child, other_child)
else
child.to_s == other_child.to_s
end
end

# +attributes_are_equal?+ sorts elements attributes by name and compares
# each attribute by calling +equal_attribute?+
# If those are +true+ the attributes are considered equal
def attributes_are_equal?(element, other_element)
first_nodes = element.attribute_nodes.sort_by { |a| a.name }
other_nodes = other_element.attribute_nodes.sort_by { |a| a.name }

return false unless first_nodes.size == other_nodes.size
first_nodes.each_with_index do |attr, i|
return false unless equal_attribute?(attr, other_nodes[i])
end
true
end

# +equal_attribute?+ compares attributes by their name and value
def equal_attribute?(attr, other_attr)
attr.name == other_attr.name && attr.value == other_attr.value
end
end
end
end
ActiveSupport::Deprecation.warn("ActionDispatch::Assertions::DomAssertions has been moved to ActionView. You can find it in action_view/testing/assertions/dom.rb.")
Loading

0 comments on commit 95c517b

Please sign in to comment.