[test-unit-commit:00167] test-unit/test-unit-capybara [master] cleanup assert_body.

null+test-unit @ clear-code.com null+test-unit @ clear-code.com
2012年 1月 4日 (水) 04:50:25 EST


Kouhei Sutou	2012-01-04 18:50:25 +0900 (Wed, 04 Jan 2012)

  New Revision: f21b782586d18c44227232511df85c1e9765b5cb

  Log:
    cleanup assert_body.

  Added files:
    test/run-test.rb
  Copied files:
    test/test-assertions.rb
      (from Gemfile)
    test/test-unit-capybara-test-utils.rb
      (from Gemfile)
  Modified files:
    Gemfile
    lib/test/unit/capybara.rb

  Modified: Gemfile (+3 -1)
===================================================================
--- Gemfile    2012-01-04 18:49:40 +0900 (34ff775)
+++ Gemfile    2012-01-04 18:50:25 +0900 (91562a3)
@@ -18,10 +18,12 @@
 
 gem "test-unit"
 gem "capybara"
+gem "json"
 
-group :development do
+group :development, :test do
   gem "rake"
   gem "jeweler"
   gem "yard"
   gem "packnga"
+  gem "test-unit-notify"
 end

  Modified: lib/test/unit/capybara.rb (+75 -17)
===================================================================
--- lib/test/unit/capybara.rb    2012-01-04 18:49:40 +0900 (21fb6a5)
+++ lib/test/unit/capybara.rb    2012-01-04 18:50:25 +0900 (0ea1ea1)
@@ -20,6 +20,7 @@ require "test/unit/capybara/version"
 
 require 'capybara'
 require 'capybara/dsl'
+require "json"
 require 'test/unit'
 
 module Test::Unit
@@ -51,28 +52,85 @@ module Test::Unit
     end
 
     module Assertions
-      def assert_body(expected, options={})
+      # Passes if @expected@ == @source @ . @source@ is a
+      # method provided by Capybara::DSL.
+      #
+      # @source@ may be parsed depended on response
+      # Content-Type before comparing. Here are parsed
+      # Content-Types:
+      #
+      # - @"application/json"@ := It's parsed by @JSON.parse @ .
+      #
+      # @param [Object] expected the expected body
+      #   content. The actual body may be parsed. It
+      #   depends on @:content_type@ option.
+      #
+      # @option options [String] :content_type (nil)
+      #   the expected Content-Type. If this value is @nil@,
+      #   Content-Type will not be compared.
+      #
+      #   This value can be specified by abbreviated. Here
+      #   are abbreviations:
+      #
+      #   - @:json@ := @"application/json"@
+      #
+      # @yield [expected_response, actual_response] the
+      #   optional compared responses normalizer.
+      # @yieldparam [Hash] expected_response the expected
+      #   response constructed in the method.
+      # @yieldparam [Hash] actual_response the actual
+      #   response constructed in the method.
+      # @yieldreturn [expected_response, actual_response] the
+      #   normalized compared responses.
+      #
+      # @example Pass case
+      #   # Actual response:
+      #   #   Content-Type: application/json
+      #   #   Body: {"status": true}
+      #   assert_body({"status" => true}, :content_type => :json)
+      #
+      # @example Failure case
+      #   # Actual response:
+      #   #   Content-Type: text/html
+      #   #   Body: <html><body>Hello</body></html>
+      #   assert_body("<html><body>World</body></html>")
+      def assert_body(expected, options={}, &block)
         content_type = options[:content_type]
+        actual_response = {
+          :content_type => page.response_headers["Content-Type"],
+        }
+        actual_response[:body] = parse_body(source,
+                                            actual_response[:content_type])
+        expected_response = {:body => expected}
+        if content_type
+          expected_response[:content_type] = normalize_content_type(content_type)
+        else
+          actual_response.delete(:content_type)
+        end
+        if block_given?
+          expected_response, actual_response = yield(expected_response,
+                                                     actual_response)
+	end
+        assert_equal(expected_response, actual_response)
+      end
+
+      private
+      def parse_body(source, content_type)
         case content_type
