Skip to content

Instantly share code, notes, and snippets.

@sjwats
Last active December 30, 2015 18:19
Show Gist options
  • Select an option

  • Save sjwats/7866429 to your computer and use it in GitHub Desktop.

Select an option

Save sjwats/7866429 to your computer and use it in GitHub Desktop.
Campaign Bravo Checkpoint - TDD - Your engineering team is working on a product that teaches students Geometry. You are tasked with coming up with a library of geometric shape classes that know how to calculate their area and perimeter. You must use TDD to drive your development.
class Circle
attr_reader :radius
def initialize(radius)
if radius <= 0
raise "The supplied value #{radius} cannot be used. Supply a positive Float or Integer."
return nil
end
@radius = radius.abs
end
def diameter
@radius * 2
end
def circumference
2 * @radius * Math::PI
end
def area
Math::PI * (@radius**2)
end
end
require 'rspec'
require_relative 'circle'
describe Circle do
it 'should have a positive number as a radius' do
circle = Circle.new(5.0)
expect(circle.radius.to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should respond to a diameter method' do
circle = Circle.new(5.0)
expect(circle).to respond_to (:diameter)
end
it 'should calculate the diameter using the radius' do
circle = Circle.new(10.0)
expect(circle.diameter).to eql(20.0)
end
it 'should respond to a circumference method' do
circle = Circle.new(5.0)
expect(circle).to respond_to (:circumference)
end
it 'should calculate the circumference' do
circle = Circle.new(5.0)
expect(circle.circumference).to eql(31.41592653589793)
end
it 'should respond to an area method' do
circle = Circle.new(5.0)
expect(circle).to respond_to (:area)
end
it 'should calculate the area' do
circle = Circle.new(5.0)
expect(circle.area).to eql(78.53981633974483)
end
end
class Rectangle
attr_reader :length, :width
def initialize(length, width)
if length <= width
raise "The supplied value #{@length} should be larger than the width value"
return nil
end
@length = length.abs
@width = width.abs
end
def perimeter
(2 * @length) + (2 * @width)
end
def area
@length * @width
end
end
require 'rspec'
require_relative 'rectangle'
describe Rectangle do
it 'should respond to a length method' do
rectangle = Rectangle.new(10.0, 5.0)
expect(rectangle).to respond_to (:length)
end
it 'should respond to a width method' do
rectangle = Rectangle.new(10.0, 5.0)
expect(rectangle).to respond_to (:width)
end
it 'should take a number as a length' do
rectangle = Rectangle.new(10.0, 5.0)
expect(rectangle.length.to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should take a positive number as a length' do
rectangle = Rectangle.new(10.0, 5.0)
expect(rectangle.length).to be > 0
end
it 'should take a number as a width' do
rectangle = Rectangle.new(10.0, 5.0)
expect(rectangle.width.to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should take a positive number as a length' do
rectangle = Rectangle.new(10.0, 5.0)
expect(rectangle.width).to be > 0
end
it 'should respond to a perimeter method' do
rectangle = Rectangle.new(10.0, 5.0)
expect(rectangle).to respond_to (:perimeter)
end
it 'should calculate the perimeter' do
rectangle = Rectangle.new(10.0, 5.0)
expect(rectangle.perimeter).to eql(30.0)
end
it 'should respond to an area method' do
rectangle = Rectangle.new(10.0, 5.0)
expect(rectangle).to respond_to (:area)
end
it 'should calculate the area' do
rectangle = Rectangle.new(10.0, 5.0)
expect(rectangle.area).to eql(50.0)
end
it 'should have an area greater than zero' do
rectangle = Rectangle.new(10.0, 5.0)
expect(rectangle.area).to be > 0
end
end
class Square
attr_reader :side_length
def initialize(side_length)
if side_length <= 0
raise "The supplied value #{side_length} cannot be used. Supply a positive Float or Integer."
return nil
end
@side_length = side_length.abs
end
def perimeter
@side_length * 4
end
def area
@side_length * @side_length
end
end
require 'rspec'
require_relative 'square'
describe Square do
it 'it should take the length of a side as an argument' do
square = Square.new(10.0)
expect(square).to respond_to (:side_length)
end
it 'should have a side length that is only a float or integer' do
square = Square.new(123.456)
expect(square.side_length.to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should have a positive side_length value' do
square = Square.new(5.0)
expect(square.side_length).to be > 0
end
it 'calculates the perimeter' do
square = Square.new(10.0)
expect(square.perimeter).to eq(40.0)
end
it 'calculates the area' do
square = Square.new(10.0)
expect(square.area).to eq(100.0)
end
end
class Trapezoid
attr_reader :short_base, :long_base, :side1, :side2, :height
def initialize(short_base, long_base, side1, side2, height)
@short_base = short_base.abs
@long_base = long_base.abs
@side1 = side1.abs
@side2 = side2.abs
@height = height.abs
end
def perimeter
@short_base + @long_base + @side1 + @side2
end
def area
((@short_base + @long_base) * @height) / 2
end
end
require 'rspec'
require_relative 'trapezoid'
describe Trapezoid do
it 'should respond to a short_base method' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid).to respond_to(:short_base)
end
it 'should respond to a long_base method' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid).to respond_to (:long_base)
end
it 'should have a longer long_base than short_base' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid.long_base).to be > trapezoid.short_base
end
it 'should respond to a side1 method' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid).to respond_to (:side1)
end
it 'should respond to a side2 method' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid).to respond_to (:side2)
end
it 'should respond to a height method' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid).to respond_to (:height)
end
it 'should take a number as short_base' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid.short_base.to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should take a number as long_base' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid.long_base.to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should take a number as side1' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid.side1.to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should take a number as side2' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid.side2.to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should take a number as height' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid.height.to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should respond to a perimeter method' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid).to respond_to (:perimeter)
end
it 'should calculate the perimeter' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid.perimeter).to eql(20.5)
end
it 'should respond to an area method' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid).to respond_to (:area)
end
it 'should calculate the area' do
trapezoid = Trapezoid.new(5.0, 8.0, 4.0, 3.5, 3.0)
expect(trapezoid.area).to eql(19.5)
end
end
class Triangle
def self.side1(side1)
side1.abs
end
def self.side2(side2)
side2.abs
end
def self.side3(side3)
side3.abs
end
def self.perimeter(side1, side2, side3)
(side1.abs + side2.abs + side3.abs)
end
#Used Heron's formula for the area calculation
def self.area(side1, side2, side3)
s = (side1.abs + side2.abs + side3.abs) / 2
num = s * (s-side1.abs)*(s-side2.abs)*(s-side3.abs)
Math::sqrt(num)
end
end
require 'rspec'
require_relative 'triangle'
describe Triangle do
it 'should respond to a side1 method' do
expect(Triangle).to respond_to (:side1)
end
it 'should respond to a side2 method' do
expect(Triangle).to respond_to (:side2)
end
it 'should respond to a side3 method' do
expect(Triangle).to respond_to (:side2)
end
it 'should only take a float or integer as side1' do
expect(Triangle.side1(5.0).to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should only take a float or integer as side2' do
expect(Triangle.side2(10.0).to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should only take a float or integer as side3' do
expect(Triangle.side3(12.0).to_s).to match(/\A\d+\.?\d*\z/)
end
it 'should have side values greater than zero' do
expect(Triangle.area(5.0, 10.0, 12.0)).to be > 0
end
it 'should respond to a perimeter method' do
expect(Triangle).to respond_to (:perimeter)
end
it 'should calculate the perimeter' do
expect(Triangle.perimeter(5.0, 10.0, 12.0)).to eql(27.0)
end
it 'should respond to an area method' do
expect(Triangle).to respond_to (:area)
end
it 'should calculate the area' do
expect(Triangle.area(5.0, 10.0, 12.0)).to eql(24.544602257930357)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment