Skip to content

Commit

Permalink
improvement: support "behavior of" syntax in ScalaTest's FlatSpec sty…
Browse files Browse the repository at this point in the history
…le (#6235)

* improvement: support "behavior of" syntax in ScalaTest's FlatSpec style

* fix formatting
  • Loading branch information
piter75 authored Mar 20, 2024
1 parent acae2b9 commit 6ef8385
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ object ScalatestTestFinder {
// collect all entries like test("testname") { ... }
template.stats.collect {
// format: off
case Term.Apply(appl @ Term.Apply(Term.Name(funName), Lit.String(testname) :: _), _)
case Term.Apply(appl @ Term.Apply(Term.Name(funName), Lit.String(testname) :: _), _)
if style.leafMethods.contains(funName) =>
// format: on
TestCaseEntry(testname, appl.pos.toLsp.toLocation(path.toURI))
Expand All @@ -139,7 +139,7 @@ object ScalatestTestFinder {
stats.flatMap {
// format: off
// gather intermediate name parts
case Term.ApplyInfix(Lit.String(lhs), Term.Name(infixOp), _, List(Term.Block(stats)))
case Term.ApplyInfix(Lit.String(lhs), Term.Name(infixOp), _, List(Term.Block(stats)))
if style.intermediateMethods.contains(infixOp) =>
// format: on
val newNamePrefix =
Expand All @@ -149,8 +149,8 @@ object ScalatestTestFinder {

// format: off
// gather leaf name part and collect test entry
case Term.ApplyInfix(lhs: Lit.String, Term.Name(infixOp), _, _)
if style.leafMethods.contains(infixOp) =>
case Term.ApplyInfix(lhs: Lit.String, Term.Name(infixOp), _, _)
if style.leafMethods.contains(infixOp) =>
// format: on
val testname = namePrefix.appended(lhs.value).mkString(" ")
TestCaseEntry(testname, lhs.pos.toLsp.toLocation(path.toURI)) :: acc
Expand All @@ -173,9 +173,15 @@ object ScalatestTestFinder {
(List.empty[TestCaseEntry], Option.empty[String])
) { case ((acc, namePrefix), stat) =>
stat match {
// format: off
// behavior of "An empty Set"
case Term.ApplyInfix(Term.Name("behavior"), Term.Name("of"), _, List(Lit.String(newPrefix))) =>
(acc, Some(newPrefix))
// format: on

// format: off
// "An empty Set" should "have size 0" in { ... }
case Term.ApplyInfix(appl @ Term.ApplyInfix(Lit.String(newPrefix), Term.Name(infixOp), _, List(Lit.String(right))), _: Term.Name, _, _) =>
case Term.ApplyInfix(appl @ Term.ApplyInfix(Lit.String(newPrefix), Term.Name(infixOp), _, List(Lit.String(right))), _: Term.Name, _, _) =>
// format: on
val testname = s"$newPrefix $infixOp $right"
val test =
Expand All @@ -184,7 +190,7 @@ object ScalatestTestFinder {

// format: off
// it should "have size 0" in { ... } - replace it with encountered name or leave empty
case Term.ApplyInfix(appl @ Term.ApplyInfix(Term.Name("it") | Term.Name("ignore"), Term.Name(infixOp), _, List(Lit.String(right))), _: Term.Name, _, _) =>
case Term.ApplyInfix(appl @ Term.ApplyInfix(Term.Name("it") | Term.Name("ignore"), Term.Name(infixOp), _, List(Lit.String(right))), _: Term.Name, _, _) =>
// format: on
val prefix = namePrefix.fold("")(_ + " ")
val testname = s"$prefix$infixOp $right"
Expand Down Expand Up @@ -215,14 +221,14 @@ object ScalatestTestFinder {
stats.flatMap {
// format: off
// gather name part from describe
case Term.Apply(Term.Apply(Term.Name("describe") | Term.Name("ignore"), (prefix: Lit.String) :: Nil), (block: Term.Block) :: Nil) =>
case Term.Apply(Term.Apply(Term.Name("describe") | Term.Name("ignore"), (prefix: Lit.String) :: Nil), (block: Term.Block) :: Nil) =>
// format: on
(acc, sharedPrefix :+ prefix.value)
loop(block.stats, sharedPrefix :+ prefix.value, acc)

// format: off
// collect test entry from it
case Term.Apply(appl @ Term.Apply(Term.Name("it"), (name: Lit.String) :: Nil), _) =>
case Term.Apply(appl @ Term.Apply(Term.Name("it"), (name: Lit.String) :: Nil), _) =>
// format: on
val testname = s"$prefix ${name.value}"
val test =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ class ScalatestFinderSuite extends FunSuite {
| ignore should "have size 1" in {
| assert(Set.empty.size == 1)
| }
|
| behavior of "Non-empty Set"
|
| it should "have size greater than 0" in {
| assert(Set(1).size > 0)
| }
|}
|""".stripMargin,
FullyQualifiedName("FlatSpec"),
Expand All @@ -118,6 +124,10 @@ class ScalatestFinderSuite extends FunSuite {
QuickRange(8, 2, 8, 65),
),
("An empty Set should have size 0", QuickRange(4, 2, 4, 37)),
(
"Non-empty Set should have size greater than 0",
QuickRange(20, 2, 20, 38),
),
),
ScalatestStyle.AnyFlatSpec,
)
Expand Down

0 comments on commit 6ef8385

Please sign in to comment.