[tool] Add Kotlin autoformatting (#5374)

Updates the tooling to fetch and use `ktfmt` for Kotlin code, the same way it currently does for the Google Java formatter, and applies that formatting (in the default mode; see discussion in the linked issue) to the repository.

In the future we could revisit the formatter or mode, but since this currently seems to be the most consistent with our other languages and to google3 formatting this is likely the option we'll want to stick with.

Fixes https://github.com/flutter/flutter/issues/118756
This commit is contained in:
stuartmorgan
2023-11-13 08:18:25 -08:00
committed by GitHub
parent 17bd92e8a3
commit 72de224b08
31 changed files with 1811 additions and 1255 deletions

View File

@ -131,10 +131,11 @@ targets:
target_file: repo_checks.yaml target_file: repo_checks.yaml
channel: master channel: master
version_file: flutter_master.version version_file: flutter_master.version
# The format check requires clang-format. # The format check requires clang-format, and the current version of ktfmt requires JDK 11+.
dependencies: >- dependencies: >-
[ [
{"dependency": "clang", "version": "git_revision:5d5aba78dbbee75508f01bcaa69aedb2ab79065a"} {"dependency": "clang", "version": "git_revision:5d5aba78dbbee75508f01bcaa69aedb2ab79065a"},
{"dependency": "open_jdk", "version": "version:17"}
] ]
- name: Linux dart_unit_test_shard_1 master - name: Linux dart_unit_test_shard_1 master

View File

@ -5,6 +5,4 @@ package dev.flutter.packages.animations.example
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -5,5 +5,4 @@ package com.example.example
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -6,5 +6,4 @@ package dev.flutter.plugins.file_selector_example
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -6,5 +6,4 @@ package dev.flutter.example
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -6,5 +6,4 @@ package com.example.example
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -5,5 +5,4 @@ package io.flutter.packages.flutter_markdown_example
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -6,5 +6,4 @@ package com.example.example
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -5,6 +5,4 @@ package io.flutter.packages.palettegenerator.imagecolors
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -146,14 +146,14 @@ private class PigeonApiImplementation: ExampleHostApi {
### Kotlin ### Kotlin
<?code-excerpt "android/app/src/main/kotlin/dev/flutter/pigeon_example_app/MainActivity.kt (kotlin-class)"?> <?code-excerpt "android/app/src/main/kotlin/dev/flutter/pigeon_example_app/MainActivity.kt (kotlin-class)"?>
```kotlin ```kotlin
private class PigeonApiImplementation: ExampleHostApi { private class PigeonApiImplementation : ExampleHostApi {
override fun getHostLanguage(): String { override fun getHostLanguage(): String {
return "Kotlin" return "Kotlin"
} }
override fun add(a: Long, b: Long): Long { override fun add(a: Long, b: Long): Long {
if (a < 0L || b < 0L) { if (a < 0L || b < 0L) {
throw FlutterError("code", "message", "details"); throw FlutterError("code", "message", "details")
} }
return a + b return a + b
} }
@ -258,9 +258,7 @@ private class PigeonFlutterApi {
} }
fun callFlutterMethod(aString: String, callback: (Result<String>) -> Unit) { fun callFlutterMethod(aString: String, callback: (Result<String>) -> Unit) {
flutterApi!!.flutterMethod(aString) { flutterApi!!.flutterMethod(aString) { echo -> callback(Result.success(echo)) }
echo -> callback(Result.success(echo))
}
} }
} }
``` ```

View File

@ -5,24 +5,23 @@
package dev.flutter.pigeon_example_app package dev.flutter.pigeon_example_app
import ExampleHostApi import ExampleHostApi
import FlutterError
import MessageData import MessageData
import MessageFlutterApi import MessageFlutterApi
import FlutterError
import androidx.annotation.NonNull import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.embedding.engine.plugins.FlutterPlugin
// #docregion kotlin-class // #docregion kotlin-class
private class PigeonApiImplementation: ExampleHostApi { private class PigeonApiImplementation : ExampleHostApi {
override fun getHostLanguage(): String { override fun getHostLanguage(): String {
return "Kotlin" return "Kotlin"
} }
override fun add(a: Long, b: Long): Long { override fun add(a: Long, b: Long): Long {
if (a < 0L || b < 0L) { if (a < 0L || b < 0L) {
throw FlutterError("code", "message", "details"); throw FlutterError("code", "message", "details")
} }
return a + b return a + b
} }
@ -47,18 +46,16 @@ private class PigeonFlutterApi {
} }
fun callFlutterMethod(aString: String, callback: (Result<String>) -> Unit) { fun callFlutterMethod(aString: String, callback: (Result<String>) -> Unit) {
flutterApi!!.flutterMethod(aString) { flutterApi!!.flutterMethod(aString) { echo -> callback(Result.success(echo)) }
echo -> callback(Result.success(echo))
}
} }
} }
// #enddocregion kotlin-class-flutter // #enddocregion kotlin-class-flutter
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine) super.configureFlutterEngine(flutterEngine)
val api = PigeonApiImplementation() val api = PigeonApiImplementation()
ExampleHostApi.setUp(flutterEngine.dartExecutor.binaryMessenger, api); ExampleHostApi.setUp(flutterEngine.dartExecutor.binaryMessenger, api)
} }
} }

View File

