Last active
April 21, 2018 11:56
-
-
Save hcarreras/9835663058ef025a14b46c8b2664bb82 to your computer and use it in GitHub Desktop.
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 | |
def full_name | |
[first_name, middle_name, last_name] | |
.compact | |
.join(" ") | |
end | |
def full_name_with_middle_initial | |
[first_name, initial_for(middle_name), last_name] | |
.compact | |
.join(" ") | |
end | |
def initials | |
[initial_for(first_name), initial_for(middle_name), initial_for(last_name)] | |
.compact | |
.join(" ") | |
end | |
private | |
attr_reader :first_name, :middle_name, :last_name | |
def initial_for(name) | |
return unless name | |
name | |
.split(" ") | |
.map{|last_name_word| "#{last_name_word[0]}."} | |
.join(" ") | |
end | |
end | |
RSpec.describe Person do | |
describe "#full_name" do | |
it "concatenates first name, middle name, and last name with spaces" do | |
person = described_class.new(first_name: "Hari", middle_name: "Lal", last_name: "Carreras Pérez") | |
expect(person.full_name).to eq("Hari Lal Carreras Pérez") | |
end | |
it "does not add extra spaces if middle name is missing" do | |
person = described_class.new(first_name: "Hari", last_name: "Carreras Pérez") | |
expect(person.full_name).to eq("Hari Carreras Pérez") | |
end | |
end | |
describe "#full_name_with_middle_initial" do | |
it "concatenates first name, initial of middle name, and last name with spaces" do | |
person = described_class.new(first_name: "Hari", middle_name: "Lal", last_name: "Carreras Pérez") | |
expect(person.full_name_with_middle_initial).to eq("Hari L. Carreras Pérez") | |
end | |
it "does not add extra spaces if middle name is missing" do | |
person = described_class.new(first_name: "Hari", last_name: "Carreras Pérez") | |
expect(person.full_name_with_middle_initial).to eq("Hari Carreras Pérez") | |
end | |
end | |
describe "#initials" do | |
it "concatenates initial of first name, initial of middle name, and initial of last name with spaces" do | |
person = described_class.new(first_name: "Hari", middle_name: "Lal", last_name: "Carreras Pérez") | |
expect(person.initials).to eq("H. L. C. P.") | |
end | |
it "does not add extra spaces if middle name is missing" do | |
person = described_class.new(first_name: "Hari", last_name: "Carreras Pérez") | |
expect(person.initials).to eq("H. C. P.") | |
end | |
context "long name" do | |
it "concatenates initial of first name, initial of middle name, and initial of last name with spaces" do | |
person = described_class.new(first_name: "Hari Federico", middle_name: "Lal Mario", last_name: "Carreras Pérez") | |
expect(person.initials).to eq("H. F. L. M. C. P.") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment