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

Multi line HTML matchers #11

Closed cimm closed 12 years ago

cimm commented 12 years ago

Not really a bug but I would like to know if this "works as designed" or if we could improve this a little bit. Suppose the following HTML snippet:

<p class='payed_at'>
  2011-10-23
</p>

And the following spec:

rendered.should have_tag(".payed_at", :text => "2011-10-23")

This won't work because of the line feeds in the HTML snippet. There are 2 ways around this. You could remove the line feeds in the HTML but that's not always the best solution or you could use a regex in the test like:

rendered.should have_tag(".payed_at", :text => /2011-10-23/)

When the date is a variable it gets a bit verbose /#{payed_at.to_s}/ and when it's a mock it even more difficult... Am I nitpicking here of could we find a way in the API to tell the gem not to worry about the whitespace?

Thanks once again!

kucaahbe commented 12 years ago

I'm sorry for so long response, had no time to spend on this, anyway answer is following:

this was issue I knew at the beginning but successfully forgot about, until someone(in this case You) will catch this, so behavior is following: you can match tags ignoring presence of leading and trailing whitespaces, but not in tags where they are important, like 'pre' tag, and maybe some other tags(not investigated yet, maybe 'textarea' too), technically this tags should be listed in PRESERVE_WHITESPACE_TAGS constant.

kucaahbe commented 12 years ago

hm, and here should be part two like:

'<p>
  one             two            three
</p>'.should have_tag('p',:text => 'one two three')

I'll think about it. Leaving issue open, current functionality already in master so you can try it. Have a nice day!

cimm commented 12 years ago

Hi Dmitry,

Whow, you are right. There is much more to it than I had expected. There are indeed some tags that are whitespace aware and the spaces within words is an issue in ran into as well so indeed, needs more investigation.

I tried the master branch on a project today and the fix seems to work for strings but when it does not when I use a Date or mock instead of the string. Not sure why yet...

Thanks again!

kucaahbe commented 12 years ago

kinda fixed and released to 0.2.2, if you still have issues with mocks, please open issue with more detailed description, for now I'm closing this issue, have a nice day!

kucaahbe commented 11 years ago

Functionality introduced here was removed as kinda of edgecase in order to not confuse people, see #16. From now special magic about whitespace, and let programmer decide how templates should be generated and tested. Sorry for possible inconvenience.