@ -4,7 +4,6 @@
// Autogenerated from Pigeon, do not edit directly. // Autogenerated from Pigeon, do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
import android.util.Log import android.util.Log
import io.flutter.plugin.common.BasicMessageChannel import io.flutter.plugin.common.BasicMessageChannel
import io.flutter.plugin.common.BinaryMessenger import io.flutter.plugin.common.BinaryMessenger
@ -19,33 +18,31 @@ private fun wrapResult(result: Any?): List<Any?> {
private fun wrapError(exception: Throwable): List<Any?> { private fun wrapError(exception: Throwable): List<Any?> {
if (exception is FlutterError) { if (exception is FlutterError) {
return listOf( return listOf(exception.code, exception.message, exception.details)
exception.code,
exception.message,
exception.details
)
} else { } else {
return listOf( return listOf(
exception.javaClass.simpleName, exception.javaClass.simpleName,
exception.toString(), exception.toString(),
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception) "Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception))
)
} }
} }
private fun createConnectionError(channelName: String): FlutterError { private fun createConnectionError(channelName: String): FlutterError {
return FlutterError("channel-error", "Unable to establish connection on channel: '$channelName'.", "")} return FlutterError(
"channel-error", "Unable to establish connection on channel: '$channelName'.", "")
}
/** /**
* Error class for passing custom error details to Flutter via a thrown PlatformException. * Error class for passing custom error details to Flutter via a thrown PlatformException.
*
* @property code The error code. * @property code The error code.
* @property message The error message. * @property message The error message.
* @property details The error details. Must be a datatype supported by the api codec. * @property details The error details. Must be a datatype supported by the api codec.
*/ */
class FlutterError ( class FlutterError(
val code: String, val code: String,
override val message: String? = null, override val message: String? = null,
val details: Any? = null val details: Any? = null
) : Throwable() ) : Throwable()
enum class Code(val raw: Int) { enum class Code(val raw: Int) {
@ -60,12 +57,11 @@ enum class Code(val raw: Int) {
} }
/** Generated class from Pigeon that represents data sent in messages. */ /** Generated class from Pigeon that represents data sent in messages. */
data class MessageData ( data class MessageData(
val name: String? = null, val name: String? = null,
val description: String? = null, val description: String? = null,
val code: Code, val code: Code,
val data: Map<String?, String?> val data: Map<String?, String?>
) { ) {
companion object { companion object {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
@ -77,12 +73,13 @@ data class MessageData (
return MessageData(name, description, code, data) return MessageData(name, description, code, data)
} }
} }
fun toList(): List<Any?> { fun toList(): List<Any?> {
return listOf<Any?>( return listOf<Any?>(
name, name,
description, description,
code.raw, code.raw,
data, data,
) )
} }
} }
@ -92,14 +89,13 @@ private object ExampleHostApiCodec : StandardMessageCodec() {
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? { override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
return when (type) { return when (type) {
128.toByte() -> { 128.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let { return (readValue(buffer) as? List<Any?>)?.let { MessageData.fromList(it) }
MessageData.fromList(it)
}
} }
else -> super.readValueOfType(type, buffer) else -> super.readValueOfType(type, buffer)
} }
} }
override fun writeValue(stream: ByteArrayOutputStream, value: Any?) {
override fun writeValue(stream: ByteArrayOutputStream, value: Any?) {
when (value) { when (value) {
is MessageData -> { is MessageData -> {
stream.write(128) stream.write(128)
@ -113,19 +109,23 @@ private object ExampleHostApiCodec : StandardMessageCodec() {
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */ /** Generated interface from Pigeon that represents a handler of messages from Flutter. */
interface ExampleHostApi { interface ExampleHostApi {
fun getHostLanguage(): String fun getHostLanguage(): String
fun add(a: Long, b: Long): Long fun add(a: Long, b: Long): Long
fun sendMessage(message: MessageData, callback: (Result<Boolean>) -> Unit) fun sendMessage(message: MessageData, callback: (Result<Boolean>) -> Unit)
companion object { companion object {
/** The codec used by ExampleHostApi. */ /** The codec used by ExampleHostApi. */
val codec: MessageCodec<Any?> by lazy { val codec: MessageCodec<Any?> by lazy { ExampleHostApiCodec }
ExampleHostApiCodec
}
/** Sets up an instance of `ExampleHostApi` to handle messages through the `binaryMessenger`. */ /** Sets up an instance of `ExampleHostApi` to handle messages through the `binaryMessenger`. */
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
fun setUp(binaryMessenger: BinaryMessenger, api: ExampleHostApi?) { fun setUp(binaryMessenger: BinaryMessenger, api: ExampleHostApi?) {
run { run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.getHostLanguage", codec) val channel =
BasicMessageChannel<Any?>(
binaryMessenger,
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.getHostLanguage",
codec)
if (api != null) { if (api != null) {
channel.setMessageHandler { _, reply -> channel.setMessageHandler { _, reply ->
var wrapped: List<Any?> var wrapped: List<Any?>
@ -141,7 +141,11 @@ interface ExampleHostApi {
} }
} }
run { run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.add", codec) val channel =
BasicMessageChannel<Any?>(
binaryMessenger,
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.add",
codec)
if (api != null) { if (api != null) {
channel.setMessageHandler { message, reply -> channel.setMessageHandler { message, reply ->
val args = message as List<Any?> val args = message as List<Any?>
@ -160,7 +164,11 @@ interface ExampleHostApi {
} }
} }
run { run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.sendMessage", codec) val channel =
BasicMessageChannel<Any?>(
binaryMessenger,
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.sendMessage",
codec)
if (api != null) { if (api != null) {
channel.setMessageHandler { message, reply -> channel.setMessageHandler { message, reply ->
val args = message as List<Any?> val args = message as List<Any?>
@ -187,25 +195,29 @@ interface ExampleHostApi {
class MessageFlutterApi(private val binaryMessenger: BinaryMessenger) { class MessageFlutterApi(private val binaryMessenger: BinaryMessenger) {
companion object { companion object {
/** The codec used by MessageFlutterApi. */ /** The codec used by MessageFlutterApi. */
val codec: MessageCodec<Any?> by lazy { val codec: MessageCodec<Any?> by lazy { StandardMessageCodec() }
StandardMessageCodec()
}
} }
fun flutterMethod(aStringArg: String?, callback: (Result<String>) -> Unit) { fun flutterMethod(aStringArg: String?, callback: (Result<String>) -> Unit) {
val channelName = "dev.flutter.pigeon.pigeon_example_package.MessageFlutterApi.flutterMethod" val channelName = "dev.flutter.pigeon.pigeon_example_package.MessageFlutterApi.flutterMethod"
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec) val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
channel.send(listOf(aStringArg)) { channel.send(listOf(aStringArg)) {
if (it is List<*>) { if (it is List<*>) {
if (it.size > 1) { if (it.size > 1) {
callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?))); callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?)))
} else if (it[0] == null) { } else if (it[0] == null) {
callback(Result.failure(FlutterError("null-error", "Flutter api returned null value for non-null return value.", ""))); callback(
Result.failure(
FlutterError(
"null-error",
"Flutter api returned null value for non-null return value.",
"")))
} else { } else {
val output = it[0] as String val output = it[0] as String
callback(Result.success(output)); callback(Result.success(output))
} }
} else { } else {
callback(Result.failure(createConnectionError(channelName))); callback(Result.failure(createConnectionError(channelName)))
} }
} }
} }

View File

@ -5,17 +5,10 @@
package com.example.test_plugin package com.example.test_plugin
import androidx.annotation.NonNull import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
/** /** This plugin handles the native side of the integration tests in example/integration_test/. */
* This plugin handles the native side of the integration tests in class TestPlugin : FlutterPlugin, HostIntegrationCoreApi {
* example/integration_test/.
*/
class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
var flutterApi: FlutterIntegrationCoreApi? = null var flutterApi: FlutterIntegrationCoreApi? = null
override fun onAttachedToEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { override fun onAttachedToEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
@ -23,13 +16,11 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
flutterApi = FlutterIntegrationCoreApi(binding.getBinaryMessenger()) flutterApi = FlutterIntegrationCoreApi(binding.getBinaryMessenger())
} }
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {}
}
// HostIntegrationCoreApi // HostIntegrationCoreApi
override fun noop() { override fun noop() {}
}
override fun echoAllTypes(everything: AllTypes): AllTypes { override fun echoAllTypes(everything: AllTypes): AllTypes {
return everything return everything
@ -40,15 +31,15 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
} }
override fun throwError(): Any? { override fun throwError(): Any? {
throw Exception("An error"); throw Exception("An error")
} }
override fun throwErrorFromVoid() { override fun throwErrorFromVoid() {
throw Exception("An error"); throw Exception("An error")
} }
override fun throwFlutterError(): Any? { override fun throwFlutterError(): Any? {
throw FlutterError("code", "message", "details"); throw FlutterError("code", "message", "details")
} }
override fun echoInt(anInt: Long): Long { override fun echoInt(anInt: Long): Long {
@ -99,8 +90,15 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
return AllClassesWrapper(AllNullableTypes(aNullableString = nullableString)) return AllClassesWrapper(AllNullableTypes(aNullableString = nullableString))
} }
override fun sendMultipleNullableTypes(aNullableBool: Boolean?, aNullableInt: Long?, aNullableString: String?): AllNullableTypes { override fun sendMultipleNullableTypes(
return AllNullableTypes(aNullableBool = aNullableBool, aNullableInt = aNullableInt, aNullableString = aNullableString) aNullableBool: Boolean?,
aNullableInt: Long?,
aNullableString: String?
): AllNullableTypes {
return AllNullableTypes(
aNullableBool = aNullableBool,
aNullableInt = aNullableInt,
aNullableString = aNullableString)
} }
override fun echoNullableInt(aNullableInt: Long?): Long? { override fun echoNullableInt(aNullableInt: Long?): Long? {
@ -159,7 +157,10 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
callback(Result.success(everything)) callback(Result.success(everything))
} }
override fun echoAsyncNullableAllNullableTypes(everything: AllNullableTypes?, callback: (Result<AllNullableTypes?>) -> Unit) { override fun echoAsyncNullableAllNullableTypes(
everything: AllNullableTypes?,
callback: (Result<AllNullableTypes?>) -> Unit
) {
callback(Result.success(everything)) callback(Result.success(everything))
} }
@ -191,7 +192,10 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
callback(Result.success(aList)) callback(Result.success(aList))
} }
override fun echoAsyncMap(aMap: Map<String?, Any?>, callback: (Result<Map<String?, Any?>>) -> Unit) { override fun echoAsyncMap(
aMap: Map<String?, Any?>,
callback: (Result<Map<String?, Any?>>) -> Unit
) {
callback(Result.success(aMap)) callback(Result.success(aMap))
} }
@ -215,7 +219,10 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
callback(Result.success(aString)) callback(Result.success(aString))
} }
override fun echoAsyncNullableUint8List(aUint8List: ByteArray?, callback: (Result<ByteArray?>) -> Unit) { override fun echoAsyncNullableUint8List(
aUint8List: ByteArray?,
callback: (Result<ByteArray?>) -> Unit
) {
callback(Result.success(aUint8List)) callback(Result.success(aUint8List))
} }
@ -227,7 +234,10 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
callback(Result.success(aList)) callback(Result.success(aList))
} }
override fun echoAsyncNullableMap(aMap: Map<String?, Any?>?, callback: (Result<Map<String?, Any?>?>) -> Unit) { override fun echoAsyncNullableMap(
aMap: Map<String?, Any?>?,
callback: (Result<Map<String?, Any?>?>) -> Unit
) {
callback(Result.success(aMap)) callback(Result.success(aMap))
} }
@ -242,6 +252,7 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
override fun callFlutterThrowError(callback: (Result<Any?>) -> Unit) { override fun callFlutterThrowError(callback: (Result<Any?>) -> Unit) {
flutterApi!!.throwError() { result -> callback(result) } flutterApi!!.throwError() { result -> callback(result) }
} }
override fun callFlutterThrowErrorFromVoid(callback: (Result<Unit>) -> Unit) { override fun callFlutterThrowErrorFromVoid(callback: (Result<Unit>) -> Unit) {
flutterApi!!.throwErrorFromVoid() { result -> callback(result) } flutterApi!!.throwErrorFromVoid() { result -> callback(result) }
} }
@ -251,13 +262,13 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
} }
override fun callFlutterSendMultipleNullableTypes( override fun callFlutterSendMultipleNullableTypes(
aNullableBool: Boolean?, aNullableBool: Boolean?,
aNullableInt: Long?, aNullableInt: Long?,
aNullableString: String?, aNullableString: String?,
callback: (Result<AllNullableTypes>) -> Unit callback: (Result<AllNullableTypes>) -> Unit
) { ) {
flutterApi!!.sendMultipleNullableTypes(aNullableBool, aNullableInt, aNullableString) { flutterApi!!.sendMultipleNullableTypes(aNullableBool, aNullableInt, aNullableString) { echo ->
echo -> callback(echo) callback(echo)
} }
} }
@ -281,11 +292,14 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
flutterApi!!.echoUint8List(aList) { echo -> callback(echo) } flutterApi!!.echoUint8List(aList) { echo -> callback(echo) }
} }
override fun callFlutterEchoList(aList: List<Any?>, callback: (Result<List<Any?>>) -> Unit){ override fun callFlutterEchoList(aList: List<Any?>, callback: (Result<List<Any?>>) -> Unit) {
flutterApi!!.echoList(aList) { echo -> callback(echo) } flutterApi!!.echoList(aList) { echo -> callback(echo) }
} }
override fun callFlutterEchoMap(aMap: Map<String?, Any?>, callback: (Result<Map<String?, Any?>>) -> Unit) { override fun callFlutterEchoMap(
aMap: Map<String?, Any?>,
callback: (Result<Map<String?, Any?>>) -> Unit
) {
flutterApi!!.echoMap(aMap) { echo -> callback(echo) } flutterApi!!.echoMap(aMap) { echo -> callback(echo) }
} }
@ -293,7 +307,10 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
flutterApi!!.echoEnum(anEnum) { echo -> callback(echo) } flutterApi!!.echoEnum(anEnum) { echo -> callback(echo) }
} }
override fun callFlutterEchoAllNullableTypes(everything: AllNullableTypes?, callback: (Result<AllNullableTypes?>) -> Unit) { override fun callFlutterEchoAllNullableTypes(
everything: AllNullableTypes?,
callback: (Result<AllNullableTypes?>) -> Unit
) {
flutterApi!!.echoAllNullableTypes(everything) { echo -> callback(echo) } flutterApi!!.echoAllNullableTypes(everything) { echo -> callback(echo) }
} }
@ -305,28 +322,42 @@ class TestPlugin: FlutterPlugin, HostIntegrationCoreApi {
flutterApi!!.echoNullableInt(anInt) { echo -> callback(echo) } flutterApi!!.echoNullableInt(anInt) { echo -> callback(echo) }
} }
override fun callFlutterEchoNullableDouble(aDouble: Double?, callback: (Result<Double?>) -> Unit) { override fun callFlutterEchoNullableDouble(
aDouble: Double?,
callback: (Result<Double?>) -> Unit
) {
flutterApi!!.echoNullableDouble(aDouble) { echo -> callback(echo) } flutterApi!!.echoNullableDouble(aDouble) { echo -> callback(echo) }
} }
override fun callFlutterEchoNullableString(aString: String?, callback: (Result<String?>) -> Unit) { override fun callFlutterEchoNullableString(
aString: String?,
callback: (Result<String?>) -> Unit
) {
flutterApi!!.echoNullableString(aString) { echo -> callback(echo) } flutterApi!!.echoNullableString(aString) { echo -> callback(echo) }
} }
override fun callFlutterEchoNullableUint8List(aList: ByteArray?, callback: (Result<ByteArray?>) -> Unit) { override fun callFlutterEchoNullableUint8List(
aList: ByteArray?,
callback: (Result<ByteArray?>) -> Unit
) {
flutterApi!!.echoNullableUint8List(aList) { echo -> callback(echo) } flutterApi!!.echoNullableUint8List(aList) { echo -> callback(echo) }
} }
override fun callFlutterEchoNullableList(aList: List<Any?>?, callback: (Result<List<Any?>?>) -> Unit) { override fun callFlutterEchoNullableList(
aList: List<Any?>?,
callback: (Result<List<Any?>?>) -> Unit
) {
flutterApi!!.echoNullableList(aList) { echo -> callback(echo) } flutterApi!!.echoNullableList(aList) { echo -> callback(echo) }
} }
override fun callFlutterEchoNullableMap(aMap: Map<String?, Any?>?, callback: (Result<Map<String?, Any?>?>) -> Unit) { override fun callFlutterEchoNullableMap(
aMap: Map<String?, Any?>?,
callback: (Result<Map<String?, Any?>?>) -> Unit
) {
flutterApi!!.echoNullableMap(aMap) { echo -> callback(echo) } flutterApi!!.echoNullableMap(aMap) { echo -> callback(echo) }
} }
override fun callFlutterEchoNullableEnum(anEnum: AnEnum?, callback: (Result<AnEnum?>) -> Unit) { override fun callFlutterEchoNullableEnum(anEnum: AnEnum?, callback: (Result<AnEnum?>) -> Unit) {
flutterApi!!.echoNullableEnum(anEnum) { echo -> callback(echo) } flutterApi!!.echoNullableEnum(anEnum) { echo -> callback(echo) }
} }
} }

View File

@ -7,95 +7,96 @@ package com.example.test_plugin
import io.flutter.plugin.common.BinaryMessenger import io.flutter.plugin.common.BinaryMessenger
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import junit.framework.TestCase
import org.junit.Test
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.util.ArrayList import java.util.ArrayList
import junit.framework.TestCase
import org.junit.Test
internal class AllDatatypesTest : TestCase() {
fun compareAllTypes(firstTypes: AllTypes?, secondTypes: AllTypes?) {
assertEquals(firstTypes == null, secondTypes == null)
if (firstTypes == null || secondTypes == null) {
return
}
assertEquals(firstTypes.aBool, secondTypes.aBool)
assertEquals(firstTypes.anInt, secondTypes.anInt)
assertEquals(firstTypes.anInt64, secondTypes.anInt64)
assertEquals(firstTypes.aDouble, secondTypes.aDouble)
assertEquals(firstTypes.aString, secondTypes.aString)
assertTrue(firstTypes.aByteArray.contentEquals(secondTypes.aByteArray))
assertTrue(firstTypes.a4ByteArray.contentEquals(secondTypes.a4ByteArray))
assertTrue(firstTypes.a8ByteArray.contentEquals(secondTypes.a8ByteArray))
assertTrue(firstTypes.aFloatArray.contentEquals(secondTypes.aFloatArray))
assertEquals(firstTypes.aList, secondTypes.aList)
assertEquals(firstTypes.aMap, secondTypes.aMap)
assertEquals(firstTypes.anEnum, secondTypes.anEnum)
assertEquals(firstTypes.anObject, secondTypes.anObject)
}
internal class AllDatatypesTest: TestCase() { fun compareAllNullableTypes(firstTypes: AllNullableTypes?, secondTypes: AllNullableTypes?) {
fun compareAllTypes(firstTypes: AllTypes?, secondTypes: AllTypes?) { assertEquals(firstTypes == null, secondTypes == null)
assertEquals(firstTypes == null, secondTypes == null) if (firstTypes == null || secondTypes == null) {
if (firstTypes == null || secondTypes == null) { return
return }
assertEquals(firstTypes.aNullableBool, secondTypes.aNullableBool)
assertEquals(firstTypes.aNullableInt, secondTypes.aNullableInt)
assertEquals(firstTypes.aNullableDouble, secondTypes.aNullableDouble)
assertEquals(firstTypes.aNullableString, secondTypes.aNullableString)
assertTrue(firstTypes.aNullableByteArray.contentEquals(secondTypes.aNullableByteArray))
assertTrue(firstTypes.aNullable4ByteArray.contentEquals(secondTypes.aNullable4ByteArray))
assertTrue(firstTypes.aNullable8ByteArray.contentEquals(secondTypes.aNullable8ByteArray))
assertTrue(firstTypes.aNullableFloatArray.contentEquals(secondTypes.aNullableFloatArray))
assertEquals(firstTypes.aNullableList, secondTypes.aNullableList)
assertEquals(firstTypes.aNullableMap, secondTypes.aNullableMap)
assertEquals(firstTypes.nullableMapWithObject, secondTypes.nullableMapWithObject)
assertEquals(firstTypes.aNullableObject, secondTypes.aNullableObject)
}
@Test
fun testNullValues() {
val everything = AllNullableTypes()
val binaryMessenger = mockk<BinaryMessenger>()
val api = FlutterIntegrationCoreApi(binaryMessenger)
every { binaryMessenger.send(any(), any(), any()) } answers
{
val codec = FlutterIntegrationCoreApi.codec
val message = arg<ByteBuffer>(1)
val reply = arg<BinaryMessenger.BinaryReply>(2)
message.position(0)
val args = codec.decodeMessage(message) as ArrayList<*>
val replyData = codec.encodeMessage(args)
replyData?.position(0)
reply.reply(replyData)
} }
assertEquals(firstTypes.aBool, secondTypes.aBool)
assertEquals(firstTypes.anInt, secondTypes.anInt) var didCall = false
assertEquals(firstTypes.anInt64, secondTypes.anInt64) api.echoAllNullableTypes(everything) {
assertEquals(firstTypes.aDouble, secondTypes.aDouble) didCall = true
assertEquals(firstTypes.aString, secondTypes.aString) val output =
assertTrue(firstTypes.aByteArray.contentEquals(secondTypes.aByteArray)) (it.getOrNull())?.let {
assertTrue(firstTypes.a4ByteArray.contentEquals(secondTypes.a4ByteArray)) assertNull(it.aNullableBool)
assertTrue(firstTypes.a8ByteArray.contentEquals(secondTypes.a8ByteArray)) assertNull(it.aNullableInt)
assertTrue(firstTypes.aFloatArray.contentEquals(secondTypes.aFloatArray)) assertNull(it.aNullableDouble)
assertEquals(firstTypes.aList, secondTypes.aList) assertNull(it.aNullableString)
assertEquals(firstTypes.aMap, secondTypes.aMap) assertNull(it.aNullableByteArray)
assertEquals(firstTypes.anEnum, secondTypes.anEnum) assertNull(it.aNullable4ByteArray)
assertEquals(firstTypes.anObject, secondTypes.anObject) assertNull(it.aNullable8ByteArray)
assertNull(it.aNullableFloatArray)
assertNull(it.aNullableList)
assertNull(it.aNullableMap)
assertNull(it.nullableMapWithObject)
}
assertNotNull(output)
} }
fun compareAllNullableTypes(firstTypes: AllNullableTypes?, secondTypes: AllNullableTypes?) { assertTrue(didCall)
assertEquals(firstTypes == null, secondTypes == null) }
if (firstTypes == null || secondTypes == null) {
return
}
assertEquals(firstTypes.aNullableBool, secondTypes.aNullableBool)
assertEquals(firstTypes.aNullableInt, secondTypes.aNullableInt)
assertEquals(firstTypes.aNullableDouble, secondTypes.aNullableDouble)
assertEquals(firstTypes.aNullableString, secondTypes.aNullableString)
assertTrue(firstTypes.aNullableByteArray.contentEquals(secondTypes.aNullableByteArray))
assertTrue(firstTypes.aNullable4ByteArray.contentEquals(secondTypes.aNullable4ByteArray))
assertTrue(firstTypes.aNullable8ByteArray.contentEquals(secondTypes.aNullable8ByteArray))
assertTrue(firstTypes.aNullableFloatArray.contentEquals(secondTypes.aNullableFloatArray))
assertEquals(firstTypes.aNullableList, secondTypes.aNullableList)
assertEquals(firstTypes.aNullableMap, secondTypes.aNullableMap)
assertEquals(firstTypes.nullableMapWithObject, secondTypes.nullableMapWithObject)
assertEquals(firstTypes.aNullableObject, secondTypes.aNullableObject)
}
@Test @Test
fun testNullValues() { fun testHasValues() {
val everything = AllNullableTypes() val everything =
val binaryMessenger = mockk<BinaryMessenger>() AllNullableTypes(
val api = FlutterIntegrationCoreApi(binaryMessenger)
every { binaryMessenger.send(any(), any(), any()) } answers {
val codec = FlutterIntegrationCoreApi.codec
val message = arg<ByteBuffer>(1)
val reply = arg<BinaryMessenger.BinaryReply>(2)
message.position(0)
val args = codec.decodeMessage(message) as ArrayList<*>
val replyData = codec.encodeMessage(args)
replyData?.position(0)
reply.reply(replyData)
}
var didCall = false
api.echoAllNullableTypes(everything) {
didCall = true
val output = (it.getOrNull())?.let {
assertNull(it.aNullableBool)
assertNull(it.aNullableInt)
assertNull(it.aNullableDouble)
assertNull(it.aNullableString)
assertNull(it.aNullableByteArray)
assertNull(it.aNullable4ByteArray)
assertNull(it.aNullable8ByteArray)
assertNull(it.aNullableFloatArray)
assertNull(it.aNullableList)
assertNull(it.aNullableMap)
assertNull(it.nullableMapWithObject)
}
assertNotNull(output)
}
assertTrue(didCall)
}
@Test
fun testHasValues() {
val everything = AllNullableTypes(
aNullableBool = false, aNullableBool = false,
aNullableInt = 1234L, aNullableInt = 1234L,
aNullableDouble = 2.0, aNullableDouble = 2.0,
@ -109,41 +110,59 @@ internal class AllDatatypesTest: TestCase() {
nullableMapWithObject = mapOf("hello" to 1234), nullableMapWithObject = mapOf("hello" to 1234),
aNullableObject = 0, aNullableObject = 0,
) )
val binaryMessenger = mockk<BinaryMessenger>() val binaryMessenger = mockk<BinaryMessenger>()
val api = FlutterIntegrationCoreApi(binaryMessenger) val api = FlutterIntegrationCoreApi(binaryMessenger)
every { binaryMessenger.send(any(), any(), any()) } answers { every { binaryMessenger.send(any(), any(), any()) } answers
val codec = FlutterIntegrationCoreApi.codec {
val message = arg<ByteBuffer>(1) val codec = FlutterIntegrationCoreApi.codec
val reply = arg<BinaryMessenger.BinaryReply>(2) val message = arg<ByteBuffer>(1)
message.position(0) val reply = arg<BinaryMessenger.BinaryReply>(2)
val args = codec.decodeMessage(message) as ArrayList<*> message.position(0)
val replyData = codec.encodeMessage(args) val args = codec.decodeMessage(message) as ArrayList<*>
replyData?.position(0) val replyData = codec.encodeMessage(args)
reply.reply(replyData) replyData?.position(0)
reply.reply(replyData)
} }
var didCall = false var didCall = false
api.echoAllNullableTypes(everything) { api.echoAllNullableTypes(everything) {
didCall = true didCall = true
compareAllNullableTypes(everything, it.getOrNull()) compareAllNullableTypes(everything, it.getOrNull())
}
assertTrue(didCall)
} }
@Test assertTrue(didCall)
fun testIntegerToLong() { }
val everything = AllNullableTypes(aNullableInt = 123L)
val list = everything.toList()
assertNotNull(list)
assertNull(list.first())
assertNotNull(list[1])
assertTrue(list[1] == 123L)
val list2 = listOf(null, 123, null, null, null, null, null, null, null, null, null, null, null, null, null, null) @Test
val everything2 = AllNullableTypes.fromList(list2) fun testIntegerToLong() {
val everything = AllNullableTypes(aNullableInt = 123L)
val list = everything.toList()
assertNotNull(list)
assertNull(list.first())
assertNotNull(list[1])
assertTrue(list[1] == 123L)
assertEquals(everything.aNullableInt, everything2.aNullableInt) val list2 =
} listOf(
null,
123,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null)
val everything2 = AllNullableTypes.fromList(list2)
assertEquals(everything.aNullableInt, everything2.aNullableInt)
}
} }

View File

@ -9,116 +9,115 @@ import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.slot import io.mockk.slot
import io.mockk.verify import io.mockk.verify
import java.nio.ByteBuffer
import junit.framework.TestCase import junit.framework.TestCase
import org.junit.Test import org.junit.Test
import java.nio.ByteBuffer
internal class AsyncHandlersTest : TestCase() {
@Test
fun testAsyncHost2Flutter() {
val binaryMessenger = mockk<BinaryMessenger>()
val api = FlutterIntegrationCoreApi(binaryMessenger)
internal class AsyncHandlersTest: TestCase() { val value = "Test"
@Test
fun testAsyncHost2Flutter() {
val binaryMessenger = mockk<BinaryMessenger>()
val api = FlutterIntegrationCoreApi(binaryMessenger)
val value = "Test" every { binaryMessenger.send(any(), any(), any()) } answers
{
every { binaryMessenger.send(any(), any(), any()) } answers { val codec = FlutterIntegrationCoreApi.codec
val codec = FlutterIntegrationCoreApi.codec val message = arg<ByteBuffer>(1)
val message = arg<ByteBuffer>(1) val reply = arg<BinaryMessenger.BinaryReply>(2)
val reply = arg<BinaryMessenger.BinaryReply>(2) message.position(0)
message.position(0) val replyData = codec.encodeMessage(listOf(value))
val replyData = codec.encodeMessage(listOf(value)) replyData?.position(0)
replyData?.position(0) reply.reply(replyData)
reply.reply(replyData)
} }
var didCall = false var didCall = false
api.echoAsyncString(value) { api.echoAsyncString(value) {
didCall = true didCall = true
assertEquals(it.getOrNull(), value) assertEquals(it.getOrNull(), value)
}
assertTrue(didCall)
verify {
binaryMessenger.send(
"dev.flutter.pigeon.pigeon_integration_tests.FlutterIntegrationCoreApi.echoAsyncString",
any(),
any()
)
}
} }
@Test assertTrue(didCall)
fun testAsyncFlutter2HostEcho() {
val binaryMessenger = mockk<BinaryMessenger>()
val api = mockk<HostSmallApi>()
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>() verify {
binaryMessenger.send(
"dev.flutter.pigeon.pigeon_integration_tests.FlutterIntegrationCoreApi.echoAsyncString",
any(),
any())
}
}
val input = "Test" @Test
val output = input fun testAsyncFlutter2HostEcho() {
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.HostSmallApi.echo" val binaryMessenger = mockk<BinaryMessenger>()
val api = mockk<HostSmallApi>()
every { val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
binaryMessenger.setMessageHandler("dev.flutter.pigeon.pigeon_integration_tests.HostSmallApi.voidVoid", any())
} returns Unit val input = "Test"
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit val output = input
every { api.echo(any(), any()) } answers { val channelName = "dev.flutter.pigeon.pigeon_integration_tests.HostSmallApi.echo"
val callback = arg<(Result<String>) -> Unit>(1)
callback(Result.success(output)) every {
binaryMessenger.setMessageHandler(
"dev.flutter.pigeon.pigeon_integration_tests.HostSmallApi.voidVoid", any())
} returns Unit
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.echo(any(), any()) } answers
{
val callback = arg<(Result<String>) -> Unit>(1)
callback(Result.success(output))
} }
HostSmallApi.setUp(binaryMessenger, api) HostSmallApi.setUp(binaryMessenger, api)
val codec = HostSmallApi.codec val codec = HostSmallApi.codec
val message = codec.encodeMessage(listOf(input)) val message = codec.encodeMessage(listOf(input))
message?.rewind() message?.rewind()
handlerSlot.captured.onMessage(message) { handlerSlot.captured.onMessage(message) {
assertNotNull(it) assertNotNull(it)
it?.rewind() it?.rewind()
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as MutableList<Any>?
val wrapped = codec.decodeMessage(it) as MutableList<Any>? assertNotNull(wrapped)
assertNotNull(wrapped) wrapped?.let { assertEquals(output, wrapped.first()) }
wrapped?.let {
assertEquals(output, wrapped.first())
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
verify { api.echo(input, any()) }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun asyncFlutter2HostVoidVoid() { verify { api.echo(input, any()) }
val binaryMessenger = mockk<BinaryMessenger>() }
val api = mockk<HostSmallApi>()
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>() @Test
fun asyncFlutter2HostVoidVoid() {
val binaryMessenger = mockk<BinaryMessenger>()
val api = mockk<HostSmallApi>()
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.HostSmallApi.voidVoid" val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit val channelName = "dev.flutter.pigeon.pigeon_integration_tests.HostSmallApi.voidVoid"
every {
binaryMessenger.setMessageHandler("dev.flutter.pigeon.pigeon_integration_tests.HostSmallApi.echo", any()) every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
} returns Unit every {
every { api.voidVoid(any()) } answers { binaryMessenger.setMessageHandler(
val callback = arg<() -> Unit>(0) "dev.flutter.pigeon.pigeon_integration_tests.HostSmallApi.echo", any())
callback() } returns Unit
every { api.voidVoid(any()) } answers
{
val callback = arg<() -> Unit>(0)
callback()
} }
HostSmallApi.setUp(binaryMessenger, api) HostSmallApi.setUp(binaryMessenger, api)
val codec = HostSmallApi.codec val codec = HostSmallApi.codec
val message = codec.encodeMessage(null) val message = codec.encodeMessage(null)
handlerSlot.captured.onMessage(message) { handlerSlot.captured.onMessage(message) {
it?.rewind() it?.rewind()
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as MutableList<Any>?
val wrapped = codec.decodeMessage(it) as MutableList<Any>? assertNull(wrapped)
assertNull(wrapped)
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
verify { api.voidVoid(any()) }
} }
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
verify { api.voidVoid(any()) }
}
} }

View File

@ -6,33 +6,23 @@ package com.example.test_plugin
import io.flutter.plugin.common.BinaryMessenger import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MessageCodec import io.flutter.plugin.common.MessageCodec
import io.flutter.plugin.common.StandardMessageCodec
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.util.ArrayList import java.util.ArrayList
internal class EchoBinaryMessenger(private val codec: MessageCodec<Any?>) : BinaryMessenger {
override fun send(channel: String, message: ByteBuffer?) {
// Method not implemented because this messenger is just for echoing.
}
internal class EchoBinaryMessenger(private val codec: MessageCodec<Any?>): BinaryMessenger { override fun send(channel: String, message: ByteBuffer?, callback: BinaryMessenger.BinaryReply?) {
override fun send(channel: String, message: ByteBuffer?) { message?.rewind()
// Method not implemented because this messenger is just for echoing. val args = codec.decodeMessage(message) as ArrayList<*>
} val replyData = codec.encodeMessage(args)
replyData?.position(0)
override fun send( callback?.reply(replyData)
channel: String, }
message: ByteBuffer?,
callback: BinaryMessenger.BinaryReply?
) {
message?.rewind()
val args = codec.decodeMessage(message) as ArrayList<*>
val replyData = codec.encodeMessage(args)
replyData?.position(0)
callback?.reply(replyData)
}
override fun setMessageHandler(
channel: String,
handler: BinaryMessenger.BinaryMessageHandler?
) {
// Method not implemented because this messenger is just for echoing.
}
override fun setMessageHandler(channel: String, handler: BinaryMessenger.BinaryMessageHandler?) {
// Method not implemented because this messenger is just for echoing.
}
} }

View File

@ -9,69 +9,69 @@ import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.slot import io.mockk.slot
import io.mockk.verify import io.mockk.verify
import junit.framework.TestCase
import org.junit.Test
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.util.ArrayList import java.util.ArrayList
import junit.framework.TestCase
import org.junit.Test
internal class EnumTest: TestCase() { internal class EnumTest : TestCase() {
@Test @Test
fun testEchoHost() { fun testEchoHost() {
val binaryMessenger = mockk<BinaryMessenger>() val binaryMessenger = mockk<BinaryMessenger>()
val api = mockk<EnumApi2Host>() val api = mockk<EnumApi2Host>()
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.EnumApi2Host.echo" val channelName = "dev.flutter.pigeon.pigeon_integration_tests.EnumApi2Host.echo"
val input = DataWithEnum(EnumState.SUCCESS) val input = DataWithEnum(EnumState.SUCCESS)
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>() val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.echo(any()) } returnsArgument 0 every { api.echo(any()) } returnsArgument 0
EnumApi2Host.setUp(binaryMessenger, api) EnumApi2Host.setUp(binaryMessenger, api)
val codec = EnumApi2Host.codec val codec = EnumApi2Host.codec
val message = codec.encodeMessage(listOf(input)) val message = codec.encodeMessage(listOf(input))
message?.rewind() message?.rewind()
handlerSlot.captured.onMessage(message) { handlerSlot.captured.onMessage(message) {
it?.rewind() it?.rewind()
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
val wrapped = codec.decodeMessage(it) as List<Any>? assertNotNull(wrapped)
assertNotNull(wrapped) wrapped?.let {
wrapped?.let { assertNotNull(wrapped[0])
assertNotNull(wrapped[0]) assertEquals(input, wrapped[0])
assertEquals(input, wrapped[0]) }
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
verify { api.echo(input) }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun testEchoFlutter() { verify { api.echo(input) }
val binaryMessenger = mockk<BinaryMessenger>() }
val api = EnumApi2Flutter(binaryMessenger)
val input = DataWithEnum(EnumState.SUCCESS) @Test
fun testEchoFlutter() {
val binaryMessenger = mockk<BinaryMessenger>()
val api = EnumApi2Flutter(binaryMessenger)
every { binaryMessenger.send(any(), any(), any()) } answers { val input = DataWithEnum(EnumState.SUCCESS)
val codec = EnumApi2Flutter.codec
val message = arg<ByteBuffer>(1) every { binaryMessenger.send(any(), any(), any()) } answers
val reply = arg<BinaryMessenger.BinaryReply>(2) {
message.position(0) val codec = EnumApi2Flutter.codec
val args = codec.decodeMessage(message) as ArrayList<*> val message = arg<ByteBuffer>(1)
val replyData = codec.encodeMessage(args) val reply = arg<BinaryMessenger.BinaryReply>(2)
replyData?.position(0) message.position(0)
reply.reply(replyData) val args = codec.decodeMessage(message) as ArrayList<*>
val replyData = codec.encodeMessage(args)
replyData?.position(0)
reply.reply(replyData)
} }
var didCall = false var didCall = false
api.echo(input) { api.echo(input) {
didCall = true didCall = true
assertEquals(input, it.getOrNull()) assertEquals(input, it.getOrNull())
}
assertTrue(didCall)
} }
assertTrue(didCall)
}
} }

View File

@ -7,37 +7,38 @@ package com.example.test_plugin
import io.flutter.plugin.common.BinaryMessenger import io.flutter.plugin.common.BinaryMessenger
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import junit.framework.TestCase
import org.junit.Test
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.util.ArrayList import java.util.ArrayList
import junit.framework.TestCase
import org.junit.Test
class ListTest: TestCase() { class ListTest : TestCase() {
@Test @Test
fun testListInList() { fun testListInList() {
val binaryMessenger = mockk<BinaryMessenger>() val binaryMessenger = mockk<BinaryMessenger>()
val api = FlutterSmallApi(binaryMessenger) val api = FlutterSmallApi(binaryMessenger)
val inside = TestMessage(listOf(1, 2, 3)) val inside = TestMessage(listOf(1, 2, 3))
val input = TestMessage(listOf(inside)) val input = TestMessage(listOf(inside))
every { binaryMessenger.send(any(), any(), any()) } answers { every { binaryMessenger.send(any(), any(), any()) } answers
val codec = FlutterSmallApi.codec {
val message = arg<ByteBuffer>(1) val codec = FlutterSmallApi.codec
val reply = arg<BinaryMessenger.BinaryReply>(2) val message = arg<ByteBuffer>(1)
message.position(0) val reply = arg<BinaryMessenger.BinaryReply>(2)
val args = codec.decodeMessage(message) as ArrayList<*> message.position(0)
val replyData = codec.encodeMessage(args) val args = codec.decodeMessage(message) as ArrayList<*>
replyData?.position(0) val replyData = codec.encodeMessage(args)
reply.reply(replyData) replyData?.position(0)
reply.reply(replyData)
} }
var didCall = false var didCall = false
api.echoWrappedList(input) { api.echoWrappedList(input) {
didCall = true didCall = true
assertEquals(input, it.getOrNull()) assertEquals(input, it.getOrNull())
}
assertTrue(didCall)
} }
assertTrue(didCall)
}
} }

View File

@ -8,69 +8,67 @@ import io.flutter.plugin.common.BinaryMessenger
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.slot import io.mockk.slot
import junit.framework.TestCase
import org.junit.Test
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.util.ArrayList import java.util.ArrayList
import junit.framework.TestCase
import org.junit.Test
class MultipleArityTests: TestCase() { class MultipleArityTests : TestCase() {
@Test @Test
fun testSimpleHost() { fun testSimpleHost() {
val binaryMessenger = mockk<BinaryMessenger>() val binaryMessenger = mockk<BinaryMessenger>()
val api = mockk<MultipleArityHostApi>() val api = mockk<MultipleArityHostApi>()
val inputX = 10L val inputX = 10L
val inputY = 5L val inputY = 5L
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.MultipleArityHostApi.subtract" val channelName = "dev.flutter.pigeon.pigeon_integration_tests.MultipleArityHostApi.subtract"
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>() val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.subtract(any(), any()) } answers { firstArg<Long>() - secondArg<Long>() } every { api.subtract(any(), any()) } answers { firstArg<Long>() - secondArg<Long>() }
MultipleArityHostApi.setUp(binaryMessenger, api) MultipleArityHostApi.setUp(binaryMessenger, api)
val codec = MultipleArityHostApi.codec val codec = MultipleArityHostApi.codec
val message = codec.encodeMessage(listOf(inputX, inputY)) val message = codec.encodeMessage(listOf(inputX, inputY))
message?.rewind() message?.rewind()
handlerSlot.captured.onMessage(message) { handlerSlot.captured.onMessage(message) {
it?.rewind() it?.rewind()
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
val wrapped = codec.decodeMessage(it) as List<Any>? assertNotNull(wrapped)
assertNotNull(wrapped) wrapped?.let { assertEquals(inputX - inputY, wrapped[0]) }
wrapped?.let { }
assertEquals(inputX - inputY, wrapped[0]) }
}
@Test
fun testSimpleFlutter() {
val binaryMessenger = mockk<BinaryMessenger>()
val api = MultipleArityFlutterApi(binaryMessenger)
val inputX = 10L
val inputY = 5L
every { binaryMessenger.send(any(), any(), any()) } answers
{
val codec = MultipleArityFlutterApi.codec
val message = arg<ByteBuffer>(1)
val reply = arg<BinaryMessenger.BinaryReply>(2)
message.position(0)
val args = codec.decodeMessage(message) as ArrayList<*>
val argX = args[0] as Long
val argY = args[1] as Long
val replyData = codec.encodeMessage(listOf(argX - argY))
replyData?.position(0)
reply.reply(replyData)
} }
var didCall = false
api.subtract(inputX, inputY) {
didCall = true
assertEquals(inputX - inputY, it.getOrNull())
} }
@Test assertTrue(didCall)
fun testSimpleFlutter() { }
val binaryMessenger = mockk<BinaryMessenger>()
val api = MultipleArityFlutterApi(binaryMessenger)
val inputX = 10L
val inputY = 5L
every { binaryMessenger.send(any(), any(), any()) } answers {
val codec = MultipleArityFlutterApi.codec
val message = arg<ByteBuffer>(1)
val reply = arg<BinaryMessenger.BinaryReply>(2)
message.position(0)
val args = codec.decodeMessage(message) as ArrayList<*>
val argX = args[0] as Long
val argY = args[1] as Long
val replyData = codec.encodeMessage(listOf(argX - argY))
replyData?.position(0)
reply.reply(replyData)
}
var didCall = false
api.subtract(inputX, inputY) {
didCall = true
assertEquals(inputX - inputY, it.getOrNull())
}
assertTrue(didCall)
}
} }

View File

@ -7,10 +7,10 @@ package com.example.test_plugin
import junit.framework.TestCase import junit.framework.TestCase
import org.junit.Test import org.junit.Test
class NonNullFieldsTests: TestCase() { class NonNullFieldsTests : TestCase() {
@Test @Test
fun testMake() { fun testMake() {
val request = NonNullFieldSearchRequest("hello") val request = NonNullFieldSearchRequest("hello")
assertEquals("hello", request.query) assertEquals("hello", request.query)
} }
} }

View File

@ -12,60 +12,58 @@ import io.mockk.verify
import junit.framework.TestCase import junit.framework.TestCase
import org.junit.Test import org.junit.Test
class NullableReturnsTest: TestCase() { class NullableReturnsTest : TestCase() {
@Test @Test
fun testNullableParameterHost() { fun testNullableParameterHost() {
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true) val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<NullableReturnHostApi>(relaxed = true) val api = mockk<NullableReturnHostApi>(relaxed = true)
val output = 1L val output = 1L
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.NullableReturnHostApi.doit" val channelName = "dev.flutter.pigeon.pigeon_integration_tests.NullableReturnHostApi.doit"
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>() val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.doit() } returns output every { api.doit() } returns output
NullableReturnHostApi.setUp(binaryMessenger, api) NullableReturnHostApi.setUp(binaryMessenger, api)
val codec = PrimitiveHostApi.codec val codec = PrimitiveHostApi.codec
val message = codec.encodeMessage(null) val message = codec.encodeMessage(null)
message?.rewind() message?.rewind()
handlerSlot.captured.onMessage(message) { handlerSlot.captured.onMessage(message) {
it?.rewind() it?.rewind()
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
val wrapped = codec.decodeMessage(it) as List<Any>? assertNotNull(wrapped)
assertNotNull(wrapped) wrapped?.let { assertEquals(output, wrapped[0]) }
wrapped?.let {
assertEquals(output, wrapped[0])
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
verify { api.doit() }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun testNullableParameterFlutter() { verify { api.doit() }
val binaryMessenger = mockk<BinaryMessenger>() }
val api = NullableReturnFlutterApi(binaryMessenger)
val output = 12L @Test
fun testNullableParameterFlutter() {
val binaryMessenger = mockk<BinaryMessenger>()
val api = NullableReturnFlutterApi(binaryMessenger)
every { binaryMessenger.send(any(), any(), any()) } answers { val output = 12L
val codec = NullableReturnFlutterApi.codec
val reply = arg<BinaryMessenger.BinaryReply>(2) every { binaryMessenger.send(any(), any(), any()) } answers
val replyData = codec.encodeMessage(listOf(output)) {
replyData?.position(0) val codec = NullableReturnFlutterApi.codec
reply.reply(replyData) val reply = arg<BinaryMessenger.BinaryReply>(2)
val replyData = codec.encodeMessage(listOf(output))
replyData?.position(0)
reply.reply(replyData)
} }
var didCall = false var didCall = false
api.doit { api.doit {
didCall = true didCall = true
assertEquals(output, it.getOrNull()) assertEquals(output, it.getOrNull())
}
assertTrue(didCall)
} }
assertTrue(didCall)
}
} }

View File

@ -11,424 +11,394 @@ import io.mockk.slot
import io.mockk.verify import io.mockk.verify
import junit.framework.TestCase import junit.framework.TestCase
import org.junit.Test import org.junit.Test
import java.nio.ByteBuffer
import java.util.ArrayList
class PrimitiveTest: TestCase() { class PrimitiveTest : TestCase() {
@Test @Test
fun testIntPrimitiveHost() { fun testIntPrimitiveHost() {
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true) val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true) val api = mockk<PrimitiveHostApi>(relaxed = true)
val input = 1 val input = 1
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.anInt" val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.anInt"
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>() val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.anInt(any()) } returnsArgument 0 every { api.anInt(any()) } returnsArgument 0
PrimitiveHostApi.setUp(binaryMessenger, api) PrimitiveHostApi.setUp(binaryMessenger, api)
val codec = PrimitiveHostApi.codec val codec = PrimitiveHostApi.codec
val message = codec.encodeMessage(listOf(input)) val message = codec.encodeMessage(listOf(input))
message?.rewind() message?.rewind()
handlerSlot.captured.onMessage(message) { handlerSlot.captured.onMessage(message) {
it?.rewind() it?.rewind()
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
val wrapped = codec.decodeMessage(it) as List<Any>? assertNotNull(wrapped)
assertNotNull(wrapped) wrapped?.let { assertEquals(input.toLong(), wrapped[0]) }
wrapped?.let {
assertEquals(input.toLong(), wrapped[0])
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
verify { api.anInt(input.toLong()) }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun testIntPrimitiveFlutter() { verify { api.anInt(input.toLong()) }
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec) }
val api = PrimitiveFlutterApi(binaryMessenger)
val input = 1L @Test
fun testIntPrimitiveFlutter() {
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec)
val api = PrimitiveFlutterApi(binaryMessenger)
var didCall = false val input = 1L
api.anInt(input) {
didCall = true
assertEquals(input, it.getOrNull())
}
assertTrue(didCall) var didCall = false
api.anInt(input) {
didCall = true
assertEquals(input, it.getOrNull())
} }
@Test assertTrue(didCall)
fun testBoolPrimitiveHost() { }
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val input = true @Test
fun testBoolPrimitiveHost() {
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aBool" val input = true
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aBool"
every { api.aBool(any()) } returnsArgument 0 val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
PrimitiveHostApi.setUp(binaryMessenger, api) every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.aBool(any()) } returnsArgument 0
val codec = PrimitiveHostApi.codec PrimitiveHostApi.setUp(binaryMessenger, api)
val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST")
val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let {
assertEquals(input, wrapped[0])
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) } val codec = PrimitiveHostApi.codec
verify { api.aBool(input) } val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let { assertEquals(input, wrapped[0]) }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun testBoolPrimitiveFlutter() { verify { api.aBool(input) }
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec) }
val api = PrimitiveFlutterApi(binaryMessenger)
val input = true @Test
fun testBoolPrimitiveFlutter() {
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec)
val api = PrimitiveFlutterApi(binaryMessenger)
var didCall = false val input = true
api.aBool(input) {
didCall = true
assertEquals(input, it.getOrNull())
}
assertTrue(didCall) var didCall = false
api.aBool(input) {
didCall = true
assertEquals(input, it.getOrNull())
} }
@Test assertTrue(didCall)
fun testStringPrimitiveHost() { }
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val input = "Hello" @Test
fun testStringPrimitiveHost() {
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aString" val input = "Hello"
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aString"
every { api.aString(any()) } returnsArgument 0 val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
PrimitiveHostApi.setUp(binaryMessenger, api) every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.aString(any()) } returnsArgument 0
val codec = PrimitiveHostApi.codec PrimitiveHostApi.setUp(binaryMessenger, api)
val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST")
val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let {
assertEquals(input, wrapped[0])
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) } val codec = PrimitiveHostApi.codec
verify { api.aString(input) } val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let { assertEquals(input, wrapped[0]) }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun testDoublePrimitiveHost() { verify { api.aString(input) }
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true) }
val api = mockk<PrimitiveHostApi>(relaxed = true)
val input = 1.0 @Test
fun testDoublePrimitiveHost() {
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aDouble" val input = 1.0
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aDouble"
every { api.aDouble(any()) } returnsArgument 0 val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
PrimitiveHostApi.setUp(binaryMessenger, api) every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.aDouble(any()) } returnsArgument 0
val codec = PrimitiveHostApi.codec PrimitiveHostApi.setUp(binaryMessenger, api)
val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST")
val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let {
assertEquals(input, wrapped[0])
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) } val codec = PrimitiveHostApi.codec
verify { api.aDouble(input) } val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let { assertEquals(input, wrapped[0]) }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun testDoublePrimitiveFlutter() { verify { api.aDouble(input) }
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec) }
val api = PrimitiveFlutterApi(binaryMessenger)
val input = 1.0 @Test
fun testDoublePrimitiveFlutter() {
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec)
val api = PrimitiveFlutterApi(binaryMessenger)
var didCall = false val input = 1.0
api.aDouble(input) {
didCall = true
assertEquals(input, it.getOrNull())
}
assertTrue(didCall) var didCall = false
api.aDouble(input) {
didCall = true
assertEquals(input, it.getOrNull())
} }
@Test assertTrue(didCall)
fun testMapPrimitiveHost() { }
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val input = mapOf<Any, Any?>("a" to 1, "b" to 2) @Test
fun testMapPrimitiveHost() {
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aMap" val input = mapOf<Any, Any?>("a" to 1, "b" to 2)
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aMap"
every { api.aMap(any()) } returnsArgument 0 val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
PrimitiveHostApi.setUp(binaryMessenger, api) every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.aMap(any()) } returnsArgument 0
val codec = PrimitiveHostApi.codec PrimitiveHostApi.setUp(binaryMessenger, api)
val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST")
val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let {
assertEquals(input, wrapped[0])
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) } val codec = PrimitiveHostApi.codec
verify { api.aMap(input) } val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let { assertEquals(input, wrapped[0]) }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun testMapPrimitiveFlutter() { verify { api.aMap(input) }
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec) }
val api = PrimitiveFlutterApi(binaryMessenger)
val input = mapOf<Any, Any?>("a" to 1, "b" to 2) @Test
fun testMapPrimitiveFlutter() {
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec)
val api = PrimitiveFlutterApi(binaryMessenger)
var didCall = false val input = mapOf<Any, Any?>("a" to 1, "b" to 2)
api.aMap(input) {
didCall = true
assertEquals(input, it.getOrNull())
}
assertTrue(didCall) var didCall = false
api.aMap(input) {
didCall = true
assertEquals(input, it.getOrNull())
} }
@Test assertTrue(didCall)
fun testListPrimitiveHost() { }
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val input = listOf(1, 2, 3) @Test
fun testListPrimitiveHost() {
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aList" val input = listOf(1, 2, 3)
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aList"
every { api.aList(any()) } returnsArgument 0 val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
PrimitiveHostApi.setUp(binaryMessenger, api) every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.aList(any()) } returnsArgument 0
val codec = PrimitiveHostApi.codec PrimitiveHostApi.setUp(binaryMessenger, api)
val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST")
val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let {
assertEquals(input, wrapped[0])
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) } val codec = PrimitiveHostApi.codec
verify { api.aList(input) } val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let { assertEquals(input, wrapped[0]) }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun testListPrimitiveFlutter() { verify { api.aList(input) }
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec) }
val api = PrimitiveFlutterApi(binaryMessenger)
val input = listOf(1, 2, 3) @Test
fun testListPrimitiveFlutter() {
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec)
val api = PrimitiveFlutterApi(binaryMessenger)
var didCall = false val input = listOf(1, 2, 3)
api.aList(input) {
didCall = true
assertEquals(input, it.getOrNull())
}
assertTrue(didCall) var didCall = false
api.aList(input) {
didCall = true
assertEquals(input, it.getOrNull())
} }
@Test assertTrue(didCall)
fun testInt32ListPrimitiveHost() { }
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val input = intArrayOf(1, 2, 3) @Test
fun testInt32ListPrimitiveHost() {
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.anInt32List" val input = intArrayOf(1, 2, 3)
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.anInt32List"
every { api.anInt32List(any()) } returnsArgument 0 val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
PrimitiveHostApi.setUp(binaryMessenger, api) every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.anInt32List(any()) } returnsArgument 0
val codec = PrimitiveHostApi.codec PrimitiveHostApi.setUp(binaryMessenger, api)
val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST")
val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let {
assertTrue(input.contentEquals(wrapped[0] as IntArray))
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) } val codec = PrimitiveHostApi.codec
verify { api.anInt32List(input) } val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let { assertTrue(input.contentEquals(wrapped[0] as IntArray)) }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun testInt32ListPrimitiveFlutter() { verify { api.anInt32List(input) }
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec) }
val api = PrimitiveFlutterApi(binaryMessenger)
val input = intArrayOf(1, 2, 3) @Test
fun testInt32ListPrimitiveFlutter() {
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec)
val api = PrimitiveFlutterApi(binaryMessenger)
var didCall = false val input = intArrayOf(1, 2, 3)
api.anInt32List(input) {
didCall = true
assertTrue(input.contentEquals(it.getOrNull()))
}
assertTrue(didCall) var didCall = false
api.anInt32List(input) {
didCall = true
assertTrue(input.contentEquals(it.getOrNull()))
} }
@Test assertTrue(didCall)
fun testBoolListPrimitiveHost() { }
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val input = listOf(true, false, true) @Test
fun testBoolListPrimitiveHost() {
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aBoolList" val input = listOf(true, false, true)
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aBoolList"
every { api.aBoolList(any()) } returnsArgument 0 val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
PrimitiveHostApi.setUp(binaryMessenger, api) every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.aBoolList(any()) } returnsArgument 0
val codec = PrimitiveHostApi.codec PrimitiveHostApi.setUp(binaryMessenger, api)
val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST")
val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let {
assertEquals(input, wrapped[0])
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) } val codec = PrimitiveHostApi.codec
verify { api.aBoolList(input) } val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let { assertEquals(input, wrapped[0]) }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun testBoolListPrimitiveFlutter() { verify { api.aBoolList(input) }
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec) }
val api = PrimitiveFlutterApi(binaryMessenger)
val input = listOf(true, false, true) @Test
fun testBoolListPrimitiveFlutter() {
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec)
val api = PrimitiveFlutterApi(binaryMessenger)
var didCall = false val input = listOf(true, false, true)
api.aBoolList(input) {
didCall = true
assertEquals(input, it.getOrNull())
}
assertTrue(didCall) var didCall = false
api.aBoolList(input) {
didCall = true
assertEquals(input, it.getOrNull())
} }
@Test assertTrue(didCall)
fun testStringIntMapPrimitiveHost() { }
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val input = mapOf<String?, Long?>("a" to 1, "b" to 2) @Test
fun testStringIntMapPrimitiveHost() {
val binaryMessenger = mockk<BinaryMessenger>(relaxed = true)
val api = mockk<PrimitiveHostApi>(relaxed = true)
val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aStringIntMap" val input = mapOf<String?, Long?>("a" to 1, "b" to 2)
val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit val channelName = "dev.flutter.pigeon.pigeon_integration_tests.PrimitiveHostApi.aStringIntMap"
every { api.aStringIntMap(any()) } returnsArgument 0 val handlerSlot = slot<BinaryMessenger.BinaryMessageHandler>()
PrimitiveHostApi.setUp(binaryMessenger, api) every { binaryMessenger.setMessageHandler(channelName, capture(handlerSlot)) } returns Unit
every { api.aStringIntMap(any()) } returnsArgument 0
val codec = PrimitiveHostApi.codec PrimitiveHostApi.setUp(binaryMessenger, api)
val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST")
val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let {
assertEquals(input, wrapped[0])
}
}
verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) } val codec = PrimitiveHostApi.codec
verify { api.aStringIntMap(input) } val message = codec.encodeMessage(listOf(input))
message?.rewind()
handlerSlot.captured.onMessage(message) {
it?.rewind()
@Suppress("UNCHECKED_CAST") val wrapped = codec.decodeMessage(it) as List<Any>?
assertNotNull(wrapped)
wrapped?.let { assertEquals(input, wrapped[0]) }
} }
@Test verify { binaryMessenger.setMessageHandler(channelName, handlerSlot.captured) }
fun testStringIntMapPrimitiveFlutter() { verify { api.aStringIntMap(input) }
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec) }
val api = PrimitiveFlutterApi(binaryMessenger)
val input = mapOf<String?, Long?>("a" to 1, "b" to 2) @Test
fun testStringIntMapPrimitiveFlutter() {
val binaryMessenger = EchoBinaryMessenger(MultipleArityFlutterApi.codec)
val api = PrimitiveFlutterApi(binaryMessenger)
var didCall = false val input = mapOf<String?, Long?>("a" to 1, "b" to 2)
api.aStringIntMap(input) {
didCall = true
assertEquals(input, it.getOrNull())
}
assertTrue(didCall) var didCall = false
api.aStringIntMap(input) {
didCall = true
assertEquals(input, it.getOrNull())
} }
assertTrue(didCall)
}
} }

