Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 1195

Show
Ignore:
Timestamp:
04/17/05 16:43:48 (3 years ago)
Author:
david
Message:

Added assert_tag and assert_no_tag as a much improved alternative to the deprecated assert_template_xpath_match #1126 [Jamis Buck]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r1194 r1195  
    22 
    33* Added Request#xml_http_request? (and an alias xhr?) to that'll return true when the request came from one of the Javascript helper methods (Ajax). This can be used to give one behavior for modern browsers supporting Ajax, another to old browsers #1127 [Sam Stephenson] 
     4 
     5* Added assert_tag and assert_no_tag as a much improved alternative to the deprecated assert_template_xpath_match #1126 [Jamis Buck] 
    46 
    57* Deprecated the majority of all the testing assertions and replaced them with a much smaller core and access to all the collections the old assertions relied on. That way the regular test/unit assertions can be used against these. Added documentation about how to use it all. 
  • trunk/actionpack/lib/action_controller/assertions.rb

    r1190 r1195  
    117117        assert_generates(path, options, defaults, extras, message) 
    118118      end 
     119 
     120      # Asserts that there is a tag/node/element in the body of the response 
     121      # that meets all of the given conditions. The +conditions+ parameter must 
     122      # be a hash of any of the following keys (all are optional): 
     123      # 
     124      # * <tt>:tag</tt>: the node type must match the corresponding value 
     125      # * <tt>:attributes</tt>: a hash. The node's attributes must match the 
     126      #   corresponding values in the hash. 
     127      # * <tt>:parent</tt>: a hash. The node's parent must match the 
     128      #   corresponding hash. 
     129      # * <tt>:child</tt>: a hash. At least one of the node's immediate children 
     130      #   must meet the criteria described by the hash. 
     131      # * <tt>:ancestor</tt>: a hash. At least one of the node's ancestors must 
     132      #   meet the criteria described by the hash. 
     133      # * <tt>:descendant</tt>: a hash. At least one of the node's descendants 
     134      #   must meet the criteria described by the hash. 
     135      # * <tt>:children</tt>: a hash, for counting children of a node. Accepts 
     136      #   the keys: 
     137      # ** <tt>:count</tt>: either a number or a range which must equal (or 
     138      #    include) the number of children that match. 
     139      # ** <tt>:less_than</tt>: the number of matching children must be less 
     140      #    than this number. 
     141      # ** <tt>:greater_than</tt>: the number of matching children must be 
     142      #    greater than this number. 
     143      # ** <tt>:only</tt>: another hash consisting of the keys to use 
     144      #    to match on the children, and only matching children will be 
     145      #    counted. 
     146      # * <tt>:content</tt>: (text nodes only). The content of the node must 
     147      #   match the given value. 
     148      # 
     149      # Conditions are matched using the following algorithm: 
     150      # 
     151      # * if the condition is a string, it must be a substring of the value. 
     152      # * if the condition is a regexp, it must match the value. 
     153      # * if the condition is a number, the value must match number.to_s. 
     154      # * if the condition is +true+, the value must not be +nil+. 
     155      # * if the condition is +false+ or +nil+, the value must be +nil+. 
     156      # 
     157      # Usage: 
     158      # 
     159      #   # assert that there is a "span" tag 
     160      #   assert_tag :tag => "span" 
     161      # 
     162      #   # assert that there is a "span" inside of a "div" 
     163      #   assert_tag :tag => "span", :parent => { :tag => "div" } 
     164      # 
     165      #   # assert that there is a "span" somewhere inside a table 
     166      #   assert_tag :tag => "span", :ancestor => { :tag => "table" } 
     167      # 
     168      #   # assert that there is a "span" with at least one "em" child 
     169      #   assert_tag :tag => "span", :child => { :tag => "em" } 
     170      # 
     171      #   # assert that there is a "span" containing a (possibly nested) 
     172      #   # "strong" tag. 
     173      #   assert_tag :tag => "span", :descendant => { :tag => "strong" } 
     174      # 
     175      #   # assert that there is a "span" containing between 2 and 4 "em" tags 
     176      #   # as immediate children 
     177      #   assert_tag :tag => "span", 
     178      #              :children => { :count => 2..4, :only => { :tag => "em" } }  
     179      # 
     180      #   # get funky: assert that there is a "div", with an "ul" ancestor 
     181      #   # and an "li" parent (with "class" = "enum"), and containing a  
     182      #   # "span" descendant that contains text matching /hello world/ 
     183      #   assert_tag :tag => "div", 
     184      #              :ancestor => { :tag => "ul" }, 
     185      #              :parent => { :tag => "li", 
     186      #                           :attributes => { :class => "enum" } }, 
     187      #              :descendant => { :tag => "span", 
     188      #                               :child => /hello world/ } 
     189      def assert_tag(opts) 
     190        tag = find_tag(opts) 
     191        assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}" 
     192      end 
     193       
     194      # Identical to #assert_tag, but asserts that a matching tag does _not_ 
     195      # exist. (See #assert_tag for a full discussion of the syntax.) 
     196      def assert_no_tag(opts) 
     197        tag = find_tag(opts) 
     198        assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}" 
     199      end 
     200     
    119201    end 
    120202  end 
  • trunk/actionpack/lib/action_controller/test_process.rb

    r1189 r1195  
    244244        # execute the request and set/volley the response 
    245245        def process(action, parameters = nil, session = nil, flash = nil) 
     246          @html_document = nil 
    246247          @request.env['REQUEST_METHOD'] ||= "GET" 
    247248          @request.action = action.to_s 
     
    304305              (parameters||{}).update(:only_path => true, :action=>action)))) 
    305306        end 
     307 
     308        def html_document 
     309          require_html_scanner 
     310          @html_document ||= HTML::Document.new(@response.body) 
     311        end 
     312         
     313        def find_tag(conditions) 
     314          html_document.find(conditions) 
     315        end 
     316 
     317        def find_all_tag(conditions) 
     318          html_document.find_all(conditions) 
     319        end 
     320 
     321        def require_html_scanner 
     322          return true if defined?(HTML::Document) 
     323          require 'html/document' 
     324        rescue LoadError 
     325          $:.unshift File.dirname(__FILE__) + "/vendor/html-scanner" 
     326          require 'html/document' 
     327        end 
    306328      end 
    307329  end 
  • trunk/actionpack/test/controller/test_test.rb

    r1103 r1195  
    99    def test_uri 
    1010      render_text @request.request_uri 
     11    end 
     12 
     13    def test_html_output 
     14      render_text <<HTML 
     15<html> 
     16  <body> 
     17    <div id="foo"> 
     18      <ul> 
     19        <li class="item">hello</li> 
     20        <li class="item">goodbye</li> 
     21      </ul> 
     22    </div> 
     23    <div id="bar"> 
     24      <form action="/somewhere"> 
     25        Name: <input type="text" name="person[name]" id="person_name" /> 
     26      </form> 
     27    </div> 
     28  </body> 
     29</html> 
     30HTML 
    1131    end 
    1232  end 
     
    4868    assert_equal @response.body, "/explicit/uri" 
    4969  end 
     70 
     71  def test_assert_tag 
     72    process :test_html_output 
     73 
     74    # there is a 'div', id='bar', with an immediate child whose 'action' 
     75    # attribute matches the regexp /somewhere/. 
     76    assert_tag :tag => "div", :attributes => { :id => "bar" }, 
     77               :child => { :attributes => { :action => /somewhere/ } } 
     78 
     79    # there is no 'div', id='foo', with a 'ul' child with more than 
     80    # 2 "li" children. 
     81    assert_no_tag :tag => "div", :attributes => { :id => "foo" }, 
     82                  :child => { 
     83                    :tag => "ul", 
     84                    :children => { :greater_than => 2, 
     85                                   :only => { :tag => "li" } } } 
     86  end 
    5087end