Skip to content

Commit

Permalink
[tests] Factor out common language injection test code to avoid code …
Browse files Browse the repository at this point in the history
…duplication #SCL-23251
  • Loading branch information
vasilmkd committed Nov 25, 2024
1 parent 671eb97 commit 2f68a92
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.jetbrains.plugins.scala.injection

abstract class InjectionInBodyTestBase extends ScalaLanguageInjectionTestBase {

protected val Quotes: String = "\"\"\""
protected lazy val LanguageAnnotationDef: String = scalaInjectionTestFixture.LanguageAnnotationDef

protected def doTestInBody(languageId: String, classBody: String, injectedFileExpectedText: String): Unit = {
val classBodyWithIndent = classBody.replaceAll("\n", "\n ")
val text =
s"""class A {
| $classBodyWithIndent
|}
|""".stripMargin
scalaInjectionTestFixture.doTest(languageId, text, injectedFileExpectedText)
}

protected def doAnnotationTestInBody(languageId: String, classBody: String, injectedFileExpectedText: String): Unit = {
val classBodyWithIndent = classBody.replaceAll("\n", "\n ")
val text =
s"""$LanguageAnnotationDef
|class A {
| $classBodyWithIndent
|}
|""".stripMargin
scalaInjectionTestFixture.doTest(languageId, text, injectedFileExpectedText)
}

protected def doAnnotationTest(languageId: String, text: String, injectedFileExpectedText: String): Unit = {
val textFinal =
s"""$LanguageAnnotationDef
|$text
|""".stripMargin
scalaInjectionTestFixture.doTest(languageId, textFinal, injectedFileExpectedText)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jetbrains.plugins.scala.injection

import org.jetbrains.plugins.scala.extensions.StringExt
import org.jetbrains.plugins.scala.injection.ScalaInjectionTestFixture.{ExpectedInjection, ShredInfo}

abstract class InjectionViaCommentTestBase extends ScalaLanguageInjectionTestBase {
protected def doInjectedViaCommentTest(
languageId: String,
text: String,
expectedText: String,
expectedShreds: Seq[ShredInfo] = null
): Unit = {
val textWithComment =
s"""//language=$languageId
|$text
|""".stripMargin.replace("'''", "\"\"\"")
val expectedInjection = ExpectedInjection(
expectedText.withNormalizedSeparator,
languageId,
Option(expectedShreds)
)
scalaInjectionTestFixture.doTest(textWithComment, expectedInjection)
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
package org.jetbrains.plugins.scala.injection

import org.jetbrains.plugins.scala.extensions.StringExt
import org.jetbrains.plugins.scala.injection.InjectionTestUtils.RegexpLangId
import org.jetbrains.plugins.scala.injection.ScalaInjectionTestFixture.{ExpectedInjection, ShredInfo}
import org.jetbrains.plugins.scala.injection.ScalaInjectionTestFixture.ShredInfo
import org.jetbrains.plugins.scala.util.TextRangeUtils.ImplicitConversions.tupleToTextRange

class InterpolatedStringWithInjectionsMultilineTest extends ScalaLanguageInjectionTestBase {

private def doInjectedViaCommentTest(
languageId: String,
text: String,
expectedText: String,
expectedShreds: Seq[ShredInfo] = null
): Unit = {
val textWithComment =
s"""//language=$languageId
|$text
|""".stripMargin.replace("'''", "\"\"\"")
val expectedInjection = ExpectedInjection(
expectedText.withNormalizedSeparator,
languageId,
Option(expectedShreds)
)
scalaInjectionTestFixture.doTest(textWithComment, expectedInjection)
}

class InterpolatedStringWithInjectionsMultilineTest extends InjectionViaCommentTestBase {
def testRawInterpolatorWithEscapeSequences(): Unit = {
doInjectedViaCommentTest(
RegexpLangId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,13 @@
package org.jetbrains.plugins.scala.injection

import com.intellij.openapi.module.Module
import com.intellij.patterns.compiler.PatternCompilerImpl.LazyPresentablePattern
import org.intellij.plugins.intelliLang.inject.config.{BaseInjection, InjectionPlace}
import org.jetbrains.plugins.scala.injection.InjectionTestUtils._
import org.jetbrains.plugins.scala.settings.ScalaProjectSettings
import org.junit.Assert._

import scala.jdk.CollectionConverters._

class ScalaLanguageInjectorTest extends ScalaLanguageInjectionTestBase {

private val Quotes = "\"\"\""
private lazy val LanguageAnnotationDef = scalaInjectionTestFixture.LanguageAnnotationDef

override def setUpLibraries(implicit module: Module): Unit = {
super.setUpLibraries

val settings = ScalaProjectSettings.getInstance(module.getProject)
val interpolatorToLangId = Map("json" -> JsonLangId).asJava
settings.setIntInjectionMapping(interpolatorToLangId)
settings.setDisableLangInjection(false)
}

protected def doTestInBody(languageId: String, classBody: String, injectedFileExpectedText: String): Unit = {
val classBodyWithIndent = classBody.replaceAll("\n", "\n ")
val text =
s"""class A {
| $classBodyWithIndent
|}
|""".stripMargin
scalaInjectionTestFixture.doTest(languageId, text, injectedFileExpectedText)
}

protected def doAnnotationTestInBody(languageId: String, classBody: String, injectedFileExpectedText: String): Unit = {
val classBodyWithIndent = classBody.replaceAll("\n", "\n ")
val text =
s"""$LanguageAnnotationDef
|class A {
| $classBodyWithIndent
|}
|""".stripMargin
scalaInjectionTestFixture.doTest(languageId, text, injectedFileExpectedText)
}

protected def doAnnotationTest(languageId: String, text: String, injectedFileExpectedText: String): Unit = {
val textFinal =
s"""$LanguageAnnotationDef
|$text
|""".stripMargin
scalaInjectionTestFixture.doTest(languageId, textFinal, injectedFileExpectedText)
}
class ScalaLanguageInjectorTest extends InjectionInBodyTestBase {

////////////////////////////////////////
// @Language annotation injection tests
Expand Down

0 comments on commit 2f68a92

Please sign in to comment.