-        when :json
-          assert_equal({
-                         :content_type => "application/json",
-                         :body => expected,
-                       },
-                       {
-                         :content_type => page.response_headers["Content-Type"],
-                         :body => JSON.parse(source),
-                       })
+        when "application/json"
+          ::JSON.parse(source)
         else
-          format = "unsupported content type: <?>\n" +
-            "expected: <?>\n" +
-            " options: <?>"
-          arguments = [content_type, expected, options]
-          assert_block(build_message(nil, format, *arguments)) do
-            false
-          end
+          source
         end
       end
+
+      # @private
+      CONTENT_TYPE_SHORTCUTS = {
+        :json => "application/json",
+      }
+      def normalize_content_type(content_type)
+        CONTENT_TYPE_SHORTCUTS[content_type] || content_type
+      end
     end
   end
 

  Added: test/run-test.rb (+14 -0) 100755
===================================================================
--- /dev/null
+++ test/run-test.rb    2012-01-04 18:50:25 +0900 (5770310)
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+
+$VERBOSE = true
+
+base_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
+lib_dir = File.join(base_dir, "lib")
+test_dir = File.join(base_dir, "test")
+
+$LOAD_PATH.unshift(lib_dir)
+$LOAD_PATH.unshift(test_dir)
+
+require "test-unit-capybara-test-utils"
+
+exit Test::Unit::AutoRunner.run(true, test_dir)

  Copied: test/test-assertions.rb (+26 -8) 57%
===================================================================
--- Gemfile    2012-01-04 18:49:40 +0900 (34ff775)
+++ test/test-assertions.rb    2012-01-04 18:50:25 +0900 (dfd0ce7)
@@ -1,4 +1,4 @@
-# -*- coding: utf-8; mode: ruby -*-
+# -*- ruby -*-
 #
 # Copyright (C) 2012  Kouhei Sutou <kou @ clear-code.com>
 #
@@ -16,12 +16,30 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-gem "test-unit"
-gem "capybara"
+require "test-unit-capybara-test-utils"
 
-group :development do
-  gem "rake"
-  gem "jeweler"
-  gem "yard"
-  gem "packnga"
+class AssertionsTest < Test::Unit::TestCase
+  include Capybara::DSL
+
+  class BodyTest < self
+    setup do
+      Capybara.app = lambda do |environment|
+        [
+          200,
+          {"Content-Type" => "application/json"},
+          [JSON.generate({"status" => true})],
+        ]
+      end
+    end
+
+    def test_with_shortcut_content_type
+      visit("/")
+      assert_body({"status" => true}, :content_type => :json)
+    end
+
+    def test_without_content_type
+      visit("/")
+      assert_body({"status" => true})
+    end
+  end
 end

  Copied: test/test-unit-capybara-test-utils.rb (+10 -9) 73%
===================================================================
--- Gemfile    2012-01-04 18:49:40 +0900 (34ff775)
+++ test/test-unit-capybara-test-utils.rb    2012-01-04 18:50:25 +0900 (021843f)
@@ -1,4 +1,4 @@
-# -*- coding: utf-8; mode: ruby -*-
+# -*- ruby -*-
 #
 # Copyright (C) 2012  Kouhei Sutou <kou @ clear-code.com>
 #
@@ -16,12 +16,13 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-gem "test-unit"
-gem "capybara"
+require "bundler/setup"
 
-group :development do
-  gem "rake"
-  gem "jeweler"
-  gem "yard"
-  gem "packnga"
-end
+require "test/unit"
+require "test/unit/notify"
+require "test/unit/capybara"
+
+tmp_path = File.expand_path(File.join("tmp", "capybara"), __FILE__)
+Capybara.save_and_open_page_path = tmp_path
+Capybara.default_driver = nil
+Capybara.current_driver = nil



test-unit-commit メーリングリストの案内