-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1176 from bjaglin/scalatest
make it possible to use testkit with ScalaTest 3.2.x
- Loading branch information
Showing
37 changed files
with
273 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
pullRequests.frequency = "0 0 1,15 * ?" | ||
updates.pin = [ | ||
# don't bump, to avoid forcing breaking changes on clients via eviction | ||
{ groupId = "org.scalatest", artifactId = "scalatest", version = "3.0.8" }, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
scalafix-testkit/src/main/scala/scalafix/testkit/AbstractSemanticRuleSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package scalafix.testkit | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Files | ||
|
||
import org.scalatest.exceptions.TestFailedException | ||
import org.scalatest.{BeforeAndAfterAll, Suite, TestRegistration} | ||
import scalafix.internal.reflect.ClasspathOps | ||
import scalafix.internal.testkit.{AssertDiff, CommentAssertion} | ||
|
||
import scala.meta._ | ||
import scala.meta.internal.io.FileIO | ||
|
||
/** Construct a test suite for running semantic Scalafix rules. | ||
* <p> | ||
* Mix-in FunSuiteLike (ScalaTest 3.0), AnyFunSuiteLike (ScalaTest 3.1+) or | ||
* the testing style of your choice if you add your own tests. | ||
*/ | ||
abstract class AbstractSemanticRuleSuite( | ||
val props: TestkitProperties, | ||
val isSaveExpect: Boolean | ||
) extends Suite | ||
with TestRegistration | ||
with DiffAssertions | ||
with BeforeAndAfterAll { self => | ||
|
||
def this(props: TestkitProperties) = this(props, isSaveExpect = false) | ||
def this() = this(TestkitProperties.loadFromResources()) | ||
|
||
private def scalaVersion: String = scala.util.Properties.versionNumberString | ||
private def scalaVersionDirectory: Option[String] = | ||
if (scalaVersion.startsWith("2.11")) Some("scala-2.11") | ||
else if (scalaVersion.startsWith("2.12")) Some("scala-2.12") | ||
else if (scalaVersion.startsWith("2.13")) Some("scala-2.13") | ||
else None | ||
|
||
def evaluateTestBody(diffTest: RuleTest): Unit = { | ||
val (rule, sdoc) = diffTest.run.apply() | ||
rule.beforeStart() | ||
val (fixed, messages) = | ||
try rule.semanticPatch(sdoc, suppress = false) | ||
finally rule.afterComplete() | ||
val tokens = fixed.tokenize.get | ||
val obtained = SemanticRuleSuite.stripTestkitComments(tokens) | ||
val expected = diffTest.path.resolveOutput(props) match { | ||
case Right(file) => | ||
FileIO.slurp(file, StandardCharsets.UTF_8) | ||
case Left(err) => | ||
if (fixed == sdoc.input.text) { | ||
// rule is a linter, no need for an output file. | ||
obtained | ||
} else { | ||
fail(err) | ||
} | ||
} | ||
|
||
val expectedLintMessages = CommentAssertion.extract(sdoc.tokens) | ||
val diff = AssertDiff(messages, expectedLintMessages) | ||
|
||
if (diff.isFailure) { | ||
println("###########> Lint <###########") | ||
println(diff.toString) | ||
} | ||
|
||
val result = compareContents(obtained, expected) | ||
if (result.nonEmpty) { | ||
println("###########> Diff <###########") | ||
println(error2message(obtained, expected)) | ||
} | ||
|
||
val isTestFailure = result.nonEmpty || diff.isFailure | ||
diffTest.path.resolveOutput(props) match { | ||
case Right(output) if isTestFailure && isSaveExpect => | ||
println(s"promoted expect test: $output") | ||
Files.write(output.toNIO, obtained.getBytes(StandardCharsets.UTF_8)) | ||
case _ => | ||
} | ||
|
||
if (isTestFailure) { | ||
throw new TestFailedException("see above", 0) | ||
} | ||
} | ||
|
||
def runOn(diffTest: RuleTest): Unit = { | ||
registerTest(diffTest.path.testName) { | ||
evaluateTestBody(diffTest) | ||
} | ||
} | ||
|
||
lazy val testsToRun = { | ||
val symtab = ClasspathOps.newSymbolTable(props.inputClasspath) | ||
val classLoader = ClasspathOps.toClassLoader(props.inputClasspath) | ||
val tests = TestkitPath.fromProperties(props) | ||
tests.map { test => | ||
RuleTest.fromPath(props, test, classLoader, symtab) | ||
} | ||
} | ||
def runAllTests(): Unit = { | ||
testsToRun.foreach(runOn) | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
scalafix-testkit/src/main/scala/scalafix/testkit/AbstractSyntacticRuleSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package scalafix.testkit | ||
|
||
import org.scalatest.{Suite, TestRegistration} | ||
import org.scalatest.Tag | ||
import scala.meta._ | ||
import scalafix.syntax._ | ||
import scalafix.v0._ | ||
import scalafix.internal.config.ScalafixConfig | ||
|
||
/** Utility to unit test syntactic rules. | ||
* <p> | ||
* Mix-in FunSuiteLike (ScalaTest 3.0), AnyFunSuiteLike (ScalaTest 3.1+) or | ||
* the testing style of your choice if you add your own tests. | ||
* | ||
* @param rule the default rule to use from `check`/`checkDiff`. | ||
*/ | ||
abstract class AbstractSyntacticRuleSuite(rule: Rule = Rule.empty) | ||
extends Suite | ||
with TestRegistration | ||
with DiffAssertions { | ||
|
||
def check(name: String, original: String, expected: String): Unit = { | ||
check(rule, name, original, expected) | ||
} | ||
|
||
def check( | ||
rule: Rule, | ||
name: String, | ||
original: String, | ||
expected: String | ||
): Unit = { | ||
check(rule, name, original, expected, Seq(): _*) | ||
} | ||
|
||
def check( | ||
rule: Rule, | ||
name: String, | ||
original: String, | ||
expected: String, | ||
testTags: Tag* | ||
): Unit = { | ||
registerTest(name, testTags: _*) { | ||
import scala.meta._ | ||
val obtained = rule.apply(Input.String(original)) | ||
assertNoDiff(obtained, expected) | ||
} | ||
} | ||
|
||
def checkDiff(original: Input, expected: String): Unit = { | ||
checkDiff(rule, original, expected, Seq(): _*) | ||
} | ||
|
||
def checkDiff(original: Input, expected: String, testTags: Tag*): Unit = { | ||
checkDiff(rule, original, expected, testTags: _*) | ||
} | ||
|
||
def checkDiff(rule: Rule, original: Input, expected: String): Unit = { | ||
checkDiff(rule, original, expected, Seq(): _*) | ||
} | ||
|
||
def checkDiff( | ||
rule: Rule, | ||
original: Input, | ||
expected: String, | ||
testTags: Tag* | ||
): Unit = { | ||
registerTest(original.label, testTags: _*) { | ||
val dialect = ScalafixConfig.default.parser.dialectForFile("Source.scala") | ||
val ctx = RuleCtx(dialect(original).parse[Source].get) | ||
val obtained = rule.diff(ctx) | ||
assertNoDiff(obtained, expected) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.