-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the first half of moon information, and then brought up questio…
…ns to be answered based on the comments left in SolarTests
- Loading branch information
Showing
11 changed files
with
411 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// File.swift | ||
// SunKit | ||
// | ||
// Created by Jim Newkirk on 2/11/25. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct LunarData: Codable, Sendable { | ||
internal init(moonRise: Date? = nil, moonSet: Date? = nil, illumination: Double, phase: LunarPhase) { | ||
self.moonRise = moonRise | ||
self.moonSet = moonSet | ||
self.illumination = illumination | ||
self.phase = phase | ||
} | ||
|
||
public let moonRise: Date? | ||
public let moonSet: Date? | ||
public let illumination: Double | ||
public let phase: LunarPhase | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// File.swift | ||
// SunKit | ||
// | ||
// Created by Jim Newkirk on 2/11/25. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum LunarPhase: String, CaseIterable, Codable, Sendable { | ||
case new = "New" | ||
case waningCrescent = "Waning Crescent" | ||
case thirdQuarter = "Third Quarter" | ||
case waningGibbous = "Waning Gibbous" | ||
case full = "Full" | ||
case waxingGibbous = "Waxing Gibbous" | ||
case firstQuarter = "First Quarter" | ||
case waxingCrescent = "Waxing Crescent" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// File.swift | ||
// SunKit | ||
// | ||
// Created by Jim Newkirk on 2/11/25. | ||
// | ||
|
||
import Foundation | ||
|
||
extension String { | ||
public func toDate() -> Date? { | ||
let formatter = ISO8601DateFormatter() | ||
|
||
return formatter.date(from: self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// File.swift | ||
// SunKit | ||
// | ||
// Created by Jim Newkirk on 2/11/25. | ||
// | ||
|
||
import CoreLocation | ||
import Foundation | ||
import SunKit | ||
import Testing | ||
|
||
struct MoonData: Codable { | ||
let rise: Date | ||
let set: Date | ||
let phase: LunarPhase | ||
let illumination: Double | ||
} | ||
|
||
struct LocationMoonInfo: Codable { | ||
internal init(name: String, date: Date, latitude: Double, longitude: Double, moonData: MoonData) { | ||
self.name = name | ||
self.date = date | ||
self.latitude = latitude | ||
self.longitude = longitude | ||
self.moonData = moonData | ||
} | ||
|
||
let name: String | ||
let date: Date | ||
let latitude: Double | ||
let longitude: Double | ||
let moonData: MoonData | ||
} | ||
|
||
extension LocationMoonInfo { | ||
var coordinate: CLLocationCoordinate2D { | ||
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) | ||
guard CLLocationCoordinate2DIsValid(coordinate) else { | ||
Issue.record("Test Location has invalid coordinates: \(coordinate)") | ||
fatalError("Test Location has invalid coordinates: \(coordinate)") | ||
} | ||
|
||
return coordinate | ||
} | ||
|
||
static func load() -> [LocationMoonInfo] { | ||
do { | ||
let url = Bundle.module.url(forResource: Constant.testMoonFile, withExtension: "json") | ||
guard let url else { | ||
Issue.record("url is nil for moonData.json") | ||
return [] | ||
} | ||
|
||
let decoder = JSONDecoder() | ||
decoder.dateDecodingStrategy = .iso8601 | ||
|
||
let data = try Data(contentsOf: url) | ||
let moonInfo = try decoder.decode([LocationMoonInfo].self, from: data) | ||
return moonInfo | ||
} catch { | ||
Issue.record(error) | ||
return [] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Test.swift | ||
// SunKit | ||
// | ||
// Created by Jim Newkirk on 2/11/25. | ||
// | ||
|
||
import Testing | ||
@testable import SunKit | ||
|
||
struct MoonTests { | ||
var testDatum: [LocationMoonInfo] = [] | ||
|
||
internal init() async throws { | ||
testDatum = LocationMoonInfo.load() | ||
} | ||
|
||
@Test func testLocationCount() async throws { | ||
#expect(testDatum.count == 5) | ||
} | ||
|
||
@Test | ||
func lunarPhase() async throws { | ||
for testData in testDatum { | ||
let solar = Solar(date: testData.date, coordinate: testData.coordinate) | ||
|
||
#expect(solar.lunar.phase == testData.moonData.phase, | ||
"Test Location: \(testData.name)") | ||
} | ||
} | ||
|
||
@Test | ||
func illumination() async throws { | ||
for testData in testDatum { | ||
let solar = Solar(date: testData.date, coordinate: testData.coordinate) | ||
|
||
#expect(solar.lunar.illumination == testData.moonData.illumination, | ||
"Test Location: \(testData.name)") | ||
} | ||
} | ||
|
||
@Test(arguments: zip( | ||
[2451549.5, 2451551.5, 2451556.9, 2451564.3, 2451579.3], | ||
[0.0, 2.0, 7.4, 14.8, 0.3] | ||
)) | ||
func moonAge(julianDate: Double, moonAge: Double) throws { | ||
#expect(moonAge == Solar.moonAge(julianDate: julianDate)) | ||
} | ||
|
||
@Test(arguments: zip( | ||
[2.0, 11.5, 18.2, 25.1], | ||
[LunarPhase.waxingCrescent, LunarPhase.waxingGibbous, LunarPhase.waningGibbous, LunarPhase.waningCrescent] | ||
)) | ||
func lunarPhaseByAge(moonAge: Double, moonPhase: LunarPhase) throws { | ||
#expect(moonPhase == Solar.lunarPhase(moonAge: moonAge)) | ||
} | ||
} |
Oops, something went wrong.