Skip to content

Instantly share code, notes, and snippets.

@boddhisattva
Last active December 27, 2018 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boddhisattva/ea7194d749ad891dcc8b76e0d8549e3a to your computer and use it in GitHub Desktop.
Save boddhisattva/ea7194d749ad891dcc8b76e0d8549e3a to your computer and use it in GitHub Desktop.
Red-Green-Refactor by Example related exercise as part of the Fundamentals of TDD course in Upcase (https://thoughtbot.com/upcase/videos/red-green-refactor-by-example)
require 'rspec/autorun'
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, middle_name ? middle_name[0] : nil, last_name].compact.join(' ')
end
def initials
[first_name[0], middle_name ? middle_name[0] : nil, last_name[0]].compact.join(' ')
end
private
attr_reader :first_name, :middle_name, :last_name
end
RSpec.describe Person do
describe '#full_name' do
it 'concatenates first name middle name and last name' do
person = Person.new(first_name: 'Raj', middle_name: 'Aryan', last_name: 'Sharma')
expect(person.full_name).to eq('Raj Aryan Sharma')
end
it "does not add extra spaces if middle name is missing" do
person = Person.new(last_name: 'Sharma', first_name: 'Raj')
expect(person.full_name).to eq('Raj Sharma')
end
end
describe "#full_name_with_middle_initial" do
it 'displays full name with middle initial' do
person = Person.new(first_name: 'Raj', middle_name: 'Aryan', last_name: 'Sharma')
expect(person.full_name_with_middle_initial).to eq('Raj A Sharma')
end
it 'displays full name with no middle initial if no middle name' do
person = Person.new(first_name: 'Raj', last_name: 'Sharma')
expect(person.full_name_with_middle_initial).to eq('Raj Sharma')
end
end
describe "#initials" do
it 'gets the initials with regard to a name' do
person = Person.new(first_name: 'Raj', middle_name: 'Aryan', last_name: 'Sharma')
expect(person.initials).to eq('R A S')
end
it 'middle name is missing. It gets the initials with regard to other parts of name' do
person = Person.new(first_name: 'Raj', last_name: 'Sharma')
expect(person.initials).to eq('R S')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment