Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use dynamic classes in the equality tests
- Loading branch information
Showing
2 changed files
with
33 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,20 @@ | ||
require 'test_helper' | ||
|
||
class Person | ||
include MongoMapper::Document | ||
|
||
one :address | ||
end | ||
|
||
class Address | ||
include MongoMapper::Document | ||
|
||
belongs_to :person | ||
end | ||
|
||
class EqualityTest < Test::Unit::TestCase | ||
context "Case equality" do | ||
setup do | ||
@person = Doc() | ||
@address = Doc() | ||
|
||
@person.one :address, :class => @address, :foreign_key => :person_id | ||
@address.belongs_to :person, :class => @person | ||
end | ||
|
||
should "work with proxies" do | ||
person = Person.create | ||
address = Address.create(:person => person) | ||
Person.should === address.person | ||
Address.should === person.address | ||
person = @person.create! | ||
address = @address.create!(:person => person) | ||
@person.should === address.person | ||
@address.should === person.address | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,38 @@ | ||
require 'test_helper' | ||
|
||
class Equality | ||
include MongoMapper::Plugins::Equality | ||
end | ||
|
||
class Equality2 < Equality; end | ||
|
||
class Faker | ||
def initialize(faked) | ||
@faked = faked | ||
end | ||
|
||
def is_a?(klass) | ||
@faked.is_a? klass | ||
end | ||
end | ||
|
||
class EqualityTest < Test::Unit::TestCase | ||
context "Case equality" do | ||
setup do | ||
@klass = Class.new do | ||
include MongoMapper::Plugins::Equality | ||
end | ||
@subklass = Class.new(@klass) | ||
|
||
@faker = Class.new do | ||
def initialize(faked) | ||
@faked = faked | ||
end | ||
|
||
def is_a?(klass) | ||
@faked.is_a? klass | ||
end | ||
end | ||
end | ||
|
||
should "work with regular instance" do | ||
Equality.should === Equality.new | ||
@klass.should === @klass.new | ||
end | ||
|
||
should "work with instances of subclasses" do | ||
Equality.should === Equality2.new | ||
@klass.should === @subklass.new | ||
end | ||
|
||
should "work with a faker class" do | ||
Equality.should === Faker.new(Equality.new) | ||
@klass.should === @faker.new(@klass.new) | ||
end | ||
|
||
should "not work with other instances" do | ||
Equality.should_not === 1 | ||
@klass.should_not === 1 | ||
end | ||
end | ||
end |