Skip to content

Commit

Permalink
Moving protocol conformance and fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drekka committed Nov 20, 2024
1 parent 5d39100 commit feff7a2
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 129 deletions.
File renamed without changes.
7 changes: 7 additions & 0 deletions Sources/Conformance/Day+Comparable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

extension Day: Comparable {
public static func < (lhs: Day, rhs: Day) -> Bool {
lhs.daysSince1970 < rhs.daysSince1970
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Foundation

extension Day: CustomStringConvertible {
public var description: String {
self.formatted()
formatted()
}
}
7 changes: 7 additions & 0 deletions Sources/Conformance/Day+Equatable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

extension Day: Equatable {
public static func == (lhs: Day, rhs: Day) -> Bool {
lhs.daysSince1970 == rhs.daysSince1970
}
}
7 changes: 7 additions & 0 deletions Sources/Conformance/Day+Hashable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

extension Day: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(daysSince1970)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import Foundation
extension Day: Strideable {

public func distance(to other: Day) -> Int {
other.daysSince1970 - self.daysSince1970
other.daysSince1970 - daysSince1970
}

public func advanced(by n: Int) -> Day {
self + n
}
Expand Down
19 changes: 0 additions & 19 deletions Sources/Day+Core.swift

This file was deleted.

36 changes: 36 additions & 0 deletions Tests/Conformance/DayCodableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import DayType
import Foundation
import Testing

extension ProtocolConformanceSuites {

@Suite("Day is Codable")
struct DayCodableTests {

private struct DummyType: Codable {
let abc: Day
}

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

let json = """
{
"abc": 19455
}
"""

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

@Test("Encoding")
func encoding() throws {
let obj = DummyType(abc: Day(daysSince1970: 19455))
let encoder = JSONEncoder()
let encoded = try #require(String(data: encoder.encode(obj), encoding: .utf8))
#expect(encoded == #"{"abc":19455}"#)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,7 @@ import DayType
import Foundation
import Testing

@Suite("Protocol conformance")
struct DayProtocols {

@Test("Hash")
func hash() {
var days: Set = [Day(2020, 01, 11), Day(2020, 01, 12)]

#expect(days.contains(Day(2020, 01, 13)) == false)
#expect(days.contains(Day(2020, 01, 12)) == true)

// Modify and try again.
days.insert(Day(2020, 01, 13))
#expect(days.count == 3)
#expect(days.contains(Day(2020, 01, 13)) == true)
#expect(days.contains(Day(2020, 01, 12)) == true)

// Duplicate check.
days.insert(Day(2020, 01, 11))
#expect(days.count == 3)
}

@Test("Equatble")
func equals() {
#expect(Day(2020, 3, 12) == Day(2020, 3, 12))
#expect(Day(2020, 3, 12) != Day(2001, 1, 5))
}
extension ProtocolConformanceSuites {

@Suite("Comparable")
struct DayComparableTests {
Expand Down
12 changes: 12 additions & 0 deletions Tests/Conformance/DayCustomStringConvertableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import DayType
import Foundation
import Testing

extension ProtocolConformanceSuites {

@Test("Day custom string convertable")
func description() {
let date = DateComponents(calendar: .current, year: 2001, month: 2, day: 3).date!
#expect(Day(2001, 2, 3).description == date.formatted(date: .abbreviated, time: .omitted))
}
}
12 changes: 12 additions & 0 deletions Tests/Conformance/DayEquatableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import DayType
import Foundation
import Testing

extension ProtocolConformanceSuites {

@Test("Equatable")
func equals() {
#expect(Day(2020, 3, 12) == Day(2020, 3, 12))
#expect(Day(2020, 3, 12) != Day(2001, 1, 5))
}
}
24 changes: 24 additions & 0 deletions Tests/Conformance/DayHashableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import DayType
import Foundation
import Testing

extension ProtocolConformanceSuites {

@Test("Hashable")
func hashable() {
var days: Set = [Day(2020, 01, 11), Day(2020, 01, 12)]

#expect(days.contains(Day(2020, 01, 13)) == false)
#expect(days.contains(Day(2020, 01, 12)) == true)

// Modify and try again.
days.insert(Day(2020, 01, 13))
#expect(days.count == 3)
#expect(days.contains(Day(2020, 01, 13)) == true)
#expect(days.contains(Day(2020, 01, 12)) == true)

// Duplicate check.
days.insert(Day(2020, 01, 11))
#expect(days.count == 3)
}
}
42 changes: 42 additions & 0 deletions Tests/Conformance/DayStrideableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import DayType
import Foundation
import Testing

extension ProtocolConformanceSuites {

@Suite("Day Stridable tests")
struct DayStrideableTests {

@Test("In a for loop with an open range")
func forEachOpenRange() {
var days: [Day] = []
for day in Day(2000, 1, 1) ... Day(2000, 1, 5) {
days.append(day)
}
#expect(days == [Day(2000, 1, 1), Day(2000, 1, 2), Day(2000, 1, 3), Day(2000, 1, 4), Day(2000, 1, 5)])
}

@Test("In a for loop with a half open range")
func forEachHalfOpenRange() {
var days: [Day] = []
for day in Day(2000, 1, 1) ..< Day(2000, 1, 5) {
days.append(day)
}
#expect(days == [Day(2000, 1, 1), Day(2000, 1, 2), Day(2000, 1, 3), Day(2000, 1, 4)])
}

@Test("With the stride function")
func forEachViaStrideFunction() {
var days: [Day] = []
for day in stride(from: Day(2000, 1, 1), to: Day(2000, 1, 5), by: 2) {
days.append(day)
}
#expect(days == [Day(2000, 1, 1), Day(2000, 1, 3)])
}

@Test("Distance function")
func distanceTo() {
#expect(Day(2020, 3, 6).distance(to: Day(2020, 3, 12)) == 6)
}
}
}
6 changes: 6 additions & 0 deletions Tests/Conformance/ProtocolConformanceSuites.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Testing

/// Groups up property test suites.
@Suite("Protocol conformance", .tags(.ProtocolConformance))
struct ProtocolConformanceSuites {}

33 changes: 0 additions & 33 deletions Tests/DayCodableTests.swift

This file was deleted.

9 changes: 0 additions & 9 deletions Tests/DayCustomStringConvertableTests.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Tests/DayOperationsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct DayOperationTests {

@Test("-")
func minus() {
#expect((Day(daysSince1970: 19445 - 5).daysSince1970) == 19440)
#expect((Day(daysSince1970: 19445) - 5).daysSince1970 == 19440)
}

@Test("+=")
Expand Down
38 changes: 0 additions & 38 deletions Tests/DayStrideableTests.swift

This file was deleted.

1 change: 1 addition & 0 deletions Tests/Tags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import Testing

extension Tag {
@Tag static var PropertyWrapper: Self
@Tag static var ProtocolConformance: Self
}

0 comments on commit feff7a2

Please sign in to comment.