Skip to content

Commit

Permalink
Deprecate assert_tag and assert_no_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Mendonça França committed Aug 18, 2014
1 parent 23379ee commit b12850b
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 94 deletions.
19 changes: 16 additions & 3 deletions lib/rails/dom/testing/assertions/tag_assertions.rb
@@ -1,3 +1,4 @@
require 'active_support/deprecation'
require 'rails/deprecated_sanitizer/html-scanner'

module Rails
Expand Down Expand Up @@ -96,8 +97,10 @@ module TagAssertions
# that allow optional closing tags (p, li, td). <em>You must explicitly
# close all of your tags to use these assertions.</em>
def assert_tag(*opts)
ActiveSupport::Deprecation.warn("assert_tag is deprecated and will be removed at Rails 5. Use assert_select to get the same feature.")

opts = opts.size > 1 ? opts.last.merge({ tag: opts.first.to_s }) : opts.first
tag = find_tag(opts)
tag = _find_tag(opts)

assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}"
end
Expand All @@ -116,21 +119,31 @@ def assert_tag(*opts)
# assert_no_tag tag: "p",
# children: { count: 1..3, only: { tag: "img" } }
def assert_no_tag(*opts)
ActiveSupport::Deprecation.warn("assert_no_tag is deprecated and will be removed at Rails 5. Use assert_select to get the same feature.")

opts = opts.size > 1 ? opts.last.merge({ tag: opts.first.to_s }) : opts.first
tag = find_tag(opts)
tag = _find_tag(opts)

assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}"
end

def find_tag(conditions)
html_scanner_document.find(conditions)
ActiveSupport::Deprecation.warn("find_tag is deprecated and will be removed at Rails 5 without replacement.")

_find_tag(conditions)
end

def find_all_tag(conditions)
ActiveSupport::Deprecation.warn("find_all_tag is deprecated and will be removed at Rails 5 without replacement. Use assert_select to get the same feature.")

html_scanner_document.find_all(conditions)
end

private
def _find_tag(conditions)
html_scanner_document.find(conditions)
end

def html_scanner_document
xml = @response.content_type =~ /xml$/
@html_document ||= HTML::Document.new(@response.body, false, xml)
Expand Down
218 changes: 127 additions & 91 deletions test/tag_assertions_test.rb
Expand Up @@ -36,147 +36,183 @@ def initialize(content_type, body)
end

def test_assert_tag_tag
# there is a 'form' tag
assert_tag tag: 'form'
# there is not an 'hr' tag
assert_no_tag tag: 'hr'
assert_deprecated do
# there is a 'form' tag
assert_tag tag: 'form'
# there is not an 'hr' tag
assert_no_tag tag: 'hr'
end
end

def test_assert_tag_attributes
# there is a tag with an 'id' of 'bar'
assert_tag attributes: { id: "bar" }
# there is no tag with a 'name' of 'baz'
assert_no_tag attributes: { name: "baz" }
assert_deprecated do
# there is a tag with an 'id' of 'bar'
assert_tag attributes: { id: "bar" }
# there is no tag with a 'name' of 'baz'
assert_no_tag attributes: { name: "baz" }
end
end

def test_assert_tag_parent
# there is a tag with a parent 'form' tag
assert_tag parent: { tag: "form" }
# there is no tag with a parent of 'input'
assert_no_tag parent: { tag: "input" }
assert_deprecated do
# there is a tag with a parent 'form' tag
assert_tag parent: { tag: "form" }
# there is no tag with a parent of 'input'
assert_no_tag parent: { tag: "input" }
end
end

def test_assert_tag_child
# there is a tag with a child 'input' tag
assert_tag child: { tag: "input" }
# there is no tag with a child 'strong' tag
assert_no_tag child: { tag: "strong" }
assert_deprecated do
# there is a tag with a child 'input' tag
assert_tag child: { tag: "input" }
# there is no tag with a child 'strong' tag
assert_no_tag child: { tag: "strong" }
end
end

