Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Specs
  • Loading branch information
wycats committed Oct 22, 2008
1 parent 296457b commit c34082b
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 6 deletions.
12 changes: 6 additions & 6 deletions css/xpath_visitor.rb
Expand Up @@ -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!(/^@@/, '@')
Expand Down
13 changes: 13 additions & 0 deletions 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
15 changes: 15 additions & 0 deletions 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
15 changes: 15 additions & 0 deletions 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
12 changes: 12 additions & 0 deletions 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
49 changes: 49 additions & 0 deletions 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.