Skip to content

Commit

Permalink
coercing non-string, non-regex values to strings for filters
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Dec 2, 2008
1 parent 9043ff2 commit 5a4ef42
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/elementor/element_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def with_text(matcher)
case matcher
when Regexp then item.text =~ matcher
when String then item.text.include?(matcher)
else item.text.include?(matcher.to_s)
end
end
end
Expand All @@ -24,6 +25,7 @@ def with_attrs(options={})
case value
when Regexp then item[key.to_s] =~ value
when String then item[key.to_s] == value
else item[key.to_s] == value.to_s
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/elementor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
head { title("This is the title") }
body {
h1("A header")
h2 {
text "Version: "
span(:rel => "0") { text "1" }
}
div(:class => "tag-cloud", :rel => "other") {
a(:href => '#foo', :class => 'tag even') { text "Foo" }
a(:href => '#bar', :class => 'tag') { text "Bar" }
Expand Down Expand Up @@ -52,6 +56,7 @@
before(:each) do
@result = elements(:from => :body) do |tag|
tag.headers "h1"
tag.versions "h2 span"
tag.tag_clouds ".tag-cloud"
tag.tags "a.tag"
tag.user_links "#user-links"
Expand Down Expand Up @@ -143,6 +148,10 @@
result.tags.with_text('zz').with_attrs(:class => /even/).should have(1).node
end

it "coerces things that aren't strings or regexes into strings" do
result.versions.with_text(1).should have(1).node
end

describe "as an rspec matcher" do
it "works with no matches" do
result.should have(0).tags.with_text("Wee")
Expand Down Expand Up @@ -184,6 +193,12 @@
end
end

context "using non-string, non-regex values" do
it "coerces value into strings" do
result.versions.with_attrs(:rel => 0).should have(1).node
end
end

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

0 comments on commit 5a4ef42

Please sign in to comment.