Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription

let package = Package(
name: "Solar",
platforms: [.iOS(.v12), .macOS(.v10_13), .watchOS(.v4), .tvOS(.v12)],
platforms: [.iOS(.v13), .macOS(.v10_15), .watchOS(.v6), .tvOS(.v13)],
products: [
.library(name: "Solar", targets: ["Solar"]),
],
Expand Down
16 changes: 8 additions & 8 deletions Solar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -887,18 +887,18 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MACOSX_DEPLOYMENT_TARGET = 10.13;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to bump to get swift testing support

MACOSX_DEPLOYMENT_TARGET = 10.15;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2";
TVOS_DEPLOYMENT_TARGET = 12.0;
TVOS_DEPLOYMENT_TARGET = 13.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
WATCHOS_DEPLOYMENT_TARGET = 4.0;
WATCHOS_DEPLOYMENT_TARGET = 6.0;
};
name = Debug;
};
Expand Down Expand Up @@ -946,18 +946,18 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MACOSX_DEPLOYMENT_TARGET = 10.13;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2";
TVOS_DEPLOYMENT_TARGET = 12.0;
TVOS_DEPLOYMENT_TARGET = 13.0;
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
WATCHOS_DEPLOYMENT_TARGET = 4.0;
WATCHOS_DEPLOYMENT_TARGET = 6.0;
};
name = Release;
};
Expand Down
226 changes: 96 additions & 130 deletions SolarTests/SolarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,164 +6,130 @@
// Copyright © 2017 Chris Howell. All rights reserved.
//

import XCTest
import Testing
import CoreLocation
@testable import Solar

