Skip to content

Commit

Permalink
Refactor tests and file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
surpher committed Dec 17, 2024
1 parent 63c93eb commit 2a046d4
Show file tree
Hide file tree
Showing 28 changed files with 941 additions and 340 deletions.
64 changes: 54 additions & 10 deletions PactSwiftMockServer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@
ADE647512D11285C00BE9AB3 /* DefaultPactFFIProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE647502D11285400BE9AB3 /* DefaultPactFFIProvider.swift */; };
ADE647522D11285C00BE9AB3 /* DefaultPactFFIProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE647502D11285400BE9AB3 /* DefaultPactFFIProvider.swift */; };
ADE647532D11285C00BE9AB3 /* DefaultPactFFIProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE647502D11285400BE9AB3 /* DefaultPactFFIProvider.swift */; };
ADE647552D11439900BE9AB3 /* PactVerificationFailureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE647542D11439300BE9AB3 /* PactVerificationFailureTests.swift */; };
ADE647562D11439900BE9AB3 /* PactVerificationFailureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE647542D11439300BE9AB3 /* PactVerificationFailureTests.swift */; };
ADE6475F2D1158C300BE9AB3 /* ProviderVerifying.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE6475E2D1158C000BE9AB3 /* ProviderVerifying.swift */; };
ADE647602D1158C300BE9AB3 /* ProviderVerifying.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE6475E2D1158C000BE9AB3 /* ProviderVerifying.swift */; };
ADE647612D1158C300BE9AB3 /* ProviderVerifying.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE6475E2D1158C000BE9AB3 /* ProviderVerifying.swift */; };
ADE647642D115A2000BE9AB3 /* MockPactFFIProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE647632D115A1800BE9AB3 /* MockPactFFIProvider.swift */; };
ADE647652D115A2000BE9AB3 /* MockPactFFIProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE647632D115A1800BE9AB3 /* MockPactFFIProvider.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -170,6 +177,9 @@
ADE647452D1121D100BE9AB3 /* ProviderVerificationErrorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProviderVerificationErrorTests.swift; sourceTree = "<group>"; };
ADE6474C2D1125EC00BE9AB3 /* PactFFIProviding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PactFFIProviding.swift; sourceTree = "<group>"; };
ADE647502D11285400BE9AB3 /* DefaultPactFFIProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultPactFFIProvider.swift; sourceTree = "<group>"; };
ADE647542D11439300BE9AB3 /* PactVerificationFailureTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PactVerificationFailureTests.swift; sourceTree = "<group>"; };
ADE6475E2D1158C000BE9AB3 /* ProviderVerifying.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProviderVerifying.swift; sourceTree = "<group>"; };
ADE647632D115A1800BE9AB3 /* MockPactFFIProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockPactFFIProvider.swift; sourceTree = "<group>"; };
ADF2E5832674623F0029507D /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -285,15 +295,15 @@
AD15982F2648E31A007CFAA5 /* Sources */ = {
isa = PBXGroup;
children = (
ADE647502D11285400BE9AB3 /* DefaultPactFFIProvider.swift */,
ADE6474C2D1125EC00BE9AB3 /* PactFFIProviding.swift */,
A7840F74294AF1D200CF22EF /* Generate.swift */,
AD1598382648E690007CFAA5 /* Headers */,
AD1598302648E32F007CFAA5 /* MockServer.swift */,
AD1598392648E6DB007CFAA5 /* Model */,
A7840F42294A923000CF22EF /* PactBuilder.swift */,
ADE6475D2D1158B000BE9AB3 /* Protocols */,
ADE6475C2D11589600BE9AB3 /* ProviderVerification */,
ADE647622D11597000BE9AB3 /* Services */,
AD957F3428A23AF300860AD1 /* Toolbox */,
ADE2550F26CE335400FA21A9 /* Verifier.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -336,8 +346,9 @@
AD1598512648F2E1007CFAA5 /* MockServerTests.swift */,
A7840F3F2949AA4200CF22EF /* PactBuilderTests.swift */,
A7840F85294C2ED800CF22EF /* PactTests.swift */,
ADE647542D11439300BE9AB3 /* PactVerificationFailureTests.swift */,
ADE647452D1121D100BE9AB3 /* ProviderVerificationErrorTests.swift */,
ADB659BC2D069CD10049A39C /* Utils */,
ADB659BC2D069CD10049A39C /* Support */,
ADC6C61226D0AFBD00844000 /* VerifierTests.swift */,
);
path = Tests;
Expand All @@ -355,14 +366,11 @@
AD1598392648E6DB007CFAA5 /* Model */ = {
isa = PBXGroup;
children = (
A7F18576296BE177003AE3F2 /* BodyBuilder.swift */,
A7F18572296BE152003AE3F2 /* HeaderBuilder.swift */,
A743EC422946EF1D00EE315D /* Interaction.swift */,
A743EC3E2946E8C700EE315D /* Pact.swift */,
A7F18595296CED58003AE3F2 /* Logging.swift */,
A743EC3E2946E8C700EE315D /* Pact.swift */,
ADBEF2FC2648FCF200486C4A /* PactVerificationFailure.swift */,
ADC15DAF26CE98140010D900 /* ProviderVerificationError.swift */,
A7F1857A296BE196003AE3F2 /* QueryBuilder.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -391,12 +399,41 @@
path = Toolbox;
sourceTree = "<group>";
};
ADB659BC2D069CD10049A39C /* Utils */ = {
ADB659BC2D069CD10049A39C /* Support */ = {
isa = PBXGroup;
children = (
ADE647632D115A1800BE9AB3 /* MockPactFFIProvider.swift */,
ADB659BD2D069CD60049A39C /* TestStatusCode.swift */,
);
path = Utils;
path = Support;
sourceTree = "<group>";
};
ADE6475C2D11589600BE9AB3 /* ProviderVerification */ = {
isa = PBXGroup;
children = (
ADE2550F26CE335400FA21A9 /* Verifier.swift */,
);
path = ProviderVerification;
sourceTree = "<group>";
};
ADE6475D2D1158B000BE9AB3 /* Protocols */ = {
isa = PBXGroup;
children = (
A7F18576296BE177003AE3F2 /* BodyBuilder.swift */,
A7F18572296BE152003AE3F2 /* HeaderBuilder.swift */,
ADE6474C2D1125EC00BE9AB3 /* PactFFIProviding.swift */,
ADE6475E2D1158C000BE9AB3 /* ProviderVerifying.swift */,
A7F1857A296BE196003AE3F2 /* QueryBuilder.swift */,
);
path = Protocols;
sourceTree = "<group>";
};
ADE647622D11597000BE9AB3 /* Services */ = {
isa = PBXGroup;
children = (
ADE647502D11285400BE9AB3 /* DefaultPactFFIProvider.swift */,
);
path = Services;
sourceTree = "<group>";
};
/* End PBXGroup section */
Expand Down Expand Up @@ -718,6 +755,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
ADE647602D1158C300BE9AB3 /* ProviderVerifying.swift in Sources */,
ADC15DB026CE98140010D900 /* ProviderVerificationError.swift in Sources */,
ADE6474D2D1125F500BE9AB3 /* PactFFIProviding.swift in Sources */,
ADBEF3012648FCF200486C4A /* PactVerificationFailure.swift in Sources */,
Expand Down Expand Up @@ -745,7 +783,9 @@
A7840F402949AA4200CF22EF /* PactBuilderTests.swift in Sources */,
A7840F83294C2ECA00CF22EF /* InteractionTests.swift in Sources */,
A7840F86294C2ED800CF22EF /* PactTests.swift in Sources */,
ADE647562D11439900BE9AB3 /* PactVerificationFailureTests.swift in Sources */,
AD1598522648F2E1007CFAA5 /* MockServerTests.swift in Sources */,
ADE647652D115A2000BE9AB3 /* MockPactFFIProvider.swift in Sources */,
ADB659BF2D069CDC0049A39C /* TestStatusCode.swift in Sources */,
A7840F78294AF20500CF22EF /* GenerateTests.swift in Sources */,
ADE647472D1121DC00BE9AB3 /* ProviderVerificationErrorTests.swift in Sources */,
Expand All @@ -756,6 +796,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
ADE6475F2D1158C300BE9AB3 /* ProviderVerifying.swift in Sources */,
ADC15DB126CE98140010D900 /* ProviderVerificationError.swift in Sources */,
ADE6474F2D1125F500BE9AB3 /* PactFFIProviding.swift in Sources */,
ADBEF3022648FCF200486C4A /* PactVerificationFailure.swift in Sources */,
Expand Down Expand Up @@ -783,7 +824,9 @@
A7840F412949AA4200CF22EF /* PactBuilderTests.swift in Sources */,
A7840F84294C2ECA00CF22EF /* InteractionTests.swift in Sources */,
A7840F87294C2ED800CF22EF /* PactTests.swift in Sources */,
ADE647552D11439900BE9AB3 /* PactVerificationFailureTests.swift in Sources */,
AD1598532648F2E1007CFAA5 /* MockServerTests.swift in Sources */,
ADE647642D115A2000BE9AB3 /* MockPactFFIProvider.swift in Sources */,
ADB659BE2D069CDC0049A39C /* TestStatusCode.swift in Sources */,
A7840F79294AF20500CF22EF /* GenerateTests.swift in Sources */,
ADE647462D1121DC00BE9AB3 /* ProviderVerificationErrorTests.swift in Sources */,
Expand All @@ -801,6 +844,7 @@
A7F1857D296BE196003AE3F2 /* QueryBuilder.swift in Sources */,
AD957F3C28A23B8400860AD1 /* SocketBinder.swift in Sources */,
A7F18579296BE177003AE3F2 /* BodyBuilder.swift in Sources */,
ADE647612D1158C300BE9AB3 /* ProviderVerifying.swift in Sources */,
ADD7CB12264B4A080091A286 /* MockServer.swift in Sources */,
ADE2551226CE335400FA21A9 /* Verifier.swift in Sources */,
A7840F45294A923000CF22EF /* PactBuilder.swift in Sources */,
Expand Down
58 changes: 24 additions & 34 deletions Sources/Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@
// Created by Oliver Jones on 15/12/2022.
// Copyright © 2022 Oliver Jones. All rights reserved.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// See LICENSE file for licensing information.
//

import Foundation
Expand All @@ -22,41 +12,41 @@ import PactMockServer
#endif

public enum Generate {

/// Generates an example string based on provided regex pattern
///
/// Only supports basic regex patterns.
///
/// - Parameters:
/// - regex: The pattern to use
/// - regex: The pattern to use.
///
/// - Returns: `nil` if the provided regex pattern is invalid.
///
public static func value(regex: String) -> String? {
let result = pactffi_generate_regex_value(regex.cString(using: .utf8))
guard result.tag == StringResult_Ok, let stringPointer = result.ok else {
return nil
}
defer {
pactffi_string_delete(stringPointer)
}

return String(cString: stringPointer)
public static func string(regex: String) -> String? {
generateString(from: regex)
}

/// Generates an example datetime string based on provided format
///
/// Returns `nil` if the provided format is invalid
/// Generates an example date-time string based on provided format
///
/// - Parameters:
/// - format: The format of date to generate
///
/// - Returns: `nil` if the provided format is invalid.
///
public static func date(format: String) -> String? {
let result = pactffi_generate_datetime_string(format.cString(using: .utf8))
guard result.tag == StringResult_Ok, let stringPointer = result.ok else {
return nil
}
defer {
pactffi_string_delete(stringPointer)
}

return String(cString: stringPointer)
generateDate(format: format)
}
}

// MARK: - Internal

extension Generate {

static func generateString(from regex: String, ffiProvider: PactFFIProviding = DefaultPactFFIProvider()) -> String? {
ffiProvider.generateString(regex: regex)
}

static func generateDate(format: String, ffiProvider: PactFFIProviding = DefaultPactFFIProvider()) -> String? {
ffiProvider.generateDateTimeString(format: format)
}
}
2 changes: 1 addition & 1 deletion Sources/MockServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public class MockServer {
let tryPort = port ?? Self.randomPort
Logging.log(.debug, message: "Starting mock server on \(socketAddress):\(tryPort)...")

let result = try self.ffiProvider.mockServerForTransferProtocol(
let result = try ffiProvider.mockServerForTransferProtocol(
pactHandle: pact.handle,
socketAddress: socketAddress,
port: tryPort,
Expand Down
26 changes: 12 additions & 14 deletions Sources/Model/Interaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ public extension Interaction {
var value: String?

/// - Parameters:
/// - description - The provider state description. It needs to be unique.
/// - description - The provider state description. It needs to be unique.
public init(description: String) {
self.description = description
}

/// - Parameters:
/// - description - The provider state description. It needs to be unique.
/// - description - The provider state description. It needs to be unique.
/// - name - Parameter name.
/// - value - Parameter value.
public init(description: String, name: String, value: String) {
Expand All @@ -272,16 +272,16 @@ public extension Interaction {

/// Adds `providerStates` to the ``Interaction``.
///
/// - Throws: ``Error`` if the interaction or Pact can't be modified (i.e. the mock server for it has already started)
///
/// - Parameters:
/// - description - The provider state description. It needs to be unique.
/// - name - Parameter name.
/// - value - Parameter value.
/// - providerStates: A set of unique provider state objects.
///
/// - Throws: ``Error`` if the interaction or Pact can't be modified (i.e. the mock server for it has already started)
///
@discardableResult
func given(_ providerStates: [ProviderState]) throws -> Self {
precondition(Set(providerStates.map(\.description)).count == providerStates.count, "ProviderState descriptions must be unique!")
guard Set(providerStates.map(\.description)).count == providerStates.count else {
throw Error.panic("ProviderState descriptions must be unique!")
}

for state in providerStates {
if let name = state.name, let value = state.value {
Expand All @@ -296,12 +296,10 @@ public extension Interaction {

/// Adds `providerStates` to the ``Interaction``.
///
/// Throws ``Error`` if the interaction or Pact can't be modified (i.e. the mock server for it has already started)
///
/// - Parameters:
/// - description - The provider state description. It needs to be unique.
/// - name - Parameter name.
/// - value - Parameter value.
/// - providerStates: A set of unique provider state objects.
///
/// - Throws: ``Error`` if the interaction or Pact can't be modified (i.e. the mock server for it has already started)
///
@discardableResult
func given(_ providerStates: ProviderState...) throws -> Self {
Expand Down Expand Up @@ -329,7 +327,7 @@ extension Interaction.Error: LocalizedError {
)
case .panic(let errorMessage):
return String.localizedStringWithFormat(
NSLocalizedString("Function paniced (error: %@)", comment: "Error message when a rust function panics"),
NSLocalizedString("Function panicked (error: %@)", comment: "Error message when a rust function panics"),
errorMessage ?? ""
)
}
Expand Down
12 changes: 1 addition & 11 deletions Sources/Model/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@
// Created by Oliver Jones on 10/1/2023.
// Copyright © 2023 Oliver Jones. All rights reserved.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// See LICENSE file for licensing information.
//

import Foundation
Expand Down
Loading

0 comments on commit 2a046d4

Please sign in to comment.