Skip to content

Commit 789cf34

Browse files
committed
leaving one test to be fixed in the future
1 parent 1c626de commit 789cf34

File tree

5 files changed

+28
-30
lines changed

5 files changed

+28
-30
lines changed

Sources/ChineseAstrologyCalendar/Date/DateComponents+Day.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import Foundation
66
import Foundation
77

88
extension DateComponents {
9-
9+
1010
// MARK: - Public
11-
11+
1212
/// 日干:根据 base 计算
1313
public var riGan: Tiangan? {
1414
guard let base else {
@@ -19,7 +19,7 @@ extension DateComponents {
1919
let index = base % 10 == 0 ? 10 : base % 10
2020
return Tiangan(rawValue: index)
2121
}
22-
22+
2323
/// 日支:根据 base 计算
2424
public var riZhi: Dizhi? {
2525
guard let base else {
@@ -30,15 +30,15 @@ extension DateComponents {
3030
let index = base % 12 == 0 ? 12 : base % 12
3131
return Dizhi(rawValue: index)
3232
}
33-
33+
3434
/// 日柱:天干与地支的组合
3535
public var riZhu: Ganzhi? {
3636
guard let gan = riGan, let zhi = riZhi else { return nil }
3737
return Ganzhi(gan: gan, zhi: zhi)
3838
}
39-
39+
4040
// MARK: - Internal Computation
41-
41+
4242
/// Computes a base value used for determining the day stem and branch.
4343
/// - Note: The algorithm works as follows:
4444
/// 1. Take the last two digits of the year (`yearInCentury`).

Sources/ChineseAstrologyCalendar/Date/DateComponents+Hour.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,18 @@ extension DateComponents {
2828
///
2929
/// The custom initializer of `Dizhi` (i.e. `Dizhi(hourOfDay:)`) is expected
3030
/// to map the hour (0–23) to the corresponding Earthly Branch.
31-
public var shiZhi: Dizhi {
31+
public var shiZhi: Dizhi? {
32+
guard let hour else {
33+
return nil
34+
}
3235
// If the hour component is missing, assume 0.
33-
let hourValue = hour ?? 0
36+
let hourValue = hour
3437
return Dizhi(hourOfDay: hourValue)
3538
}
3639

3740
/// Combines the hour Heavenly Stem and Earthly Branch to form the hour pillar (时柱).
3841
public var shiZhu: Ganzhi? {
39-
guard let stem = shiGan else { return nil }
42+
guard let stem = shiGan, let shiZhi else { return nil }
4043
return Ganzhi(gan: stem, zhi: shiZhi)
4144
}
4245
}

Tests/ChineseAstrologyCalendarTests/IntegrationTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ final class IntegrationTests: XCTestCase {
5656
XCTAssertEqual(date.dateComponentsFromChineseCalendar().yueZhi, Dizhi.you)
5757
}
5858

59-
func testYueGanToBeYi() {
59+
func testYueGanToBeJi() {
6060
setupDateTwo()
61-
XCTAssertEqual(date.dateComponentsFromChineseCalendar().yueGan, Tiangan.yi)
61+
XCTAssertEqual(date.dateComponentsFromChineseCalendar().yueGan, Tiangan.ji)
6262
}
6363

6464
func testNianGanToBeRen() {

Tests/ChineseAstrologyCalendarTests/RiZhuTests.swift

+8-13
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ final class RiZhuTests: XCTestCase {
3636
/// Test the month pillar (月柱) for 2023-01-23.
3737
/// Assuming the computed property is called `yue` (or adjust to `yueZhu` as needed).
3838
func test_monthPillar_for2023_01_23() throws {
39-
let component = DateComponents(calendar: .current, year: 2023, month: 1, day: 23)
39+
let component = DateComponents(calendar: .chineseCalendar, year: 2023, month: 1, day: 23)
4040

41-
// Expected month pillar for this date should be "丙寅" as per your algorithm.
42-
XCTAssertEqual(component.yue?.description, "丙寅", "The month pillar for 2023-01-23 should be 丙寅")
41+
// Expected month pillar for this date should be "甲寅" as per your algorithm.
42+
XCTAssertEqual(component.yue?.description, "甲寅", "The month pillar for 2023-01-23 should be 甲寅")
4343
}
4444

4545
// MARK: - Hour Pillar Tests
@@ -60,35 +60,30 @@ final class RiZhuTests: XCTestCase {
6060

6161
// Also check individual components.
6262
XCTAssertEqual(component.shiGan?.chineseCharactor, "", "The hour heavenly stem for 3 AM should be 辛")
63-
XCTAssertEqual(component.shiZhi.chineseCharactor, "", "The hour earthly branch for 3 AM should be 寅")
63+
XCTAssertEqual(component.shiZhi?.chineseCharactor, "", "The hour earthly branch for 3 AM should be 寅")
6464
}
6565

6666
/// Test the hour pillar when the hour component is missing.
6767
/// In such a case, a default (e.g. 0 hour) may be assumed.
6868
func test_hourPillar_whenHourIsMissing() {
6969
let component = DateComponents(calendar: .current, year: 2010, month: 4, day: 12)
7070

71-
// Depending on your implementation of `Dizhi(hourOfDay:)`, if hour is missing,
72-
// it might default to 0. Adjust the expected value accordingly.
73-
// For example, if hour 0 maps to 子, then the expected hour pillar might be computed
74-
// with a default hour of 0.
75-
// Here we only verify that shiZhi returns a valid Dizhi.
76-
XCTAssertNotNil(component.shiZhi, "The hour earthly branch should have a default value when hour is missing")
71+
XCTAssertNil(component.shiZhi, "The hour earthly branch nil when hour is missing")
7772
}
7873

7974
// MARK: - Additional Boundary and Consistency Tests
8075

8176
/// Test that the computed pillars wrap around correctly.
8277
func test_moduloWrapping() {
8378
// Create a component with a date that yields a base value near the modulo boundaries.
84-
let component = DateComponents(calendar: .current, year: 1999, month: 12, day: 31, hour: 23)
79+
let component = DateComponents(calendar: .current, year: 1999, month: 12, day: 29, hour: 23, minute: 59, second: 59)
8580

8681
// The actual expected values depend on your algorithm.
8782
// For demonstration, suppose we expect:
8883
// Day pillar: "癸亥", Month pillar: "己丑", Hour pillar: "壬子"
8984
// (Adjust these expected values according to your correct algorithm.)
90-
XCTAssertEqual(component.riZhu?.description, "癸亥", "Day pillar should wrap correctly at the modulo boundary")
91-
XCTAssertEqual(component.yue?.description, "己丑", "Month pillar should wrap correctly at the modulo boundary")
85+
XCTAssertEqual(component.riZhu?.description, "壬辰", "Day pillar should wrap correctly at the modulo boundary")
86+
XCTAssertEqual(component.yue?.description, "戊寅", "Month pillar should wrap correctly at the modulo boundary")
9287
XCTAssertEqual(component.shiZhu?.description, "壬子", "Hour pillar should wrap correctly at the modulo boundary")
9388
}
9489

Tests/ChineseAstrologyCalendarTests/ShiZhuTests.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class ShiZhuTests: XCTestCase {
1717

1818
XCTAssertEqual(component.shiZhu?.description, "辛寅", "Hour pillar for 3 AM should be 辛寅")
1919
XCTAssertEqual(component.shiGan?.chineseCharactor, "", "Hour heavenly stem for 3 AM should be 辛")
20-
XCTAssertEqual(component.shiZhi.chineseCharactor, "", "Hour earthly branch for 3 AM should be 寅")
20+
XCTAssertEqual(component.shiZhi?.chineseCharactor, "", "Hour earthly branch for 3 AM should be 寅")
2121
}
2222

2323
/// Test the hour pillar for 2010-04-12 at 11 PM.
@@ -32,7 +32,7 @@ final class ShiZhuTests: XCTestCase {
3232

3333
XCTAssertEqual(component.shiZhu?.description, "辛子", "Hour pillar for 11 PM should be 辛子")
3434
XCTAssertEqual(component.shiGan?.chineseCharactor, "", "Hour heavenly stem for 11 PM should be 辛")
35-
XCTAssertEqual(component.shiZhi.chineseCharactor, "", "Hour earthly branch for 11 PM should be 子")
35+
XCTAssertEqual(component.shiZhi?.chineseCharactor, "", "Hour earthly branch for 11 PM should be 子")
3636
}
3737

3838
// MARK: - Default Hour Tests
@@ -44,9 +44,9 @@ final class ShiZhuTests: XCTestCase {
4444
let calendar = Calendar(identifier: .gregorian)
4545
let component = DateComponents(calendar: calendar, year: 2010, month: 4, day: 12)
4646

47-
XCTAssertEqual(component.shiZhi.chineseCharactor, "", "Missing hour should default to 0, mapping to 子")
47+
XCTAssertNil(component.shiZhi?.chineseCharactor, "Missing hour nil")
4848
XCTAssertEqual(component.shiGan?.chineseCharactor, "", "Hour heavenly stem should be computed as 辛")
49-
XCTAssertEqual(component.shiZhu?.description, "辛子", "Hour pillar for missing hour should be 辛子")
49+
XCTAssertNil(component.shiZhu?.description, "Hour pillar for missing hour should be nil")
5050
}
5151

5252
// MARK: - Boundary Hour Tests
@@ -58,7 +58,7 @@ final class ShiZhuTests: XCTestCase {
5858
let component = DateComponents(calendar: calendar, year: 2010, month: 4, day: 12, hour: 0)
5959

6060
XCTAssertEqual(component.shiZhu?.description, "辛子", "Hour pillar for midnight should be 辛子")
61-
XCTAssertEqual(component.shiZhi.chineseCharactor, "", "Hour earthly branch for midnight should be 子")
61+
XCTAssertEqual(component.shiZhi?.chineseCharactor, "", "Hour earthly branch for midnight should be 子")
6262
}
6363

6464
/// Test the hour pillar for noon (12 PM).
@@ -70,6 +70,6 @@ final class ShiZhuTests: XCTestCase {
7070
let component = DateComponents(calendar: calendar, year: 2010, month: 4, day: 12, hour: 12)
7171

7272
XCTAssertEqual(component.shiZhu?.description, "辛午", "Hour pillar for noon should be 辛午")
73-
XCTAssertEqual(component.shiZhi.chineseCharactor, "", "Hour earthly branch for noon should be 午")
73+
XCTAssertEqual(component.shiZhi?.chineseCharactor, "", "Hour earthly branch for noon should be 午")
7474
}
7575
}

0 commit comments

Comments
 (0)