Skip to content

Commit 318868c

Browse files
authored
[test] Migrate unit tests to Swift Testing (#88)
Migrate all unti tests to Swift Testing ### Motivation: Allows to compile and run test on open source Swift toolchain 6 ### Modifications: Migrate according to https://developer.apple.com/documentation/testing/migratingfromxctest ### Result: ``` 􁁛 Test run with 101 tests passed after 0.012 seconds. ```
1 parent 0025b1a commit 318868c

29 files changed

+894
-855
lines changed

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
with:
2424
linux_5_9_enabled: false
2525
linux_5_10_enabled: false
26-
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error"
26+
linux_nightly_next_arguments_override: "--explicit-target-dependency-import-check error"
2727
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error"
2828

2929
swift-6-language-mode:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ xcuserdata
1010
Package.resolved
1111
.serverless
1212
.devcontainer
13+
.amazonq

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.8
1+
// swift-tools-version:6.0
22

33
import PackageDescription
44

[email protected]

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// swift-tools-version:5.10
2+
3+
import PackageDescription
4+
5+
let swiftSettings: [SwiftSetting] = [.enableExperimentalFeature("StrictConcurrency=complete")]
6+
7+
let package = Package(
8+
name: "swift-aws-lambda-events",
9+
platforms: [.macOS(.v14)],
10+
products: [
11+
.library(name: "AWSLambdaEvents", targets: ["AWSLambdaEvents"])
12+
],
13+
dependencies: [
14+
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
15+
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.0"),
16+
],
17+
targets: [
18+
.target(
19+
name: "AWSLambdaEvents",
20+
dependencies: [
21+
.product(name: "HTTPTypes", package: "swift-http-types")
22+
],
23+
swiftSettings: swiftSettings
24+
)
25+
]
26+
)

[email protected]

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// swift-tools-version:5.8
2+
3+
import PackageDescription
4+
5+
let swiftSettings: [SwiftSetting] = [.enableExperimentalFeature("StrictConcurrency=complete")]
6+
7+
let package = Package(
8+
name: "swift-aws-lambda-events",
9+
platforms: [.macOS(.v14)],
10+
products: [
11+
.library(name: "AWSLambdaEvents", targets: ["AWSLambdaEvents"])
12+
],
13+
dependencies: [
14+
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
15+
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.0"),
16+
],
17+
targets: [
18+
.target(
19+
name: "AWSLambdaEvents",
20+
dependencies: [
21+
.product(name: "HTTPTypes", package: "swift-http-types")
22+
],
23+
swiftSettings: swiftSettings
24+
)
25+
]
26+
)

Tests/AWSLambdaEventsTests/ALBTests.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import XCTest
15+
import Foundation
16+
import Testing
1617

1718
@testable import AWSLambdaEvents
1819

19-
class ALBTests: XCTestCase {
20+
@Suite
21+
struct ALBTests {
2022
static let exampleSingleValueHeadersEventBody = """
2123
{
2224
"requestContext":{
@@ -45,21 +47,21 @@ class ALBTests: XCTestCase {
4547
}
4648
"""
4749

48-
func testRequestWithSingleValueHeadersEvent() {
50+
@Test func requestWithSingleValueHeadersEvent() {
4951
let data = ALBTests.exampleSingleValueHeadersEventBody.data(using: .utf8)!
5052
do {
5153
let decoder = JSONDecoder()
5254

5355
let event = try decoder.decode(ALBTargetGroupRequest.self, from: data)
5456

55-
XCTAssertEqual(event.httpMethod, .get)
56-
XCTAssertEqual(event.body, "")
57-
XCTAssertEqual(event.isBase64Encoded, false)
58-
XCTAssertEqual(event.headers?.count, 11)
59-
XCTAssertEqual(event.path, "/")
60-
XCTAssertEqual(event.queryStringParameters, [:])
57+
#expect(event.httpMethod == .get)
58+
#expect(event.body == "")
59+
#expect(event.isBase64Encoded == false)
60+
#expect(event.headers?.count == 11)
61+
#expect(event.path == "/")
62+
#expect(event.queryStringParameters == [:])
6163
} catch {
62-
XCTFail("Unexpected error: \(error)")
64+
Issue.record("Unexpected error: \(error)")
6365
}
6466
}
6567
}

Tests/AWSLambdaEventsTests/APIGateway+EncodableTests.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@ struct APIGatewayEncodableResponseTests {
3535
#expect(throws: Never.self) {
3636
try response = APIGatewayV2Response(statusCode: .ok, encodableBody: businessResponse)
3737
}
38-
try #require(response?.body != nil)
3938

4039
// when
41-
let body = response?.body?.data(using: .utf8)
42-
try #require(body != nil)
40+
let body = try #require(response?.body?.data(using: .utf8))
4341

4442
#expect(throws: Never.self) {
45-
let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body!)
43+
let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body)
4644

4745
// then
4846
#expect(encodedBody == businessResponse)
@@ -63,7 +61,6 @@ struct APIGatewayEncodableResponseTests {
6361

6462
// when
6563
let body = response?.body?.data(using: .utf8)
66-
try #require(body != nil)
6764

6865
#expect(throws: Never.self) {
6966
let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body!)

Tests/AWSLambdaEventsTests/APIGateway+V2IAMTests.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import XCTest
15+
import Foundation
16+
import Testing
1617

1718
@testable import AWSLambdaEvents
1819

19-
class APIGatewayV2IAMTests: XCTestCase {
20+
@Suite
21+
struct APIGatewayV2IAMTests {
2022
static let getEventWithIAM = """
2123
{
2224
"version": "2.0",
@@ -133,30 +135,28 @@ class APIGatewayV2IAMTests: XCTestCase {
133135

134136
// MARK: Decoding
135137

136-
func testRequestDecodingGetRequestWithIAM() {
138+
@Test func requestDecodingGetRequestWithIAM() throws {
137139
let data = APIGatewayV2IAMTests.getEventWithIAM.data(using: .utf8)!
138-
var req: APIGatewayV2Request?
139-
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data))
140+
let req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
140141

141-
XCTAssertEqual(req?.rawPath, "/hello")
142-
XCTAssertEqual(req?.context.authorizer?.iam?.accessKey, "ASIA-redacted")
143-
XCTAssertEqual(req?.context.authorizer?.iam?.accountId, "012345678912")
144-
XCTAssertNil(req?.body)
142+
#expect(req.rawPath == "/hello")
143+
#expect(req.context.authorizer?.iam?.accessKey == "ASIA-redacted")
144+
#expect(req.context.authorizer?.iam?.accountId == "012345678912")
145+
#expect(req.body == nil)
145146
}
146147

147-
func testRequestDecodingGetRequestWithIAMWithCognito() {
148+
@Test func requestDecodingGetRequestWithIAMWithCognito() throws {
148149
let data = APIGatewayV2IAMTests.getEventWithIAMAndCognito.data(using: .utf8)!
149-
var req: APIGatewayV2Request?
150-
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data))
150+
let req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
151151

152-
XCTAssertEqual(req?.rawPath, "/hello")
153-
XCTAssertEqual(req?.context.authorizer?.iam?.accessKey, "ASIA-redacted")
154-
XCTAssertEqual(req?.context.authorizer?.iam?.accountId, "012345678912")
152+
#expect(req.rawPath == "/hello")
153+
#expect(req.context.authorizer?.iam?.accessKey == "ASIA-redacted")
154+
#expect(req.context.authorizer?.iam?.accountId == "012345678912")
155155

156156
// test the cognito identity part
157-
XCTAssertEqual(req?.context.authorizer?.iam?.cognitoIdentity?.identityId, "us-east-1:68bc0ecd-9d5e--redacted")
158-
XCTAssertEqual(req?.context.authorizer?.iam?.cognitoIdentity?.amr?.count, 3)
157+
#expect(req.context.authorizer?.iam?.cognitoIdentity?.identityId == "us-east-1:68bc0ecd-9d5e--redacted")
158+
#expect(req.context.authorizer?.iam?.cognitoIdentity?.amr?.count == 3)
159159

160-
XCTAssertNil(req?.body)
160+
#expect(req.body == nil)
161161
}
162162
}

Tests/AWSLambdaEventsTests/APIGateway+V2Tests.swift

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import XCTest
15+
import Foundation
16+
import Testing
1617

1718
@testable import AWSLambdaEvents
1819

19-
class APIGatewayV2Tests: XCTestCase {
20+
@Suite
21+
struct APIGatewayV2Tests {
2022
static let exampleGetEventBody = """
2123
{
2224
"routeKey":"GET /hello",
@@ -189,36 +191,37 @@ class APIGatewayV2Tests: XCTestCase {
189191

190192
// MARK: Decoding
191193

192-
func testRequestDecodingExampleGetRequest() {
194+
@Test func requestDecodingExampleGetRequest() throws {
193195
let data = APIGatewayV2Tests.exampleGetEventBody.data(using: .utf8)!
194-
var req: APIGatewayV2Request?
195-
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data))
196+
let req = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
196197

197-
XCTAssertEqual(req?.rawPath, "/hello")
198-
XCTAssertEqual(req?.context.http.method, .get)
199-
XCTAssertEqual(req?.queryStringParameters.count, 1)
200-
XCTAssertEqual(req?.rawQueryString, "foo=bar")
201-
XCTAssertEqual(req?.headers.count, 8)
202-
XCTAssertEqual(req?.context.authorizer?.jwt?.claims?["aud"], "customers")
198+
#expect(req.rawPath == "/hello")
199+
#expect(req.context.http.method == .get)
200+
#expect(req.queryStringParameters.count == 1)
201+
#expect(req.rawQueryString == "foo=bar")
202+
#expect(req.headers.count == 8)
203+
#expect(req.context.authorizer?.jwt?.claims?["aud"] == "customers")
203204

204-
XCTAssertNil(req?.body)
205+
#expect(req.body == nil)
205206
}
206207

207-
func testDecodingRequestClientCert() throws {
208+
@Test func decodingRequestClientCert() throws {
208209
let data = APIGatewayV2Tests.fullExamplePayload.data(using: .utf8)!
209210
let request = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
210211
let clientCert = request.context.authentication?.clientCert
211212

212-
XCTAssertEqual(clientCert?.clientCertPem, "CERT_CONTENT")
213-
XCTAssertEqual(clientCert?.subjectDN, "www.example.com")
214-
XCTAssertEqual(clientCert?.issuerDN, "Example issuer")
215-
XCTAssertEqual(clientCert?.serialNumber, "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1")
216-
XCTAssertEqual(clientCert?.validity.notBefore, "May 28 12:30:02 2019 GMT")
217-
XCTAssertEqual(clientCert?.validity.notAfter, "Aug 5 09:36:04 2021 GMT")
213+
#expect(clientCert?.clientCertPem == "CERT_CONTENT")
214+
#expect(clientCert?.subjectDN == "www.example.com")
215+
#expect(clientCert?.issuerDN == "Example issuer")
216+
#expect(clientCert?.serialNumber == "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1")
217+
#expect(clientCert?.validity.notBefore == "May 28 12:30:02 2019 GMT")
218+
#expect(clientCert?.validity.notAfter == "Aug 5 09:36:04 2021 GMT")
218219
}
219220

220-
func testDecodingNilCollections() {
221+
@Test func decodingNilCollections() throws {
221222
let data = APIGatewayV2Tests.exampleGetEventBodyNilHeaders.data(using: .utf8)!
222-
XCTAssertNoThrow(_ = try JSONDecoder().decode(APIGatewayV2Request.self, from: data))
223+
#expect(throws: Never.self) {
224+
_ = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
225+
}
223226
}
224227
}

0 commit comments

Comments
 (0)