From c1041c6c6743f348c613c0c3f17bf27041a06bec Mon Sep 17 00:00:00 2001 From: Ian White Date: Thu, 26 Aug 2010 10:28:11 +0100 Subject: [PATCH] Added equality for Pickle::Ref, to enable more robust specs in the Api --- lib/pickle/ref.rb | 4 ++++ spec/pickle/ref_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/pickle/ref.rb b/lib/pickle/ref.rb index 3184f305..7835ec24 100644 --- a/lib/pickle/ref.rb +++ b/lib/pickle/ref.rb @@ -32,6 +32,10 @@ def inspect "#" end + def ==(other) + [factory, index, label] == [other.factory, other.index, other.label] + end + protected def validate! raise InvalidPickleRefError, "#{inspect} requires a factory or label" if factory.blank? && label.blank? diff --git a/spec/pickle/ref_spec.rb b/spec/pickle/ref_spec.rb index 7e74d818..eaa31f60 100644 --- a/spec/pickle/ref_spec.rb +++ b/spec/pickle/ref_spec.rb @@ -13,6 +13,30 @@ end end + describe "equality: two pickle refs are equal when their index, factory, label are equal. ie:" do + [ + ['the user: "fred"', {:label => "fred", :factory => 'user'}], + ['user', 'another user'], + ['the 2nd user', {:index => 1, :factory => 'user'}] + ].each do |pair| + describe Pickle::Ref.new(pair.first).inspect do + subject { Pickle::Ref.new(pair.first) } + it { should == Pickle::Ref.new(pair.last) } + end + end + + [ + ['the user: "geoff"', {:label => "fred", :factory => 'user'}], + ['user', '1st user'], + ['the 2nd user', {:index => 1, :factory => 'project'}] + ].each do |pair| + describe Pickle::Ref.new(pair.first).inspect do + subject { Pickle::Ref.new(pair.first) } + it { should_not == Pickle::Ref.new(pair.last) } + end + end + end + describe "(factory) " do shared_examples_for 'pickle ref with :factory => "colour"' do its(:index) { should be_nil }