aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-03-11 22:42:47 -0700
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-03-11 22:42:47 -0700
commita29ceffc9476c99ff02f0617d2e38627c526bac2 (patch)
treefc9aa702ad9a2f506a4a51b71791798fb8a6ca4b /spec
parent98527c8f7dd64f086895c1576fb33e8b91071142 (diff)
downloadrails-a29ceffc9476c99ff02f0617d2e38627c526bac2.tar.gz
rails-a29ceffc9476c99ff02f0617d2e38627c526bac2.tar.bz2
rails-a29ceffc9476c99ff02f0617d2e38627c526bac2.zip
implemented hashing macro; implemented custom matcher testing this macro
Diffstat (limited to 'spec')
-rw-r--r--spec/active_relation/unit/primitives/attribute_spec.rb7
-rw-r--r--spec/active_relation/unit/relations/table_spec.rb5
-rw-r--r--spec/matchers/hash_the_same_as.rb26
-rw-r--r--spec/spec_helper.rb2
4 files changed, 36 insertions, 4 deletions
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