diff options
| author | mo k <m@mokhan.ca> | 2011-11-12 09:21:25 -0700 |
|---|---|---|
| committer | mo k <m@mokhan.ca> | 2011-11-12 09:21:25 -0700 |
| commit | ce1ba7b763880671510d469b2810b3fc4c0e907e (patch) | |
| tree | 7ea94c64be2c9c511fdf9f10e9f5d4949cf786dc | |
| parent | d4be9fb0a52e4620175989b8f1e7024a3ec27190 (diff) | |
update equality for Book.
| -rw-r--r-- | lib/book.rb | 7 | ||||
| -rw-r--r-- | 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) |
