kucaahbe / rspec-html-matchers

Old school have_tag, with_tag(and more) matchers for rspec 3 (Nokogiri powered)
http://rubygems.org/gems/rspec-html-matchers
MIT License
199 stars 90 forks source link

missing matcher for script tag content #20

Closed HoneyryderChuck closed 12 years ago

HoneyryderChuck commented 12 years ago

Hi,

In order for me to fully replace rspec_tag_matchers with this one (which I'd really like to, since the other is rspec-rails-dependent and this is only nokogiri-dependent), I'd need one matcher for the content of a script tag, namely access its script and check whether certain text is available inside. Currently there doesn't seem to be one, or haven't I looked further enough?

Regards

kucaahbe commented 12 years ago

you can match any tag's content by using have_tag, if you have some special case I'll appreciate if you post it here.

HoneyryderChuck commented 12 years ago

True, but it's more about the way you do it. I would like to have something like this:

html.should have_tag("script") do
  with_text("function bla()")
  with_text("var i = 2")
  with_text(/BAMALAM\.com/)
end

Currently text parsing is only available as a direct param of have tag, so the above example is now written as:

html.should have_tag("script", :text => "function bla()")
html.should have_tag("script", :text => "var i = 2")
html.should have_tag("script", :text => /BAMALAM\.com/)

This would even be useful to check multiple text assertions on some div assertion and so on.

kucaahbe commented 12 years ago

Got it. I'm working on something similar right now, so as you suggest I'll add with_text matcher. Thanks for this!

HoneyryderChuck commented 12 years ago

Hi, thx for the quick feature! I'm closing the issue then. This is also going to be referenced as soon as you make it into a versioned release, right?

kucaahbe commented 12 years ago

yes

sodabrew commented 12 years ago

Hi! Quick question about with_text: can I use a regex with captures and access those in my scope? e.g.

rendered.should have_tag("script") do
  with_text /foo = "([^"]+)"/
  $1.should == "Contents of the Foo variable in my JS"
end
kucaahbe commented 12 years ago

@sodabrew don't think so, it wasn't designed for such cases

sodabrew commented 12 years ago

Yeah, I tried it within have_tag and the captures did not survive into the block. I wonder if there's a way to do this at all within nokogiri -- I have some code that I'm porting from rspec 1 and it used havetag { ... $ ... } where $_ was the text contents of the tag, and was further tested with a capturing regex.