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

Test code lenses within packages #971

Merged
merged 1 commit into from
Oct 8, 2019
Merged
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
132 changes: 84 additions & 48 deletions tests/unit/src/test/scala/tests/CodeLensesSlowSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,57 @@ package tests
import scala.concurrent.Future

object CodeLensesSlowSuite extends BaseSlowSuite("codeLenses") {
check("main")(
check("empty-package")(
"""<<run>>
|object Main {
| def main(args: Array[String]): Unit = {}
|}
|""".stripMargin
)

check("class")(
"""class Main {
| def main(args: Array[String]): Unit = {}
|}
|""".stripMargin
)

check("main")(
"""package foo
|<<run>>
|object Main {
| def main(args: Array[String]): Unit = {}
|}
|""".stripMargin
)

check("non-ascii")(
"""<<run>>
"""package foo.bar
|<<run>>
|object :: {
| def main(args: Array[String]): Unit = {}
|}
|""".stripMargin
)

check("test-suite-class", library = "org.scalatest::scalatest:3.0.5")(
"""|<<test>>
"""|package foo.bar
|<<test>>
|class Foo extends org.scalatest.FunSuite {
| test("foo") {}
|}
|""".stripMargin
)

check("test-suite-object", library = "com.lihaoyi::utest:0.7.1")(
"""|<<test>>
"""|package foo.bar
|<<test>>
|object Foo extends utest.TestSuite {
| val tests = utest.Tests {}
|}
|""".stripMargin
)

def check(name: String, library: String = "")(expected: String): Unit =
testAsync(name) {
val original = expected.replaceAll("<<.*>>", "")
val dependencies =
if (library.isEmpty) ""
else s""""libraryDependencies": [ "$library" ]"""

for {
_ <- server.initialize(
s"""|/metals.json
|{
| "a": { $dependencies }
|}
|
|/a/src/main/scala/Foo.scala
|$original
|""".stripMargin
)
_ <- assertCodeLenses("a/src/main/scala/Foo.scala", expected)
} yield ()
}

private def assertCodeLenses(
relativeFile: String,
expected: String
): Future[Unit] = {
val path = server.toPath(relativeFile)
for {
_ <- server.server.compilations.compileFiles(List(path))
_ <- server.server.compilations.compileFiles(List(path))
obtained <- server.codeLenses(relativeFile)
} yield assertNoDiff(obtained, expected)
}

testAsync("run-many-main-files") {
for {
_ <- server.initialize(
Expand All @@ -77,11 +62,13 @@ object CodeLensesSlowSuite extends BaseSlowSuite("codeLenses") {
|}
|
|/a/src/main/scala/Foo.scala
|package foo.bar
|object Foo {
| def main(args: Array[String]): Unit = {}
|}
|
|/a/src/main/scala/Bar.scala
|package foo.bar
|object Bar {
| def main(args: Array[String]): Unit = {}
|}
Expand All @@ -90,19 +77,21 @@ object CodeLensesSlowSuite extends BaseSlowSuite("codeLenses") {
_ <- server.didOpen("a/src/main/scala/Foo.scala") // compile `a` to populate its cache
_ <- assertCodeLenses(
"a/src/main/scala/Foo.scala",
"""<<run>>
|object Foo {
| def main(args: Array[String]): Unit = {}
|}
|""".stripMargin
"""|package foo.bar
|<<run>>
|object Foo {
| def main(args: Array[String]): Unit = {}
|}
|""".stripMargin
)
_ <- assertCodeLenses(
"a/src/main/scala/Bar.scala",
"""<<run>>
|object Bar {
| def main(args: Array[String]): Unit = {}
|}
|""".stripMargin
"""|package foo.bar
|<<run>>
|object Bar {
| def main(args: Array[String]): Unit = {}
|}
|""".stripMargin
)
} yield ()
}
Expand Down Expand Up @@ -169,4 +158,51 @@ object CodeLensesSlowSuite extends BaseSlowSuite("codeLenses") {

} yield ()
}

def check(name: String, library: String = "")(expected: String): Unit = {
testAsync(name) {
val original = expected.replaceAll("<<.*>>\\W", "")

val sourceFile = {
val file = """package (.*).*""".r
.findFirstMatchIn(original)
.map(_.group(1))
.map(packageName => packageName.replaceAll("\\.", "/"))
.map(packageDir => s"$packageDir/Foo.scala")
.getOrElse("Foo.scala")

s"a/src/main/scala/$file"
}

val dependencies =
if (library.isEmpty) ""
else s""""libraryDependencies": [ "$library" ]"""

for {
_ <- server.initialize(
s"""|/metals.json
|{
| "a": { $dependencies }
|}
|
|/$sourceFile
|$original
|""".stripMargin
)
_ <- assertCodeLenses(sourceFile, expected)
} yield ()
}
}

private def assertCodeLenses(
relativeFile: String,
expected: String
): Future[Unit] = {
val path = server.toPath(relativeFile)
for {
_ <- server.server.compilations.compileFiles(List(path))
_ <- server.server.compilations.compileFiles(List(path))
obtained <- server.codeLenses(relativeFile)
} yield assertNoDiff(obtained, expected)
}
}