Skip to content

Commit

Permalink
introduce BaseRangeFormatterSuite for common checker logic
Browse files Browse the repository at this point in the history
  • Loading branch information
majk-p authored and tgodzik committed Feb 2, 2025
1 parent 8195f99 commit 74dcbb9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 85 deletions.
44 changes: 44 additions & 0 deletions tests/unit/src/main/scala/tests/BaseRangeFormatterSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package tests

import munit.Location
import munit.TestOptions
import org.eclipse.lsp4j.FormattingOptions

abstract class BaseRangeFormatterSuite(name: String)
extends BaseLspSuite(name) {
def check(
name: TestOptions,
testCase: String,
paste: String,
expectedCase: String,
)(implicit loc: Location): Unit = {
val tripleQuote = "\"\"\""
def unmangle(string: String): String =
string.replaceAll("'''", tripleQuote)

val testCode = unmangle(testCase)
val base = testCode.replaceAll("(@@)", "")
val expected = unmangle(expectedCase)
test(name) {
for {
_ <- initialize(
s"""/metals.json
|{"a":{}}
|/a/src/main/scala/a/Main.scala
|""".stripMargin + base
)
_ <- server.didOpen("a/src/main/scala/a/Main.scala")
_ <- server.rangeFormatting(
"a/src/main/scala/a/Main.scala",
testCode, // bez @@
expected,
unmangle(paste),
workspace,
Some(formattingOptions),
)
} yield ()
}
}

val formattingOptions = new FormattingOptions(2, true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@ package tests.rangeFormatting

import scala.meta.internal.metals.UserConfiguration

import munit.Location
import munit.TestOptions
import org.eclipse.lsp4j.FormattingOptions
import tests.BaseLspSuite
import tests.BaseRangeFormatterSuite

class MultilineStringRangeFormattingWhenPastingSuite
extends BaseLspSuite("rangeFormatting") {
val formattingOptions: FormattingOptions = new FormattingOptions(
/** tabSize: */
2,
/** insertSpaces */
true,
)
extends BaseRangeFormatterSuite("rangeFormatting") {

override def userConfig: UserConfiguration =
super.userConfig.copy(enableIndentOnPaste = true)
Expand Down Expand Up @@ -352,37 +343,4 @@ class MultilineStringRangeFormattingWhenPastingSuite
|}""".stripMargin,
)

def check(
name: TestOptions,
testCase: String,
paste: String,
expectedCase: String,
)(implicit loc: Location): Unit = {
val tripleQuote = "\"\"\""
def unmangle(string: String): String =
string.replaceAll("'''", tripleQuote)

val testCode = unmangle(testCase)
val base = testCode.replaceAll("(@@)", "")
val expected = unmangle(expectedCase)
test(name) {
for {
_ <- initialize(
s"""/metals.json
|{"a":{}}
|/a/src/main/scala/a/Main.scala
|""".stripMargin + base
)
_ <- server.didOpen("a/src/main/scala/a/Main.scala")
_ <- server.rangeFormatting(
"a/src/main/scala/a/Main.scala",
testCode, // bez @@
expected,
unmangle(paste),
workspace,
Some(formattingOptions),
)
} yield ()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package tests.rangeFormatting

import munit.Location
import munit.TestOptions
import org.eclipse.lsp4j.FormattingOptions
import tests.BaseLspSuite
import tests.BaseRangeFormatterSuite

class ScalaCliDependencyRangeFormatterPastingSuite
extends BaseLspSuite("MillifyRangeFormatting") {
extends BaseRangeFormatterSuite("MillifyRangeFormatting") {

check(
"change-dep-format-on-paste",
Expand Down Expand Up @@ -61,40 +58,4 @@ class ScalaCliDependencyRangeFormatterPastingSuite
|}""".stripMargin,
)

val formattingOptions = new FormattingOptions(2, true)

// TODO this function should be deduplicated across suites
def check(
name: TestOptions,
testCase: String,
paste: String,
expectedCase: String,
)(implicit loc: Location): Unit = {
val tripleQuote = "\"\"\""
def unmangle(string: String): String =
string.replaceAll("'''", tripleQuote)

val testCode = unmangle(testCase)
val base = testCode.replaceAll("(@@)", "")
val expected = unmangle(expectedCase)
test(name) {
for {
_ <- initialize(
s"""/metals.json
|{"a":{}}
|/a/src/main/scala/a/Main.scala
|""".stripMargin + base
)
_ <- server.didOpen("a/src/main/scala/a/Main.scala")
_ <- server.rangeFormatting(
"a/src/main/scala/a/Main.scala",
testCode, // bez @@
expected,
unmangle(paste),
workspace,
Some(formattingOptions),
)
} yield ()
}
}
}

0 comments on commit 74dcbb9

Please sign in to comment.