def test_assert_tag_ancestor
# there is a 'li' tag with an ancestor having an id of 'foo'
assert_tag ancestor: { attributes: { id: "foo" } }, tag: "li"
# there is no tag of any kind with an ancestor having an href matching 'foo'
assert_no_tag ancestor: { attributes: { href: /foo/ } }
assert_deprecated do
# there is a 'li' tag with an ancestor having an id of 'foo'
assert_tag ancestor: { attributes: { id: "foo" } }, tag: "li"
# there is no tag of any kind with an ancestor having an href matching 'foo'
assert_no_tag ancestor: { attributes: { href: /foo/ } }
end
end

def test_assert_tag_descendant
# there is a tag with a descendant 'li' tag
assert_tag descendant: { tag: "li" }
# there is no tag with a descendant 'html' tag
assert_no_tag descendant: { tag: "html" }
assert_deprecated do
# there is a tag with a descendant 'li' tag
assert_tag descendant: { tag: "li" }
# there is no tag with a descendant 'html' tag
assert_no_tag descendant: { tag: "html" }
end
end

def test_assert_tag_sibling
# there is a tag with a sibling of class 'item'
assert_tag sibling: { attributes: { class: "item" } }
# there is no tag with a sibling 'ul' tag
assert_no_tag sibling: { tag: "ul" }
assert_deprecated do
# there is a tag with a sibling of class 'item'
assert_tag sibling: { attributes: { class: "item" } }
# there is no tag with a sibling 'ul' tag
assert_no_tag sibling: { tag: "ul" }
end
end

def test_assert_tag_after
# there is a tag following a sibling 'div' tag
assert_tag after: { tag: "div" }
# there is no tag following a sibling tag with id 'bar'
assert_no_tag after: { attributes: { id: "bar" } }
assert_deprecated do
# there is a tag following a sibling 'div' tag
assert_tag after: { tag: "div" }
# there is no tag following a sibling tag with id 'bar'
assert_no_tag after: { attributes: { id: "bar" } }
end
end

def test_assert_tag_before
# there is a tag preceding a tag with id 'bar'
assert_tag before: { attributes: { id: "bar" } }
# there is no tag preceding a 'form' tag
assert_no_tag before: { tag: "form" }
assert_deprecated do
# there is a tag preceding a tag with id 'bar'
assert_tag before: { attributes: { id: "bar" } }
# there is no tag preceding a 'form' tag
assert_no_tag before: { tag: "form" }
end
end

def test_assert_tag_children_count
# there is a tag with 2 children
assert_tag children: { count: 2 }
# in particular, there is a <ul> tag with two children (a nameless pair of <li>s)
assert_tag tag: 'ul', children: { count: 2 }
# there is no tag with 4 children
assert_no_tag children: { count: 4 }
assert_deprecated do
# there is a tag with 2 children
assert_tag children: { count: 2 }
# in particular, there is a <ul> tag with two children (a nameless pair of <li>s)
assert_tag tag: 'ul', children: { count: 2 }
# there is no tag with 4 children
assert_no_tag children: { count: 4 }
end
end

def test_assert_tag_children_less_than
# there is a tag with less than 5 children
assert_tag children: { less_than: 5 }
# there is no 'ul' tag with less than 2 children
assert_no_tag children: { less_than: 2 }, tag: "ul"
assert_deprecated do
# there is a tag with less than 5 children
assert_tag children: { less_than: 5 }
# there is no 'ul' tag with less than 2 children
assert_no_tag children: { less_than: 2 }, tag: "ul"
end
end

def test_assert_tag_children_greater_than
# there is a 'body' tag with more than 1 children
assert_tag children: { greater_than: 1 }, tag: "body"
# there is no tag with more than 10 children
assert_no_tag children: { greater_than: 10 }
assert_deprecated do
# there is a 'body' tag with more than 1 children
assert_tag children: { greater_than: 1 }, tag: "body"
# there is no tag with more than 10 children
assert_no_tag children: { greater_than: 10 }
end
end

