Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
drekka committed Nov 18, 2024
1 parent 91fecad commit 891829e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.7
// swift-tools-version:5.10

import PackageDescription

Expand Down
21 changes: 12 additions & 9 deletions Tests/DayCodableTests.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import DayType
import Nimble
import XCTest
import Foundation
import Testing

class DayCodableTests: XCTestCase {
@Suite("Day is Codable")
struct DayCodableTests {

struct DummyType: Codable {
private struct DummyType: Codable {
let abc: Day
}

func testBaseDecoding() throws {
@Test("Decoding")
func decoding() throws {

let json = """
{
Expand All @@ -18,13 +20,14 @@ class DayCodableTests: XCTestCase {

let decoder = JSONDecoder()
let day = try decoder.decode(DummyType.self, from: json.data(using: .utf8)!)
expect(day.abc.daysSince1970) == 19455
#expect(day.abc.daysSince1970 == 19455)
}

func testEncoding() throws {
@Test("Encoding")
func encoding() throws {
let obj = DummyType(abc: Day(daysSince1970: 19455))
let encoder = JSONEncoder()
let encoded = try String(data: encoder.encode(obj), encoding: .utf8)
expect(encoded).to(contain("\"abc\":19455"))
let encoded = try #require(String(data: encoder.encode(obj), encoding: .utf8))
#expect(encoded == #"{"abc":19455}"#)
}
}
41 changes: 22 additions & 19 deletions Tests/DayConversionTests.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import XCTest
import DayType
import Nimble
import Foundation
import Testing

class DayConversionTests: XCTestCase {
@Suite("Day to Date conversions")
struct DayConversionTests {

func testDateInCalendarCurrent() {
let day = Day(2000,1,1)
@Test("To a Date")
func toDate() {
let day = Day(2000, 1, 1)
let date = day.date()
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour], from: date)
expect(dateComponents.year) == 2000
expect(dateComponents.month) == 1
expect(dateComponents.day) == 1
expect(dateComponents.hour) == 0
#expect(dateComponents.year == 2000)
#expect(dateComponents.month == 1)
#expect(dateComponents.day == 1)
#expect(dateComponents.hour == 0)
}

@Test("Time zones are correct")
func testDateInTimeZone() {

let day = Day(2000,1,1)
let day = Day(2000, 1, 1)

// Get the date of the day in the Melbourne time zone.
let melbourne = TimeZone(secondsFromGMT: 11 * 3600)!
Expand All @@ -27,20 +30,20 @@ class DayConversionTests: XCTestCase {
let gmtDate = day.date(timeZone: gmt)

// We expect that the two points in time are different.
expect(melbourneDate.timeIntervalSince1970) != gmtDate.timeIntervalSince1970
#expect(melbourneDate.timeIntervalSince1970 != gmtDate.timeIntervalSince1970)

// Check that the Melbourne date is midnight in the Melbourne time zone.
let melbourneComponentsInMelbourne = Calendar.current.dateComponents(in: melbourne, from: melbourneDate)
expect(melbourneComponentsInMelbourne.year) == 2000
expect(melbourneComponentsInMelbourne.month) == 1
expect(melbourneComponentsInMelbourne.day) == 1
expect(melbourneComponentsInMelbourne.hour) == 0
#expect(melbourneComponentsInMelbourne.year == 2000)
#expect(melbourneComponentsInMelbourne.month == 1)
#expect(melbourneComponentsInMelbourne.day == 1)
#expect(melbourneComponentsInMelbourne.hour == 0)

// Check that the GMT date is midnight in the GMT time zone.
let gmtComponentsInGMT = Calendar.current.dateComponents(in: gmt, from: gmtDate)
expect(gmtComponentsInGMT.year) == 2000
expect(gmtComponentsInGMT.month) == 1
expect(gmtComponentsInGMT.day) == 1
expect(gmtComponentsInGMT.hour) == 0
#expect(gmtComponentsInGMT.year == 2000)
#expect(gmtComponentsInGMT.month == 1)
#expect(gmtComponentsInGMT.day == 1)
#expect(gmtComponentsInGMT.hour == 0)
}
}
48 changes: 21 additions & 27 deletions Tests/Property wrappers/DateStringTests.swift
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
import DayType
import Nimble
import XCTest
import Foundation
import Testing

// MARK: - ISO8601 Decoding

private struct DateStringContainer<Configurator>: Codable where Configurator: DateStringConfigurator {
private struct DayContainer<Configurator>: Codable where Configurator: DateStringConfigurator {
@DateString<Day, Configurator> var d1: Day
init(d1: Day) {
self.d1 = d1
}
}

private struct DateStringOptionalContainer<Configurator>: Codable where Configurator: DateStringConfigurator {
private struct OptionalDayContainer<Configurator>: Codable where Configurator: DateStringConfigurator {
@DateString<Day?, Configurator> var d1: Day?
init(d1: Day?) {
self.d1 = d1
}
}

class DateStringTests: XCTestCase {

func testDecodingISO8601DateString() throws {
struct DateStringTests {

@Test("Decoding am ISO8601 string")
func decodingISO8601DateString() throws {
let json = #"{"d1": "2012-02-01"}"#
let result = try JSONDecoder().decode(DateStringContainer<DateStringConfig.ISO>.self, from: json.data(using: .utf8)!)
expect(result.d1) == Day(2012, 02, 01)
let result = try JSONDecoder().decode(DayContainer<DateStringConfig.ISO>.self, from: json.data(using: .utf8)!)
#expect(result.d1 == Day(2012, 02, 01))
}

func testDecodingAustralianDateStrings() throws {
let json = #"{"d1": "01/02/2012"}"#
let result = try JSONDecoder().decode(DateStringContainer<DateStringConfig.DMY>.self, from: json.data(using: .utf8)!)
let result = try JSONDecoder().decode(DayContainer<DateStringConfig.DMY>.self, from: json.data(using: .utf8)!)
expect(result.d1) == Day(2012, 02, 01)
}

func testDecodingAmericanDateStrings() throws {
let json = #"{"d1": "02/01/2012"}"#
let result = try JSONDecoder().decode(DateStringContainer<DateStringConfig.MDY>.self, from: json.data(using: .utf8)!)
let result = try JSONDecoder().decode(DayContainer<DateStringConfig.MDY>.self, from: json.data(using: .utf8)!)
expect(result.d1) == Day(2012, 02, 01)
}
}
Expand All @@ -43,35 +37,35 @@ class CodableAsDateStroingOptionalTests: XCTestCase {

func testDecodingISO8601DateString() throws {
let json = #"{"d1": "2012-02-01"}"#
let result = try JSONDecoder().decode(DateStringOptionalContainer<DateStringConfig.ISO>.self, from: json.data(using: .utf8)!)
let result = try JSONDecoder().decode(OptionalDayContainer<DateStringConfig.ISO>.self, from: json.data(using: .utf8)!)
expect(result.d1) == Day(2012, 02, 01)
}

func testDecodingAustralianDateStrings() throws {
let json = #"{"d1": "01/02/2012"}"#
let result = try JSONDecoder().decode(DateStringOptionalContainer<DateStringConfig.DMY>.self, from: json.data(using: .utf8)!)
let result = try JSONDecoder().decode(OptionalDayContainer<DateStringConfig.DMY>.self, from: json.data(using: .utf8)!)
expect(result.d1) == Day(2012, 02, 01)
}

func testDecodingAmericanDateStrings() throws {
let json = #"{"d1": "02/01/2012"}"#
let result = try JSONDecoder().decode(DateStringOptionalContainer<DateStringConfig.MDY>.self, from: json.data(using: .utf8)!)
let result = try JSONDecoder().decode(OptionalDayContainer<DateStringConfig.MDY>.self, from: json.data(using: .utf8)!)
expect(result.d1) == Day(2012, 02, 01)
}

func testDecodingNilAustralianDateStrings() throws {
let json = #"{"d1": null}"#
let result = try JSONDecoder().decode(DateStringOptionalContainer<DateStringConfig.DMY>.self, from: json.data(using: .utf8)!)
let result = try JSONDecoder().decode(OptionalDayContainer<DateStringConfig.DMY>.self, from: json.data(using: .utf8)!)
expect(result.d1) == nil
}

func testDecodingInvalidAustralianDateStringsThrows() throws {

let json = #"{"d1": "xxx"}"#
let decoder = JSONDecoder()
expect(try decoder.decode(DateStringOptionalContainer<DateStringConfig.DMY>.self, from: json.data(using: .utf8)!))
expect(try decoder.decode(OptionalDayContainer<DateStringConfig.DMY>.self, from: json.data(using: .utf8)!))
.to(throwError { error in
guard case DecodingError.dataCorrupted(let context) = error else {
guard case let DecodingError.dataCorrupted(context) = error else {
fail("Incorrect error \(error)")
return
}
Expand All @@ -84,7 +78,7 @@ class CodableAsDateStroingOptionalTests: XCTestCase {
class CodableAsDateStringEncodingTests: XCTestCase {

func testEncoding() throws {
let instance = DateStringContainer<DateStringConfig.DMY>(d1: Day(2012, 02, 01))
let instance = DayContainer<DateStringConfig.DMY>(d1: Day(2012, 02, 01))
let encoder = JSONEncoder()
encoder.outputFormatting = .withoutEscapingSlashes
let result = try encoder.encode(instance)
Expand All @@ -95,15 +89,15 @@ class CodableAsDateStringEncodingTests: XCTestCase {
class CodableAsDateStringOptionalTests: XCTestCase {

func testEncoding() throws {
let instance = DateStringOptionalContainer<DateStringConfig.DMY>(d1: Day(2012, 02, 01))
let instance = OptionalDayContainer<DateStringConfig.DMY>(d1: Day(2012, 02, 01))
let encoder = JSONEncoder()
encoder.outputFormatting = .withoutEscapingSlashes
let result = try encoder.encode(instance)
expect(String(data: result, encoding: .utf8)!) == #"{"d1":"01/02/2012"}"#
}

func testEncodingNil() throws {
let instance = DateStringOptionalContainer<DateStringConfig.DMY>(d1: nil)
let instance = OptionalDayContainer<DateStringConfig.DMY>(d1: nil)
let encoder = JSONEncoder()
encoder.outputFormatting = .withoutEscapingSlashes
let result = try encoder.encode(instance)
Expand Down
6 changes: 6 additions & 0 deletions Tests/Tags.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Testing

extension Tag {
@Tag static var PropertyWrapper: Self
}

0 comments on commit 891829e

Please sign in to comment.