From 3e9067cc63ef3aea6d717961dbd827d8cd2cac06 Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Wed, 13 Apr 2022 15:35:27 -0400 Subject: [PATCH] Tests for Event Info. --- Clocker/ClockerUnitTests/EventInfoTests.swift | 95 +++++++++++++++++++ .../CalendarHandler.swift | 4 +- 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 Clocker/ClockerUnitTests/EventInfoTests.swift diff --git a/Clocker/ClockerUnitTests/EventInfoTests.swift b/Clocker/ClockerUnitTests/EventInfoTests.swift new file mode 100644 index 0000000..dd71411 --- /dev/null +++ b/Clocker/ClockerUnitTests/EventInfoTests.swift @@ -0,0 +1,95 @@ +// Copyright © 2015 Abhishek Banthia + +import XCTest +@testable import Clocker +import EventKit + +class EventInfoTests: XCTestCase { + private let eventStore = EKEventStore() + + func testMetadataForUpcomingEventHappeningInFiveMinutes() throws { + let futureChunk = TimeChunk(seconds: 10, minutes: 5, hours: 0, days: 0, weeks: 0, months: 0, years: 0) + let mockEvent = EKEvent(eventStore: eventStore) + mockEvent.title = "Mock Title" + mockEvent.startDate = Date().add(futureChunk) + + let mockEventInfo = EventInfo(event: mockEvent, + isStartDate: false, + isEndDate: false, + isAllDay: false, + isSingleDay: true, + meetingURL: nil, + attendeStatus: .accepted) + XCTAssert(mockEventInfo.metadataForMeeting() == "in 5m", + "Metadata for meeting: \(mockEventInfo.metadataForMeeting()) doesn't match expectation") + } + + func testMetadataForUpcomingEventHappeningInTenSeconds() throws { + let futureChunk = TimeChunk(seconds: 10, minutes: 0, hours: 0, days: 0, weeks: 0, months: 0, years: 0) + let mockEvent = EKEvent(eventStore: eventStore) + mockEvent.title = "Mock Title" + mockEvent.startDate = Date().add(futureChunk) + + let mockEventInfo = EventInfo(event: mockEvent, + isStartDate: false, + isEndDate: false, + isAllDay: false, + isSingleDay: true, + meetingURL: nil, + attendeStatus: .accepted) + XCTAssert(mockEventInfo.metadataForMeeting() == "in <1m", + "Metadata for meeting: \(mockEventInfo.metadataForMeeting()) doesn't match expectation") + } + + func testMetadataForEventPastTwoMinutes() throws { + let pastChunk = TimeChunk(seconds: 10, minutes: 2, hours: 0, days: 0, weeks: 0, months: 0, years: 0) + let mockEvent = EKEvent(eventStore: eventStore) + mockEvent.title = "Mock Title" + mockEvent.startDate = Date().subtract(pastChunk) + + let mockEventInfo = EventInfo(event: mockEvent, + isStartDate: false, + isEndDate: false, + isAllDay: false, + isSingleDay: true, + meetingURL: nil, + attendeStatus: .accepted) + XCTAssert(mockEventInfo.metadataForMeeting() == "started +2m.", + "Metadata for meeting: \(mockEventInfo.metadataForMeeting()) doesn't match expectation") + } + + func testMetadataForEventPastTenMinutes() throws { + let pastChunk = TimeChunk(seconds: 10, minutes: 10, hours: 0, days: 0, weeks: 0, months: 0, years: 0) + let mockEvent = EKEvent(eventStore: eventStore) + mockEvent.title = "Mock Title" + mockEvent.startDate = Date().subtract(pastChunk) + + let mockEventInfo = EventInfo(event: mockEvent, + isStartDate: false, + isEndDate: false, + isAllDay: false, + isSingleDay: true, + meetingURL: nil, + attendeStatus: .accepted) + XCTAssert(mockEventInfo.metadataForMeeting() == "Error", + "Metadata for meeting: \(mockEventInfo.metadataForMeeting()) doesn't match expectation") + } + + func testMetadataForEventHappeningTomorrow() throws { + let pastChunk = TimeChunk(seconds: 10, minutes: 0, hours: 25, days: 0, weeks: 0, months: 0, years: 0) + let mockEvent = EKEvent(eventStore: eventStore) + mockEvent.title = "Mock Title" + mockEvent.startDate = Date().add(pastChunk) + + let mockEventInfo = EventInfo(event: mockEvent, + isStartDate: false, + isEndDate: false, + isAllDay: false, + isSingleDay: true, + meetingURL: nil, + attendeStatus: .accepted) + XCTAssert(mockEventInfo.metadataForMeeting() == "in 25h", + "Metadata for meeting: \(mockEventInfo.metadataForMeeting()) doesn't match expectation") + } + +} diff --git a/Clocker/Events and Reminders/CalendarHandler.swift b/Clocker/Events and Reminders/CalendarHandler.swift index 59df4ce..6fad75e 100644 --- a/Clocker/Events and Reminders/CalendarHandler.swift +++ b/Clocker/Events and Reminders/CalendarHandler.swift @@ -492,13 +492,13 @@ struct EventInfo { let timeIntervalSinceNowForMeeting = event.startDate.timeIntervalSinceNow if timeIntervalSinceNowForMeeting < 0, timeIntervalSinceNowForMeeting > -300 { return "started +\(event.startDate.shortTimeAgoSinceNow)." - } else if event.startDate.isToday { + } else if event.startDate.isToday, timeIntervalSinceNowForMeeting > 0 { let timeSince = Date().timeAgo(since: event.startDate).lowercased() let withoutAn = timeSince.replacingOccurrences(of: "an", with: CLEmptyString) let withoutAgo = withoutAn.replacingOccurrences(of: "ago", with: CLEmptyString) // If the user has not turned on seconds granularity for one of the timezones, // we return "in 12 seconds" which looks weird. - return withoutAgo.contains("seconds") ? "in <1m" : "in \(withoutAgo.lowercased())" + return withoutAgo.contains("seconds") ? "in <1m" : "in \(withoutAgo.lowercased())".trimmingCharacters(in: .whitespaces) } else if event.startDate.isTomorrow { let hoursUntil = event.startDate.hoursUntil return "in \(hoursUntil)h"