final class SolarTests: XCTestCase {
private let testDate = Date(timeIntervalSince1970: 1486598400)
struct SolarTests {

private static let testDate = Date(timeIntervalSince1970: 1486598400)

/// How accurate, in minutes either side of the actual sunrise sunset, we want to be
/// This is necessary as the algorithm uses assumptions during calculation
private let testAccuracy: TimeInterval = 60 * 5
private var cities: [City]!
private static let testAccuracy: TimeInterval = 60 * 5

let bundle: Bundle = {
private final class BundleToken {}

private static let bundle: Bundle = {
#if SWIFT_PACKAGE
return Bundle.module
#else
return Bundle(for: SolarTests.self)
return Bundle(for: BundleToken.self)
#endif
}()

override func setUpWithError() throws {
try super.setUpWithError()
let resultsURL = try XCTUnwrap(bundle.url(forResource: "CorrectResults", withExtension: "json"))
let data = try Data(contentsOf: resultsURL)
let cityDictionaries = try XCTUnwrap(try JSONSerialization.jsonObject(with: data, options: []) as? [[String : Any]])
cities = cityDictionaries.map(City.init(json:))
}

func testSunrise() {
for city in cities {
let solar = Solar(for: testDate, coordinate: city.coordinate)

guard
let sunrise = solar?.sunrise
else {
XCTFail("Sunrise cannot be generated for city \(city.name)")
return
}

XCTAssertEqual(sunrise.timeIntervalSince1970, city.sunrise.timeIntervalSince1970, accuracy: testAccuracy, "\(city.name): \(sunrise) not close to \(city.sunrise)")
}
static let cities: [City] = {
let resultsURL = bundle.url(forResource: "CorrectResults", withExtension: "json")!
let data = try! Data(contentsOf: resultsURL)
let cityDictionaries = try! JSONSerialization.jsonObject(with: data, options: []) as! [[String: Any]]
return cityDictionaries.map(City.init(json:))
}()

@Test("Sunrise is close to expected time", arguments: cities)
func sunrise(for city: City) throws {
let solar = Solar(for: Self.testDate, coordinate: city.coordinate)

let sunrise = try #require(solar?.sunrise, "Sunrise cannot be generated for city \(city.name)")

#expect(abs(sunrise.timeIntervalSince1970 - city.sunrise.timeIntervalSince1970) <= Self.testAccuracy, "\(city.name): \(sunrise) not close to \(city.sunrise)")
}

func testSunrise_isNil_whenNoSunriseOccurs() {
let solar = Solar(for: testDate, coordinate: CLLocationCoordinate2D(latitude: 78.2186, longitude: 15.64007)) // Location: Longyearbyen
XCTAssertNotNil(solar)
XCTAssertNil(solar?.sunrise)

@Test("Sunrise is nil when no sunrise occurs")
func sunriseIsNilWhenNoSunriseOccurs() {
let solar = Solar(for: Self.testDate, coordinate: CLLocationCoordinate2D(latitude: 78.2186, longitude: 15.64007)) // Location: Longyearbyen
#expect(solar != nil)
#expect(solar?.sunrise == nil)
}

func testSunset() {
for city in cities {

let solar = Solar(for: testDate, coordinate: city.coordinate)

guard
let sunset = solar?.sunset
else {
XCTFail("Sunset cannot be generated for city \(city.name)")
return
}

XCTAssertEqual(sunset.timeIntervalSince1970, city.sunset.timeIntervalSince1970, accuracy: testAccuracy, "\(city.name): \(sunset) not close to \(city.sunset)")
}

@Test("Sunset is close to expected time", arguments: cities)
func sunset(for city: City) throws {
let solar = Solar(for: Self.testDate, coordinate: city.coordinate)

let sunset = try #require(solar?.sunset, "Sunset cannot be generated for city \(city.name)")

#expect(abs(sunset.timeIntervalSince1970 - city.sunset.timeIntervalSince1970) <= Self.testAccuracy, "\(city.name): \(sunset) not close to \(city.sunset)")
}
func testSunset_isNil_whenNoSunsetOccurs() {
let solar = Solar(for: testDate, coordinate: CLLocationCoordinate2D(latitude: 78.2186, longitude: 15.64007)) // Location: Longyearbyen
XCTAssertNotNil(solar)
XCTAssertNil(solar?.sunset)

@Test("Sunset is nil when no sunset occurs")
func sunsetIsNilWhenNoSunsetOccurs() {
let solar = Solar(for: Self.testDate, coordinate: CLLocationCoordinate2D(latitude: 78.2186, longitude: 15.64007)) // Location: Longyearbyen
#expect(solar != nil)
#expect(solar?.sunset == nil)
}

func testIsDayTime_isTrue_betweenSunriseAndSunset() {

@Test("isDaytime is true between sunrise and sunset")
func isDaytimeIsTrueBetweenSunriseAndSunset() throws {
let daytime = Date(timeIntervalSince1970: 1486641600) // noon
let city = cities.first(where: { $0.name == "London" })!

guard
let solar = Solar(for: daytime, coordinate: city.coordinate)
else {
XCTFail("Cannot get solar")
return
}

XCTAssertTrue(solar.isDaytime, "isDaytime is false for date: \(daytime) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
XCTAssertFalse(solar.isNighttime, "isNighttime is true for date: \(daytime) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
let city = try #require(Self.cities.first(where: { $0.name == "London" }))

let solar = try #require(Solar(for: daytime, coordinate: city.coordinate))

#expect(solar.isDaytime, "isDaytime is false for date: \(daytime) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
#expect(!solar.isNighttime, "isNighttime is true for date: \(daytime) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
}

func testIsDayTime_isTrue_exactlyAtSunrise() {

@Test("isDaytime is true exactly at sunrise")
func isDaytimeIsTrueExactlyAtSunrise() throws {
let sunrise = Date(timeIntervalSince1970: 1486625181)
let city = cities.first(where: { $0.name == "London" })!

guard
let solar = Solar(for: sunrise, coordinate: city.coordinate)
else {
XCTFail("Cannot get solar")
return
}

XCTAssertTrue(solar.isDaytime, "isDaytime is false for date: \(sunrise) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
XCTAssertFalse(solar.isNighttime, "isNighttime is true for date: \(sunrise) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
let city = try #require(Self.cities.first(where: { $0.name == "London" }))

let solar = try #require(Solar(for: sunrise, coordinate: city.coordinate))

#expect(solar.isDaytime, "isDaytime is false for date: \(sunrise) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
#expect(!solar.isNighttime, "isNighttime is true for date: \(sunrise) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
}

func testIsDayTime_isFalse_exactlyAtSunset() {

@Test("isDaytime is false exactly at sunset")
func isDaytimeIsFalseExactlyAtSunset() throws {
let sunset = Date(timeIntervalSince1970: 1486659846)
let city = cities.first(where: { $0.name == "London" })!

guard
let solar = Solar(for: sunset, coordinate: city.coordinate)
else {
XCTFail("Cannot get solar")
return
}

XCTAssertFalse(solar.isDaytime, "isDaytime is false for date: \(sunset) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
XCTAssertTrue(solar.isNighttime, "isNighttime is true for date: \(sunset) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
let city = try #require(Self.cities.first(where: { $0.name == "London" }))

let solar = try #require(Solar(for: sunset, coordinate: city.coordinate))

#expect(!solar.isDaytime, "isDaytime is false for date: \(sunset) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
#expect(solar.isNighttime, "isNighttime is true for date: \(sunset) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
}

func testIsDayTime_isFalse_beforeSunrise() {

@Test("isDaytime is false before sunrise")
func isDaytimeIsFalseBeforeSunrise() throws {
let beforeSunrise = Date(timeIntervalSince1970: 1486624980)
let city = cities.first(where: { $0.name == "London" })!

guard
let solar = Solar(for: beforeSunrise, coordinate: city.coordinate)
else {
XCTFail("Cannot get solar")
return
}

XCTAssertFalse(solar.isDaytime, "isDaytime is true for date: \(beforeSunrise) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
XCTAssertTrue(solar.isNighttime, "isNighttime is false for date: \(beforeSunrise) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
let city = try #require(Self.cities.first(where: { $0.name == "London" }))

let solar = try #require(Solar(for: beforeSunrise, coordinate: city.coordinate))

#expect(!solar.isDaytime, "isDaytime is true for date: \(beforeSunrise) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
#expect(solar.isNighttime, "isNighttime is false for date: \(beforeSunrise) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
}

func testIsDayTime_isFalse_afterSunset() {

@Test("isDaytime is false after sunset")
func isDaytimeIsFalseAfterSunset() throws {
let afterSunset = Date(timeIntervalSince1970: 1486659960)
let city = cities.first(where: { $0.name == "London" })!

guard
let solar = Solar(for: afterSunset, coordinate: city.coordinate)
else {
XCTFail("Cannot get solar")
return
}

XCTAssertFalse(solar.isDaytime, "isDaytime is true for date: \(afterSunset) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
XCTAssertTrue(solar.isNighttime, "isNighttime is false for date: \(afterSunset) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
let city = try #require(Self.cities.first(where: { $0.name == "London" }))

let solar = try #require(Solar(for: afterSunset, coordinate: city.coordinate))

#expect(!solar.isDaytime, "isDaytime is true for date: \(afterSunset) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
#expect(solar.isNighttime, "isNighttime is false for date: \(afterSunset) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)")
}

func testSolar_ShouldReturnNilOnInit_GivenInvalidcoordinate() {
@Test("Solar init returns nil given an invalid coordinate")
func solarInitReturnsNilGivenInvalidCoordinate() {
let invalidCoordinate1 = CLLocationCoordinate2D(latitude: -100, longitude: 0)
XCTAssertFalse(CLLocationCoordinate2DIsValid(invalidCoordinate1))
let solar1 = Solar(for: testDate, coordinate: invalidCoordinate1)
XCTAssertNil(solar1)

#expect(!CLLocationCoordinate2DIsValid(invalidCoordinate1))
#expect(Solar(for: Self.testDate, coordinate: invalidCoordinate1) == nil)

let invalidCoordinate2 = CLLocationCoordinate2D(latitude: 180, longitude: 190)
XCTAssertFalse(CLLocationCoordinate2DIsValid(invalidCoordinate2))
let solar2 = Solar(for: testDate, coordinate: invalidCoordinate2)
XCTAssertNil(solar2)
#expect(!CLLocationCoordinate2DIsValid(invalidCoordinate2))
#expect(Solar(for: Self.testDate, coordinate: invalidCoordinate2) == nil)
}
}
Loading