Skip to content

Commit

Permalink
Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
PJ Fechner authored and PJ Fechner committed Jan 9, 2025
1 parent 79d9274 commit 89b6187
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 37 deletions.
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/Quick/Nimble.git", .upToNextMajor(from: "13.0.0")),
.package(url: "https://github.com/Quick/Quick.git", .upToNextMajor(from: "7.6.0")),
// .package(url: "https://github.com/swiftlang/swift-testing.git", .upToNextMajor(from: "6.0.0")),
// swiftlint is kinda big to pull in and build right now...maybe later
// .package(url: "https://github.com/realm/SwiftLint.git", .upToNextMajor(from: "0.52.0")),
.package(url: "https://github.com/swiftlang/swift-syntax.git", "508.0.0"..."600.0.1"),
Expand All @@ -37,11 +38,13 @@ let package = Package(
targets: [
.target(
name: "CodableWrappers",
dependencies: ["CodableWrapperMacros"],
dependencies: ["CodableWrapperMacros",
// .product(name: "Testing", package: "swift-testing"),
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]),
// plugins: [.plugin(name: "SwiftLintPlugin", package: "SwiftLint")]),
// plugins: [.plugin(name: "SwiftLintPlugin", package: "SwifutLint")]),

.testTarget(
name: "CodableWrappersTests",
Expand Down
22 changes: 20 additions & 2 deletions Tests/CodableWrappersTests/Decoder/DecodingTestSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ import Foundation
import Quick
import Nimble

protocol CodingTests: DecodingTests, EncodingTests { }
extension CodingTests {

static var emptyJSON: String { "{\n\n}" }
static var emptyPList: String { """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>
"""

Check warning on line 23 in Tests/CodableWrappersTests/Decoder/DecodingTestSpec.swift

View check run for this annotation

Codecov / codecov/patch

Tests/CodableWrappersTests/Decoder/DecodingTestSpec.swift#L16-L23

Added lines #L16 - L23 were not covered by tests
}
}

protocol CodingTestSpec {

}
Expand All @@ -26,12 +40,16 @@ extension CodingTestSpec {
}
}

protocol DecodingTestSpec: CodingTestSpec {
protocol DecodingTests {
static var jsonDecoder: JSONDecoder { get }
static var plistDecoder: PropertyListDecoder { get }
}

extension DecodingTestSpec {
protocol DecodingTestSpec: DecodingTests, CodingTestSpec {

}

extension DecodingTests {
static var jsonDecoder: JSONDecoder {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
Expand Down
7 changes: 4 additions & 3 deletions Tests/CodableWrappersTests/Encoder/EncodingTestSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import Foundation
import Quick
import Nimble

protocol EncodingTestSpec: CodingTestSpec {
protocol EncodingTests {
static var jsonEncoder: JSONEncoder { get }
static var plistEncoder: PropertyListEncoder { get }
}

extension EncodingTestSpec {
protocol EncodingTestSpec: EncodingTests, CodingTestSpec { }

extension EncodingTests {
static var jsonEncoder: JSONEncoder {
let encoder = JSONEncoder()
// if #available(OSX 10.13, *) {
Expand Down
49 changes: 20 additions & 29 deletions Tests/CodableWrappersTests/PartialImplementationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,27 @@

import CodableWrappers
import Foundation
import Quick
import Nimble
//import Quick
//import Nimble

class CompositionTests: QuickSpec, DecodingTestSpec, EncodingTestSpec {
override class func spec() {
describe("StaticCoder") {
context("OnlyCustomDecoding") {
it("EncodesWithDefault") {
let currentDate = Date()
let encodingModel = DecodingModel(time: currentDate)
let encoded = try! self.jsonEncoder.encode(encodingModel)
expect {_ = try self.jsonDecoder.decode(DecodingModel.self, from: encoded)}.toNot(throwError())
let decoded = try? self.jsonDecoder.decode(DecodingModel.self, from: encoded)
expect(decoded).toNot(beNil())
// This means it was decoded using the SecondsSince1970DateDecoding, but encoded using the default
expect(decoded?.time.timeIntervalSince1970) == currentDate.timeIntervalSinceReferenceDate
}
}
context("OnlyCustomEncoding") {
it("DecodesWithDefault") {
let currentDate = Date()
let encodingModel = EncodingModel(time: currentDate)
let encoded = try! self.jsonEncoder.encode(encodingModel)
expect {_ = try self.jsonDecoder.decode(EncodingModel.self, from: encoded)}.toNot(throwError())
let decoded = try? self.jsonDecoder.decode(EncodingModel.self, from: encoded)
expect(decoded).toNot(beNil())
// This means it was decoded using the SecondsSince1970DateDecoding, but encoded using the default
expect(decoded?.time.timeIntervalSinceReferenceDate) == currentDate.timeIntervalSince1970
}
}
import Testing

struct CompositionTest {
struct StaticCoder: CodingTests {
@Test func customDecodingEncodesWithDefaults() async throws {
let currentDate = Date()
let encodingModel = DecodingModel(time: currentDate)
let encoded = try Self.jsonEncoder.encode(encodingModel)
let decoded = try Self.jsonDecoder.decode(DecodingModel.self, from: encoded)
#expect(decoded.time.timeIntervalSince1970 == currentDate.timeIntervalSinceReferenceDate)
}

@Test func customEncodingDecodesWithDefaults() async throws {
let currentDate = Date()
let encodingModel = EncodingModel(time: currentDate)
let encoded = try Self.jsonEncoder.encode(encodingModel)
let decoded = try Self.jsonDecoder.decode(EncodingModel.self, from: encoded)
#expect(decoded.time.timeIntervalSinceReferenceDate == currentDate.timeIntervalSince1970)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let allTestClasses = [
EmptyDefaultsEncodingTests.self,
NullEncodingTests.self,
OptionalEncodingTests.self,
CompositionTests.self,
// CompositionTests.self,
PartialImplementationTests.self,
]
#if os(Linux)
Expand Down

0 comments on commit 89b6187

Please sign in to comment.