@@ -20,7 +20,24 @@ import XCTest
2020
2121@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
2222class LambdaTestingTests : XCTestCase {
23- func testCodableClosure( ) {
23+ func testBasics( ) async throws {
24+ struct MyLambda : LambdaHandler {
25+ typealias Event = String
26+ typealias Output = String
27+
28+ init ( context: LambdaInitializationContext ) { }
29+
30+ func handle( _ event: String , context: LambdaContext ) async throws -> String {
31+ return event
32+ }
33+ }
34+
35+ let uuid = UUID ( ) . uuidString
36+ let result = try await Lambda . test ( MyLambda . self, with: uuid)
37+ XCTAssertEqual ( result, uuid)
38+ }
39+
40+ func testCodableClosure( ) async throws {
2441 struct Request : Codable {
2542 let name : String
2643 }
@@ -41,36 +58,36 @@ class LambdaTestingTests: XCTestCase {
4158 }
4259
4360 let request = Request ( name: UUID ( ) . uuidString)
44- var response : Response ?
45- XCTAssertNoThrow ( response = try Lambda . test ( MyLambda . self, with: request) )
46- XCTAssertEqual ( response? . message, " echo " + request. name)
61+ let response = try await Lambda . test ( MyLambda . self, with: request)
62+ XCTAssertEqual ( response. message, " echo " + request. name)
4763 }
4864
49- // DIRTY HACK: To verify the handler was actually invoked, we change a global variable.
50- static var VoidLambdaHandlerInvokeCount : Int = 0
51- func testCodableVoidClosure( ) {
65+ func testCodableVoidClosure( ) async throws {
5266 struct Request : Codable {
5367 let name : String
5468 }
5569
5670 struct MyLambda : LambdaHandler {
71+ // DIRTY HACK: To verify the handler was actually invoked, we change a global variable.
72+ static var VoidLambdaHandlerInvokeCount : Int = 0
73+
5774 typealias Event = Request
5875 typealias Output = Void
5976
6077 init ( context: LambdaInitializationContext ) { }
6178
6279 func handle( _ event: Request , context: LambdaContext ) async throws {
63- LambdaTestingTests . VoidLambdaHandlerInvokeCount += 1
80+ Self . VoidLambdaHandlerInvokeCount += 1
6481 }
6582 }
6683
67- Self . VoidLambdaHandlerInvokeCount = 0
6884 let request = Request ( name: UUID ( ) . uuidString)
69- XCTAssertNoThrow ( try Lambda . test ( MyLambda . self, with: request) )
70- XCTAssertEqual ( Self . VoidLambdaHandlerInvokeCount, 1 )
85+ MyLambda . VoidLambdaHandlerInvokeCount = 0
86+ try await Lambda . test ( MyLambda . self, with: request)
87+ XCTAssertEqual ( MyLambda . VoidLambdaHandlerInvokeCount, 1 )
7188 }
7289
73- func testInvocationFailure( ) {
90+ func testInvocationFailure( ) async throws {
7491 struct MyError : Error { }
7592
7693 struct MyLambda : LambdaHandler {
@@ -84,12 +101,15 @@ class LambdaTestingTests: XCTestCase {
84101 }
85102 }
86103
87- XCTAssertThrowsError ( try Lambda . test ( MyLambda . self, with: UUID ( ) . uuidString) ) { error in
104+ do {
105+ try await Lambda . test ( MyLambda . self, with: UUID ( ) . uuidString)
106+ XCTFail ( " expected to throw " )
107+ } catch {
88108 XCTAssert ( error is MyError )
89109 }
90110 }
91111
92- func testAsyncLongRunning( ) {
112+ func testAsyncLongRunning( ) async throws {
93113 struct MyLambda : LambdaHandler {
94114 typealias Event = String
95115 typealias Output = String
@@ -103,7 +123,8 @@ class LambdaTestingTests: XCTestCase {
103123 }
104124
105125 let uuid = UUID ( ) . uuidString
106- XCTAssertEqual ( try Lambda . test ( MyLambda . self, with: uuid) , uuid)
126+ let result = try await Lambda . test ( MyLambda . self, with: uuid)
127+ XCTAssertEqual ( result, uuid)
107128 }
108129}
109130#endif
0 commit comments