Skip to content

Instantly share code, notes, and snippets.

@rebdev
Created June 1, 2012 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebdev/2850037 to your computer and use it in GitHub Desktop.
Save rebdev/2850037 to your computer and use it in GitHub Desktop.
NB: The "shape" attribute test is failing. Can't work out why, would love to find out the cause of the problem.
# Assignment: Fill in this Table class so the tests pass!
# Exercise Details:
#- Make a Table object class
#- Define at least 5 properties of a Table (legs, material, items_on_it, etc…)
#- Define an initialize method that sets one or more of the attributes when you call Table.new
#- Define a pretty_print method that prints out some useful information about your table
#- Email us a link to your code in a Gist
# Extra Credit:
#- Make tests for your Table class
# - Try the Exercises in the Gist for the Book class
class Table
attr_accessor :color, :legs, :seats, :material, :character, :items_on_it, :shape
def initialize(initial_hash=nil)
initial_hash.each do |k,v|
instance_variable_set("@#{k}", v)
end if initial_hash
end
def pretty_print
"The table is #{@table.shape} and #{@table.color} with #{@table.legs} legs,
and can seat #{@table.seats}."
end
end
# Unit tests
require 'minitest/autorun'
class TestTable < MiniTest::Unit::TestCase
def setup
@table = Table.new(shape: "square",
color: "brown",
seats: 4,
material: "wood",
character: "rustic",
legs: 4,
items_on_it: { placemats: 4, glasses: 4, vase: 1 })
end
def test_that_table_has_color
assert_equal "brown", @table.color
end
def test_that_table_has_material
assert_equal "wood", @table.material
end
def test_that_table_has_character
assert_equal "rustic", @table.character
end
# Won't pass. I'm stumped. What's wrong?
def test_that_table_has_shape
assert_equal "square", @table.shape
end
def test_that_table_has_seats
assert_equal 4, @table.seats
end
def test_that_table_has_legs
assert_equal 4, @table.legs
end
def test_that_table_has_items_on_it
items_on_it_hash = { placemats: 4, glasses: 4, vase: 1 }
assert_equal items_on_it_hash, @table.items_on_it
end
def test_that_table_can_print_pretty
assert_equal "The table is #{@table.shape} and #{@table.color} with #{@table.legs} legs,
and can seat #{@table.seats}.", @table.pretty_print
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment