From 860f2b11956e1d461838aaac7f421a1c079558b8 Mon Sep 17 00:00:00 2001 From: Johnnei Date: Mon, 26 Feb 2024 20:01:17 +0700 Subject: [PATCH] refactor: Decouple scala version checks from exact version list --- .../scala/com/sksamuel/scapegoat/package.scala | 18 ++++++++++++++---- .../com/sksamuel/scapegoat/ScapegoatTest.scala | 14 ++++++++++++++ .../UnnecessaryConversionTest.scala | 18 +++++++++--------- 3 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 src/test/scala/com/sksamuel/scapegoat/ScapegoatTest.scala diff --git a/src/main/scala/com/sksamuel/scapegoat/package.scala b/src/main/scala/com/sksamuel/scapegoat/package.scala index 19ac8849..5a1446b1 100644 --- a/src/main/scala/com/sksamuel/scapegoat/package.scala +++ b/src/main/scala/com/sksamuel/scapegoat/package.scala @@ -1,9 +1,19 @@ package com.sksamuel +import scala.util.Try + package object scapegoat { - val scalaVersion: String = util.Properties.versionNumberString - private val shortScalaVersion = scalaVersion.split('.').dropRight(1).mkString(".") + private val scalaVersion: String = util.Properties.versionNumberString + private val (major, minor, patch) = extractComponents(scalaVersion) + + val isScala213: Boolean = major == 2 && minor == 13 + val isScala21313OrLater: Boolean = isScala213 && patch >= 12 - val isScala213: Boolean = shortScalaVersion == "2.13" - val isScala21312: Boolean = scalaVersion == "2.13.12" + private[scapegoat] def extractComponents(version: String) = { + def parseInt(s: String) = Try(s.toInt).getOrElse(0) + version.split('.').toList.map(parseInt) match { + case List(major, minor, patch) => (major, minor, patch) + case _ => (1, 0, 0) + } + } } diff --git a/src/test/scala/com/sksamuel/scapegoat/ScapegoatTest.scala b/src/test/scala/com/sksamuel/scapegoat/ScapegoatTest.scala new file mode 100644 index 00000000..7344481a --- /dev/null +++ b/src/test/scala/com/sksamuel/scapegoat/ScapegoatTest.scala @@ -0,0 +1,14 @@ +package com.sksamuel.scapegoat + +import org.scalatest.freespec.AnyFreeSpec +import org.scalatest.matchers.should.Matchers +import com.sksamuel.scapegoat._ + +class ScapegoatTest extends AnyFreeSpec with Matchers { + + "ScalaVersionPattern" - { + "shoud extract components" in { + extractComponents("2.13.12") shouldBe (2, 13, 12) + } + } +} diff --git a/src/test/scala/com/sksamuel/scapegoat/inspections/unnecessary/UnnecessaryConversionTest.scala b/src/test/scala/com/sksamuel/scapegoat/inspections/unnecessary/UnnecessaryConversionTest.scala index c8180c13..7c9ecae8 100644 --- a/src/test/scala/com/sksamuel/scapegoat/inspections/unnecessary/UnnecessaryConversionTest.scala +++ b/src/test/scala/com/sksamuel/scapegoat/inspections/unnecessary/UnnecessaryConversionTest.scala @@ -1,7 +1,7 @@ package com.sksamuel.scapegoat.inspections.unnecessary import com.sksamuel.scapegoat.inspections.unneccesary.UnnecessaryConversion -import com.sksamuel.scapegoat.{isScala21312, Inspection, InspectionTest} +import com.sksamuel.scapegoat.{isScala21313OrLater, Inspection, InspectionTest} /** @author Stephen Samuel */ class UnnecessaryConversionTest extends InspectionTest { @@ -17,7 +17,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21313OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -28,7 +28,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21313OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -39,7 +39,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 2 + val expectedWarnings = if (isScala21313OrLater) 0 else 2 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -50,7 +50,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21313OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -61,7 +61,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21313OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -72,7 +72,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21313OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -83,7 +83,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21313OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -94,7 +94,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21313OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } }