Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
110 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |