Skip to content

Commit

Permalink
Fix logic for isBirthdayComing to not include current date
Browse files Browse the repository at this point in the history
  • Loading branch information
dragosrobertn committed Jan 14, 2020
1 parent 0549ced commit 3411df7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions KNContacts/KNContact.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ public struct KNContact {
- Author: dragosrobertn
- Parameters:
- in: The number of days as an integer representing the number of days to check if the birthday is upcoming
- startingDate: The date from which to start checking if the birthday is upcoming. Default is today's date.
- startingDate: The date from which to start checking if the birthday is upcoming. Default is today's date and the starting date will be excluded..

- Returns: Returns a bool representing whether the today is the contact's birthday. False if the contact doesn't have birthday information available.
- Version: 1.2.1
*/
public func isBirthdayComing(in days: Int, startingDate: Date = Date()) -> Bool {
guard let birthday = self.getBirthday(forCurrentYear: true) else { return false }
let birthdayComponents = calendar.dateComponents([.day, .month], from: birthday)
let dateComponents: [DateComponents] = (0...days)
let dateComponents: [DateComponents] = (1...days)
.compactMap({ number in return calendar.date(byAdding: .day, value: number, to: startingDate)! })
.compactMap({ date in return calendar.dateComponents([.day, .month], from: date)})

Expand Down
16 changes: 14 additions & 2 deletions KNContactsTests/KNContactTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ class KNContactTests: XCTestCase {
XCTAssertFalse(contact.isBirthdayComing(in: 7))
}

func testIsBirthdayComingIsFalseWhenBirthdayIsToday() {
let mutableContact = UnitTestsContactHelpers.getMutableContact()

let todayComponents = calendar.dateComponents([.day, .month], from: Date())
mutableContact.birthday?.day = todayComponents.day
mutableContact.birthday?.month = todayComponents.month

let contact = KNContact(for: mutableContact)

XCTAssertFalse(contact.isBirthdayComing(in: 7))
}

func testIsBirthdayComingIsTrue() {
let mutableContact = UnitTestsContactHelpers.getMutableContact()
let aWeekFromToday = calendar.date(byAdding: .day, value: 7, to: Date())!
Expand Down Expand Up @@ -157,7 +169,7 @@ class KNContactTests: XCTestCase {

func testIsBirthdayComingIsTrueForContactWithBirthdayInLateDecemberWhichMakesTheCheckPassAYear() {
let mutableContact = UnitTestsContactHelpers.getMutableContact()
mutableContact.birthday?.day = 27
mutableContact.birthday?.day = 28
mutableContact.birthday?.month = 12

let dateUtilsFormatter = KNDatesUtils.formatter(with: .fullDate)
Expand All @@ -169,7 +181,7 @@ class KNContactTests: XCTestCase {

func testIsBirthdayComingIsFalseForContactWithBirthdayInLateDecemberThatJustPassedWhichMakesTheCheckPassAYear() {
let mutableContact = UnitTestsContactHelpers.getMutableContact()
mutableContact.birthday?.day = 26
mutableContact.birthday?.day = 27
mutableContact.birthday?.month = 12

let dateUtilsFormatter = KNDatesUtils.formatter(with: .fullDate)
Expand Down

0 comments on commit 3411df7

Please sign in to comment.