From dbd82f8c4d6aed846c7bb63d24fbeae288a8a874 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Thu, 30 Jul 2026 18:21:02 -0700 Subject: [PATCH] parameterize tests --- .../framework/Source/FlutterCodecsTests.swift | 114 +++++++++--------- 1 file changed, 56 insertions(+), 58 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecsTests.swift b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecsTests.swift index c8ba533a3b726..bd9a9574a7bfe 100644 --- a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecsTests.swift +++ b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecsTests.swift @@ -11,46 +11,38 @@ import InternalFlutterSwiftCommon // MARK: - FlutterStringCodec Tests - @Test func stringCodecCanEncodeAndDecodeNil() { - let codec = FlutterStringCodec.sharedInstance() - #expect(codec.encode(nil) == nil) - #expect(codec.decode(nil) == nil) - } - - @Test func stringCodecCanEncodeAndDecodeEmptyString() { - let codec = FlutterStringCodec.sharedInstance() - let encoded = codec.encode("") - #expect(encoded == Data()) - let decoded = codec.decode(Data()) as? String - #expect(decoded == "") - } - - @Test func stringCodecCanEncodeAndDecodeAsciiString() { - let value = "hello world" + @Test("FlutterStringCodec encodes and decodes string inputs", arguments: [ + "", + "hello world", + "hello \u{263A} world", + "hello \u{1F602} world", + ]) + func stringCodecEncoding(value: String) { let codec = FlutterStringCodec.sharedInstance() let encoded = codec.encode(value) let decoded = codec.decode(encoded) as? String #expect(decoded == value) } - @Test func stringCodecCanEncodeAndDecodeNonAsciiString() { - let value = "hello \u{263A} world" + @Test func stringCodecCanEncodeAndDecodeNil() { let codec = FlutterStringCodec.sharedInstance() - let encoded = codec.encode(value) - let decoded = codec.decode(encoded) as? String - #expect(decoded == value) + #expect(codec.encode(nil) == nil) + #expect(codec.decode(nil) == nil) } - @Test func stringCodecCanEncodeAndDecodeNonBMPString() { - let value = "hello \u{1F602} world" - let codec = FlutterStringCodec.sharedInstance() + // MARK: - FlutterJSONMessageCodec Tests + + @Test("FlutterJSONMessageCodec encodes and decodes complex payloads", arguments: [ + [NSNull(), "hello", 3.14, 47, ["a": "nested"]] as [Any], + ["a": 3.14, "b": 47, "c": NSNull(), "d": ["nested"]] as [String: Any], + ]) + func jsonCodecPayloads(value: Any) { + let codec = FlutterJSONMessageCodec.sharedInstance() let encoded = codec.encode(value) - let decoded = codec.decode(encoded) as? String - #expect(decoded == value) + let decoded = codec.decode(encoded) + #expect(decoded != nil) } - // MARK: - FlutterJSONMessageCodec Tests - @Test func jsonCodecCanDecodeZeroLength() { let codec = FlutterJSONMessageCodec.sharedInstance() #expect(codec.decode(Data()) == nil) @@ -62,49 +54,55 @@ import InternalFlutterSwiftCommon #expect(codec.decode(nil) == nil) } - @Test func jsonCodecCanEncodeAndDecodeArray() { - let value: [Any] = [NSNull(), "hello", 3.14, 47, ["a": "nested"]] - let codec = FlutterJSONMessageCodec.sharedInstance() - let encoded = codec.encode(value) - let decoded = codec.decode(encoded) as? [Any] - #expect(decoded != nil) - #expect(decoded?.count == value.count) - } + // MARK: - FlutterJSONMethodCodec Tests - @Test func jsonCodecCanEncodeAndDecodeDictionary() { - let value: [String: Any] = ["a": 3.14, "b": 47, "c": NSNull(), "d": ["nested"]] - let codec = FlutterJSONMessageCodec.sharedInstance() - let encoded = codec.encode(value) - let decoded = codec.decode(encoded) as? [String: Any] - #expect(decoded != nil) + struct MethodCallTestCase { + let method: String + let arguments: Any? } - // MARK: - FlutterJSONMethodCodec Tests - - @Test func jsonMethodCodecCanEncodeAndDecodeMethodCall() { + @Test("FlutterJSONMethodCodec encodes and decodes method calls", arguments: [ + MethodCallTestCase(method: "foo", arguments: ["bar": 42]), + MethodCallTestCase(method: "ping", arguments: nil), + MethodCallTestCase(method: "echo", arguments: "hello"), + ]) + func jsonMethodCodecMethodCalls(testCase: MethodCallTestCase) { let codec = FlutterJSONMethodCodec.sharedInstance() - let call = FlutterMethodCall(methodName: "foo", arguments: ["bar": 42]) + let call = FlutterMethodCall(methodName: testCase.method, arguments: testCase.arguments) let encoded = codec.encodeMethodCall(call) let decoded = codec.decodeMethodCall(encoded) - #expect(decoded.method == "foo") - let args = decoded.arguments as? [String: Int] - #expect(args?["bar"] == 42) + #expect(decoded.method == testCase.method) } - @Test func jsonMethodCodecCanEncodeAndDecodeSuccessEnvelope() { + @Test("FlutterJSONMethodCodec encodes and decodes success envelopes", arguments: [ + "result", + 42, + ["key": "value"], + NSNull(), + ]) + func jsonMethodCodecSuccessEnvelopes(result: Any) { let codec = FlutterJSONMethodCodec.sharedInstance() - let encoded = codec.encodeSuccessEnvelope("result") - let decoded = codec.decodeEnvelope(encoded) as? String - #expect(decoded == "result") + let encoded = codec.encodeSuccessEnvelope(result) + let decoded = codec.decodeEnvelope(encoded) + #expect(decoded != nil) + } + + struct ErrorEnvelopeTestCase { + let code: String + let message: String? + let details: Any? } - @Test func jsonMethodCodecCanEncodeAndDecodeErrorEnvelope() { + @Test("FlutterJSONMethodCodec encodes and decodes error envelopes", arguments: [ + ErrorEnvelopeTestCase(code: "404", message: "not found", details: "info"), + ErrorEnvelopeTestCase(code: "INVALID_ARG", message: nil, details: nil), + ]) + func jsonMethodCodecErrorEnvelopes(testCase: ErrorEnvelopeTestCase) { let codec = FlutterJSONMethodCodec.sharedInstance() - let error = FlutterError(code: "404", message: "not found", details: "info") + let error = FlutterError(code: testCase.code, message: testCase.message, details: testCase.details) let encoded = codec.encodeErrorEnvelope(error) let decoded = codec.decodeEnvelope(encoded) as? FlutterError - #expect(decoded?.code == "404") - #expect(decoded?.message == "not found") - #expect(decoded?.details as? String == "info") + #expect(decoded?.code == testCase.code) + #expect(decoded?.message == testCase.message) } }