Last active
December 13, 2016 12:30
-
-
Save esteedqueen/5bcba2b2be708c3cfe23fb8db5125c90 to your computer and use it in GitHub Desktop.
Rspec TDD intro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tdd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rspec/autorun" | |
# calc = Calculator.new | |
# calc.add(5, 10) # => 15 | |
class Calculator | |
def add(a,b) | |
a + b | |
end | |
# def factorial(n) | |
# if (n <= 1) | |
# 1 | |
# else | |
# n * factorial(n - 1) | |
# end | |
# end | |
# refactored to handle large numbers and fix recursive problems | |
def factorial(n) | |
if (n <= 1) | |
1 | |
else | |
(1..n).reduce(:*) | |
end | |
end | |
end | |
describe Calculator do | |
describe "#add" do | |
it "returns the sum of two arguments" do | |
calc = Calculator.new | |
expect(calc.add(5, 10)).to eq(15) | |
end | |
end | |
describe "#factorial" do | |
it "returns 1 when given 0" do | |
calc = Calculator.new | |
expect(calc.factorial(0)).to eq(1) | |
end | |
it "returns 120 when given 5" do | |
calc = Calculator.new | |
expect(calc.factorial(5)).to eq(120) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use TDD principles to build out name functionality for a Person. | |
# Here are the requirements: | |
# - Add a method to return the full name as a string. A full name includes | |
# first, middle, and last name. If the middle name is missing, there shouldn't | |
# have extra spaces. | |
# - Add a method to return a full name with a middle initial. If the middle name | |
# is missing, there shouldn't be extra spaces or a period. | |
# - Add a method to return all initials. If the middle name is missing, the | |
# initials should only have two characters. | |
# | |
# We've already sketched out the spec descriptions for the #full_name. Try | |
# building the specs for that method, watch them fail, then write the code to | |
# make them pass. Then move on to the other two methods, but this time you'll | |
# create the descriptions to match the requirements above. | |
class Person | |
def initialize(first_name:, middle_name: nil, last_name:) | |
@first_name = first_name | |
@middle_name = middle_name | |
@last_name = last_name | |
end | |
# implement your behavior here | |
# def full_name | |
# if @middle_name.nil? | |
# "#{@first_name}" + " " + "#{@last_name}" | |
# else | |
# "#{@first_name}" + " " + "#{@middle_name}" + " " + "#{@last_name}" | |
# end | |
# end | |
# def full_name_with_middle_initial | |
# if @middle_name.nil? | |
# "#{@first_name}" + " " + "#{@last_name}" | |
# else | |
# "#{@first_name}" + " " + "#{initial(@middle_name)}" + ". " + "#{@last_name}" | |
# end | |
# end | |
# def initials | |
# if @middle_name.nil? | |
# "#{initial(@first_name)}" + ". " "#{initial(@last_name)}" | |
# else | |
# "#{initial(@first_name)}" + ". " + "#{initial(@middle_name)}" + ". " "#{initial(@last_name)}" | |
# end | |
# end | |
# refactor methods | |
def full_name | |
squish("#{@first_name} #{@middle_name if @middle_name} #{@last_name}") | |
end | |
def full_name_with_middle_initial | |
squish("#{@first_name} #{initial(@middle_name) if @middle_name} #{@last_name}") | |
end | |
def initials | |
squish("#{initial(@first_name)} #{initial(@middle_name) if @middle_name} #{initial(@last_name)}") | |
end | |
private | |
def initial(s) | |
s[0] | |
# s.chars.first | |
end | |
def squish(s) | |
s.strip.gsub(/\s+/, " ") | |
end | |
end | |
RSpec.describe Person do | |
describe "#full_name" do | |
it "concatenates first name, middle name, and last name with spaces" do | |
person = Person.new(first_name: "Esther", middle_name: "Aina", last_name: "Olatunde") | |
expect(person.full_name).to eq("Esther Aina Olatunde") | |
end | |
it "does not add extra spaces if middle name is missing" do | |
person = Person.new(first_name: "Esther", last_name: "Olatunde") | |
expect(person.full_name).to eq("Esther Olatunde") | |
end | |
end | |
describe "#full_name_with_middle_initial" do | |
it "concatenates and return full name with a middle name initial" do | |
person = Person.new(first_name: "Esther", middle_name: "Aina", last_name: "Olatunde") | |
expect(person.full_name_with_middle_initial).to eq("Esther A Olatunde") | |
end | |
it "does not add extra spaces or a period if middle name is missing" do | |
person = Person.new(first_name: "Esther", last_name: "Olatunde") | |
expect(person.full_name_with_middle_initial).to eq("Esther Olatunde") | |
end | |
end | |
describe "#initials" do | |
it "concatenates the initials of the first_name, middle_name and last_name" do | |
person = Person.new(first_name: "Esther", middle_name: "Aina", last_name: "Olatunde") | |
expect(person.initials).to eq("E A O") | |
end | |
it "returns only two initials if middle_name is missing and does not add extra space or period" do | |
person = Person.new(first_name: "Esther", last_name: "Olatunde") | |
expect(person.initials).to eq("E O") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment