Skip to content

Commit

Permalink
[pigeon] Run swift-format on ungenerated example app Swift files (#5934)
Browse files Browse the repository at this point in the history
Run `swift-format` linter/formatter on Swift files in example apps.  This only touches Swift files in example/test code.  

This also doesn't format any autogenerated Swift files, which would require changed in the pigeon generator: flutter/flutter#141799

Part of turning on swift-format CI formatting #5928
  • Loading branch information
jmagman authored Jan 19, 2024
1 parent 9379e4a commit 129e08c
Show file tree
Hide file tree
Showing 19 changed files with 493 additions and 388 deletions.
10 changes: 6 additions & 4 deletions packages/pigeon/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ private class PigeonApiImplementation: ExampleHostApi {
}

func add(_ a: Int64, to b: Int64) throws -> Int64 {
if (a < 0 || b < 0) {
throw FlutterError(code: "code", message: "message", details: "details");
if a < 0 || b < 0 {
throw FlutterError(code: "code", message: "message", details: "details")
}
return a + b
}

func sendMessage(message: MessageData, completion: @escaping (Result<Bool, Error>) -> Void) {
if (message.code == Code.one) {
if message.code == Code.one {
completion(.failure(FlutterError(code: "code", message: "message", details: "details")))
return
}
Expand Down Expand Up @@ -237,7 +237,9 @@ private class PigeonFlutterApi {
flutterAPI = MessageFlutterApi(binaryMessenger: binaryMessenger)
}

func callFlutterMethod(aString aStringArg: String?, completion: @escaping (Result<String, Error>) -> Void) {
func callFlutterMethod(
aString aStringArg: String?, completion: @escaping (Result<String, Error>) -> Void
) {
flutterAPI.flutterMethod(aString: aStringArg) {
completion(.success($0))
}
Expand Down
10 changes: 6 additions & 4 deletions packages/pigeon/example/app/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ private class PigeonApiImplementation: ExampleHostApi {
}

func add(_ a: Int64, to b: Int64) throws -> Int64 {
if (a < 0 || b < 0) {
throw FlutterError(code: "code", message: "message", details: "details");
if a < 0 || b < 0 {
throw FlutterError(code: "code", message: "message", details: "details")
}
return a + b
}

func sendMessage(message: MessageData, completion: @escaping (Result<Bool, Error>) -> Void) {
if (message.code == Code.one) {
if message.code == Code.one {
completion(.failure(FlutterError(code: "code", message: "message", details: "details")))
return
}
Expand All @@ -39,7 +39,9 @@ private class PigeonFlutterApi {
flutterAPI = MessageFlutterApi(binaryMessenger: binaryMessenger)
}

func callFlutterMethod(aString aStringArg: String?, completion: @escaping (Result<String, Error>) -> Void) {
func callFlutterMethod(
aString aStringArg: String?, completion: @escaping (Result<String, Error>) -> Void
) {
flutterAPI.flutterMethod(aString: aStringArg) {
completion(.success($0))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import FlutterMacOS
import Cocoa
import FlutterMacOS
import XCTest

@testable import alternate_language_test_plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import UIKit
import Flutter
import UIKit

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import Flutter
import XCTest

@testable import test_plugin

class AllDatatypesTests: XCTestCase {
Expand All @@ -17,25 +18,25 @@ class AllDatatypesTests: XCTestCase {

api.echoNullable(everything) { result in
switch result {
case .success(let res) :
XCTAssertNotNil(res)
XCTAssertNil(res!.aNullableBool)
XCTAssertNil(res!.aNullableInt)
XCTAssertNil(res!.aNullableDouble)
XCTAssertNil(res!.aNullableString)
XCTAssertNil(res!.aNullableByteArray)
XCTAssertNil(res!.aNullable4ByteArray)
XCTAssertNil(res!.aNullable8ByteArray)
XCTAssertNil(res!.aNullableFloatArray)
XCTAssertNil(res!.aNullableList)
XCTAssertNil(res!.aNullableMap)
XCTAssertNil(res!.nullableNestedList)
XCTAssertNil(res!.nullableMapWithAnnotations)
XCTAssertNil(res!.nullableMapWithObject)
expectation.fulfill()
case .failure(_) :
return
case .success(let res):
XCTAssertNotNil(res)
XCTAssertNil(res!.aNullableBool)
XCTAssertNil(res!.aNullableInt)
XCTAssertNil(res!.aNullableDouble)
XCTAssertNil(res!.aNullableString)
XCTAssertNil(res!.aNullableByteArray)
XCTAssertNil(res!.aNullable4ByteArray)
XCTAssertNil(res!.aNullable8ByteArray)
XCTAssertNil(res!.aNullableFloatArray)
XCTAssertNil(res!.aNullableList)
XCTAssertNil(res!.aNullableMap)
XCTAssertNil(res!.nullableNestedList)
XCTAssertNil(res!.nullableMapWithAnnotations)
XCTAssertNil(res!.nullableMapWithObject)
expectation.fulfill()
case .failure(_):
return

}
}

Expand All @@ -55,36 +56,36 @@ class AllDatatypesTests: XCTestCase {
aNullableMap: ["hello": 1234],
nullableNestedList: [[true, false], [true]],
nullableMapWithAnnotations: ["hello": "world"],
nullableMapWithObject: ["hello": 1234, "goodbye" : "world"],
nullableMapWithObject: ["hello": 1234, "goodbye": "world"],
aNullableString: "123"
)

let binaryMessenger = EchoBinaryMessenger(codec: FlutterIntegrationCoreApiCodec.shared)
let api = FlutterIntegrationCoreApi(binaryMessenger: binaryMessenger)

let expectation = XCTestExpectation(description: "callback")

api.echoNullable(everything) { result in
switch result {
case .success(let res) :
XCTAssertNotNil(res)
XCTAssertEqual(res!.aNullableBool, everything.aNullableBool)
XCTAssertEqual(res!.aNullableInt, everything.aNullableInt)
XCTAssertEqual(res!.aNullableDouble, everything.aNullableDouble)
XCTAssertEqual(res!.aNullableString, everything.aNullableString)
XCTAssertEqual(res!.aNullableByteArray, everything.aNullableByteArray)
XCTAssertEqual(res!.aNullable4ByteArray, everything.aNullable4ByteArray)
XCTAssertEqual(res!.aNullable8ByteArray, everything.aNullable8ByteArray)
XCTAssertEqual(res!.aNullableFloatArray, everything.aNullableFloatArray)
XCTAssert(equalsList(res!.aNullableList, everything.aNullableList))
XCTAssert(equalsDictionary(res!.aNullableMap, everything.aNullableMap))
XCTAssertEqual(res!.nullableNestedList, everything.nullableNestedList)
XCTAssertEqual(res!.nullableMapWithAnnotations, everything.nullableMapWithAnnotations)
XCTAssert(equalsDictionary(res!.nullableMapWithObject, everything.nullableMapWithObject))
expectation.fulfill()
return
case .failure(_) :
return
switch result {
case .success(let res):
XCTAssertNotNil(res)
XCTAssertEqual(res!.aNullableBool, everything.aNullableBool)
XCTAssertEqual(res!.aNullableInt, everything.aNullableInt)
XCTAssertEqual(res!.aNullableDouble, everything.aNullableDouble)
XCTAssertEqual(res!.aNullableString, everything.aNullableString)
XCTAssertEqual(res!.aNullableByteArray, everything.aNullableByteArray)
XCTAssertEqual(res!.aNullable4ByteArray, everything.aNullable4ByteArray)
XCTAssertEqual(res!.aNullable8ByteArray, everything.aNullable8ByteArray)
XCTAssertEqual(res!.aNullableFloatArray, everything.aNullableFloatArray)
XCTAssert(equalsList(res!.aNullableList, everything.aNullableList))
XCTAssert(equalsDictionary(res!.aNullableMap, everything.aNullableMap))
XCTAssertEqual(res!.nullableNestedList, everything.nullableNestedList)
XCTAssertEqual(res!.nullableMapWithAnnotations, everything.nullableMapWithAnnotations)
XCTAssert(equalsDictionary(res!.nullableMapWithObject, everything.nullableMapWithObject))
expectation.fulfill()
return
case .failure(_):
return
}
}
wait(for: [expectation], timeout: 1.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.
import Flutter
import XCTest

@testable import test_plugin

class MockHostSmallApi: HostSmallApi {
Expand All @@ -27,20 +28,21 @@ class AsyncHandlersTest: XCTestCase {

let expectation = XCTestExpectation(description: "callback")
flutterApi.echo(value) { result in
switch result {
case .success(let res) :
XCTAssertEqual(res, value)
expectation.fulfill()
case .failure(_) :
return
switch result {
case .success(let res):
XCTAssertEqual(res, value)
expectation.fulfill()
case .failure(_):
return
}

}
wait(for: [expectation], timeout: 1.0)
}

func testAsyncFlutter2HostVoidVoid() throws {
let binaryMessenger = MockBinaryMessenger<String>(codec: FlutterStandardMessageCodec.sharedInstance())
let binaryMessenger = MockBinaryMessenger<String>(
codec: FlutterStandardMessageCodec.sharedInstance())
let mockHostSmallApi = MockHostSmallApi()
HostSmallApiSetup.setUp(binaryMessenger: binaryMessenger, api: mockHostSmallApi)
let channelName = "dev.flutter.pigeon.pigeon_integration_tests.HostSmallApi.voidVoid"
Expand All @@ -49,14 +51,15 @@ class AsyncHandlersTest: XCTestCase {
let expectation = XCTestExpectation(description: "voidvoid callback")
binaryMessenger.handlers[channelName]?(nil) { data in
let outputList = binaryMessenger.codec.decode(data) as? [Any]
XCTAssertEqual(outputList?.first as! NSNull, NSNull())
XCTAssertEqual(outputList?.first as! NSNull, NSNull())
expectation.fulfill()
}
wait(for: [expectation], timeout: 1.0)
}

func testAsyncFlutter2Host() throws {
let binaryMessenger = MockBinaryMessenger<String>(codec: FlutterStandardMessageCodec.sharedInstance())
let binaryMessenger = MockBinaryMessenger<String>(
codec: FlutterStandardMessageCodec.sharedInstance())
let mockHostSmallApi = MockHostSmallApi()
let value = "Test"
mockHostSmallApi.output = value
Expand All @@ -69,7 +72,7 @@ class AsyncHandlersTest: XCTestCase {
let expectation = XCTestExpectation(description: "echo callback")
binaryMessenger.handlers[channelName]?(inputEncoded) { data in
let outputList = binaryMessenger.codec.decode(data) as? [Any]
let output = outputList?.first as? String
let output = outputList?.first as? String
XCTAssertEqual(output, value)
expectation.fulfill()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import Foundation
import Flutter
import Foundation

class EchoBinaryMessenger: NSObject, FlutterBinaryMessenger {
let codec: FlutterMessageCodec
private(set) var count = 0
var defaultReturn: Any?

init(codec: FlutterMessageCodec) {
self.codec = codec
super.init()
}

func send(onChannel channel: String, message: Data?) {
// Method not implemented because this messenger is just for echoing
}

func send(
onChannel channel: String,
message: Data?,
binaryReply callback: FlutterBinaryReply? = nil
) {
guard let callback = callback else { return }

guard
let args = self.codec.decode(message) as? [Any?],
let firstArg = args.first,
Expand All @@ -34,20 +34,21 @@ class EchoBinaryMessenger: NSObject, FlutterBinaryMessenger {
callback(self.defaultReturn.flatMap { self.codec.encode([$0]) })
return
}

callback(self.codec.encode([castedFirstArg]))
}

func setMessageHandlerOnChannel(
_ channel: String, binaryMessageHandler handler:
FlutterBinaryMessageHandler? = nil
_ channel: String,
binaryMessageHandler handler:
FlutterBinaryMessageHandler? = nil
) -> FlutterBinaryMessengerConnection {
self.count += 1
return FlutterBinaryMessengerConnection(self.count)
}

func cleanUpConnection(_ connection: FlutterBinaryMessengerConnection) {
// Method not implemented because this messenger is just for echoing
// Method not implemented because this messenger is just for echoing
}

private func nilOrValue<T>(_ value: Any?) -> T? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import XCTest

@testable import test_plugin

class MockEnumApi2Host: EnumApi2Host {
Expand Down Expand Up @@ -32,7 +33,7 @@ class EnumTests: XCTestCase {
binaryMessenger.handlers[channelName]?(inputEncoded) { data in
let outputMap = binaryMessenger.codec.decode(data) as? [Any]
XCTAssertNotNil(outputMap)

let output = outputMap?.first as? DataWithEnum
XCTAssertEqual(output, input)
XCTAssertTrue(outputMap?.count == 1)
Expand All @@ -49,11 +50,11 @@ class EnumTests: XCTestCase {
let expectation = XCTestExpectation(description: "callback")
api.echo(data: data) { result in
switch result {
case .success(let res) :
XCTAssertEqual(res.state, res.state)
expectation.fulfill()
case .failure(_) :
return
case .success(let res):
XCTAssertEqual(res.state, res.state)
expectation.fulfill()
case .failure(_):
return
}
}
wait(for: [expectation], timeout: 1.0)
Expand Down
Loading

0 comments on commit 129e08c

Please sign in to comment.