Skip to content

Commit

Permalink
Add tests for new GregorianDay methods & fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Mar 26, 2024
1 parent 6beb098 commit 8d18f32
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/HandySwift/Types/GregorianDay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public struct GregorianDay {
/// ```
public func advanced(byMonths months: Int) -> Self {
let (overflowingYears, newMonth) = (self.month + months - 1).quotientAndRemainder(dividingBy: 12)
return self.with { $0.year += overflowingYears; $0.month = newMonth }
return self.with { $0.year += overflowingYears; $0.month = newMonth + 1 }
}

/// Reverses the date by the specified number of months.
Expand Down Expand Up @@ -133,7 +133,7 @@ public struct GregorianDay {
/// let yesterday = GregorianDay.today.reversed(byYears: 1)
/// ```
public func reversed(byYears years: Int) -> Self {
self.advanced(byMonths: -years)
self.advanced(byYears: -years)
}

/// Returns the start of the day represented by the date.
Expand Down
24 changes: 24 additions & 0 deletions Tests/HandySwiftTests/Structs/GregorianDayTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation

@testable import HandySwift
import XCTest

final class GregorianDayTests: XCTestCase {
func testAdvancedByMonths() {
let day = GregorianDay(year: 2024, month: 03, day: 26)
let advancedByAMonth = day.advanced(byMonths: 1)

XCTAssertEqual(advancedByAMonth.year, 2024)
XCTAssertEqual(advancedByAMonth.month, 04)
XCTAssertEqual(advancedByAMonth.day, 26)
}

func testReversedByYears() {
let day = GregorianDay(year: 2024, month: 03, day: 26)
let reversedByTwoYears = day.reversed(byYears: 2)

XCTAssertEqual(reversedByTwoYears.year, 2022)
XCTAssertEqual(reversedByTwoYears.month, 03)
XCTAssertEqual(reversedByTwoYears.day, 26)
}
}

0 comments on commit 8d18f32

Please sign in to comment.