Skip to content

Commit

Permalink
goodbye XCTest, hello Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gereons committed Oct 11, 2024
1 parent 8c574f2 commit b6575ca
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 44 deletions.
18 changes: 9 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
{
"originHash" : "4e766abbb273cc20334bf5fbd1dbc1772d169cb913232c62e7331d7fc90f1f6d",
"originHash" : "ed02909f0137dd429f57e2e37253c8c3bcce6ef0c87ac77d9aa375f6bc18da33",
"pins" : [
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "0fbc8848e389af3bb55c182bc19ca9d5dc2f255b",
"version" : "1.4.0"
"revision" : "41982a3656a71c768319979febd796c6fd111d5c",
"version" : "1.5.0"
}
},
{
"identity" : "swift-custom-dump",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-custom-dump",
"state" : {
"revision" : "f01efb26f3a192a0e88dcdb7c3c391ec2fc25d9c",
"version" : "1.3.0"
"revision" : "82645ec760917961cfa08c9c0c7104a57a0fa4b1",
"version" : "1.3.3"
}
},
{
"identity" : "swiftlintplugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/lukepistrol/SwiftLintPlugin",
"state" : {
"revision" : "a0f7b12c7be228592d924f29f654ebbd924ac9c5",
"version" : "0.55.1"
"revision" : "bea71d23db993c58934ee704f798a66d7b8cb626",
"version" : "0.57.0"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "6f30bdba373bbd7fbfe241dddd732651f2fbd1e2",
"version" : "1.1.2"
"revision" : "770f990d3e4eececb57ac04a6076e22f8c97daeb",
"version" : "1.4.2"
}
}
],
Expand Down
10 changes: 6 additions & 4 deletions Tests/ArrayOfArrayTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
// Copyright © 2024 onvista media GmbH. All rights reserved.
//

import Foundation
import CustomDump
import XCTest
import Testing
@testable import modelgen

// test 2d array items

