[test-unit-commit:00189] test-unit/test-unit-capybara [master] add assert_not_find.
null+test-unit @ clear-code.com
null+test-unit @ clear-code.com
2012年 1月 9日 (月) 05:43:37 EST
Kouhei Sutou 2012-01-09 19:43:37 +0900 (Mon, 09 Jan 2012)
New Revision: 6df0914d9d2ce8ef0ce61e35d3fdb40803a20441
Log:
add assert_not_find.
Modified files:
lib/test/unit/capybara.rb
test/test-assertions.rb
Modified: lib/test/unit/capybara.rb (+97 -0)
===================================================================
--- lib/test/unit/capybara.rb 2012-01-09 18:10:17 +0900 (979aa61)
+++ lib/test/unit/capybara.rb 2012-01-09 19:43:37 +0900 (2c9a3de)
@@ -321,6 +321,103 @@ EOT
end
end
+ # @param [...] args (see {::Capybara::Node::Finders#find})
+ #
+ # @see ::Capybara::Node::Finders#find
+ #
+ # @overload assert_not_find(*args, &block)
+ # Passes if the selector doesn't find any elements
+ # from the current node.
+ #
+ # @example Pass case
+ # # Actual response:
+ # # <html>
+ # # <body>
+ # # <h1>Hello</h1>
+ # # <h2>Yay!</h2>
+ # # <div class="section">
+ # # <h2>World</h2>
+ # # </div>
+ # # </body>
+ # # </html>
+ # assert_not_find("h3")
+ #
+ # @example Failure case
+ # # Actual response:
+ # # <html>
+ # # <body>
+ # # <h1>Hello</h1>
+ # # <h2>Yay!</h2>
+ # # <div class="section">
+ # # <h2>World</h2>
+ # # </div>
+ # # </body>
+ # # </html>
+ # assert_not_find("h1")
+ #
+ # @overload assert_not_find(node, *args, &block)
+ # Passes if the selector doesn't find any element from @node @ .
+ #
+ # @param [::Capybara::Node::Base] node The target node.
+ #
+ # @example Pass case
+ # # Actual response:
+ # # <html>
+ # # <body>
+ # # <h1>Hello</h1>
+ # # <h2>Yay!</h2>
+ # # <div class="section">
+ # # <h2>World</h2>
+ # # </div>
+ # # </body>
+ # # </html>
+ # section = assert_find("section")
+ # p section
+ # # => #<Capybara::Element tag="h2" path="/html/body/div">
+ # assert_not_find(section, "h1")
+ #
+ # @example Failure case
+ # # Actual response:
+ # # <html>
+ # # <body>
+ # # <h1>Hello</h1>
+ # # <h2>Yay!</h2>
+ # # <div class="section">
+ # # <h2>World</h2>
+ # # </div>
+ # # </body>
+ # # </html>
+ # section = assert_find("section")
+ # p section
+ # # => #<Capybara::Element tag="h2" path="/html/body/div">
+ # assert_not_find(section, "h2")
+ def assert_not_find(*args, &block)
+ node = nil
+ node = args.shift if args[0].is_a?(::Capybara::Node::Base)
+ args = normalize_page_finder_arguments(args)
+ if node
+ element = node.first(*args[:finder_arguments])
+ else
+ element = first(*args[:finder_arguments])
+ end
+ format = <<-EOT
+<?>(?) expected to not be found a element but was
+<?> in
+<?>
+EOT
+ element_source = nil
+ element_source = node_source(element) if element
+ full_message = build_message(args[:message],
+ format,
+ args[:locator],
+ args[:kind],
+ element_source,
+ node_source(node))
+ assert_block(full_message) do
+ element.nil?
+ end
+ end
+
private
def page_content_type
page.response_headers["Content-Type"]
Modified: test/test-assertions.rb (+97 -0)
===================================================================
--- test/test-assertions.rb 2012-01-09 18:10:17 +0900 (95fc374)
+++ test/test-assertions.rb 2012-01-09 19:43:37 +0900 (1894067)
@@ -210,4 +210,101 @@ EOM
end
end
end
+
+ class NotFindTest < self
+ setup do
+ @html = <<-HTML
+<html>
+ <body>
+ <h1>Hello</h1>
+ <h2>Yay!</h2>
+ <div class="section">
+ <h2>World</h2>
+ </div>
+ </body>
+</html>
+HTML
+ Capybara.app = lambda do |environment|
+ [
+ 200,
+ {"Content-Type" => "text/html"},
+ [@html],
+ ]
+ end
+ end
+
+ class WithoutNodeTest < self
+ def test_no_kind
+ visit("/")
+ assert_not_find("h3")
+ end
+
+ def test_css
+ visit("/")
+ assert_not_find(:css, "h3")
+ end
+
+ def test_xpath
+ visit("/")
+ assert_not_find(:xpath, "//h3")
+ end
+
+ def test_block
+ visit("/")
+ assert_find("div.section") do
+ assert_not_find("h3")
+ end
+ end
+
+ def test_fail
+ visit("/")
+ h1_html = @html.scan(/<h1>.*?<\/h1>/m)[0]
+ message = <<-EOM.strip
+<"h1">(:css) expected to not be found a element but was
+<#{PP.pp(h1_html, "").chomp}> in
+<#{PP.pp(@html, "").chomp}>
+EOM
+ exception = Test::Unit::AssertionFailedError.new(message)
+ assert_raise(exception) do
+ assert_not_find("h1")
+ end
+ end
+ end
+
+ class WithNodeTest < self
+ def test_no_kind
+ visit("/")
+ section = assert_find("div.section")
+ assert_not_find(section, "h1")
+ end
+
+ def test_css
+ visit("/")
+ section = assert_find("div.section")
+ assert_not_find(section, :css, "h1")
+ end
+
+ def test_xpath
+ visit("/")
+ section = assert_find("div.section")
+ assert_not_find(section, :xpath, ".//h1")
+ end
+
+ def test_fail
+ visit("/")
+ section = assert_find("div.section")
+ section_html = @html.scan(/<div class="section">.*?<\/div>/m)[0]
+ h2_html = section_html.scan(/<h2>.*?<\/h2>/m)[0]
+ message = <<-EOM.strip
+<"h2">(:css) expected to not be found a element but was
+<#{PP.pp(h2_html, "").chomp}> in
+<#{PP.pp(section_html, "").chomp}>
+EOM
+ exception = Test::Unit::AssertionFailedError.new(message)
+ assert_raise(exception) do
+ assert_not_find(section, "h2")
+ end
+ end
+ end
+ end
end
test-unit-commit メーリングリストの案内