View File

@ -6,5 +6,4 @@ package com.example.test_plugin_example
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -6,5 +6,4 @@ package dev.flutter.plaform_example
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -5,5 +5,4 @@ package dev.flutter.rfw.examples.hello
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -5,5 +5,4 @@ package dev.flutter.rfw.examples.local
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -5,5 +5,4 @@ package dev.flutter.rfw.examples.remote
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -6,5 +6,4 @@ package com.example.example
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() { class MainActivity : FlutterActivity() {}
}

View File

@ -32,9 +32,12 @@ const int _exitJavaFormatFailed = 5;
const int _exitGitFailed = 6; const int _exitGitFailed = 6;
const int _exitDependencyMissing = 7; const int _exitDependencyMissing = 7;
const int _exitSwiftFormatFailed = 8; const int _exitSwiftFormatFailed = 8;
const int _exitKotlinFormatFailed = 9;
final Uri _googleFormatterUrl = Uri.https('github.com', final Uri _javaFormatterUrl = Uri.https('github.com',
'/google/google-java-format/releases/download/google-java-format-1.3/google-java-format-1.3-all-deps.jar'); '/google/google-java-format/releases/download/google-java-format-1.3/google-java-format-1.3-all-deps.jar');
final Uri _kotlinFormatterUrl = Uri.https('maven.org',
'/maven2/com/facebook/ktfmt/0.46/ktfmt-0.46-jar-with-dependencies.jar');
/// A command to format all package code. /// A command to format all package code.
class FormatCommand extends PackageCommand { class FormatCommand extends PackageCommand {
@ -62,14 +65,15 @@ class FormatCommand extends PackageCommand {
@override @override
final String description = final String description =
'Formats the code of all packages (Java, Objective-C, C++, Dart, and ' 'Formats the code of all packages (C++, Dart, Java, Kotlin, Objective-C, '
'optionally Swift).\n\n' 'and optionally Swift).\n\n'
'This command requires "git", "flutter" and "clang-format" v5 to be in ' 'This command requires "git", "flutter", "java", and "clang-format" v5 '
'your path.'; 'to be in your path.';
@override @override
Future<void> run() async { Future<void> run() async {
final String googleFormatterPath = await _getGoogleFormatterPath(); final String javaFormatterPath = await _getJavaFormatterPath();
final String kotlinFormatterPath = await _getKotlinFormatterPath();
// This class is not based on PackageLoopingCommand because running the // This class is not based on PackageLoopingCommand because running the
// formatters separately for each package is an order of magnitude slower, // formatters separately for each package is an order of magnitude slower,
@ -77,7 +81,8 @@ class FormatCommand extends PackageCommand {
final Iterable<String> files = final Iterable<String> files =
await _getFilteredFilePaths(getFiles(), relativeTo: packagesDir); await _getFilteredFilePaths(getFiles(), relativeTo: packagesDir);
await _formatDart(files); await _formatDart(files);
await _formatJava(files, googleFormatterPath); await _formatJava(files, javaFormatterPath);
await _formatKotlin(files, kotlinFormatterPath);
await _formatCppAndObjectiveC(files); await _formatCppAndObjectiveC(files);
final String? swiftFormat = getNullableStringArg(_swiftFormatArg); final String? swiftFormat = getNullableStringArg(_swiftFormatArg);
if (swiftFormat != null) { if (swiftFormat != null) {
@ -187,8 +192,7 @@ class FormatCommand extends PackageCommand {
throw ToolExit(_exitDependencyMissing); throw ToolExit(_exitDependencyMissing);
} }
Future<void> _formatJava( Future<void> _formatJava(Iterable<String> files, String formatterPath) async {
Iterable<String> files, String googleFormatterPath) async {
final Iterable<String> javaFiles = final Iterable<String> javaFiles =
_getPathsWithExtensions(files, <String>{'.java'}); _getPathsWithExtensions(files, <String>{'.java'});
if (javaFiles.isNotEmpty) { if (javaFiles.isNotEmpty) {
@ -202,7 +206,7 @@ class FormatCommand extends PackageCommand {
print('Formatting .java files...'); print('Formatting .java files...');
final int exitCode = await _runBatched( final int exitCode = await _runBatched(
java, <String>['-jar', googleFormatterPath, '--replace'], java, <String>['-jar', formatterPath, '--replace'],
files: javaFiles); files: javaFiles);
if (exitCode != 0) { if (exitCode != 0) {
printError('Failed to format Java files: exit code $exitCode.'); printError('Failed to format Java files: exit code $exitCode.');
@ -211,6 +215,30 @@ class FormatCommand extends PackageCommand {
} }
} }
Future<void> _formatKotlin(
Iterable<String> files, String formatterPath) async {
final Iterable<String> kotlinFiles =
_getPathsWithExtensions(files, <String>{'.kt'});
if (kotlinFiles.isNotEmpty) {
final String java = getStringArg('java');
if (!await _hasDependency(java)) {
printError(
'Unable to run "java". Make sure that it is in your path, or '
'provide a full path with --java.');
throw ToolExit(_exitDependencyMissing);
}
print('Formatting .kt files...');
final int exitCode = await _runBatched(
java, <String>['-jar', formatterPath],
files: kotlinFiles);
if (exitCode != 0) {
printError('Failed to format Kotlin files: exit code $exitCode.');
throw ToolExit(_exitKotlinFormatFailed);
}
}
}
Future<void> _formatDart(Iterable<String> files) async { Future<void> _formatDart(Iterable<String> files) async {
final Iterable<String> dartFiles = final Iterable<String> dartFiles =
_getPathsWithExtensions(files, <String>{'.dart'}); _getPathsWithExtensions(files, <String>{'.dart'});
@ -283,7 +311,7 @@ class FormatCommand extends PackageCommand {
(String filePath) => extensions.contains(path.extension(filePath))); (String filePath) => extensions.contains(path.extension(filePath)));
} }
Future<String> _getGoogleFormatterPath() async { Future<String> _getJavaFormatterPath() async {
final String javaFormatterPath = path.join( final String javaFormatterPath = path.join(
path.dirname(path.fromUri(platform.script)), path.dirname(path.fromUri(platform.script)),
'google-java-format-1.3-all-deps.jar'); 'google-java-format-1.3-all-deps.jar');
@ -292,13 +320,29 @@ class FormatCommand extends PackageCommand {
if (!javaFormatterFile.existsSync()) { if (!javaFormatterFile.existsSync()) {
print('Downloading Google Java Format...'); print('Downloading Google Java Format...');
final http.Response response = await http.get(_googleFormatterUrl); final http.Response response = await http.get(_javaFormatterUrl);
javaFormatterFile.writeAsBytesSync(response.bodyBytes); javaFormatterFile.writeAsBytesSync(response.bodyBytes);
} }
return javaFormatterPath; return javaFormatterPath;
} }
Future<String> _getKotlinFormatterPath() async {
final String kotlinFormatterPath = path.join(
path.dirname(path.fromUri(platform.script)),
'ktfmt-0.46-jar-with-dependencies.jar');
final File kotlinFormatterFile =
packagesDir.fileSystem.file(kotlinFormatterPath);
if (!kotlinFormatterFile.existsSync()) {
print('Downloading ktfmt...');
final http.Response response = await http.get(_kotlinFormatterUrl);
kotlinFormatterFile.writeAsBytesSync(response.bodyBytes);
}
return kotlinFormatterPath;
}
/// Returns true if [command] can be run successfully. /// Returns true if [command] can be run successfully.
Future<bool> _hasDependency(String command) async { Future<bool> _hasDependency(String command) async {
// Some versions of Java accept both -version and --version, but some only // Some versions of Java accept both -version and --version, but some only

View File

@ -22,6 +22,7 @@ void main() {
late FormatCommand analyzeCommand; late FormatCommand analyzeCommand;
late CommandRunner<void> runner; late CommandRunner<void> runner;
late String javaFormatPath; late String javaFormatPath;
late String kotlinFormatPath;
setUp(() { setUp(() {
fileSystem = MemoryFileSystem(); fileSystem = MemoryFileSystem();
@ -34,12 +35,16 @@ void main() {
platform: mockPlatform, platform: mockPlatform,
); );
// Create the java formatter file that the command checks for, to avoid // Create the Java and Kotlin formatter files that the command checks for,
// a download. // to avoid a download.
final p.Context path = analyzeCommand.path; final p.Context path = analyzeCommand.path;
javaFormatPath = path.join(path.dirname(path.fromUri(mockPlatform.script)), javaFormatPath = path.join(path.dirname(path.fromUri(mockPlatform.script)),
'google-java-format-1.3-all-deps.jar'); 'google-java-format-1.3-all-deps.jar');
fileSystem.file(javaFormatPath).createSync(recursive: true); fileSystem.file(javaFormatPath).createSync(recursive: true);
kotlinFormatPath = path.join(
path.dirname(path.fromUri(mockPlatform.script)),
'ktfmt-0.46-jar-with-dependencies.jar');
fileSystem.file(kotlinFormatPath).createSync(recursive: true);
runner = CommandRunner<void>('format_command', 'Test for format_command'); runner = CommandRunner<void>('format_command', 'Test for format_command');
runner.addCommand(analyzeCommand); runner.addCommand(analyzeCommand);
@ -428,6 +433,62 @@ void main() {
])); ]));
}); });
group('kotlin-format', () {
test('formats .kt files', () async {
const List<String> files = <String>[
'android/src/main/kotlin/io/flutter/plugins/a_plugin/a.kt',
'android/src/main/kotlin/io/flutter/plugins/a_plugin/b.kt',
];
final RepositoryPackage plugin = createFakePlugin(
'a_plugin',
packagesDir,
extraFiles: files,
);
await runCapturingPrint(runner, <String>['format']);
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
const ProcessCall('java', <String>['-version'], null),
ProcessCall(
'java',
<String>[
'-jar',
kotlinFormatPath,
...getPackagesDirRelativePaths(plugin, files)
],
packagesDir.path),
]));
});
test('fails if Kotlin formatter fails', () async {
const List<String> files = <String>[
'android/src/main/kotlin/io/flutter/plugins/a_plugin/a.kt',
'android/src/main/kotlin/io/flutter/plugins/a_plugin/b.kt',
];
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
processRunner.mockProcessesForExecutable['java'] = <FakeProcessInfo>[
FakeProcessInfo(
MockProcess(), <String>['-version']), // check for working java
FakeProcessInfo(MockProcess(exitCode: 1), <String>['-jar']), // format
];
Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['format'], errorHandler: (Error e) {
commandError = e;
});
expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains('Failed to format Kotlin files: exit code 1.'),
]));
});
});
group('swift-format', () { group('swift-format', () {
test('formats Swift if --swift-format flag is provided', () async { test('formats Swift if --swift-format flag is provided', () async {
const List<String> files = <String>[ const List<String> files = <String>[