Skip to content

Commit

Permalink
improvement: add support for weaver cats effects
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Jan 31, 2024
1 parent 0ea507c commit 3204a2f
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,15 @@ object TestFramework {
case "JUnit" => JUnit4
case "munit" => MUnit
case "ScalaTest" => Scalatest
case "weaver-cats-effect" => WeaverCatsEffect
case _ => Unknown
}
.getOrElse(Unknown)
}
case object JUnit4 extends TestFramework(true)
case object MUnit extends TestFramework(true)
case object Scalatest extends TestFramework(true)
case object WeaverCatsEffect extends TestFramework(true)
case object Unknown extends TestFramework(false)

object BuildTargetClasses {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import scala.meta.internal.metals.debug.MUnit
import scala.meta.internal.metals.debug.Scalatest
import scala.meta.internal.metals.debug.TestFramework
import scala.meta.internal.metals.debug.Unknown
import scala.meta.internal.metals.debug.WeaverCatsEffect
import scala.meta.internal.metals.testProvider.TestExplorerEvent._
import scala.meta.internal.metals.testProvider.frameworks.JunitTestFinder
import scala.meta.internal.metals.testProvider.frameworks.MunitTestFinder
import scala.meta.internal.metals.testProvider.frameworks.ScalatestTestFinder
import scala.meta.internal.metals.testProvider.frameworks.WeaverCatsEffectTestFinder
import scala.meta.internal.mtags
import scala.meta.internal.mtags.GlobalSymbolIndex
import scala.meta.internal.mtags.Semanticdbs
Expand Down Expand Up @@ -66,6 +68,8 @@ final class TestSuitesProvider(
new MunitTestFinder(trees, symbolIndex, semanticdbs)
private val scalatestTestFinder =
new ScalatestTestFinder(trees, symbolIndex, semanticdbs)
private val weaverCatsEffect =
new WeaverCatsEffectTestFinder(trees, symbolIndex, semanticdbs)

private def isExplorerEnabled = clientConfig.isTestExplorerProvider() &&
userConfig().testUserInterface == TestUserInterfaceKind.TestExplorer
Expand Down Expand Up @@ -322,6 +326,13 @@ final class TestSuitesProvider(
suiteName = suite.fullyQualifiedName,
symbol = suite.symbol,
)
case WeaverCatsEffect =>
weaverCatsEffect.findTests(
doc = semanticdb,
path = path,
suiteName = suite.fullyQualifiedName,
symbol = suite.symbol,
)
case Unknown => Vector.empty
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ class MunitTestFinder(
) {

// depending on the munit version test method symbol varies.
private val baseParentClasses = Set("munit/BaseFunSuite#", "munit/FunSuite#")
private val testMethodSymbols = baseParentClasses.map(_ + "test")
protected val baseParentClasses: Set[String] =
Set("munit/BaseFunSuite#", "munit/FunSuite#")
protected val testFunctionsNames: Set[String] = Set("test")
private def testMethodSymbols = baseParentClasses.flatMap(base =>
testFunctionsNames.map(func => base + func)
)

/**
* Find test cases for the given suite.
Expand Down Expand Up @@ -198,7 +202,8 @@ class MunitTestFinder(
def loop(acc: List[Tree]): Option[(Term.Name, Lit.String)] = acc match {
case head :: tail =>
head match {
case Term.Apply(term @ Term.Name("test"), args) =>
case Term.Apply(term @ Term.Name(name), args)
if (testFunctionsNames(name)) =>
extractLiteralName(args) match {
case Some(lit) =>
Some((term, lit))
Expand Down Expand Up @@ -243,7 +248,7 @@ class MunitTestFinder(
def loop(acc: List[Tree]): Boolean = acc match {
case head :: tail =>
head match {
case term @ Term.Name("test") =>
case term @ Term.Name(name) if (testFunctionsNames(name)) =>
val range = term.pos.toSemanticdb
val isValid = occurences
.exists(occ => occ.range.exists(_.isEqual(range)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import scala.meta.Pkg
import scala.meta.Template
import scala.meta.Term
import scala.meta.Tree
import scala.meta.Type

private[frameworks] object TreeUtils {

Expand All @@ -31,10 +30,13 @@ private[frameworks] object TreeUtils {
): Option[Template] = {
t match {
case cls: Defn.Class
if isValid(cls.name, currentPackage, fullyQualifiedName) =>
if isValid(cls.name.value, currentPackage, fullyQualifiedName) =>
Some(cls.templ)
case obj: Defn.Object
if isValid(obj.name.value, currentPackage, fullyQualifiedName) =>
Some(obj.templ)
case trt: Defn.Trait
if isValid(trt.name, currentPackage, fullyQualifiedName) =>
if isValid(trt.name.value, currentPackage, fullyQualifiedName) =>
Some(trt.templ)
// short-circuit to not go deeper into unuseful defns
case _: Defn => None
Expand Down Expand Up @@ -62,11 +64,11 @@ private[frameworks] object TreeUtils {
* Class definition is valid when package + class name is equal to one we are looking for
*/
private def isValid(
name: Type.Name,
name: String,
currentPackage: Vector[String],
searched: String,
): Boolean = {
val fullyQualifiedName = currentPackage.appended(name.value).mkString(".")
val fullyQualifiedName = currentPackage.appended(name).mkString(".")
fullyQualifiedName == searched
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package scala.meta.internal.metals.testProvider.frameworks

import scala.meta.internal.mtags.GlobalSymbolIndex
import scala.meta.internal.mtags.Semanticdbs
import scala.meta.internal.parsing.Trees

class WeaverCatsEffectTestFinder(
trees: Trees,
symbolIndex: GlobalSymbolIndex,
semanticdbs: Semanticdbs,
) extends MunitTestFinder(trees, symbolIndex, semanticdbs) {
override protected val baseParentClasses: Set[String] =
Set("weaver/MutableFSuite#", "weaver/FunSuiteF#")
override protected val testFunctionsNames: Set[String] =
Set("test", "pureTest", "loggedTest")
}
3 changes: 3 additions & 0 deletions tests/unit/src/main/scala/tests/QuickBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ object QuickBuild {
),
"org.scalameta::munit" -> Config.TestFramework.munit,
"junit:junit" -> Config.TestFramework.JUnit,
"com.disneystreaming::weaver-cats" -> Config.TestFramework(
List("weaver.framework.CatsEffect")
),
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,62 @@ class TestSuitesProviderSuite extends BaseLspSuite("testSuitesFinderSuite") {
},
)

checkEvents(
"weaver",
List(
"com.disneystreaming::weaver-cats:0.8.4"
),
s"""|/app/src/main/scala/a/b/WeaverTest.scala
|package a.b
|import cats.effect.IO
|import weaver.SimpleIOSuite
|
|object WeaverTest extends SimpleIOSuite {
|
| test("example-test") {
| for {
| _ <- IO.raiseError(new RuntimeException("test"))
| } yield expect.same(true, true)
| }
|}
|
|""".stripMargin,
"app/src/main/scala/a/b/WeaverTest.scala",
() => {
List(
rootBuildTargetUpdate(
"app",
targetUri,
List[TestExplorerEvent](
AddTestSuite(
"a.b.WeaverTest",
"WeaverTest",
"a/b/WeaverTest.",
QuickLocation(
classUriFor("app/src/main/scala/a/b/WeaverTest.scala"),
(4, 7, 4, 17),
).toLsp,
canResolveChildren = true,
),
AddTestCases(
"a.b.WeaverTest",
"WeaverTest",
List(
TestCaseEntry(
"example-test",
QuickLocation(
classUriFor("app/src/main/scala/a/b/WeaverTest.scala"),
(6, 2, 6, 6),
).toLsp,
)
).asJava,
),
).asJava,
)
)
},
)

/**
* Discovers all tests in project or test cases in file
*
Expand Down

0 comments on commit 3204a2f

Please sign in to comment.