Permalink
Browse files

Specs

  • Loading branch information...
1 parent 296457b commit c34082bfcde01ad2d7b9ed4f061ae0a64f869916 @wycats committed Oct 22, 2008
Showing with 110 additions and 6 deletions.
  1. +6 −6 css/xpath_visitor.rb
  2. +13 −0 spec/any_element_spec.rb
  3. +15 −0 spec/attribute_equals.rb
  4. +15 −0 spec/attribute_spec.rb
  5. +12 −0 spec/element_spec.rb
  6. +49 −0 spec/spec_helper.rb
View
12 css/xpath_visitor.rb
@@ -55,12 +55,12 @@ def visit_id node
end
def visit_attribute_condition node
- attribute = if (node.value.first.type == :FUNCTION) or (node.value.first.value.first =~ /^@/)
- ''
- else
- 'child::'
- end
- attribute += node.value.first.accept(self)
+ # attribute = if (node.value.first.type == :FUNCTION) or (node.value.first.value.first =~ /^@/)
+ # ''
+ # else
+ # 'child::'
+ # end
+ attribute = "@#{node.value.first.accept(self)}"
# Support non-standard css
attribute.gsub!(/^@@/, '@')
View
13 spec/any_element_spec.rb
@@ -0,0 +1,13 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
+
+describe "*" do
+ it "should get a list of all nodes" do
+ HTML.should have_nodes("*", 6)
+ end
+end
+
+describe "E *" do
+ it "should get a list of descendant nodes of E" do
+ HTML.should have_nodes("div *", 2)
+ end
+end
View
15 spec/attribute_equals.rb
@@ -0,0 +1,15 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
+
+describe "Attributes" do
+ it "<[foo]> should return a list of all nodes with the attribute" do
+ HTML.should have_nodes("[foo]", 2)
+ end
+
+ it "<*[foo]> should return a list of all nodes with the attribute" do
+ HTML.should have_nodes("*[foo]", 2)
+ end
+
+ it "<E[foo]> should return a list of all nodes with the attribute" do
+ HTML.should have_nodes("span[foo]", 1)
+ end
+end
View
15 spec/attribute_spec.rb
@@ -0,0 +1,15 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
+
+describe "Attributes" do
+ it "<[foo]> should return a list of all nodes with the attribute" do
+ HTML.should have_nodes("[foo]", 2)
+ end
+
+ it "<*[foo]> should return a list of all nodes with the attribute" do
+ HTML.should have_nodes("*[foo]", 2)
+ end
+
+ it "<E[foo]> should return a list of all nodes with the attribute" do
+ HTML.should have_nodes("span[foo]", 1)
+ end
+end
View
12 spec/element_spec.rb
@@ -0,0 +1,12 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
+
+describe "Elements" do
+ it "<E> should return a list of all nodes with the element" do
+ HTML.should have_nodes("span", 2)
+ end
+
+ it "<E> should return a list of all nodes with the element" do
+ pending
+ HTML.should have_nodes("SPAN", 2)
+ end
+end
View
49 spec/spec_helper.rb
@@ -0,0 +1,49 @@
+require "libxml"
+require File.join(File.dirname(__FILE__), "..", "css")
+
+HTML = <<HERE
+<html>
+ <body>
+ <div foo='bar'>Hello
+ <span foo='bard'>My name</span>
+ <span food='bar'>is wycats</span>
+ </div>
+ <p>Some text</p>
+ </body>
+</html>
+HERE
+
+module Nokogiri
+ module Test
+ module Matchers
+
+ class HaveNodes
+ def initialize(selector, count)
+ @selector, @count = selector, count
+ end
+
+ def matches?(text)
+ @doc = LibXML::XML::HTMLParser.string(text).parse
+ @xpath = Nokogiri::CSS::Parser.new.parse(@selector)[0].to_xpath
+ @actual_count = @doc.find(@xpath).size
+ @actual_count == @count
+ end
+
+ def failure_message
+ "Expected the following text to have #{@count} nodes matching " \
+ "#{@selector}, but it had #{@actual_count}. The generated " \
+ "xpath was #{@xpath}:\n#{@doc.inspect}" \
+ end
+ end
+
+ def have_nodes(selector, count)
+ HaveNodes.new(selector, count)
+ end
+
+ end
+ end
+end
+
+Spec::Runner.configure do |config|
+ config.include Nokogiri::Test::Matchers
+end

0 comments on commit c34082b

Please sign in to comment.