From 937d21d0c62ab4b699b2d6a54a9a7b8c1e288b2c Mon Sep 17 00:00:00 2001 From: Tyler Thrailkill Date: Thu, 8 Aug 2024 07:10:21 -0700 Subject: [PATCH] Add test examples for non-ideal situations (#488) Summary: Pull Request resolved: https://github.com/facebook/ktfmt/pull/488 Reviewed By: strulovich Differential Revision: D58881706 Pulled By: hick209 fbshipit-source-id: ea59e78846b192588e2526bff4788a3df70e6328 --- .../facebook/ktfmt/format/FormatterTest.kt | 74 +++++++++++++++++++ .../format/GoogleStyleFormatterKtTest.kt | 62 ++++++++++++++++ 2 files changed, 136 insertions(+) diff --git a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt index 8f424f02..4e818422 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt @@ -1585,6 +1585,80 @@ class FormatterTest { |""" .trimMargin()) + @Test + fun `Trailing comma forces variable value in list onto new line with manageTrailingCommas turned off`() = + assertFormatted( + """ + val aVar = + setOf( + Env.Dev, + Env.Prod, + ) + val aVar = setOf(Env.Dev, Env.Prod) + + """ + .trimIndent(), + deduceMaxWidth = false, + ) + + @Test + fun `nested functions, maps, and statements in named parameters - default`() = + assertFormatted( + """ + |//////////////////////////////////////////////////////////////////// + |function( + | param = + | (rate downTo min step step).drop(1).map { + | nestedFun( + | rate = + | rate( + | value = + | firstArg().info.get(0).rate.value)) + | }) + |""" + .trimMargin(), + deduceMaxWidth = true, + ) + + @Test + fun `nested functions, maps, and statements in named parameters kotlin lang formats differently than default`() = + assertFormatted( + """ + |///////////////////////////////////////////////////////////////////// + |function( + | param = + | (rate downTo min step step).drop(1).map { + | nestedFun( + | rate = + | rate( + | value = + | firstArg().info.get(0).rate.value + | ) + | ) + | } + |) + |""" + .trimMargin(), + formattingOptions = Formatter.KOTLINLANG_FORMAT, + deduceMaxWidth = true, + ) + + @Test + fun `complex calls and calculation in named parameters without wrapping`() = + assertFormatted( + """ + calculateMath( + r = apr.sc(10) / BigDecimal(100) / BigDecimal(12), + n = 12 * term, + numerator = ((BigDecimal.ONE + r).pow(n)) - BigDecimal.ONE, + denominator = r * (BigDecimal.ONE + r).pow(n), + ) + + """ + .trimIndent(), + deduceMaxWidth = false, + ) + @Test fun `Arguments are blocks`() = assertFormatted( diff --git a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt index 2e52c52b..a8078c61 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt @@ -587,6 +587,68 @@ class GoogleStyleFormatterKtTest { .trimMargin(), ) + @Test + fun `long call chains in named parameters`() = + assertFormatted( + """ + |///////////////////////////////////////////////// + |declareOne( + | kind = DeclarationKind.FIELD, + | modifiers = property.modifierList, + | valOrVarKeyword = + | property.valOrVarKeyword.text, + | multiline = + | property.one.two.three.four.five.six.seven + | .eight + | .nine + | .ten, + | typeParametersBlaBla = + | property.typeParameterList, + | receiver = property.receiverTypeReference, + | name = property.nameIdentifier?.text, + | type = property.typeReference, + | typeConstraintList = + | property.typeConstraintList, + | delegate = property.delegate, + | initializer = property.initializer, + |) + |""" + .trimMargin(), + deduceMaxWidth = true) + + @Test + fun `'if' expression functions wraps to next line`() = + assertFormatted( + """ + |////////////////////////////////////////////////////////////////// + |private fun parseRequest( + | isWrapped: Boolean, + | json: Json, + | inputText: String, + |) = + | if (isWrapped) { + | runCatching { json.decodeFromString(inputText) } + | .mapCatching { + | requireNotNull(it.body) { + | "Request#body must not be null or empty" + | } + | it.body!! + | } + | .fold({ Success(it) }, { Failure(it) }) + | } else { + | runCatching { + | json.decodeFromString(inputText) + | } + | .fold({ Success(it) }, { Failure(it) }) + | } + | .mapFailure { + | // slightly long text here that is an example of a comment + | Response(false, 400, listOfNotNull(it.message)) + | } + |""" + .trimMargin(), + deduceMaxWidth = true) + @Test fun `Arguments are blocks`() = assertFormatted(