mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
implemented hashing macro; implemented custom matcher testing this macro
This commit is contained in:
parent
98527c8f7d
commit
a29ceffc94
9 changed files with 47 additions and 14 deletions
BIN
lib/active_relation/.DS_Store
vendored
BIN
lib/active_relation/.DS_Store
vendored
Binary file not shown.
|
@ -1,4 +1,12 @@
|
|||
class Object
|
||||
class Object
|
||||
def self.hash_on(delegatee)
|
||||
def eql?(other)
|
||||
self == other
|
||||
end
|
||||
|
||||
delegate :hash, :to => delegatee
|
||||
end
|
||||
|
||||
def bind(relation)
|
||||
ActiveRelation::Scalar.new(self, relation)
|
||||
end
|
||||
|
|
|
@ -48,10 +48,7 @@ module ActiveRelation
|
|||
|
||||
module Congruence
|
||||
def self.included(klass)
|
||||
klass.class_eval do
|
||||
alias_method :eql?, :==
|
||||
delegate :hash, :to => :name
|
||||
end
|
||||
klass.hash_on :name
|
||||
end
|
||||
|
||||
def history
|
||||
|
|
|
@ -102,10 +102,6 @@ module ActiveRelation
|
|||
false
|
||||
end
|
||||
|
||||
def eql?(other)
|
||||
self == other
|
||||
end
|
||||
|
||||
def to_sql(strategy = Sql::Relation.new(engine))
|
||||
strategy.select [
|
||||
"SELECT #{attributes.collect{ |a| a.to_sql(Sql::Projection.new(engine)) }.join(', ')}",
|
||||
|
|
|
@ -3,7 +3,7 @@ module ActiveRelation
|
|||
cattr_accessor :engine
|
||||
attr_reader :name, :engine
|
||||
|
||||
delegate :hash, :to => :name
|
||||
hash_on :name
|
||||
|
||||
def initialize(name, engine = Table.engine)
|
||||
@name, @engine = name.to_s, engine
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
26
spec/matchers/hash_the_same_as.rb
Normal file
26
spec/matchers/hash_the_same_as.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue