From 409bc0970aa6145dfaeb6d79363a74e0244002a0 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 6 May 2005 16:42:01 +0000 Subject: Added functionality to assert_tag, so you can now do tests on the siblings of a node, to assert that some element comes before or after the element in question, or just to assert that some element exists as a sibling #1226 [Jamis Buck] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1291 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 + actionpack/lib/action_controller/assertions.rb | 6 +++ .../vendor/html-scanner/html/node.rb | 54 ++++++++++++++++++++-- .../vendor/html-scanner/html/tokenizer.rb | 2 +- 4 files changed, 59 insertions(+), 5 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 1630a26a2b..cc2029c2c2 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added functionality to assert_tag, so you can now do tests on the siblings of a node, to assert that some element comes before or after the element in question, or just to assert that some element exists as a sibling #1226 [Jamis Buck] + * Added better error handling for regexp caching expiration * Fixed handling of requests coming from unknown HTTP methods not to kill the server diff --git a/actionpack/lib/action_controller/assertions.rb b/actionpack/lib/action_controller/assertions.rb index 00388f8f59..5d1826b72a 100644 --- a/actionpack/lib/action_controller/assertions.rb +++ b/actionpack/lib/action_controller/assertions.rb @@ -136,6 +136,12 @@ module Test #:nodoc: # meet the criteria described by the hash. # * :descendant: a hash. At least one of the node's descendants # must meet the criteria described by the hash. + # * :sibling: a hash. At least one of the node's siblings must + # meet the criteria described by the hash. + # * :after: a hash. The node must be after any sibling meeting + # the criteria described by the hash, and at least one sibling must match. + # * :before: a hash. The node must be before any sibling meeting + # the criteria described by the hash, and at least one sibling must match. # * :children: a hash, for counting children of a node. Accepts # the keys: # * :count: either a number or a range which must equal (or diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb b/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb index b3979fa3cb..2d32962ba8 100644 --- a/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb +++ b/actionpack/lib/action_controller/vendor/html-scanner/html/node.rb @@ -13,7 +13,8 @@ module HTML#:nodoc: # keys are valid, and require no further processing when :attributes then hash[k] = keys_to_strings(v) - when :parent, :child, :ancestor, :descendant + when :parent, :child, :ancestor, :descendant, :sibling, :before, + :after hash[k] = Conditions.new(v) when :children hash[k] = v = keys_to_symbols(v) @@ -119,12 +120,20 @@ module HTML#:nodoc: end class </) or raise "expected > (got #{scanner.rest.inspect} for #{content}, #{attributes.inspect})" + unless scanner.scan(/\s*>/) + if strict + raise "expected > (got #{scanner.rest.inspect} for #{content}, #{attributes.inspect})" + else + # throw away all text until we find what we're looking for + scanner.skip_until(/>/) or scanner.terminate + end + end Tag.new(parent, line, pos, name, attributes, closing) end @@ -296,6 +312,12 @@ module HTML#:nodoc: # meet the criteria described by the hash. # * :descendant: a hash. At least one of the node's descendants # must meet the criteria described by the hash. + # * :sibling: a hash. At least one of the node's siblings must + # meet the criteria described by the hash. + # * :after: a hash. The node must be after any sibling meeting + # the criteria described by the hash, and at least one sibling must match. + # * :before: a hash. The node must be before any sibling meeting + # the criteria described by the hash, and at least one sibling must match. # * :children: a hash, for counting children of a node. Accepts the # keys: # ** :count: either a number or a range which must equal (or @@ -404,6 +426,30 @@ module HTML#:nodoc: end end end + + # test siblings + if conditions[:sibling] || conditions[:before] || conditions[:after] + siblings = parent ? parent.children : [] + self_index = siblings.index(self) + + if conditions[:sibling] + return false unless siblings.detect do |s| + s != self && s.match(conditions[:sibling]) + end + end + + if conditions[:before] + return false unless siblings[self_index+1..-1].detect do |s| + s != self && s.match(conditions[:before]) + end + end + + if conditions[:after] + return false unless siblings[0,self_index].detect do |s| + s != self && s.match(conditions[:after]) + end + end + end true end diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb b/actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb index 2c3ee922d9..45066319f5 100644 --- a/actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +++ b/actionpack/lib/action_controller/vendor/html-scanner/html/tokenizer.rb @@ -92,4 +92,4 @@ module HTML#:nodoc: end end -end \ No newline at end of file +end -- cgit v1.2.3