Skip to content

Commit

Permalink
adds tests for 3rd party models
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Kvyatkovskiy committed Aug 1, 2022
1 parent aaa50ca commit a9e52c7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ let package = Package(
.target(
name: "Autofixtures",
dependencies: []),
.target(name: "ExampleModels",
dependencies: []),
.testTarget(
name: "AutofixturesTests",
dependencies: ["Autofixtures"]),
dependencies: ["Autofixtures","ExampleModels"]),
]
)
11 changes: 0 additions & 11 deletions Sources/Autofixtures/Autofixtures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,3 @@ public extension Decodable {
return copy
}
}


public extension KeyedDecodingContainer {
func decode<C: CaseIterable>(_: C.Type, forKey key: Key) throws -> C where C: Decodable {
let decoder = try superDecoder()
if decoder is FixtureDecoder {
return C.fix
}
return try C.init(from: decoder)
}
}
4 changes: 4 additions & 0 deletions Sources/Autofixtures/FixtureDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ struct FixtureDecoder: Decoder {
}
}

extension Decimal: Override {
static public var fixOverride: Decimal = .init(Double.fix)
}

extension String {
func repeating(_ times: Int) -> String {
let arr = Array(repeating: self, count: times)
Expand Down
19 changes: 19 additions & 0 deletions Sources/ExampleModels/Example.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Foundation

public enum ExampleType: Decodable, CaseIterable {
case one
case two
}

public struct Model: Decodable {
public let type: ExampleType
public let string: String
private let privateProp: ExampleType
public init(type: ExampleType,
string: String,
privateProp: ExampleType) {
self.type = type
self.string = string
self.privateProp = privateProp
}
}
20 changes: 19 additions & 1 deletion Tests/AutofixturesTests/AutofixturesTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Autofixtures
import XCTest
import ExampleModels

enum Enum: String, Decodable, CaseIterable {
case one
Expand All @@ -21,9 +22,22 @@ extension Simple: Override {
static var fixOverride: Simple = .fixDecoded.set(\.string, "NAME")
}

extension ExampleType: Override {}
extension Enum: Override {}

final class AutofixturesTests: XCTestCase {

func testSingleValues() {
func testDecimal() {
XCTAssertEqual(Decimal.fix, Decimal(Double.fix))
}

func test3rdPartyModels() {
let model = Model.fix
XCTAssertEqual(model.type, .one)
XCTAssertEqual(model.string, "FIX")
}

func testSingleValues() throws {
XCTAssertEqual(String.fix, "FIX")
XCTAssertEqual(Bool.fix, false)
XCTAssertEqual(Int.fix, 333)
Expand Down Expand Up @@ -125,6 +139,10 @@ final class AutofixturesTests: XCTestCase {
}
XCTAssertEqual(WithClosure.fix.closure(()), 9)
}

func testThatJSONDecoderWorksAsExpected() {

}
}

/// Closure wrapper for decodable support
Expand Down

0 comments on commit a9e52c7

Please sign in to comment.