Skip to content

Instantly share code, notes, and snippets.

@scottcreynolds
Created October 15, 2013 14:41
Show Gist options
  • Save scottcreynolds/6992629 to your computer and use it in GitHub Desktop.
Save scottcreynolds/6992629 to your computer and use it in GitHub Desktop.
RSpec.configure do |config|
# Use color in STDOUT
config.color_enabled = true
# Use color not only in STDOUT but also in pagers and files
config.tty = true
# Use the specified formatter
config.formatter = :progress # :progress, :html, :textmate
end
module SimplePolygon
def area
@base * @height
end
def perimeter
(@base * 2) + (@height * 2)
end
end
#implement a Rectangle class that includes functionality from SimplePolygon
class Rectangle
end
#implement Square. It should inherit from Rectangle
#and contain no code other than an initialize method
class Square
end
#Implement Triangle. It should include SimplePolygon
#and override area and perimeter as needed.
#hint: the result of SimplePolygon area is usable to
#find a triangle's area. The result of SimplePolygon
#perimeter is not
class Triangle
end
describe Rectangle do
it "knows its area" do
Rectangle.new(3,5).area.should eq(15)
end
it "knows its perimeter" do
Rectangle.new(3,5).perimeter.should eq(16)
end
end
describe Square do
it "is a Rectangle" do
Square.new(5).class.superclass.should eq(Rectangle)
end
it "knows its area" do
Square.new(5).area.should eq(25)
end
it "knows its perimeter" do
Square.new(5).perimeter.should eq(20)
end
end
describe Triangle do
it "knows its area" do
Triangle.new(4,4,4).area.should eq(8)
end
it "knows its perimeter" do
Triangle.new(4,4,4).perimeter.should eq(12)
end
context "utlizing SimplePolygon" do
before(:each) do
#monkeypatch SimplePolygon to control area
#you'd never do this in real life but it's
#the only way to make this test work
module SimplePolygon
def area
100
end
def perimeter
throw Exception
end
end
@triangle = Triangle.new(4,4,4)
end
it "utilizes the area method" do
@triangle.area.should eq(50)
end
it "completely overrides the perimeter method" do
@triangle.perimeter.should eq(12)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment