Skip to content

Commit b57fc5f

Browse files
authored
Merge pull request #1336 from yadavan88/validate-test-names
Validation for unit test names
2 parents fd1f1a1 + b32a366 commit b57fc5f

File tree

138 files changed

+300
-171
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+300
-171
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ lazy val spark_scala = (project in file("spark-scala"))
715715

716716
addCommandAlias(
717717
"ci",
718-
";compile;test:compile;it:compile;scalafmtCheckAll;test"
718+
";compile;test:compile;it:compile;scalafmtCheckAll;validateUnitTestNames;test"
719719
)
720720

721721
addCommandAlias(

cats-effects/src/test/scala/com/baeldung/scala/catseffects/CancellationSpec.scala renamed to cats-effects/src/test/scala/com/baeldung/scala/catseffects/CancellationUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import java.util.concurrent.atomic.AtomicBoolean
1010

1111
import scala.concurrent.duration.DurationInt
1212

13-
class CancellationSpec extends AnyWordSpec with Matchers {
13+
class CancellationUnitTest extends AnyWordSpec with Matchers {
1414

1515
"Cancellation" should {
1616
"cancel the fiber directly and execute the action on cancellation" in {

cats-effects/src/test/scala/com/baeldung/scala/catseffects/ErrorHandlingSpec.scala renamed to cats-effects/src/test/scala/com/baeldung/scala/catseffects/ErrorHandlingUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.scalatest.wordspec.AnyWordSpec
1010
import scala.util.Try
1111
import scala.util.control.NoStackTrace;
1212

13-
class ErrorHandlingSpec extends AnyWordSpec with Matchers {
13+
class ErrorHandlingUnitTest extends AnyWordSpec with Matchers {
1414

1515
"Calculator" should {
1616
"return left value using attempt in case of division by zero" in {

internal-scripts/ClassRenamer.scala

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/** This is an internal scala-cli script written just to reduce manual work of
2+
* renaming files and class names to follow naming standards. This doesn't
3+
* fully correct all the class names, however reduces the manual effort by 90%.
4+
*/
5+
//> using toolkit default
6+
import os._
7+
8+
object RenameClassNames {
9+
def main(args: Array[String]): Unit = {
10+
val directory = os.pwd / os.up
11+
12+
os.walk(directory)
13+
.filter(_.ext == "scala")
14+
.filter(_.toString.contains("src/test/scala"))
15+
.filterNot(_.toString.endsWith("UnitTest.scala"))
16+
.filter(f =>
17+
f.toString.endsWith("Test.scala") || f.toString.endsWith(
18+
"Spec.scala"
19+
) || f.toString.endsWith("Tests.scala") || f.toString
20+
.endsWith("Suite.scala")
21+
)
22+
.foreach { _filePath =>
23+
val _fileName = _filePath.last
24+
25+
val newFileName = _filePath.toString
26+
.replace("Test.scala", "UnitTest.scala")
27+
.replace("Spec.scala", "UnitTest.scala")
28+
.replace("Suite.scala", "UnitTest.scala")
29+
.replace("Tests.scala", "UnitTest.scala")
30+
31+
// rename file
32+
val newFilePath = os.Path(newFileName)
33+
os.move(_filePath, newFilePath)
34+
35+
val content = os.read(newFilePath)
36+
val existingClassName = getClassFromContent(content)
37+
38+
val fileNameWithoutExtension = newFilePath.last.dropRight(6)
39+
40+
def isTestClass(existingClassName: String): Boolean = {
41+
existingClassName
42+
.endsWith("Spec") || existingClassName.endsWith("Test")
43+
}
44+
45+
// Rename the class if it doesn't match the filename
46+
if (
47+
isTestClass(
48+
existingClassName
49+
) && existingClassName.toLowerCase != fileNameWithoutExtension.toLowerCase
50+
) {
51+
// Update the content with the new class name
52+
val updatedContent = content.replaceAll(
53+
s"class\\s+$existingClassName",
54+
s"class $fileNameWithoutExtension"
55+
)
56+
57+
// Now, rename the class
58+
os.write.over(newFilePath, updatedContent)
59+
println(
60+
s"Renamed class in ${newFilePath.last} to $fileNameWithoutExtension"
61+
)
62+
}
63+
}
64+
}
65+
66+
// Extract class name from file content
67+
def getClassFromContent(content: String): String = {
68+
val classNameRegex = """class\s+(\w+)""".r
69+
val matchResult = classNameRegex.findFirstMatchIn(content)
70+
matchResult match {
71+
case Some(m) => m.group(1)
72+
case None => ""
73+
}
74+
}
75+
}

play-scala/application-tests/test/com/baeldung/arrival/actions/SourceActionsTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/actions/SourceActionsUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import play.api.mvc.Headers
88
import play.api.mvc.Results.NoContent
99
import play.api.test.{FakeRequest, Helpers}
1010

11-
class SourceActionsTest
11+
class SourceActionsUnitTest
1212
extends AnyWordSpec
1313
with SourceActions
1414
with ScalaFutures {

play-scala/application-tests/test/com/baeldung/arrival/controller/ArrivalControllerH2Test.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/controller/ArrivalControllerH2UnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import play.api.libs.ws.{WSClient, WSResponse}
1111

1212
import scala.concurrent.Future
1313

14-
class ArrivalControllerH2Test
14+
class ArrivalControllerH2UnitTest
1515
extends AnyWordSpec
1616
with WsScalaTestClient
1717
with GuiceOneServerPerTest

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalDecoratorServiceTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalDecoratorServiceUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.scalatestplus.play.MixedPlaySpec
55
import play.api.Configuration
66
import play.api.inject.guice.GuiceApplicationBuilder
77

8-
class ArrivalDecoratorServiceTest extends MixedPlaySpec {
8+
class ArrivalDecoratorServiceUnitTest extends MixedPlaySpec {
99

1010
"ArrivalDecoratorService#decorate" should {
1111
"mark as short an arrival with plane name length = 5" in new App(

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceH2Test.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceH2UnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.scalatestplus.play.guice.GuiceOneAppPerTest
88

99
import scala.language.postfixOps
1010

11-
class ArrivalServiceH2Test
11+
class ArrivalServiceH2UnitTest
1212
extends AnyWordSpec
1313
with GuiceOneAppPerTest
1414
with ScalaFutures

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceIsolatedTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceIsolatedUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.scalatestplus.play.guice.GuiceOneAppPerTest
1313
import play.api.inject.guice.GuiceApplicationBuilder
1414
import play.api.{Application, Configuration, inject}
1515

16-
class ArrivalServiceIsolatedTest
16+
class ArrivalServiceIsolatedUnitTest
1717
extends AnyWordSpec
1818
with GuiceOneAppPerTest
1919
with ScalaFutures {

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceMocksTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceMocksUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import slick.dbio.{DBIO, SuccessAction}
1515

1616
import scala.concurrent.Future
1717

18-
class ArrivalServiceMocksTest
18+
class ArrivalServiceMocksUnitTest
1919
extends AnyWordSpec
2020
with GuiceOneAppPerTest
2121
with ScalaFutures

0 commit comments

Comments
 (0)