Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support kotlin tests * Add KotlinTest to detect kotlin test classes * Add scripted test for simple test and mixed tests
- Loading branch information
1 parent
931fa36
commit 2d50715
Showing
14 changed files
with
168 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package sbt | ||
|
||
import sbt.Keys._ | ||
import sbt.classfile.Analyze | ||
import sbt.classpath.ClasspathUtilities | ||
import sbt.inc.{Analysis, IncOptions, IncrementalCompile} | ||
import xsbti.compile.SingleOutput | ||
|
||
object KotlinTest { | ||
val kotlinTests = Def.task { | ||
val out = ((target in Test).value ** "scala-*").get.head / "test-classes" | ||
val srcs = ((sourceDirectory in Test).value ** "*.kt").get.toList | ||
val xs = (out ** "*.class").get.toList | ||
|
||
val loader = ClasspathUtilities.toLoader((fullClasspath in Test).value map { | ||
_.data | ||
}) | ||
val log = streams.value.log | ||
val a0 = IncrementalCompile( | ||
srcs.toSet, s => None, | ||
(fs, changs, callback) => { | ||
def readAPI(source: File, classes: Seq[Class[_]]): Set[String] = { | ||
val (api, inherits) = ClassToAPI.process(classes) | ||
callback.api(source, api) | ||
inherits.map(_.getName) | ||
} | ||
|
||
Analyze(xs, srcs, log)(callback, loader, readAPI) | ||
}, | ||
Analysis.Empty, f => None, | ||
new SingleOutput { | ||
def outputDirectory = out | ||
}, | ||
log, | ||
IncOptions.Default)._2 | ||
val frameworks = (loadedTestFrameworks in Test).value.values.toList | ||
log.info(s"Compiling ${srcs.length} Kotlin source to ${out}...") | ||
Tests.discover(frameworks, a0, log)._1 | ||
} | ||
} |
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,27 @@ | ||
import sbt.complete.Parsers.spaceDelimited | ||
|
||
import scala.xml.{NodeSeq, XML} | ||
|
||
kotlinLib("stdlib") | ||
|
||
libraryDependencies ++= Seq( | ||
"com.novocode" % "junit-interface" % "0.11" % Test | ||
) | ||
|
||
lazy val checkTestPass = inputKey[Unit]("Check if a given test-report has one success test") | ||
checkTestPass := { | ||
val args: Seq[String] = spaceDelimited("<arg>").parsed | ||
val testName = args.head | ||
|
||
val xml = XML.load(s"target/test-reports/$testName.xml") | ||
val totalTests = getInt(xml \\ "testsuite" \ "@tests") | ||
val failures = getInt(xml \\ "testsuite" \ "@failures") | ||
val errors = getInt(xml \\ "testsuite" \ "@errors") | ||
val skipped = getInt(xml \\ "testsuite" \ "@skipped") | ||
|
||
if (totalTests == 0 || failures > 0 || errors > 0 || skipped > 0) { | ||
sys.error("Tests not passed") | ||
} | ||
} | ||
|
||
def getInt(path: NodeSeq): Int = path.text.toInt |
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,9 @@ | ||
{ | ||
val ver = System.getProperty("plugin.version") | ||
if (ver == null) | ||
throw new RuntimeException(""" | ||
|The system property 'plugin.version' is not defined. | ||
|Specify this property using scriptedLaunchOpts -Dplugin.version.""" | ||
.stripMargin) | ||
else addSbtPlugin("com.hanhuy.sbt" % "kotlin-plugin" % ver) | ||
} |
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,6 @@ | ||
package demo | ||
|
||
fun main(args: Array<String>) { | ||
println("Hello, world!") | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
src/sbt-test/kotlin/basic-tests/src/test/kotlin/SimpleTest.kt
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,11 @@ | ||
import org.junit.Test | ||
import junit.framework.TestCase.assertEquals | ||
|
||
class SimpleTest { | ||
|
||
@Test | ||
fun `should works`() { | ||
assertEquals(4, 2 + 2) | ||
} | ||
|
||
} |
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,3 @@ | ||
> compile | ||
> test | ||
> "checkTestPass SimpleTest" |
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,27 @@ | ||
import sbt.complete.Parsers.spaceDelimited | ||
|
||
import scala.xml.{NodeSeq, XML} | ||
|
||
kotlinLib("stdlib") | ||
|
||
libraryDependencies ++= Seq( | ||
"com.novocode" % "junit-interface" % "0.11" % Test | ||
) | ||
|
||
lazy val checkTestPass = inputKey[Unit]("Check if a given test-report has one success test") | ||
checkTestPass := { | ||
val args: Seq[String] = spaceDelimited("<arg>").parsed | ||
val testName = args.head | ||
|
||
val xml = XML.load(s"target/test-reports/$testName.xml") | ||
val totalTests = getInt(xml \\ "testsuite" \ "@tests") | ||
val failures = getInt(xml \\ "testsuite" \ "@failures") | ||
val errors = getInt(xml \\ "testsuite" \ "@errors") | ||
val skipped = getInt(xml \\ "testsuite" \ "@skipped") | ||
|
||
if (totalTests == 0 || failures > 0 || errors > 0 || skipped > 0) { | ||
sys.error("Tests not passed") | ||
} | ||
} | ||
|
||
def getInt(path: NodeSeq): Int = path.text.toInt |
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,9 @@ | ||
{ | ||
val ver = System.getProperty("plugin.version") | ||
if (ver == null) | ||
throw new RuntimeException(""" | ||
|The system property 'plugin.version' is not defined. | ||
|Specify this property using scriptedLaunchOpts -Dplugin.version.""" | ||
.stripMargin) | ||
else addSbtPlugin("com.hanhuy.sbt" % "kotlin-plugin" % ver) | ||
} |
9 changes: 9 additions & 0 deletions
9
src/sbt-test/kotlin/mixed-tests/src/main/java/demo/JavaCalculator.java
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,9 @@ | ||
package demo; | ||
|
||
public class JavaCalculator { | ||
|
||
public int sum(int a, int b) { | ||
return a + b; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/sbt-test/kotlin/mixed-tests/src/main/kotlin/Calculator.kt
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,7 @@ | ||
package demo | ||
|
||
class Calculator(private val calculator: JavaCalculator) { | ||
|
||
fun sum(a: Int, b: Int): Int = calculator.sum(a, b) | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/sbt-test/kotlin/mixed-tests/src/test/kotlin/MixedTest.kt
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,14 @@ | ||
import org.junit.Test | ||
import junit.framework.TestCase.assertEquals | ||
import demo.Calculator | ||
import demo.JavaCalculator | ||
|
||
class MixedTest { | ||
|
||
@Test | ||
fun `should sum 2 plus 2`() { | ||
val calculator = Calculator(JavaCalculator()) | ||
assertEquals(4, calculator.sum(2, 2)) | ||
} | ||
|
||
} |
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,3 @@ | ||
> compile | ||
> test | ||
> "checkTestPass MixedTest" |