From a29ceffc9476c99ff02f0617d2e38627c526bac2 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Tue, 11 Mar 2008 22:42:47 -0700 Subject: implemented hashing macro; implemented custom matcher testing this macro --- .../unit/primitives/attribute_spec.rb | 7 ++++++ spec/active_relation/unit/relations/table_spec.rb | 5 ++--- spec/matchers/hash_the_same_as.rb | 26 ++++++++++++++++++++++ spec/spec_helper.rb | 2 +- 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 spec/matchers/hash_the_same_as.rb (limited to 'spec') diff --git a/spec/active_relation/unit/primitives/attribute_spec.rb b/spec/active_relation/unit/primitives/attribute_spec.rb index 95c972d814..d424bf4dff 100644 --- a/spec/active_relation/unit/primitives/attribute_spec.rb +++ b/spec/active_relation/unit/primitives/attribute_spec.rb @@ -68,6 +68,13 @@ module ActiveRelation Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name, :ancestor => Attribute.new(@relation, :name)) end end + + describe 'hashing' do + it "implements hash equality" do + Attribute.new(@relation, 'name').should hash_the_same_as(Attribute.new(@relation, 'name')) + Attribute.new(@relation, 'name').should_not hash_the_same_as(Attribute.new(@relation, 'id')) + end + end end describe '#to_sql' do diff --git a/spec/active_relation/unit/relations/table_spec.rb b/spec/active_relation/unit/relations/table_spec.rb index 95ad7133db..41ed2dc404 100644 --- a/spec/active_relation/unit/relations/table_spec.rb +++ b/spec/active_relation/unit/relations/table_spec.rb @@ -75,9 +75,8 @@ module ActiveRelation describe 'hashing' do it "implements hash equality" do - hash = {} - hash[Table.new(:users)] = 1 - hash[Table.new(:users)].should == 1 + Table.new(:users).should hash_the_same_as(Table.new(:users)) + Table.new(:users).should_not hash_the_same_as(Table.new(:photos)) end end diff --git a/spec/matchers/hash_the_same_as.rb b/spec/matchers/hash_the_same_as.rb new file mode 100644 index 0000000000..86e98f31a3 --- /dev/null +++ b/spec/matchers/hash_the_same_as.rb @@ -0,0 +1,26 @@ +module HashTheSameAsMatcher + class HashTheSameAs + def initialize(expected) + @expected = expected + end + + def matches?(target) + @target = target + hash = {} + hash[@expected] = :some_arbitrary_value + hash[@target] == :some_arbitrary_value + end + + def failure_message + "expected #{@target} to hash the same as #{@expected}; they must be `eql?` and have the same `#hash` value" + end + + def negative_failure_message + "expected #{@target} to hash differently than #{@expected}; they must not be `eql?` or have a differing `#hash` values" + end + end + + def hash_the_same_as(expected) + HashTheSameAs.new(expected) + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cd9c8e96cb..6d00064e81 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -26,7 +26,7 @@ class Hash end Spec::Runner.configure do |config| - config.include(BeLikeMatcher) + config.include(BeLikeMatcher, HashTheSameAsMatcher) config.mock_with :rr config.before do ActiveRelation::Table.engine = ActiveRecord::Base.connection -- cgit v1.2.3