Skip to content

Conversation

@rehevkor5
Copy link

  • The Rspec matcher example now follows recommended approach for 1.2.4 and up. The BaseMatcher class is an internal class that RSpec advises people should not use.

- The Rspec matcher example now follows recommended approach for 1.2.4 and up. The BaseMatcher class is an internal class that RSpec advises people should not use.
@locochris
Copy link

Thanks for the update!
(BTW the repo has now moved to http://github.com/diff-matcher/diff_matcher)

@rehevkor5
Copy link
Author

You're welcome, and thank you for making open source stuff!

@locochris
Copy link

right back at ya!

hey, btw, would you be able to send me a real life example of how you use
it (in the wild)?

ie. the diff matcher pattern that you're using and an example of the data
you are matching against it.

(when you write a tool you never know how people will end up using it - and
how it might benefit from getting
stretched in the direction they try to stretch it)

On Fri, Dec 7, 2012 at 3:02 PM, Shannon Carey [email protected]:

You're welcome, and thank you for making open source stuff!


Reply to this email directly or view it on GitHubhttps://github.com//pull/3#issuecomment-11118397.

@rehevkor5
Copy link
Author

So far my use cases are very simple.

Here's the data matched against:

{
  "_links"=>{
    "self"=>{
      :href=>"/articles/1", :name=>nil, :title=>nil, :templated=>nil
    },
    "images"=>[
      {:href=>"/images/a", :name=>0, :title=>nil, :templated=>nil},
      {:href=>"/images/b", :name=>1, :title=>nil, :templated=>nil}
    ], 
   "business"=>{:href=>"/businesses/link", :name=>nil, :title=>nil, :templated=>nil}
  }
}

And here's the relevant part of the Rspec code:

describe ArticleDecorator do
  let(:article) {
    article = double('Article', hermes_image_ids: ['a','b'], to_param: '1', business: 'link')
  }

  let(:decorated) { ArticleDecorator.decorate(article) }

  describe '#as_json' do
    let(:expected) do
      {
        "_links" => {
          'self' => {:href=>"/articles/1"},
          'images' => [
                      {:href=>"/images/a", :name=> 0 },
                      {:href=>"/images/b", :name=> 1 }
                  ]
        }
      }
    end

    before do
      article.should_receive(:as_json).with(any_args).and_return({})
    end

    subject { decorated.as_json }

    it { should be_matching(expected).with_options(ignore_additional: true) }
  end
end

@locochris
Copy link

We like to make the matchers as explicit as possible.

You'd be suprised how many times subtle little changes crept into our JSON,
but got picked up by the
detail of the matchers.

eg. Instead of defining a path to be a String actually writie a proper
regex. Then when
your decorator gets "/articles" mixed up with "/images" you find the error.
Or use Fixnum for name so that 1 gets matched instead of "1".

I had a crack at writing a more explicit matcher for you....

How's this look?

DiffMatcher::Matcher.new(
{
'_links' => {
'self' => link_matcher(%r{^/articles/\d+$}),
'images' => DiffMatcher::AllMatcher.new(
link_matcher(%r{^/images/(a|b)$}),
size: 2
),
'business' => link_matcher(%r{^/businesses/[a-z]+$}),
}
},
optional_keys: ['business']
)

def link_matcher(path)
DiffMatcher::Matcher.new(
{
href: path,
name: DiffMatcher::Matcher[Fixnum, nil],
title: nil,
templated: nil
}
)
end

On Sat, Dec 8, 2012 at 3:09 AM, Shannon Carey [email protected]:

So far my use cases are very simple.

Here's the data matched against:

{
"_links"=>{
"self"=>{
:href=>"/articles/1", :name=>nil, :title=>nil, :templated=>nil
},
"images"=>[
{:href=>"/images/a", :name=>0, :title=>nil, :templated=>nil},
{:href=>"/images/b", :name=>1, :title=>nil, :templated=>nil}
],
"business"=>{:href=>"/businesses/link", :name=>nil, :title=>nil, :templated=>nil}
}
}

And here's the relevant part of the Rspec code:

describe ArticleDecorator do
let(:article) {
article = double('Article', hermes_image_ids: ['a','b'], to_param: '1', business: 'link')
}

let(:decorated) { ArticleDecorator.decorate(article) }

describe '#as_json' do
let(:expected) do
{
"_links" => {
'self' => {:href=>"/articles/1"},
'images' => [
{:href=>"/images/a", :name=> 0 },
{:href=>"/images/b", :name=> 1 }
]
}
}
end

before do
  article.should_receive(:as_json).with(any_args).and_return({})
end

subject { decorated.as_json }

it { should be_matching(expected).with_options(ignore_additional: true) }

end
end


Reply to this email directly or view it on GitHubhttps://github.com//pull/3#issuecomment-11135403.

@locochris
Copy link

ie.

describe ArticleDecorator do
let(:article) {
article = double('Article', hermes_image_ids: ['a','b'], to_param:
'1', business: 'link')
}

let(:decorated) { ArticleDecorator.decorate(article) }

describe '#as_json' do
let(:expected) do
DiffMatcher::Matcher.new(
{
'_links' => {
'self' => link_matcher(%r{^/articles/\d+$}),
'images' => DiffMatcher::AllMatcher.new(
link_matcher(%r{^/images/(a|b)$}),
size: 2
),
'business' => link_matcher(%r{^/businesses/[a-z]+$}),
}
},
optional_keys: ['business']
)
end

before do
  article.should_receive(:as_json).with(any_args).and_return({})
end

subject { decorated.as_json }

it { should be_matching expected }

end
end

private

def link_matcher(path)
DiffMatcher::Matcher.new(
{
href: path,
name: DiffMatcher::Matcher[Fixnum, nil],
title: nil,
templated: nil
}
)
end

On Sat, Dec 8, 2012 at 8:30 AM, Chris Ottrey [email protected] wrote:

We like to make the matchers as explicit as possible.

You'd be suprised how many times subtle little changes crept into our
JSON, but got picked up by the
detail of the matchers.

eg. Instead of defining a path to be a String actually writie a proper
regex. Then when
your decorator gets "/articles" mixed up with "/images" you find the error.
Or use Fixnum for name so that 1 gets matched instead of "1".

I had a crack at writing a more explicit matcher for you....

How's this look?

DiffMatcher::Matcher.new(
{
'_links' => {
'self' => link_matcher(%r{^/articles/\d+$}),
'images' => DiffMatcher::AllMatcher.new(
link_matcher(%r{^/images/(a|b)$}),
size: 2
),
'business' => link_matcher(%r{^/businesses/[a-z]+$}),
}
},
optional_keys: ['business']
)

def link_matcher(path)
DiffMatcher::Matcher.new(
{
href: path,
name: DiffMatcher::Matcher[Fixnum, nil],
title: nil,
templated: nil
}
)
end

On Sat, Dec 8, 2012 at 3:09 AM, Shannon Carey [email protected]:

So far my use cases are very simple.

Here's the data matched against:

{
"_links"=>{
"self"=>{
:href=>"/articles/1", :name=>nil, :title=>nil, :templated=>nil
},
"images"=>[
{:href=>"/images/a", :name=>0, :title=>nil, :templated=>nil},
{:href=>"/images/b", :name=>1, :title=>nil, :templated=>nil}
],
"business"=>{:href=>"/businesses/link", :name=>nil, :title=>nil, :templated=>nil}
}
}

And here's the relevant part of the Rspec code:

describe ArticleDecorator do
let(:article) {
article = double('Article', hermes_image_ids: ['a','b'], to_param: '1', business: 'link')
}

let(:decorated) { ArticleDecorator.decorate(article) }

describe '#as_json' do
let(:expected) do
{
"_links" => {
'self' => {:href=>"/articles/1"},
'images' => [
{:href=>"/images/a", :name=> 0 },
{:href=>"/images/b", :name=> 1 }
]
}
}
end

before do
  article.should_receive(:as_json).with(any_args).and_return({})
end

subject { decorated.as_json }

it { should be_matching(expected).with_options(ignore_additional: true) }

end
end


Reply to this email directly or view it on GitHubhttps://github.com//pull/3#issuecomment-11135403.

@rehevkor5
Copy link
Author

Thanks! I'll try to move that direction.

@sheerun
Copy link

sheerun commented Jan 14, 2014

+1 on this one. I had the same issue.

@locochris
Copy link

@sheerun - fyi diff_matcher has now moved to http://github.com/diff-matcher/diff_matcher
If you find the same issue with the latest version ie. v2.5.0, can you please open an issue there.

@sheerun
Copy link

sheerun commented Jan 14, 2014

I understand author abandoned project and don't want to change readme or transfer project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants