Kotlin2Js Gradle plugin tests

This commit is contained in:
Zoltan Nagy
2014-11-15 23:26:12 +01:00
committed by Zalim Bashorov
parent b1723668ce
commit e08fa1cab2
11 changed files with 239 additions and 1 deletions
@@ -11,6 +11,7 @@ import kotlin.test.assertFalse
import kotlin.test.assertEquals
import kotlin.test.fail
import org.gradle.api.logging.LogLevel
import kotlin.test.assertFalse
open class BaseGradleIT(resourcesRoot: String = "src/test/resources") {
@@ -59,8 +60,28 @@ open class BaseGradleIT(resourcesRoot: String = "src/test/resources") {
return this
}
fun CompiledProject.fileInWorkingDir(path: String) = File(File(workingDir, project.projectName), path)
fun CompiledProject.assertReportExists(pathToReport: String = ""): CompiledProject {
assertTrue(File(File(workingDir, project.projectName), pathToReport).exists(), "The report [$pathToReport] does not exist.")
assertTrue(fileInWorkingDir(pathToReport).exists(), "The report [$pathToReport] does not exist.")
return this
}
fun CompiledProject.assertFileExists(path: String = ""): CompiledProject {
assertTrue(fileInWorkingDir(path).exists(), "The file [$path] does not exist.")
return this
}
fun CompiledProject.assertNoSuchFile(path: String = ""): CompiledProject {
assertFalse(fileInWorkingDir(path).exists(), "The file [$path] exists.")
return this
}
fun CompiledProject.assertFileContains(path: String, vararg expected: String): CompiledProject {
val text = fileInWorkingDir(path).readText()
expected.forEach {
assertTrue(text.contains(it), "$path should contain '$it', actual file contents:\n$text")
}
return this
}
@@ -0,0 +1,68 @@
package org.jetbrains.kotlin.gradle
import org.junit.Test
import org.jetbrains.kotlin.gradle.BaseGradleIT.Project
class Kotlin2JsGradlePluginIT : BaseGradleIT() {
Test fun testBuildAndClean() {
val project = Project("kotlin2JsProject", "1.6")
project.build("build") {
assertSuccessful()
assertReportExists()
assertContains(":libraryProject:jarSources\n",
":mainProject:compileKotlin2Js\n", ":mainProject:copyKotlinJs\n",
":libraryProject:compileKotlin2Js\n", ":libraryProject:copyKotlinJs\n")
listOf("mainProject/web/js/app.js", "mainProject/web/js/lib/kotlin.js",
"libraryProject/build/kotlin2js/main/app.js", "libraryProject/build/kotlin2js/main/kotlin.js",
"mainProject/web/js/app.js.map"
).forEach { assertFileExists(it) }
// TODO Should be updated to `new _.example.library.Counter` once namespaced imports from libraryFiles are implemented
// TODO It would be better to test these by behavior instead of implementation, for example by loading the files
// into Rhino and running assertions on that. See https://github.com/abesto/kotlin/commit/120ec1bda3d95630189d4d33d0b2afb4253b5186
// for the (original) discussion on this.
assertFileContains("libraryProject/build/kotlin2js/main/app.js", "Counter: Kotlin.createClass")
assertFileContains("mainProject/web/js/app.js", "var counter = new Counter(counterText);")
}
project.build("build") {
assertSuccessful()
assertContains(":mainProject:compileKotlin2Js UP-TO-DATE", ":libraryProject:compileTestKotlin2Js UP-TO-DATE")
}
project.build("clean") {
assertSuccessful()
assertReportExists()
assertContains(":mainProject:cleanCompileKotlin2Js\n", ":mainProject:cleanCopyKotlinJs\n")
assertNoSuchFile("mainProject/web/js/app.js")
assertNoSuchFile("mainProject/web/js/lib/kotlin.js")
// Test that we don't accidentally remove the containing directory
// This would fail if we used the default clean task of the copy task
assertFileExists("mainProject/web/js/lib")
assertNoSuchFile("main/project/web/js/app.js.map")
assertNoSuchFile("main/project/web/js/example/main.kt")
}
project.build("clean") {
assertSuccessful()
assertReportExists()
assertContains(":mainProject:cleanCompileKotlin2Js UP-TO-DATE", ":mainProject:cleanCopyKotlinJs UP-TO-DATE")
}
}
Test fun testNoOutputFileFails() {
val project = Project("kotlin2JsNoOutputFileProject", "1.6")
project.build("build") {
assertReportExists()
assertContains("compileKotlin2Js.kotlinOptions.outputFile must be set to a string")
}
}
}
@@ -0,0 +1,25 @@
buildscript {
repositories {
mavenCentral()
maven {
url "file://" + pathToKotlinPlugin
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:0.1-SNAPSHOT"
}
}
apply plugin: "kotlin2js"
repositories {
maven {
url "file://" + pathToKotlinPlugin
}
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-js-library:0.1-SNAPSHOT"
}
@@ -0,0 +1,35 @@
buildscript {
repositories {
mavenCentral()
maven {
url 'file://' + pathToKotlinPlugin
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:0.1-SNAPSHOT"
}
}
apply plugin: 'kotlin2js'
repositories {
maven {
url 'file://' + pathToKotlinPlugin
}
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-js-library:0.1-SNAPSHOT"
}
task jarSources(type:Jar){
from sourceSets.main.allSource
classifier = 'source'
}
artifacts {
compile jarSources
}
compileKotlin2Js.kotlinOptions.outputFile = "${buildDir}/kotlin2js/main/app.js"
@@ -0,0 +1,6 @@
package example
class Adder {
public fun add(x: Int, y: Int): Int = x + y
}
@@ -0,0 +1,15 @@
package example.library
import org.w3c.dom.Text
import kotlin.js.dom.html.window
public class Counter(val el: Text) {
fun step(n: Int) {
el.replaceWholeText("Counter: ${n}")
window.setTimeout({step(n+1)}, 1000)
}
fun start() {
step(0)
}
}
@@ -0,0 +1,33 @@
buildscript {
repositories {
mavenCentral()
maven {
url "file://" + pathToKotlinPlugin
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:0.1-SNAPSHOT"
}
}
apply plugin: "kotlin2js"
repositories {
maven {
url "file://" + pathToKotlinPlugin
}
mavenCentral()
}
dependencies {
compile project(":libraryProject"),
"org.jetbrains.kotlin:kotlin-js-library:0.1-SNAPSHOT"
}
compileKotlin2Js.kotlinOptions.sourceMap = true
compileKotlin2Js.kotlinOptions.outputFile = "${projectDir}/web/js/app.js"
compileKotlin2Js.kotlinOptions.suppressWarnings = true
compileKotlin2Js.kotlinOptions.verbose = true
copyKotlinJs.into "${projectDir}/web/js/lib"
@@ -0,0 +1,19 @@
package example
import kotlin.js.dom.html.document
import example.library.Counter
fun main(args: Array<String>) {
val el = document.createElement("div")
el.appendChild(document.createTextNode("Hello!"))
document.body.appendChild(el)
val counterDiv = document.createElement("div")
val counterText = document.createTextNode("Counter!")
counterDiv.appendChild(counterText)
document.body.appendChild(counterDiv)
val counter = Counter(counterText)
counter.start()
}
@@ -0,0 +1 @@
include 'mainProject', 'libraryProject'
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="js/lib/kotlin.js"></script>
</head>
<body>
<script src="js/app.js"></script>
</body>
</html>