Skip to content

Commit

Permalink
Add tests for formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullah-original committed Jul 15, 2024
1 parent 82384f3 commit d9ca758
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/ExtendedDatePickerFormatter.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public class ExtendedDatePickerFormatter {
final class ExtendedDatePickerFormatter {
private let dateFormatter: DateFormatter
private let dateIntervalFormatter: DateIntervalFormatter
private let calendar: Calendar
Expand Down
165 changes: 165 additions & 0 deletions Tests/ExtendedDatePickerFormatterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
@testable import ExtendedDatePicker
import XCTest

final class ExtendedDatePickerFormatterTests: XCTestCase {

private var sut: ExtendedDatePickerFormatter!
private var calendar: Calendar = .current

func testHourMode_shouldOnlyDisplayHour() {
// Arrange
self.calendar.locale = .init(identifier: "en-GB")
self.sut = .init(calendar: calendar)

let hour = calendar.date(
from: .init(
year: 2024,
month: 7,
day: 15,
hour: 17
)
)!

// Act
let result = sut.format(from: hour, mode: .hour)

// Assert
XCTAssertEqual(result, "17:00")
}

func testDateMode_shouldOnlyDisplayDate() {
// Arrange
self.calendar.locale = .init(identifier: "en-US")
self.sut = .init(calendar: calendar)

let hour = calendar.date(
from: .init(
year: 2024,
month: 7,
day: 15,
hour: 17,
minute: 37
)
)!

// Act
let result = sut.format(from: hour, mode: .date)

// Assert
XCTAssertEqual(result, "July 15, 2024")
}

func testDateTimeMode_shouldDisplayBothDateAndTime() {
// Arrange
self.calendar.locale = .init(identifier: "en-US")
self.sut = .init(calendar: calendar)

let hour = calendar.date(
from: .init(
year: 2024,
month: 7,
day: 15,
hour: 17,
minute: 37
)
)!

// Act
let result = sut.format(from: hour, mode: .dateTime)

// Assert
XCTAssertEqual(result, "July 15, 2024 at 5:37 PM")
}

func testDateTimeMode_givenENGBLocale_shouldDisplayBothDateAndTime() {
// Arrange
self.calendar.locale = .init(identifier: "en-GB")
self.sut = .init(calendar: calendar)

let hour = calendar.date(
from: .init(
year: 2024,
month: 7,
day: 15,
hour: 17,
minute: 37
)
)!

// Act
let result = sut.format(from: hour, mode: .dateTime)

// Assert
XCTAssertEqual(result, "15 July 2024 at 17:37")
}

func testWeekMode_shouldDisplayDayRange() {
// Arrange
self.calendar.locale = .init(identifier: "en-GB")
self.sut = .init(calendar: calendar)

let hour = calendar.date(
from: .init(
year: 2024,
month: 7,
day: 15,
hour: 17,
minute: 37
)
)!

// Act
let result = sut.format(from: hour, mode: .week)

// Assert
XCTAssertEqual(result, "15 – 21 Jul 2024")
}



func testMonthYearMode_shouldNotDisplayDay() {
// Arrange
self.calendar.locale = .init(identifier: "en-US")
self.sut = .init(calendar: calendar)

let hour = calendar.date(
from: .init(
year: 2024,
month: 7,
day: 15,
hour: 17,
minute: 37
)
)!

// Act
let result = sut.format(from: hour, mode: .monthYear)

// Assert
XCTAssertEqual(result, "July 2024")
}

func testYearMode_shouldNotDisplayMonth() {
// Arrange
self.calendar.locale = .init(identifier: "en-US")
self.sut = .init(calendar: calendar)

let hour = calendar.date(
from: .init(
year: 2024,
month: 7,
day: 15,
hour: 17,
minute: 37
)
)!

// Act
let result = sut.format(from: hour, mode: .year)

// Assert
XCTAssertEqual(result, "2024")
}


}

0 comments on commit d9ca758

Please sign in to comment.