final class ArrayOfArrayTest: XCTestCase {
@Suite("Array of Array test")
struct ArrayOfArrayTest {
private let spec = """
{
"info": {
Expand Down Expand Up @@ -105,11 +106,12 @@ public struct ArrayItem: Codable {
extension ArrayItem: ParentProtocol {}
"""#

@Test("child class table")
func testChildClassTable() throws {
let spec = try JSONDecoder().decode(OpenApiSpec.self, from: spec.data(using: .utf8)!)
let generator = Generator(spec: spec, config: .test)
generator.generate(modelName: "ArrayItem")

XCTAssertNoDifference(String(generator.buffer.dropLast(1)), expectedResult)
expectNoDifference(String(generator.buffer.dropLast(1)), expectedResult)
}
}
9 changes: 6 additions & 3 deletions Tests/EnumTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
// Copyright © 2024 onvista media GmbH. All rights reserved.
//

import Foundation
import CustomDump
import XCTest
import Testing
@testable import modelgen

final class EnumTest: XCTestCase {
@Suite("EnumTests")
struct EnumTest {
private let spec = """
{
"info": {
Expand Down Expand Up @@ -41,11 +43,12 @@ final class EnumTest: XCTestCase {
}
"""

@Test("Enum")
func testEnum() throws {
let spec = try JSONDecoder().decode(OpenApiSpec.self, from: spec.data(using: .utf8)!)
let generator = Generator(spec: spec, config: .test)
generator.generate(modelName: "Enum")
XCTAssertNoDifference(String(generator.buffer.dropLast(1)), expected)
expectNoDifference(String(generator.buffer.dropLast(1)), expected)
}

}
9 changes: 6 additions & 3 deletions Tests/EnumTest2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
// Created by Gereon Steffens on 19.06.24.
//

import Foundation
import CustomDump
import Testing
@testable import modelgen
import XCTest

final class EnumTest2: XCTestCase {
@Suite("Enum Tests 2")
struct EnumTest2 {
private let spec = """
{
"info": {
Expand Down Expand Up @@ -89,10 +91,11 @@ final class EnumTest2: XCTestCase {
}
"""

@Test("test Enum order")
func testEnumOrder() throws {
let spec = try JSONDecoder().decode(OpenApiSpec.self, from: spec.data(using: .utf8)!)
let generator = Generator(spec: spec, config: .test)
generator.generate(modelName: "Enum")
XCTAssertNoDifference(String(generator.buffer.dropLast(1)), expected)
expectNoDifference(String(generator.buffer.dropLast(1)), expected)
}
}
12 changes: 8 additions & 4 deletions Tests/InheritanceTest1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
// Copyright © 2024 onvista media GmbH. All rights reserved.
//

import Foundation
import CustomDump
import XCTest
import Testing
@testable import modelgen

// test inheritance with an enum as the discriminator

final class InheritanceTest1: XCTestCase {
@Suite("Inheritance test 1")
struct InheritanceTest1 {
private let spec = """
{
"info": {
Expand Down Expand Up @@ -138,17 +140,19 @@ public struct Dog: Codable {
extension Dog: AnimalProtocol {}
"""#

@Test("test base class")
func testBaseClass() throws {
let spec = try JSONDecoder().decode(OpenApiSpec.self, from: spec.data(using: .utf8)!)
let generator = Generator(spec: spec, config: .test)
generator.generate(modelName: "Animal")
XCTAssertNoDifference(String(generator.buffer.dropLast(1)), expectedBase)
expectNoDifference(String(generator.buffer.dropLast(1)), expectedBase)
}

@Test("test child class dog")
func testChildClassDog() throws {
let spec = try JSONDecoder().decode(OpenApiSpec.self, from: spec.data(using: .utf8)!)
let generator = Generator(spec: spec, config: .test)
generator.generate(modelName: "Dog")
XCTAssertNoDifference(String(generator.buffer.dropLast(1)), expectedDog)
expectNoDifference(String(generator.buffer.dropLast(1)), expectedDog)
}
}
13 changes: 8 additions & 5 deletions Tests/InheritanceTest2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
// Copyright © 2024 onvista media GmbH. All rights reserved.
//

import Foundation
import CustomDump
import XCTest
import Testing
@testable import modelgen

// test inheritance with a string as the discriminator

final class InheritanceTest2: XCTestCase {
@Suite("Interitance test 2")
struct InheritanceTest2 {
private let spec = """
{
"info": {
Expand Down Expand Up @@ -199,17 +200,19 @@ public struct Dog: Codable {
extension Dog: AnimalProtocol {}
"""#

@Test("test base class")
func testBaseClass() throws {
let spec = try JSONDecoder().decode(OpenApiSpec.self, from: spec.data(using: .utf8)!)
let generator = Generator(spec: spec, config: .test)
generator.generate(modelName: "Animal")
XCTAssertNoDifference(String(generator.buffer.dropLast(1)), expectedBase)
expectNoDifference(String(generator.buffer.dropLast(1)), expectedBase)
}

@Test("test child class dog")
func testChildClassDog() throws {
let spec = try JSONDecoder().decode(OpenApiSpec.self, from: spec.data(using: .utf8)!)
let generator = Generator(spec: spec, config: .test)
generator.generate(modelName: "Dog")
XCTAssertNoDifference(String(generator.buffer.dropLast(1)), expectedDog)
expectNoDifference(String(generator.buffer.dropLast(1)), expectedDog)
}
}
12 changes: 8 additions & 4 deletions Tests/PODTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
// Copyright © 2024 onvista media GmbH. All rights reserved.
//

import Foundation
import CustomDump
import XCTest
import Testing
@testable import modelgen

final class PODTest: XCTestCase {
@Suite("POD test")
struct PODTest {
private let spec = """
{
"info": {
Expand Down Expand Up @@ -187,19 +189,21 @@ final class PODTest: XCTestCase {
}
"""

@Test("test POD")
func testPOD() throws {
let spec = try JSONDecoder().decode(OpenApiSpec.self, from: spec.data(using: .utf8)!)
let generator = Generator(spec: spec, config: .test)
generator.generate(modelName: "POD")
let output = String(generator.buffer.dropLast(1))
XCTAssertNoDifference(output, expected)
expectNoDifference(output, expected)
}

@Test("test POD with defaults")
func testPODWithDefaults() throws {
let spec = try JSONDecoder().decode(OpenApiSpec.self, from: spec.data(using: .utf8)!)
let generator = Generator(spec: spec, config: .init(defaultValues: ["foobar"], skipHeader: true))
generator.generate(modelName: "POD")
let output = String(generator.buffer.dropLast(1))
XCTAssertNoDifference(output, expectedWithDefaults)
expectNoDifference(output, expectedWithDefaults)
}
}
17 changes: 11 additions & 6 deletions Tests/ParseTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
// Created by Gereon Steffens on 03.05.23.
//

import XCTest
import Foundation
import Testing
@testable import modelgen

final class ParseTest: XCTestCase {
@Suite("Parse test")
struct ParseTest {
private let json = """
[
{
Expand Down Expand Up @@ -39,25 +41,28 @@ final class ParseTest: XCTestCase {
]
"""

@Test("parse array")
func testParseArray() throws {
let animals = try JSONDecoder().decode([Animal].self, from: json.data(using: .utf8)!)
XCTAssertEqual(animals.count, 2)
#expect(animals.count == 2)
}

@Test("parse array failure")
func testParseArrayFailure() throws {
do {
_ = try JSONDecoder().decode([Animal].self, from: json2.data(using: .utf8)!)
XCTFail("decoding should fail")
Issue.record("decoding should fail")
} catch DecodingError.typeMismatch {
// expected
} catch {
XCTFail("unexpected error \(error)")
Issue.record("unexpected error \(error)")
}
}

@Test("parse lossy array")
func testParseLossyArray() throws {
let animals = try JSONDecoder().decode(LossyDecodableArray<Animal>.self, from: json2.data(using: .utf8)!).elements
XCTAssertEqual(animals.count, 2)
#expect(animals.count == 2)
}

}
16 changes: 10 additions & 6 deletions Tests/RequestTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
// Created by Gereon Steffens on 03.05.24.
//

import Foundation
import CustomDump
import XCTest
import Testing
@testable import modelgen

final class RequestTest: XCTestCase {
@Suite("Request test")
struct RequestTest {
private let spec = """
{
"info": {
Expand Down Expand Up @@ -233,19 +235,21 @@ public struct GetStatusRequest {
}
"""#

@Test("test request")
func testRequest() throws {
let spec = try JSONDecoder().decode(OpenApiSpec.self, from: spec.data(using: .utf8)!)
let generator = Generator(spec: spec, config: .init(tag: "testTag", skipHeader: true))
let req = try XCTUnwrap(spec.paths?["/status"]?["get"])
let req = try #require(spec.paths?["/status"]?["get"])
generator.generate(path: "/status", method: "GET", request: req)
XCTAssertNoDifference(String(generator.buffer.dropLast(1)), expectedOutput)
expectNoDifference(String(generator.buffer.dropLast(1)), expectedOutput)
}

@Test("test request with defaults")
func testRequestWithDefaults() throws {
let spec = try JSONDecoder().decode(OpenApiSpec.self, from: spec.data(using: .utf8)!)
let generator = Generator(spec: spec, config: .init(defaultValues: ["foo"], tag: "testTag", skipHeader: true))
let req = try XCTUnwrap(spec.paths?["/status"]?["get"])
let req = try #require(spec.paths?["/status"]?["get"])
generator.generate(path: "/status", method: "GET", request: req)
XCTAssertNoDifference(String(generator.buffer.dropLast(1)), expectedOutputWithDefaults)
expectNoDifference(String(generator.buffer.dropLast(1)), expectedOutputWithDefaults)
}
}

0 comments on commit b6575ca

Please sign in to comment.