Repository: scalameta/scalameta
Pull request #3461 (2024-01-05 00:04:57)

Add common base for {Parse,Tokenize}Exception


Patch:
diff --git a/project/plugins.sbt b/project/plugins.sbt
index 7007fb2e6..04ccb355b 100644
--- a/project/plugins.sbt
+++ b/project/plugins.sbt
@@ -4,7 +4,7 @@ addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.5")
 
 addSbtPlugin("com.github.sbt" % "sbt-unidoc" % "0.5.0")
 
-addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.6")
+addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.7")
 
 addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0")
 
diff --git a/scalameta/inputs/shared/src/main/scala/scala/meta/inputs/Errors.scala b/scalameta/inputs/shared/src/main/scala/scala/meta/inputs/Errors.scala
new file mode 100644
index 000000000..54b8ccaa0
--- /dev/null
+++ b/scalameta/inputs/shared/src/main/scala/scala/meta/inputs/Errors.scala
@@ -0,0 +1,9 @@
+package scala.meta.inputs
+
+import scala.meta.internal.inputs._
+
+class InputException(pos: Position, shortMessage: String)
+    extends Exception(pos.formatMessage("error", shortMessage)) {
+  def fullMessage: String = getMessage
+  override def toString = getMessage
+}
diff --git a/scalameta/parsers/shared/src/main/scala/scala/meta/parsers/Errors.scala b/scalameta/parsers/shared/src/main/scala/scala/meta/parsers/Errors.scala
index 93ef66c0b..dec06c1ed 100644
--- a/scalameta/parsers/shared/src/main/scala/scala/meta/parsers/Errors.scala
+++ b/scalameta/parsers/shared/src/main/scala/scala/meta/parsers/Errors.scala
@@ -32,7 +32,4 @@ object Parsed {
 }
 
 case class ParseException(pos: Position, shortMessage: String)
-    extends Exception(pos.formatMessage("error", shortMessage)) {
-  def fullMessage = getMessage
-  override def toString = fullMessage
-}
+    extends InputException(pos, shortMessage)
diff --git a/scalameta/tokenizers/shared/src/main/scala/scala/meta/tokenizers/Errors.scala b/scalameta/tokenizers/shared/src/main/scala/scala/meta/tokenizers/Errors.scala
index dcc012085..1fa434592 100644
--- a/scalameta/tokenizers/shared/src/main/scala/scala/meta/tokenizers/Errors.scala
+++ b/scalameta/tokenizers/shared/src/main/scala/scala/meta/tokenizers/Errors.scala
@@ -33,7 +33,6 @@ object Tokenized {
 }
 
 @data class TokenizeException(pos: Position, shortMessage: String)
-    extends Exception(pos.formatMessage("error", shortMessage)) {
-  def fullMessage = getMessage
-  override def toString = fullMessage
+    extends InputException(pos, shortMessage) {
+  override def toString = getMessage
 }


Test patch:
diff --git a/tests/jvm/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala b/tests/jvm/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala
index 50cfcf5c1..2e8126f92 100644
--- a/tests/jvm/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala
+++ b/tests/jvm/src/test/scala-2.13/scala/meta/tests/api/SurfaceSuite.scala
@@ -123,6 +123,7 @@ class SurfaceSuite extends FunSuite {
       |scala.meta.inputs.Input.Stream *
       |scala.meta.inputs.Input.String *
       |scala.meta.inputs.Input.VirtualFile *
+      |scala.meta.inputs.InputException *
       |scala.meta.inputs.Position
       |scala.meta.inputs.Position.None *
       |scala.meta.inputs.Position.Range *
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/ParseSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/ParseSuite.scala
index d5da230de..dbd604d9a 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/ParseSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/ParseSuite.scala
@@ -81,7 +81,7 @@ class ParseSuite extends TreeSuiteBase with CommonTrees {
       code: String,
       expected: String
   )(implicit parser: (String, Dialect) => T, dialect: Dialect, loc: munit.Location): Unit = {
-    val error = intercept[ParseException] {
+    val error = intercept[inputs.InputException] {
       val result = parser(code, dialect)
       throw new ParseException(
         Position.None,
diff --git a/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/PublicSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/PublicSuite.scala
index bca7db69a..85efb1042 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/PublicSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/PublicSuite.scala
@@ -191,6 +191,8 @@ class PublicSuite extends TreeSuiteBase {
     assertNoDiff(scala.meta.dialects.ParadiseTypelevel212.toString, "ParadiseTypelevel212")
   }
 
+  test("scala.meta.inputs.InputException") {}
+
   test("scala.meta.inputs.Input.toString") {
     // covered below
   }
@@ -359,28 +361,24 @@ class PublicSuite extends TreeSuiteBase {
   }
 
   test("scala.meta.tokenizers.TokenizeException.toString") {
-    intercept[TokenizeException] {
-      try """"c""".tokenize.get
-      catch {
-        case ex: TokenizeException =>
-          assert(ex.toString == """
-            |<input>:1: error: unclosed string literal
-            |"c
-            |^
-          """.trim.stripMargin.split('\n').mkString(EOL))
-          throw ex
-      }
-    }
+    interceptMessage[TokenizeException](
+      """|<input>:1: error: unclosed string literal
+         |"c
+         |^""".stripMargin.replace("\n", EOL)
+    )(""""c""".tokenize.get)
   }
 
   test("scala.meta.tokenizers.Tokenized.Error.toString") {
-    val tokenized = """"c""".tokenize
-    tokenized match { case _: Tokenized.Error => ; case _ => }
-    assert(tokenized.toString == """
-      |<input>:1: error: unclosed string literal
-      |"c
-      |^
-    """.trim.stripMargin.split('\n').mkString(EOL))
+    """"c""".tokenize match {
+      case x: Tokenized.Error =>
+        assertEquals(
+          x.toString,
+          """|<input>:1: error: unclosed string literal
+             |"c
+             |^""".stripMargin.replace("\n", EOL)
+        )
+      case x => fail(s"tokenized is not an error: $x")
+    }
   }
 
   test("scala.meta.tokenizers.Tokenized.Success.toString") {