Skip to content

Commit

Permalink
added failing test cases for chaining filters
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Nov 26, 2008
1 parent 18e9154 commit 5b437c3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 67 deletions.
6 changes: 1 addition & 5 deletions lib/elementor/element_set.rb
Expand Up @@ -26,11 +26,7 @@ def with_attrs(options={})
end
end
end

def inspect
map(&:text).inspect
end


def method_missing(sym, *args, &block)
result.try(sym, doc, *args) || super
end
Expand Down
129 changes: 67 additions & 62 deletions spec/elementor_spec.rb
@@ -1,19 +1,19 @@
require 'spec/spec_helper'
require File.dirname(__FILE__) + '/spec_helper'

HTML = Nokogiri::HTML::Builder.new {
html {
head { title("This is the title") }
body {
h1("A header")
div(:class => "tag-cloud") {
a(:href => '#foo', :class => 'even') { text "Foo" }
a(:href => '#bar') { text "Bar" }
div(:class => "tag-cloud", :rel => "other") {
a(:href => '#foo', :class => 'tag even') { text "Foo" }
a(:href => '#bar', :class => 'tag') { text "Bar" }
}
div(:id => "user-links") {
h1("User Link Section")
div(:class => "tag-cloud") {
a(:href => '#fizz', :class => 'even') { text "Fizz" }
a(:href => '#buzz') { text "Buzz" }
a(:href => '#fizz', :class => 'tag even') { text "Fizz" }
a(:href => '#buzz', :class => 'tag') { text "Buzz" }
}
}
}
Expand Down Expand Up @@ -48,49 +48,12 @@
end
end

describe "error handling" do
context "before methods have been called on result object" do
it "swallows errors" do
view = Class.new { def render; send(:foo!) end }.new

meta_def(:whoops!) { view.render }

proc {
@result = elements(:from => :whoops!) do |tag|
tag.headers "h1"
tag.tags ".tag-cloud a"
tag.user_links "#user-links"
end

@result.instance_variable_get("@this").doc
}.should_not raise_error
end
end

context "after methods have been called on result object" do
it "re-raises errors that occur when :from blows up" do
view = Class.new { def render; send(:foo!) end }.new

meta_def(:whoops!) { view.render }

proc {
@result = elements(:from => :whoops!) do |tag|
tag.headers "h1"
tag.tags ".tag-cloud a"
tag.user_links "#user-links"
end

result.should have(0).headers
}.should raise_error(NoMethodError)
end
end
end

context "with elements defined" do
before(:each) do
@result = elements(:from => :body) do |tag|
tag.headers "h1"
tag.tags ".tag-cloud a"
tag.tag_clouds ".tag-cloud"
tag.tags "a.tag"
tag.user_links "#user-links"
end
end
Expand All @@ -105,10 +68,6 @@
result.search('h1').should have(2).nodes
end

it "creates more readable inspect" do
result.headers.inspect.should == "[\"A header\", \"User Link Section\"]"
end

describe "chaining selectors" do
it "can chain selector aliases" do
result.user_links.headers.should have(1).node
Expand All @@ -120,7 +79,7 @@
end

it "works with with_attrs filter" do
result.user_links.tags.with_attrs(:class => 'even').should have(1).node
result.user_links.tags.with_attrs(:class => /even/).should have(1).node
end

describe "as an rspec matcher" do
Expand All @@ -137,18 +96,19 @@
end

it "allows chaining" do
result.user_links.should have(1).tags.with_attrs(:class => "even").with_text('Fizz')
result.user_links.should have(1).tags.with_attrs(:class => /even/).with_text('Fizz')
end
end
end

describe ":from option" do
it "determines which method should be called to get the markup" do
@result = elements(:from => :body) do |tag|
meta_eval { alias_method :other_body, :body }

@result = elements(:from => :other_body) do |tag|
tag.headers "h1"
tag.tags ".tag-cloud a"
tag.user_links "#user-links"
tag.bodies "body"
end

result.should have(2).headers
Expand Down Expand Up @@ -179,8 +139,12 @@
result.tags.with_text(/foo|bar/i).should have(2).nodes
end

it "allows chaining" do
result.tags.with_text('zz').with_attrs(:class => "even").should have(1).node
it "allows chaining with other filters" do
result.tags.with_text('zz').with_attrs(:class => /even/).should have(1).node
end

it "allows chaining with selector aliases" do
result.tag_clouds.with_text('Fizz').tags.should have(2).nodes
end

describe "as an rspec matcher" do
Expand All @@ -197,7 +161,7 @@
end

it "allows chaining" do
result.should have(1).tags.with_text("zz").with_attrs(:class => "even")
result.should have(1).tags.with_text("zz").with_attrs(:class => /even/)
end
end
end
Expand All @@ -206,11 +170,11 @@
context "using string values" do
it "limits results by one attribute" do
result.tags.with_attrs(:href => '#foo').should have(1).node
result.tags.with_attrs(:class => 'even').should have(2).nodes
result.tags.with_attrs(:class => "tag even").should have(2).nodes
end

it "limits results by multiple attributes" do
result.tags.with_attrs(:class => 'even', :href => '#foo').should have(1).node
result.tags.with_attrs(:class => /even/, :href => '#foo').should have(1).node
end
end

Expand All @@ -224,8 +188,12 @@
end
end

it "allows chaining" do
result.tags.with_attrs(:class => 'even').with_text('Fizz').should have(1).node
it "allows chaining with other filters" do
result.tags.with_attrs(:class => /even/).with_text('Fizz').should have(1).node
end

it "allows chaining with selector aliases" do
result.tag_clouds.with_attrs(:rel => 'other').tags.should have(2).nodes
end

describe "as an rspec matcher" do
Expand All @@ -238,15 +206,52 @@
end

it "works with many matches" do
result.should have(2).tags.with_attrs(:class => 'even')
result.should have(2).tags.with_attrs(:class => /even/)
end

it "allows chaining" do
result.should have(1).tags.with_attrs(:class => "even").with_text('Fizz')
result.should have(1).tags.with_attrs(:class => /even/).with_text('Fizz')
end
end
end
end
describe "error handling" do
context "before methods have been called on result object" do
it "swallows errors" do
view = Class.new { def render; send(:foo!) end }.new

meta_def(:whoops!) { view.render }

proc {
@result = elements(:from => :whoops!) do |tag|
tag.headers "h1"
tag.tags ".tag-cloud a"
tag.user_links "#user-links"
end

@result.instance_variable_get("@this").doc
}.should_not raise_error
end
end

context "after methods have been called on result object" do
it "re-raises errors that occur when :from blows up" do
view = Class.new { def render; send(:foo!) end }.new

meta_def(:whoops!) { view.render }

proc {
@result = elements(:from => :whoops!) do |tag|
tag.headers "h1"
tag.tags ".tag-cloud a"
tag.user_links "#user-links"
end

result.should have(0).headers
}.should raise_error(NoMethodError)
end
end
end

describe "#parse!" do
it "parses new markup" do
Expand Down

0 comments on commit 5b437c3

Please sign in to comment.