def test_assert_tag_children_only
# there is a tag containing only one child with an id of 'foo'
assert_tag children: { count: 1,
only: { attributes: { id: "foo" } } }
# there is no tag containing only one 'li' child
assert_no_tag children: { count: 1, only: { tag: "li" } }
assert_deprecated do
# there is a tag containing only one child with an id of 'foo'
assert_tag children: { count: 1,
only: { attributes: { id: "foo" } } }
# there is no tag containing only one 'li' child
assert_no_tag children: { count: 1, only: { tag: "li" } }
end
end

def test_assert_tag_content
# the output contains the string "Name"
assert_tag content: /Name/
# the output does not contain the string "test"
assert_no_tag content: /test/
assert_deprecated do
# the output contains the string "Name"
assert_tag content: /Name/
# the output does not contain the string "test"
assert_no_tag content: /test/
end
end

def test_assert_tag_multiple
# there is a 'div', id='bar', with an immediate child whose 'action'
# attribute matches the regexp /somewhere/.
assert_tag tag: "div", attributes: { id: "bar" },
child: { attributes: { action: /somewhere/ } }

# there is no 'div', id='foo', with a 'ul' child with more than
# 2 "li" children.
assert_no_tag tag: "div", attributes: { id: "foo" },
child: { tag: "ul",
children: { greater_than: 2, only: { tag: "li" } } }
assert_deprecated do
# there is a 'div', id='bar', with an immediate child whose 'action'
# attribute matches the regexp /somewhere/.
assert_tag tag: "div", attributes: { id: "bar" },
child: { attributes: { action: /somewhere/ } }

# there is no 'div', id='foo', with a 'ul' child with more than
# 2 "li" children.
assert_no_tag tag: "div", attributes: { id: "foo" },
child: { tag: "ul",
children: { greater_than: 2, only: { tag: "li" } } }
end
end

def test_assert_tag_children_without_content
# there is a form tag with an 'input' child which is a self closing tag
assert_tag tag: "form",
children: { count: 1,
only: { tag: "input" } }

# the body tag has an 'a' child which in turn has an 'img' child
assert_tag tag: "body",
children: { count: 1,
only: { tag: "a",
assert_deprecated do
# there is a form tag with an 'input' child which is a self closing tag
assert_tag tag: "form",
children: { count: 1,
only: { tag: "input" } }

# the body tag has an 'a' child which in turn has an 'img' child
assert_tag tag: "body",
children: { count: 1,
only: { tag: "img" } } } }
only: { tag: "a",
children: { count: 1,
only: { tag: "img" } } } }
end
end

def test_assert_tag_attribute_matching
@response.body = '<input type="text" name="my_name">'
assert_tag tag: 'input',
attributes: { name: /my/, type: 'text' }
assert_no_tag tag: 'input',
attributes: { name: 'my', type: 'text' }
assert_no_tag tag: 'input',
attributes: { name: /^my$/, type: 'text' }
assert_deprecated do
@response.body = '<input type="text" name="my_name">'
assert_tag tag: 'input',
attributes: { name: /my/, type: 'text' }
assert_no_tag tag: 'input',
attributes: { name: 'my', type: 'text' }
assert_no_tag tag: 'input',
attributes: { name: /^my$/, type: 'text' }
end
end

def test_assert_tag_content_matching
@response.body = "<p>hello world</p>"
assert_tag tag: "p", content: "hello world"
assert_tag tag: "p", content: /hello/
assert_no_tag tag: "p", content: "hello"
assert_deprecated do
@response.body = "<p>hello world</p>"
assert_tag tag: "p", content: "hello world"
assert_tag tag: "p", content: /hello/
assert_no_tag tag: "p", content: "hello"
end
end
end
end

0 comments on commit b12850b

Please sign in to comment.