Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for Swift 6 pt.3 #46

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
max_line_length = 180
trim_trailing_whitespace = true

[*.{md,yml,rb,podspec,js}]
indent_size = 2

[Makefile]
indent_style = tab
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public func stubCondition(@StubConditionBuilder builder: () -> any StubCondition

@discardableResult
public func stub(@StubConditionBuilder builder: () -> any StubCondition,
withResponse stubResponse: @escaping (URLRequest) -> StubResponse) -> Stub {
withResponse stubResponse: @escaping @Sendable (URLRequest) -> StubResponse) -> Stub {
stub(stubCondition(builder: builder),
withResponse: stubResponse)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/StubNetworkKit/Response/StubResponse+Fixtures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ public extension StubResponse {
headers: headers)
}

static func json(_ jsonObject: [AnyHashable: Any],
static func json(_ jsonObject: JSONObject,
statusCode: Int = 200,
headers: [String: String]? = nil) -> Self {
self.init(jsonObject: jsonObject,
statusCode: statusCode,
headers: headers)
}

static func json(_ jsonArray: [Any],
static func json(_ jsonArray: JSONArray,
statusCode: Int = 200,
headers: [String: String]? = nil) -> Self {
self.init(jsonArray: jsonArray,
Expand Down
4 changes: 2 additions & 2 deletions Sources/StubNetworkKit/Response/StubResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public extension StubResponse {

// MARK: JSON
public extension StubResponse {
init(jsonObject: [AnyHashable: Any],
init(jsonObject: JSONObject,
statusCode: Int = 200,
headers appendingHeaders: [String: String]? = nil) {
do {
Expand All @@ -57,7 +57,7 @@ public extension StubResponse {
}
}

init(jsonArray: [Any],
init(jsonArray: JSONArray,
statusCode: Int = 200,
headers appendingHeaders: [String: String]? = nil) {
do {
Expand Down
8 changes: 4 additions & 4 deletions Sources/StubNetworkKit/Stub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Foundation
import FoundationNetworking
#endif

public final class Stub {
typealias Response = (URLRequest) -> StubResponse
public final class Stub: @unchecked Sendable {
typealias Response = @Sendable (URLRequest) -> StubResponse

/// Matcher to judge if use stub response.
private(set) var matcher: StubMatcher
Expand Down Expand Up @@ -33,7 +33,7 @@ extension Stub {
/// - Returns: Created stub object.
@discardableResult
public func stub(_ matcher: @escaping StubMatcher,
withResponse stubResponse: @escaping (URLRequest) -> StubResponse) -> Stub {
withResponse stubResponse: @escaping @Sendable (URLRequest) -> StubResponse) -> Stub {
let stub = Stub(matcher: matcher,
response: stubResponse)
StubURLProtocol.register(stub)
Expand All @@ -58,7 +58,7 @@ public func stub(_ matcher: @escaping StubMatcher) -> Stub {
/// - Returns: Created stub object.
@discardableResult
public func stub(_ condition: any StubCondition,
withResponse stubResponse: @escaping (URLRequest) -> StubResponse) -> Stub {
withResponse stubResponse: @escaping @Sendable (URLRequest) -> StubResponse) -> Stub {
let stub = Stub(matcher: condition.matcher,
response: stubResponse)
StubURLProtocol.register(stub)
Expand Down
8 changes: 4 additions & 4 deletions Sources/StubNetworkKit/StubNetworking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func dumpCondition<T: Equatable>(expected: T?,
print("\u{001B}[\(result ? 32 : 31)m[\(file):L\(line)] expected: \(expected), actual: \(actual)\u{001B}[m")
}

func dumpCondition(expected: [Any]?,
actual: [Any]?,
func dumpCondition(expected: JSONArray?,
actual: JSONArray?,
file: StaticString = #file,
line: UInt = #line) {
guard StubNetworking.option.debugConditions else { return }
Expand All @@ -108,8 +108,8 @@ func dumpCondition(expected: [Any]?,
print("\u{001B}[\(result ? 32 : 31)m[\(file):L\(line)] expected: \(String(describing: expected)), actual: \(String(describing: actual))\u{001B}[m")
}

func dumpCondition(expected: [AnyHashable: Any]?,
actual: [AnyHashable: Any]?,
func dumpCondition(expected: JSONObject?,
actual: JSONObject?,
file: StaticString = #file,
line: UInt = #line) {
guard StubNetworking.option.debugConditions else { return }
Expand Down
18 changes: 9 additions & 9 deletions Tests/StubNetworkKitTests/Matcher/StubMatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import StubNetworkKit
@Suite struct StubMatcherTests {
init() {
StubNetworking.option(printDebugLog: true,
debugConditions: false)
debugConditions: true)
}

@Suite struct MethodIs {
Expand Down Expand Up @@ -367,7 +367,7 @@ import StubNetworkKit
func complexObject(_ jsonString: String, _ expected: Bool) throws {
var req = URLRequest(url: URL(string: "foo://bar")!)
req.httpBody = jsonString.data(using: .utf8)
#expect(Body.isJson(["foo": "bar", "baz": ["qux": true, "quux": ["spam", "ham", "eggs"]]]).matcher(req) == expected)
#expect(Body.isJson(["foo": "bar", "baz": ["qux": true, "quux": ["spam", "ham", "eggs"] as JSONArray] as JSONObject]).matcher(req) == expected)
}
}

Expand All @@ -377,9 +377,9 @@ import StubNetworkKit
(#"[]"#, true),
(#"{}"#, false),
])
func empty(_ json: String, _ expected: Bool) throws {
func empty(_ jsonString: String, _ expected: Bool) throws {
var req = URLRequest(url: URL(string: "foo://bar")!)
req.httpBody = json.data(using: .utf8)
req.httpBody = jsonString.data(using: .utf8)
#expect(Body.isJson([]).matcher(req) == expected)
}

Expand All @@ -390,19 +390,19 @@ import StubNetworkKit
(#"["bar", "foo", "baz", 42, "qux", true]"#, false),
(#"["foo", "bar", "baz", 41, "qux", true]"#, false),
])
func stringArray(_ json: String, _ expected: Bool) throws {
func stringArray(_ jsonString: String, _ expected: Bool) throws {
var req = URLRequest(url: URL(string: "foo://bar")!)
req.httpBody = json.data(using: .utf8)
req.httpBody = jsonString.data(using: .utf8)
#expect(Body.isJson(["foo", "bar", "baz", 42, "qux", true]).matcher(req) == expected)
}

@Test(arguments: [
(#"[["foo", "bar", "baz"], {"qux": true, "quux": ["spam", "ham", "eggs"]}]"#, true),
])
func arrayWithMultipleObjects(_ json: String, _ expected: Bool) throws {
func arrayWithMultipleObjects(_ jsonString: String, _ expected: Bool) throws {
var req = URLRequest(url: URL(string: "foo://bar")!)
req.httpBody = json.data(using: .utf8)
#expect(Body.isJson([["foo", "bar", "baz"], ["qux": true, "quux": ["spam", "ham", "eggs"]]]).matcher(req) == expected)
req.httpBody = jsonString.data(using: .utf8)
#expect(Body.isJson([["foo", "bar", "baz"] as JSONArray, ["qux": true, "quux": ["spam", "ham", "eggs"] as JSONArray] as JSONObject]).matcher(req) == expected)
}
}

Expand Down
Loading