Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test examples for non-ideal situations #488

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,81 @@ class FormatterTest {
|"""
.trimMargin())

@Test
@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(),
formattingOptions = FormattingOptions(
blockIndent = 4,
continuationIndent = 4,
manageTrailingCommas = false
),
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,66 @@ class GoogleStyleFormatterKtTest {
.trimMargin(),
)

@Test
fun `long call chains in named parameters`() =
hick209 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading