Ervin Weber
2013-Apr-09 20:50 UTC
[rspec-users] Different description in custom RSpec::Matcher depending on output formater
Hello is it possible to make something like following: RSpec::Matchers.define :be_json do |expected| match do |actual| JSON.parse(actual) == expected end description do if html? # to html formatter output this "return JSON:\n#{JSON.pretty_generate(expected)}" else # for other formats condensed output "return JSON:\n#{expected}" end end end I plan to use this running two formatters at same time -f doc and -f html at same time to produce pretty HTML file, and relatively condensed doc in console. Big thanks, forward, Ervin.
David Chelimsky
2013-Apr-10 07:14 UTC
[rspec-users] Different description in custom RSpec::Matcher depending on output formater
On Tue, Apr 9, 2013 at 3:50 PM, Ervin Weber <webervin at gmail.com> wrote:> Hello is it possible to make something like following: > > RSpec::Matchers.define :be_json do |expected| > match do |actual| > JSON.parse(actual) == expected > end > > description do > if html? # to html formatter output this > "return JSON:\n#{JSON.pretty_generate(expected)}" > else # for other formats condensed output > "return JSON:\n#{expected}" > end > end > end > > I plan to use this running two formatters at same time -f doc and -f > html at same time to produce pretty HTML file, and relatively > condensed doc in console.I don''t think there''s any good way to do this. There is no concept of "the current formatter" and I don''t think this one case would be sufficient to introduce one. Custom formatters won''t work because the formatters don''t have access to the matcher to get its description (that happens in a different part of the system). Also, even if there were such a mechanism, it would only work with the one liner (e.g. it { should be_json(expected_json) }) form - another reason why it wouldn''t be general enough to add to RSpec IMO. If you have different ideas about how it might work, feel free to submit a feature request to https://github.com/rspec/rspec-core/issues. Cheers, David -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20130410/8de14405/attachment.html>