Skip to content

Instantly share code, notes, and snippets.

@sdanko11
Created December 9, 2013 04:34
Show Gist options
  • Select an option

  • Save sdanko11/7867444 to your computer and use it in GitHub Desktop.

Select an option

Save sdanko11/7867444 to your computer and use it in GitHub Desktop.
TDD Shapes
class Circle
def initialize(radius)
@radius = radius
end
def area
(Math::PI * (@radius*@radius)).round(2)
end
def perimeter
(2 * Math::PI * @radius).round(2)
end
end
class Trapazoid
def initialize(bottom_base, top_base, height)
@bottom_base = bottom_base
@top_base = top_base
@height = height
end
def area
@height * @bottom_base * @top_base
end
def perimeter
@height * (@bottom_base + @top_base) / 2
end
end
require_relative 'square'
require_relative 'triangle'
require_relative 'rectangle'
require_relative 'circle'
require_relative 'trapazoid'
require 'rspec'
describe Square do
let(:square) { Square.new(5) }
it "should calculate the area of the square" do
expect(square.calculate_area).to eql(25)
end
it "should calulate the perimeter of the square" do
expect(square.perimeter).to eql(20)
end
end
describe Triangle do
let(:triangle) { Triangle.new(10, 5, 5, 5) }
it "should calculate the perimeter of a triange" do
expect(triangle.perimeter).to eql(20)
end
it "should calculate the area of the triange" do
expect(Triangle.new(2, 5, 1,3).perimeter).to eql(6)
end
it "should calculate the area of the triangle" do
expect(triangle.area).to eql(25)
end
it "calculate the area of a triangel" do
expect(Triangle.new(5).area).to eql(10)
end
it "should calculate area of the triangle" do
expect(Triangle.new(5, 3, 8, 15).perimeter).to eql(28)
end
end
describe Rectangle do
let(:rectangle) { Rectangle.new(5,10)}
it "should calculate the perimeter of the rectangle" do
expect(rectangle.perimeter).to eql(30)
end
it "should calculate the area of the triangle" do
expect(rectangle.area).to eql(50)
end
end
describe Circle do
let(:circle) { Circle.new(5) }
it "should calculate the area of a circle" do
expect(circle.area).to eql(78.54)
end
it "should calculate the perimeter of a circle" do
expect(circle.perimeter).to eql(31.42)
end
end
describe Trapazoid do
let(:trapazoid) { Trapazoid.new(10,20,40) }
it "should calculate the area of the trapazoid" do
expect(trapazoid.area).to eql(8000)
end
it "should calculate the perimeter of the trapazoid" do
expect(trapazoid.perimeter).to eql(600)
end
end
class Square
def initialize(width)
@width = width
end
def calculate_area
@width * @width
end
def perimeter
@width * 4
end
end
class Triangle
def initialize(base, height = base-1, side1 = base, side2 = base)
@base = base
@side1 = side1
@side2 = side2
@height = height
end
def perimeter
@base + @side1 + @side2
end
def area
@base * @height / 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment