Skip to content

Commit

Permalink
Add test examples for non-ideal situations (#488)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #488

Reviewed By: strulovich

Differential Revision: D58881706

Pulled By: hick209

fbshipit-source-id: ea59e78846b192588e2526bff4788a3df70e6328
  • Loading branch information
snowe2010 authored and facebook-github-bot committed Aug 8, 2024
1 parent 94bb2fa commit 937d21d
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
74 changes: 74 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Input>().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<Input>().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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Request>(inputText) }
| .mapCatching {
| requireNotNull(it.body) {
| "Request#body must not be null or empty"
| }
| it.body!!
| }
| .fold({ Success(it) }, { Failure(it) })
| } else {
| runCatching {
| json.decodeFromString<AnotherRequest>(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(
Expand Down

0 comments on commit 937d21d

Please sign in to comment.