From ce1ba7b763880671510d469b2810b3fc4c0e907e Mon Sep 17 00:00:00 2001 From: mo k Date: Sat, 12 Nov 2011 09:21:25 -0700 Subject: update equality for Book. --- lib/book.rb | 7 +++++-- test/book_specs.rb | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/book.rb b/lib/book.rb index a883e49..60557e5 100644 --- a/lib/book.rb +++ b/lib/book.rb @@ -1,12 +1,15 @@ class Book - attr_reader :title + attr_reader :title, :author, :publisher, :year_published def initialize(title, author, publisher, year_published) @title=title + @author=author + @publisher=publisher + @year_published = year_published end def is_titled?(title) @title == title end def ==(other) - other.kind_of?( Book ) && @title == other.title + other.kind_of?( Book ) && @title == other.title && @author == other.author && @publisher == other.publisher && @year_published == other.year_published end end diff --git a/test/book_specs.rb b/test/book_specs.rb index 3129d76..805aef6 100644 --- a/test/book_specs.rb +++ b/test/book_specs.rb @@ -21,6 +21,24 @@ class TestBook < Test::Unit::TestCase end end +describe Book do + describe "when testing equality" do + def setup + @publisher = Publisher.new("Pear press"); + @book = Book.new("Brain Rules for Baby", "John Medina", @publisher, 2010) + end + it "should be equal when it's the same instance" do + assert_equal(@book, @book) + end + it "should be equal when they are the same book" do + assert_equal(@book, Book.new("Brain Rules for Baby", "John Medina", @publisher, 2010)) + end + it "should not be equal when they are different books" do + refute_equal(@book, Book.new("Brain Rules for Baby", "some other guy", @publisher, 2009)) + end + end +end + class Game attr_reader :title; def initialize(title, author, publisher, year) -- cgit v1.2.3