From a7a1d878b4b211ed739ad93317cf765deb82c2bb Mon Sep 17 00:00:00 2001 From: Student Date: Thu, 19 Mar 2020 08:23:00 +0000 Subject: [PATCH 01/18] Edit README with my approach to the exercise The content of the README that was included in the forked repo was deleted and replaced with a description of my approach to the exercise. So far it only includes the user stories I've derived from the exercise requirements. --- README.md | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index e310277..a8629fd 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,46 @@ -### Quick Start +# Birthdays -Fork this repository to your github account and clone it to your machine. Then install the dependencies: -```bash -> git clone https://github.com/makersacademy/birthdays.git -> cd birthdays -> bundle -``` +This repository contains the project I created working through the Birthdays TDD exercise during week 1 at Makers Academy. It was initially forked from a repository that contains a lot of scaffolding, a quick-start and instructions. + +## Exercise instructions -### Instructions +- Test-drive an implementation of the given requirements +- Lint your code +- Open a Pull Request when you're finished -- Test-drive an implementation of the requirements -- Make sure your code is [linted](https://github.com/rubocop-hq/rubocop) -- [Open a PR](https://services.github.com/on-demand/github-cli/open-pull-request-github) when you've finished +## Requirements -### Requirements +I want a program that I can load in IRB that allows me to: -I want a program that I can load in IRB that allows me to - Store all of my friends’ birthdays so I can keep track of them - See them all at once with their names and birthdays each on a line in a tidy format - Check whose birthday it is today - the program can check through the birthdays I have stored and check each one to see if it’s someone’s birthday, and tells me something like "It's Mary Poppin's birthday today! They are 124 years old!" - otherwise it won't say anything. More requirements: + - Test-drive extracting a birthday class - Isolate your birthday list class using a mock for Birthday -### TDD resources +## My approach + +I intend to go through the full process of creating user stories, tests and code implementation for this exercise so I can practice the processes I've been learning so far on the course. + +### User stories -- https://github.com/makersacademy/course/blob/master/pills/tdd.md -- https://github.com/makersacademy/course/blob/master/pills/tdd_quality_discussion.md +``` +As a user +I can store my friends' birthdays so I can keep track of them +``` -### Mocking +``` +As a user +I can see all of my friends' names and birthdays. +- it must print each friend on one line in a tidy format +``` -- https://relishapp.com/rspec/rspec-mocks/docs/basics/test-doubles +``` +As a user +I can check whose birthday it is today +- it must tell me something like ""It's Mary Poppin's birthday today! They are 124 years old!" +- if there are no birthdays for that day it must print nothing +``` \ No newline at end of file From 7e756d2afbe3b0554355b64e65d98c07c06addc9 Mon Sep 17 00:00:00 2001 From: Student Date: Thu, 19 Mar 2020 08:34:58 +0000 Subject: [PATCH 02/18] Update README with domain model --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a8629fd..3140655 100644 --- a/README.md +++ b/README.md @@ -43,4 +43,13 @@ As a user I can check whose birthday it is today - it must tell me something like ""It's Mary Poppin's birthday today! They are 124 years old!" - if there are no birthdays for that day it must print nothing -``` \ No newline at end of file +``` + +### Domain modelling + +| Object | Message | +| ------ | ------- | +| User | | +| Birthday_List | store\_birthday(friend) | +| Birthday_List | print\_birthdays | +| Birthday_List | show\_todays\_birthdays | \ No newline at end of file From 38395a3a82ec800cdd0b2fa193efa46f4e989493 Mon Sep 17 00:00:00 2001 From: Student Date: Thu, 19 Mar 2020 08:42:05 +0000 Subject: [PATCH 03/18] Implement BirthdayList class and add store method --- lib/birthday_list.rb | 4 ++++ spec/birthday_list_spec.rb | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/lib/birthday_list.rb b/lib/birthday_list.rb index 8b13789..0420a13 100644 --- a/lib/birthday_list.rb +++ b/lib/birthday_list.rb @@ -1 +1,5 @@ +class BirthdayList + def store(name, birthday) + end +end diff --git a/spec/birthday_list_spec.rb b/spec/birthday_list_spec.rb index 8b13789..df22cd6 100644 --- a/spec/birthday_list_spec.rb +++ b/spec/birthday_list_spec.rb @@ -1 +1,7 @@ +require 'birthday_list' +describe BirthdayList do + it 'stores friends birthdays' do + expect(subject).to respond_to(:store).with(2).argument + end +end \ No newline at end of file From 81d4a302be93d3f2c3ed79063fde946af629ed6c Mon Sep 17 00:00:00 2001 From: Student Date: Thu, 19 Mar 2020 08:45:58 +0000 Subject: [PATCH 04/18] Make BirthdayList confirm when a birthday has been stored --- lib/birthday_list.rb | 2 +- spec/birthday_list_spec.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/birthday_list.rb b/lib/birthday_list.rb index 0420a13..30bfc61 100644 --- a/lib/birthday_list.rb +++ b/lib/birthday_list.rb @@ -1,5 +1,5 @@ class BirthdayList def store(name, birthday) - + "Birthday stored!" end end diff --git a/spec/birthday_list_spec.rb b/spec/birthday_list_spec.rb index df22cd6..f5a5012 100644 --- a/spec/birthday_list_spec.rb +++ b/spec/birthday_list_spec.rb @@ -4,4 +4,8 @@ it 'stores friends birthdays' do expect(subject).to respond_to(:store).with(2).argument end + + it 'confirms that a friends birthday has been stored' do + expect(subject.store("Phil", Time.now)).to eq("Birthday stored!") + end end \ No newline at end of file From 0a26b7f7fddb3574224c66fc9c40d49cf40c4b06 Mon Sep 17 00:00:00 2001 From: Student Date: Thu, 19 Mar 2020 08:58:20 +0000 Subject: [PATCH 05/18] Add print method that works for a single stored friend --- lib/birthday_list.rb | 4 ++++ spec/birthday_list_spec.rb | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/birthday_list.rb b/lib/birthday_list.rb index 30bfc61..84ec056 100644 --- a/lib/birthday_list.rb +++ b/lib/birthday_list.rb @@ -2,4 +2,8 @@ class BirthdayList def store(name, birthday) "Birthday stored!" end + + def print + puts "Phil\t\t\t1st January" + end end diff --git a/spec/birthday_list_spec.rb b/spec/birthday_list_spec.rb index f5a5012..c0a247d 100644 --- a/spec/birthday_list_spec.rb +++ b/spec/birthday_list_spec.rb @@ -6,6 +6,11 @@ end it 'confirms that a friends birthday has been stored' do - expect(subject.store("Phil", Time.now)).to eq("Birthday stored!") + expect(subject.store("Phil", "1st January")).to eq("Birthday stored!") + end + + it 'prints all of the stored friends and their birthdays, each on a single line with a tidy format when a single friend is stored' do + subject.store("Phil,", "1st January") + expect { subject.print }.to output("Phil\t\t\t1st January\n").to_stdout end end \ No newline at end of file From 7ebc8845327f102f8641c6b8a44ef5e0ff66c529 Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 07:56:56 +0000 Subject: [PATCH 06/18] Implement printing a list of multiple friends/birthdays --- lib/birthday_list.rb | 22 +++++++++++++++++++--- spec/birthday_list_spec.rb | 10 ++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/birthday_list.rb b/lib/birthday_list.rb index 84ec056..51e916f 100644 --- a/lib/birthday_list.rb +++ b/lib/birthday_list.rb @@ -1,9 +1,25 @@ class BirthdayList + + COLUMN_SPACER = "\t\t\t" + + def initialize + @friends_and_birthdays = {} + end + def store(name, birthday) + @friends_and_birthdays[name] = birthday "Birthday stored!" end - def print - puts "Phil\t\t\t1st January" + def print_stored_friends + @friends_and_birthdays.each do |friend, birthday| + print_friend_and_birthday(friend, birthday) + end + end + + private + + def print_friend_and_birthday(friend, birthday) + puts "#{friend}#{COLUMN_SPACER}#{birthday}" end -end +end \ No newline at end of file diff --git a/spec/birthday_list_spec.rb b/spec/birthday_list_spec.rb index c0a247d..ea95cec 100644 --- a/spec/birthday_list_spec.rb +++ b/spec/birthday_list_spec.rb @@ -10,7 +10,13 @@ end it 'prints all of the stored friends and their birthdays, each on a single line with a tidy format when a single friend is stored' do - subject.store("Phil,", "1st January") - expect { subject.print }.to output("Phil\t\t\t1st January\n").to_stdout + subject.store("Phil", "1st January") + expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1st January\n").to_stdout + end + + it 'prints all stored friends and their birthdays, each on a single line with a tidy format when multiple friends are stored' do + subject.store("Phil", "1st January") + subject.store("Steve", "15th August") + expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1st January\nSteve#{BirthdayList::COLUMN_SPACER}15th August\n").to_stdout end end \ No newline at end of file From cc07ccf163b933ddd3c0d9b189742be69ddbc01f Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 08:14:10 +0000 Subject: [PATCH 07/18] Create Birthday class --- lib/birthday.rb | 5 +++++ spec/birthday_spec.rb | 8 ++++++++ 2 files changed, 13 insertions(+) create mode 100644 lib/birthday.rb create mode 100644 spec/birthday_spec.rb diff --git a/lib/birthday.rb b/lib/birthday.rb new file mode 100644 index 0000000..c8aee20 --- /dev/null +++ b/lib/birthday.rb @@ -0,0 +1,5 @@ +class Birthday + def initialize(birthday) + + end +end \ No newline at end of file diff --git a/spec/birthday_spec.rb b/spec/birthday_spec.rb new file mode 100644 index 0000000..dd08a20 --- /dev/null +++ b/spec/birthday_spec.rb @@ -0,0 +1,8 @@ +require 'birthday' + +describe Birthday do + it 'stores a birthday when you create it' do + birthday = Birthday.new("1st September") + expect(Birthday).to respond_to(:new).with(1).argument + end +end \ No newline at end of file From 40ec438fd1931e1ffa14fb0861cec0c95c5c9102 Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 08:20:54 +0000 Subject: [PATCH 08/18] Implement Birthdays printing their dates --- lib/birthday.rb | 6 +++++- spec/birthday_spec.rb | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/birthday.rb b/lib/birthday.rb index c8aee20..288e725 100644 --- a/lib/birthday.rb +++ b/lib/birthday.rb @@ -1,5 +1,9 @@ class Birthday - def initialize(birthday) + def initialize(date) + @date = date + end + def print_date + print @date end end \ No newline at end of file diff --git a/spec/birthday_spec.rb b/spec/birthday_spec.rb index dd08a20..e1ae704 100644 --- a/spec/birthday_spec.rb +++ b/spec/birthday_spec.rb @@ -1,8 +1,19 @@ require 'birthday' describe Birthday do - it 'stores a birthday when you create it' do - birthday = Birthday.new("1st September") + it 'stores a date when you create it' do expect(Birthday).to respond_to(:new).with(1).argument end + + it 'prints its date' do + birthday = Birthday.new("1st September") + expect { birthday.print_date }.to output("1st September").to_stdout + end + + it 'different birthdays print the correct dates' do + first_birthday = Birthday.new("1st September") + second_birthday = Birthday.new("5th August") + expect { first_birthday.print_date }.to output("1st September").to_stdout + expect { second_birthday.print_date }.to output("5th August").to_stdout + end end \ No newline at end of file From 9a865d591bba1e22ed20267a4235a9d20dcf37ba Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 08:45:14 +0000 Subject: [PATCH 09/18] Implement multiple birthdays printing their dates, and refactor --- lib/birthday.rb | 5 +---- lib/birthday_list.rb | 2 +- spec/birthday_list_spec.rb | 10 ++++++---- spec/birthday_spec.rb | 8 ++++---- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/birthday.rb b/lib/birthday.rb index 288e725..9ff6e74 100644 --- a/lib/birthday.rb +++ b/lib/birthday.rb @@ -1,9 +1,6 @@ class Birthday + attr_reader :date def initialize(date) @date = date end - - def print_date - print @date - end end \ No newline at end of file diff --git a/lib/birthday_list.rb b/lib/birthday_list.rb index 51e916f..5d0f0c9 100644 --- a/lib/birthday_list.rb +++ b/lib/birthday_list.rb @@ -20,6 +20,6 @@ def print_stored_friends private def print_friend_and_birthday(friend, birthday) - puts "#{friend}#{COLUMN_SPACER}#{birthday}" + puts "#{friend}#{COLUMN_SPACER}#{birthday.date}" end end \ No newline at end of file diff --git a/spec/birthday_list_spec.rb b/spec/birthday_list_spec.rb index ea95cec..20106ad 100644 --- a/spec/birthday_list_spec.rb +++ b/spec/birthday_list_spec.rb @@ -6,17 +6,19 @@ end it 'confirms that a friends birthday has been stored' do - expect(subject.store("Phil", "1st January")).to eq("Birthday stored!") + birthday = Birthday.new("1st January") + expect(subject.store("Phil", birthday)).to eq("Birthday stored!") end it 'prints all of the stored friends and their birthdays, each on a single line with a tidy format when a single friend is stored' do - subject.store("Phil", "1st January") + birthday = Birthday.new("1st January") + subject.store("Phil", birthday) expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1st January\n").to_stdout end it 'prints all stored friends and their birthdays, each on a single line with a tidy format when multiple friends are stored' do - subject.store("Phil", "1st January") - subject.store("Steve", "15th August") + subject.store("Phil", Birthday.new("1st January")) + subject.store("Steve", Birthday.new("15th August")) expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1st January\nSteve#{BirthdayList::COLUMN_SPACER}15th August\n").to_stdout end end \ No newline at end of file diff --git a/spec/birthday_spec.rb b/spec/birthday_spec.rb index e1ae704..f2f1cc9 100644 --- a/spec/birthday_spec.rb +++ b/spec/birthday_spec.rb @@ -5,15 +5,15 @@ expect(Birthday).to respond_to(:new).with(1).argument end - it 'prints its date' do + it 'tells you its date' do birthday = Birthday.new("1st September") - expect { birthday.print_date }.to output("1st September").to_stdout + expect(birthday.date).to eq "1st September" end it 'different birthdays print the correct dates' do first_birthday = Birthday.new("1st September") second_birthday = Birthday.new("5th August") - expect { first_birthday.print_date }.to output("1st September").to_stdout - expect { second_birthday.print_date }.to output("5th August").to_stdout + expect(first_birthday.date).to eq "1st September" + expect(second_birthday.date).to eq "5th August" end end \ No newline at end of file From 93c2e30d96e7136e535f7016bd1b8fca930a3082 Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 08:58:21 +0000 Subject: [PATCH 10/18] Remove birthday_list_spec dependence on Birthday class Doubling and mocking were used to remove the BirthdayList test spec's dependence on the Birthday class. --- spec/birthday_list_spec.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/spec/birthday_list_spec.rb b/spec/birthday_list_spec.rb index 20106ad..e101fa3 100644 --- a/spec/birthday_list_spec.rb +++ b/spec/birthday_list_spec.rb @@ -1,24 +1,25 @@ require 'birthday_list' describe BirthdayList do + let(:first_birthday) { double(:birthday, :date => "1st January") } + let(:second_birthday) { double(:birthday, :date => "15th August") } + it 'stores friends birthdays' do expect(subject).to respond_to(:store).with(2).argument end it 'confirms that a friends birthday has been stored' do - birthday = Birthday.new("1st January") - expect(subject.store("Phil", birthday)).to eq("Birthday stored!") + expect(subject.store("Phil", first_birthday)).to eq("Birthday stored!") end it 'prints all of the stored friends and their birthdays, each on a single line with a tidy format when a single friend is stored' do - birthday = Birthday.new("1st January") - subject.store("Phil", birthday) + subject.store("Phil", first_birthday) expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1st January\n").to_stdout end it 'prints all stored friends and their birthdays, each on a single line with a tidy format when multiple friends are stored' do - subject.store("Phil", Birthday.new("1st January")) - subject.store("Steve", Birthday.new("15th August")) + subject.store("Phil", first_birthday) + subject.store("Steve", second_birthday) expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1st January\nSteve#{BirthdayList::COLUMN_SPACER}15th August\n").to_stdout end end \ No newline at end of file From cbec42a90a87f2ae932711584f2f50cc2e87bbc0 Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 09:10:12 +0000 Subject: [PATCH 11/18] Refactor Birthday class to use instance of Time Previously the class used a string to represent its date. It now uses an instance of the Time class. --- lib/birthday.rb | 5 +++-- spec/birthday_spec.rb | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/birthday.rb b/lib/birthday.rb index 9ff6e74..25a65fd 100644 --- a/lib/birthday.rb +++ b/lib/birthday.rb @@ -1,6 +1,7 @@ class Birthday attr_reader :date - def initialize(date) - @date = date + + def initialize(day, month, year) + @date = Time.new(year, month, day) end end \ No newline at end of file diff --git a/spec/birthday_spec.rb b/spec/birthday_spec.rb index f2f1cc9..3984c3a 100644 --- a/spec/birthday_spec.rb +++ b/spec/birthday_spec.rb @@ -6,14 +6,14 @@ end it 'tells you its date' do - birthday = Birthday.new("1st September") - expect(birthday.date).to eq "1st September" + birthday = Birthday.new(1, 9, 1977) + expect(birthday.date).to eq Time.new(1977, 9, 1) end it 'different birthdays print the correct dates' do - first_birthday = Birthday.new("1st September") - second_birthday = Birthday.new("5th August") - expect(first_birthday.date).to eq "1st September" - expect(second_birthday.date).to eq "5th August" + first_birthday = Birthday.new(1, 9, 1977) + second_birthday = Birthday.new(5, 8, 1985) + expect(first_birthday.date).to eq Time.new(1977, 9, 1) + expect(second_birthday.date).to eq Time.new(1985, 8, 5) end end \ No newline at end of file From f7869e72fef39d9270249fa71a118dac8cf1286e Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 09:15:53 +0000 Subject: [PATCH 12/18] Override to_s in Birthday class to_s was overriden to return a 'pretty' version of the birthday's date. --- lib/birthday.rb | 6 +++++- spec/birthday_spec.rb | 9 +++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/birthday.rb b/lib/birthday.rb index 25a65fd..ba9dacd 100644 --- a/lib/birthday.rb +++ b/lib/birthday.rb @@ -1,7 +1,11 @@ class Birthday attr_reader :date - + def initialize(day, month, year) @date = Time.new(year, month, day) end + + def to_s + "#{@date.day} #{@date.month} #{@date.year}" + end end \ No newline at end of file diff --git a/spec/birthday_spec.rb b/spec/birthday_spec.rb index 3984c3a..ced12f7 100644 --- a/spec/birthday_spec.rb +++ b/spec/birthday_spec.rb @@ -2,7 +2,7 @@ describe Birthday do it 'stores a date when you create it' do - expect(Birthday).to respond_to(:new).with(1).argument + expect(Birthday).to respond_to(:new).with(3).argument end it 'tells you its date' do @@ -10,10 +10,15 @@ expect(birthday.date).to eq Time.new(1977, 9, 1) end - it 'different birthdays print the correct dates' do + it 'different birthdays tell you the correct dates' do first_birthday = Birthday.new(1, 9, 1977) second_birthday = Birthday.new(5, 8, 1985) expect(first_birthday.date).to eq Time.new(1977, 9, 1) expect(second_birthday.date).to eq Time.new(1985, 8, 5) end + + it 'can be converted to a human readable string' do + birthday = Birthday.new(1, 9, 1977) + expect(birthday.to_s).to eq "1 9 1977" + end end \ No newline at end of file From b79e2d81199e89a605c86c9d53d705d3bcca6144 Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 10:31:41 +0000 Subject: [PATCH 13/18] Refactor birthday_list_spec The BirthdatList tests were refactored to remove errors occurring with the use of a Time variable to store the date in the Birthday class. --- spec/birthday_list_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/birthday_list_spec.rb b/spec/birthday_list_spec.rb index e101fa3..bb00e15 100644 --- a/spec/birthday_list_spec.rb +++ b/spec/birthday_list_spec.rb @@ -3,6 +3,7 @@ describe BirthdayList do let(:first_birthday) { double(:birthday, :date => "1st January") } let(:second_birthday) { double(:birthday, :date => "15th August") } + let(:first_birthday_new) {double(:birthday, :date => Time.new(1977, 1, 1), :to_s => "1 1 1977")} it 'stores friends birthdays' do expect(subject).to respond_to(:store).with(2).argument From 455af2236da54d2b101667aa9d68a99fc54d8990 Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 10:37:36 +0000 Subject: [PATCH 14/18] Implement method to print todays birthdays The method currently only passes a test to print nothing if there are no friends stored with birthdays today. --- lib/birthday_list.rb | 4 ++++ spec/birthday_list_spec.rb | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/lib/birthday_list.rb b/lib/birthday_list.rb index 5d0f0c9..36c95fe 100644 --- a/lib/birthday_list.rb +++ b/lib/birthday_list.rb @@ -17,6 +17,10 @@ def print_stored_friends end end + def print_todays_birthdays + + end + private def print_friend_and_birthday(friend, birthday) diff --git a/spec/birthday_list_spec.rb b/spec/birthday_list_spec.rb index bb00e15..e32ced7 100644 --- a/spec/birthday_list_spec.rb +++ b/spec/birthday_list_spec.rb @@ -23,4 +23,9 @@ subject.store("Steve", second_birthday) expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1st January\nSteve#{BirthdayList::COLUMN_SPACER}15th August\n").to_stdout end + + it 'when you ask it to print todays birthdays and there are none, it prints nothing' do + subject.store("Phil", first_birthday) + expect { subject.print_todays_birthdays }.to output("").to_stdout + end end \ No newline at end of file From c7ce986356261ea32f390945c55dd11b2dcb6905 Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 10:46:07 +0000 Subject: [PATCH 15/18] Fix errors in birthday_list_spec The incorrect doubles were being used in the tests, which were not mocking the birthday class with the date as an instance of Time. --- lib/birthday_list.rb | 2 +- spec/birthday_list_spec.rb | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/birthday_list.rb b/lib/birthday_list.rb index 36c95fe..3fcd49e 100644 --- a/lib/birthday_list.rb +++ b/lib/birthday_list.rb @@ -24,6 +24,6 @@ def print_todays_birthdays private def print_friend_and_birthday(friend, birthday) - puts "#{friend}#{COLUMN_SPACER}#{birthday.date}" + puts "#{friend}#{COLUMN_SPACER}#{birthday.to_s}" end end \ No newline at end of file diff --git a/spec/birthday_list_spec.rb b/spec/birthday_list_spec.rb index e32ced7..6fd76fd 100644 --- a/spec/birthday_list_spec.rb +++ b/spec/birthday_list_spec.rb @@ -1,9 +1,8 @@ require 'birthday_list' describe BirthdayList do - let(:first_birthday) { double(:birthday, :date => "1st January") } - let(:second_birthday) { double(:birthday, :date => "15th August") } - let(:first_birthday_new) {double(:birthday, :date => Time.new(1977, 1, 1), :to_s => "1 1 1977")} + let(:first_birthday) {double(:birthday, :date => Time.new(1977, 1, 1), :to_s => "1 1 1977")} + let(:second_birthday) {double(:birthday, :date => Time.new(1958, 8, 15), :to_s => "15 8 1958")} it 'stores friends birthdays' do expect(subject).to respond_to(:store).with(2).argument @@ -15,13 +14,13 @@ it 'prints all of the stored friends and their birthdays, each on a single line with a tidy format when a single friend is stored' do subject.store("Phil", first_birthday) - expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1st January\n").to_stdout + expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1 1 1977\n").to_stdout end it 'prints all stored friends and their birthdays, each on a single line with a tidy format when multiple friends are stored' do subject.store("Phil", first_birthday) subject.store("Steve", second_birthday) - expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1st January\nSteve#{BirthdayList::COLUMN_SPACER}15th August\n").to_stdout + expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1 1 1977\nSteve#{BirthdayList::COLUMN_SPACER}15 8 1958\n").to_stdout end it 'when you ask it to print todays birthdays and there are none, it prints nothing' do From 7df2e9c9d02197d9c63d6ec4fa7b90b8d569f4c0 Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 10:46:07 +0000 Subject: [PATCH 16/18] Fix errors in birthday_list_spec The incorrect doubles were being used in the tests, which were not mocking the birthday class with the date as an instance of Time. --- lib/birthday_list.rb | 18 ++++++++++++++++-- spec/birthday_list_spec.rb | 25 ++++++++++++++++++++----- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/birthday_list.rb b/lib/birthday_list.rb index 36c95fe..896e9e0 100644 --- a/lib/birthday_list.rb +++ b/lib/birthday_list.rb @@ -18,12 +18,26 @@ def print_stored_friends end def print_todays_birthdays - + today = Time.now + @friends_and_birthdays.each do |friend, birthday| + if is_birthday_today?(birthday, today) + friend_age = calculate_friend_age(birthday, today) + puts "It's #{friend}'s birthday today! They are #{friend_age} years old!" + end + end end private def print_friend_and_birthday(friend, birthday) - puts "#{friend}#{COLUMN_SPACER}#{birthday.date}" + puts "#{friend}#{COLUMN_SPACER}#{birthday.to_s}" + end + + def is_birthday_today?(birthday, today) + birthday.date.month == today.month && birthday.date.day == today.day + end + + def calculate_friend_age(birthday, today) + today.year - birthday.date.year end end \ No newline at end of file diff --git a/spec/birthday_list_spec.rb b/spec/birthday_list_spec.rb index e32ced7..37d6b46 100644 --- a/spec/birthday_list_spec.rb +++ b/spec/birthday_list_spec.rb @@ -1,9 +1,8 @@ require 'birthday_list' describe BirthdayList do - let(:first_birthday) { double(:birthday, :date => "1st January") } - let(:second_birthday) { double(:birthday, :date => "15th August") } - let(:first_birthday_new) {double(:birthday, :date => Time.new(1977, 1, 1), :to_s => "1 1 1977")} + let(:first_birthday) {double(:birthday, :date => Time.new(1977, 1, 1), :to_s => "1 1 1977")} + let(:second_birthday) {double(:birthday, :date => Time.new(1958, 8, 15), :to_s => "15 8 1958")} it 'stores friends birthdays' do expect(subject).to respond_to(:store).with(2).argument @@ -15,17 +14,33 @@ it 'prints all of the stored friends and their birthdays, each on a single line with a tidy format when a single friend is stored' do subject.store("Phil", first_birthday) - expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1st January\n").to_stdout + expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1 1 1977\n").to_stdout end it 'prints all stored friends and their birthdays, each on a single line with a tidy format when multiple friends are stored' do subject.store("Phil", first_birthday) subject.store("Steve", second_birthday) - expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1st January\nSteve#{BirthdayList::COLUMN_SPACER}15th August\n").to_stdout + expect { subject.print_stored_friends }.to output("Phil#{BirthdayList::COLUMN_SPACER}1 1 1977\nSteve#{BirthdayList::COLUMN_SPACER}15 8 1958\n").to_stdout end it 'when you ask it to print todays birthdays and there are none, it prints nothing' do subject.store("Phil", first_birthday) expect { subject.print_todays_birthdays }.to output("").to_stdout end + + it 'when you ask it to print todays birthdays and there are some, it prints them' do + current_month = Time.now.month + current_date = Time.now.day + + birthday_today_1 = double(:birthday, :date => Time.new(1956, current_month, current_date), :to_s => "#{current_date} #{current_month} #{1956}") + birthday_today_2 = double(:birthday, :date => Time.new(1984, current_month, current_date), :to_s => "#{current_date} #{current_month} #{1984}") + + subject.store("Phil", birthday_today_1) + subject.store("Emma", first_birthday) + subject.store("Steve", birthday_today_2) + + expected_output = "It's Phil's birthday today! They are #{Time.now.year - 1956} years old!\n" + expected_output << "It's Steve's birthday today! They are #{Time.now.year - 1984} years old!\n" + expect { subject.print_todays_birthdays }.to output(expected_output).to_stdout + end end \ No newline at end of file From 77e9a9d420bd70b4146e2ee743f702a517c20d90 Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 11:57:59 +0000 Subject: [PATCH 17/18] Clean up README --- README.md | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/README.md b/README.md index 3140655..0a45ad7 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,6 @@ More requirements: - Test-drive extracting a birthday class - Isolate your birthday list class using a mock for Birthday -## My approach - -I intend to go through the full process of creating user stories, tests and code implementation for this exercise so I can practice the processes I've been learning so far on the course. - ### User stories ``` @@ -43,13 +39,4 @@ As a user I can check whose birthday it is today - it must tell me something like ""It's Mary Poppin's birthday today! They are 124 years old!" - if there are no birthdays for that day it must print nothing -``` - -### Domain modelling - -| Object | Message | -| ------ | ------- | -| User | | -| Birthday_List | store\_birthday(friend) | -| Birthday_List | print\_birthdays | -| Birthday_List | show\_todays\_birthdays | \ No newline at end of file +``` \ No newline at end of file From de4a04f61e542f9856337b29a5e262de94832366 Mon Sep 17 00:00:00 2001 From: Phil Vigus Date: Fri, 20 Mar 2020 16:10:31 +0000 Subject: [PATCH 18/18] Fix bug where require is missing from birthday_list birthday_list was missing a require to use the Birthday class. --- lib/birthday_list.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/birthday_list.rb b/lib/birthday_list.rb index 896e9e0..cccb3aa 100644 --- a/lib/birthday_list.rb +++ b/lib/birthday_list.rb @@ -1,5 +1,6 @@ -class BirthdayList +require_relative './birthday' +class BirthdayList COLUMN_SPACER = "\t\t\t" def initialize