Skip to content

Commit 5c8478e

Browse files
committed
Update test style
1 parent bb34d70 commit 5c8478e

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

Tests/TencentSCFEventsTests/APIGatewayTests.swift

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,10 @@ class APIGatewayTests: XCTestCase {
9595
}
9696
"""#
9797

98-
static let sortedEncoder = { () -> JSONEncoder in
99-
let encoder = JSONEncoder()
100-
encoder.outputFormatting = .sortedKeys
101-
return encoder
102-
}()
103-
104-
struct Body: Decodable, Equatable {
105-
let test: String
106-
}
107-
10898
func testRequestDecodingRequest() {
99+
struct Body: Decodable, Equatable {
100+
let test: String
101+
}
109102
let data = Self.eventBody.data(using: .utf8)!
110103
var req: APIGateway.Request<Body>?
111104
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGateway.Request<Body>.self, from: data))
@@ -171,6 +164,9 @@ class APIGatewayTests: XCTestCase {
171164
}
172165

173166
func testResponseEncodingWithText() {
167+
let encoder = JSONEncoder()
168+
encoder.outputFormatting = .sortedKeys
169+
174170
let resp = APIGateway.Response(
175171
statusCode: .ok,
176172
type: .text,
@@ -179,7 +175,7 @@ class APIGatewayTests: XCTestCase {
179175
let expectedJson = #"{"body":"abc123","headers":{"Content-Type":"text\/plain"},"isBase64Encoded":false,"statusCode":200}"#
180176

181177
var data: Data?
182-
XCTAssertNoThrow(data = try Self.sortedEncoder.encode(resp))
178+
XCTAssertNoThrow(data = try encoder.encode(resp))
183179
if let data = data,
184180
let json = String(data: data, encoding: .utf8)
185181
{
@@ -190,6 +186,9 @@ class APIGatewayTests: XCTestCase {
190186
}
191187

192188
func testResponseEncodingWithData() {
189+
let encoder = JSONEncoder()
190+
encoder.outputFormatting = .sortedKeys
191+
193192
let body = #"{"hello":"swift"}"#
194193
let resp = APIGateway.Response(
195194
statusCode: .ok,
@@ -198,7 +197,7 @@ class APIGatewayTests: XCTestCase {
198197
let expectedJson = #"{"body":"\#(body.data(using: .utf8)!.base64EncodedString())","headers":{"Content-Type":"application\/octet-stream"},"isBase64Encoded":true,"statusCode":200}"#
199198

200199
var data: Data?
201-
XCTAssertNoThrow(data = try Self.sortedEncoder.encode(resp))
200+
XCTAssertNoThrow(data = try encoder.encode(resp))
202201
if let data = data,
203202
let json = String(data: data, encoding: .utf8)
204203
{
@@ -209,6 +208,9 @@ class APIGatewayTests: XCTestCase {
209208
}
210209

211210
func testResponseEncodingWithCodable() {
211+
let encoder = JSONEncoder()
212+
encoder.outputFormatting = .sortedKeys
213+
212214
struct Point: Encodable, Equatable {
213215
let x, y: Double
214216
}
@@ -223,7 +225,7 @@ class APIGatewayTests: XCTestCase {
223225
]
224226

225227
var data: Data?
226-
XCTAssertNoThrow(data = try Self.sortedEncoder.encode(resp))
228+
XCTAssertNoThrow(data = try encoder.encode(resp))
227229
if let data = data,
228230
let json = String(data: data, encoding: .utf8)
229231
{
@@ -234,11 +236,14 @@ class APIGatewayTests: XCTestCase {
234236
}
235237

236238
func testResponseEncodingWithNil() {
239+
let encoder = JSONEncoder()
240+
encoder.outputFormatting = .sortedKeys
241+
237242
let resp = APIGateway.Response(statusCode: .ok)
238243
let expectedJson = #"{"body":"","headers":{"Content-Type":"text\/plain"},"isBase64Encoded":false,"statusCode":200}"#
239244

240245
var data: Data?
241-
XCTAssertNoThrow(data = try Self.sortedEncoder.encode(resp))
246+
XCTAssertNoThrow(data = try encoder.encode(resp))
242247
if let data = data,
243248
let json = String(data: data, encoding: .utf8)
244249
{
@@ -249,6 +254,9 @@ class APIGatewayTests: XCTestCase {
249254
}
250255

251256
func testResponseEncodingWithCustomMIME() {
257+
let encoder = JSONEncoder()
258+
encoder.outputFormatting = .sortedKeys
259+
252260
let mime = "application/x-javascript"
253261
let resp = APIGateway.Response(
254262
statusCode: .ok,
@@ -258,7 +266,7 @@ class APIGatewayTests: XCTestCase {
258266
let expectedJson = #"{"body":"console.log(\"Hello world!\");","headers":{"Content-Type":"application\/x-javascript"},"isBase64Encoded":false,"statusCode":200}"#
259267

260268
var data: Data?
261-
XCTAssertNoThrow(data = try Self.sortedEncoder.encode(resp))
269+
XCTAssertNoThrow(data = try encoder.encode(resp))
262270
if let data = data,
263271
let json = String(data: data, encoding: .utf8)
264272
{

Tests/TencentSCFEventsTests/TencentCloudTests.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,28 @@ class TencentCloudTests: XCTestCase {
2727
}
2828

2929
func testRegionCodable() throws {
30-
let encoder = JSONEncoder()
31-
let decoder = JSONDecoder()
3230
for region in Self.allRegions {
3331
let wrapped = Wrapped(value: region)
3432
let json = #"{"value":"\#(region.rawValue)"}"#
35-
let encoded = try encoder.encode(wrapped)
36-
let decoded = try decoder.decode(Wrapped<TencentCloud.Region>.self, from: json.data(using: .utf8)!)
33+
34+
let encoded = try JSONEncoder().encode(wrapped)
35+
let decoded = try JSONDecoder().decode(Wrapped<TencentCloud.Region>.self, from: json.data(using: .utf8)!)
3736
XCTAssertEqual(String(data: encoded, encoding: .utf8), json)
3837
XCTAssertEqual(region, decoded.value)
3938
}
4039
}
4140

4241
func testZoneWithRawAndCodable() throws {
43-
let encoder = JSONEncoder()
44-
let decoder = JSONDecoder()
4542
for region in Self.allRegions {
4643
let number = UInt8.random(in: UInt8.min ... UInt8.max)
4744
let zone = TencentCloud.Zone(rawValue: "\(region)-\(number)")
4845
XCTAssertNotNil(zone)
46+
4947
let wrapped = Wrapped(value: zone!)
5048
let json = #"{"value":"\#(zone!.rawValue)"}"#
51-
let encoded = try encoder.encode(wrapped)
52-
let decoded = try decoder.decode(Wrapped<TencentCloud.Zone>.self, from: json.data(using: .utf8)!)
49+
50+
let encoded = try JSONEncoder().encode(wrapped)
51+
let decoded = try JSONDecoder().decode(Wrapped<TencentCloud.Zone>.self, from: json.data(using: .utf8)!)
5352
XCTAssertEqual(String(data: encoded, encoding: .utf8), json)
5453
XCTAssertEqual(zone, decoded.value)
5554
}

Tests/TencentSCFEventsTests/Utils/Base64Tests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ class Base64Tests: XCTestCase {
7676
}
7777
}
7878

79-
func testNSStringToDecode() {
79+
func testStringToDecode() {
8080
let test = "1234567"
81-
let nsstring = test.data(using: .utf8)!.base64EncodedString()
81+
let string = test.data(using: .utf8)!.base64EncodedString()
8282

83-
XCTAssertNoThrow(try nsstring.base64decoded())
83+
XCTAssertNoThrow(try string.base64decoded())
8484
}
8585
}

0 commit comments

Comments
 (0)