compile the assertions and a test case to QUnit on JS and got a working example to run the unit test case - yay!

This commit is contained in:
James Strachan
2012-06-13 16:14:13 +01:00
parent 9394e21208
commit 3765e85ca8
12 changed files with 170 additions and 76 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
package java.lang package java.lang
import java.util.Iterator; import java.util.Iterator
import js.library import js.library
library library
+1
View File
@@ -2,3 +2,4 @@ package org.junit;
native native
annotation class Test(name : String = "") {} annotation class Test(name : String = "") {}
+11
View File
@@ -0,0 +1,11 @@
package QUnit
import js.native
/**
* The [QUnit](http://qunitjs.com/) API
*/
native
fun ok(actual: Boolean, message: String): Unit = js.noImpl;
+39
View File
@@ -0,0 +1,39 @@
package kotlin.test
/**
* Comments out a block of test code until it is implemented while keeping a link to the code
* to implement in your unit test output
*/
public inline fun todo(block: ()-> Any) {
// println("TODO at " + (Exception() as java.lang.Throwable).getStackTrace()?.get(1) + " for " + block)
println("TODO at " + block)
}
/**
* Provides the JS implementation of asserter using [QUnit](http://QUnitjs.com/)
*/
public var asserter: Asserter = QUnitAsserter()
public class QUnitAsserter(): Asserter {
public override fun assertTrue(message: String, actual: Boolean) {
QUnit.ok(actual, message)
}
public override fun assertEquals(message: String, expected: Any?, actual: Any?) {
QUnit.ok(expected == actual, "$message. Expected <$expected> actual <$actual>")
}
public override fun assertNotNull(message: String, actual: Any?) {
QUnit.ok(actual != null, message)
}
public override fun assertNull(message: String, actual: Any?) {
QUnit.ok(actual == null, message)
}
public override fun fail(message: String) {
QUnit.ok(false, message)
}
}
@@ -17,6 +17,7 @@
*/ */
package org.jetbrains.k2js.test.semantics; package org.jetbrains.k2js.test.semantics;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -57,13 +58,22 @@ abstract class StdLibTestSupport extends SingleFileTranslationTest {
"/dom/html/window.kt"); "/dom/html/window.kt");
// lets add the standard JS library files // lets add the standard JS library files
for (String libFileName : Config.LIB_FILE_NAMES) { Iterable<String> names = Iterables.concat(Config.LIB_FILE_NAMES, Config.LIB_FILE_NAMES_DEPENDENT_ON_STDLIB);
for (String libFileName : names) {
if (!ignoreFiles.contains(libFileName)) { if (!ignoreFiles.contains(libFileName)) {
System.out.println("Compiling " + libFileName); System.out.println("Compiling " + libFileName);
files.add(Config.LIBRARIES_LOCATION + libFileName); files.add(Config.LIBRARIES_LOCATION + libFileName);
} }
} }
// lets add the standard Kotlin library files
for (String libFileName : Config.STDLIB_FILE_NAMES) {
if (!ignoreFiles.contains(libFileName)) {
System.out.println("Compiling " + libFileName);
files.add(Config.STDLIB_LOCATION + libFileName);
}
}
// now lets try invoke the compiler // now lets try invoke the compiler
for (EcmaVersion version : ecmaVersions) { for (EcmaVersion version : ecmaVersions) {
K2JSCompiler compiler = new K2JSCompiler(); K2JSCompiler compiler = new K2JSCompiler();
@@ -39,6 +39,8 @@ public class StdLibToJSTest extends StdLibTestSupport {
generateJavaScriptFiles(EcmaVersion.all(), generateJavaScriptFiles(EcmaVersion.all(),
"libraries/stdlib/src", "libraries/stdlib/src",
"kotlin/Preconditions.kt", "kotlin/dom/Dom.kt", "kotlin/support/AbstractIterator.kt"); "kotlin/Preconditions.kt",
"kotlin/dom/Dom.kt",
"kotlin/support/AbstractIterator.kt");
} }
} }
+2 -1
View File
@@ -6,7 +6,8 @@
<link rel="stylesheet" href="qunit.css"> <link rel="stylesheet" href="qunit.css">
<script src="qunit.js"></script> <script src="qunit.js"></script>
<script src="../testFiles/kotlin_lib_ecma3.js"></script> <script src="../testFiles/kotlin_lib_ecma3.js"></script>
<script src="../testFiles/kotlin_lib.js"></script>
<script src="../testFiles/stdlib/out/GenerateTestCase.compiler_v3.js"></script> <script src="../testFiles/stdlib/out/GenerateTestCase.compiler_v3.js"></script>
<script src="deepEqual.js"></script> <script src="deepEqual.js"></script>
@@ -69,11 +69,33 @@ public abstract class Config {
"/dom/html/htmlcore.kt", "/dom/html/htmlcore.kt",
"/dom/html5/canvas.kt", "/dom/html5/canvas.kt",
"/dom/html/window.kt", "/dom/html/window.kt",
"/junit/core.kt" "/junit/core.kt",
"/qunit/core.kt"
);
/**
* the library files which depend on the STDLIB files to be able to compile
*/
@NotNull
public static final List<String> LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList(
"/stdlib/test.kt"
); );
public static final String LIBRARIES_LOCATION = "js/js.libraries/src"; public static final String LIBRARIES_LOCATION = "js/js.libraries/src";
/**
* The file names in the standard library to compile
*/
@NotNull
public static final List<String> STDLIB_FILE_NAMES = Arrays.asList(
"/kotlin/test/Test.kt"
);
/**
* The location of the stdlib sources
*/
public static final String STDLIB_LOCATION = "libraries/stdlib/src";
@NotNull @NotNull
private final Project project; private final Project project;
@Nullable @Nullable
@@ -236,14 +236,14 @@ public final class Translation {
prefix = "var "; prefix = "var ";
declaredVar = true; declaredVar = true;
} }
rawStatements.add(prefix + "_testCase = new " + className + "();"); rawStatements.add(prefix + "_testCase = new Kotlin.defs." + className + "();");
} }
rawStatements.add("QUnit.test( \"" + className + "." + funName + "()\" , function() {"); rawStatements.add("QUnit.test( \"" + className + "." + funName + "()\" , function() {");
rawStatements.add(" expect(0);"); //rawStatements.add(" expect(0);");
rawStatements.add(" _testCase." + funName + "();"); rawStatements.add(" _testCase." + funName + "();");
} else { } else {
rawStatements.add("QUnit.test( \"" + funName + "()\", function() {"); rawStatements.add("QUnit.test( \"" + funName + "()\", function() {");
rawStatements.add(" expect(0);"); //rawStatements.add(" expect(0);");
rawStatements.add(" " + funName + "();"); rawStatements.add(" " + funName + "();");
} }
rawStatements.add("});"); rawStatements.add("});");
+2 -68
View File
@@ -3,32 +3,8 @@
*/ */
package kotlin.test package kotlin.test
import java.util.ServiceLoader // TODO should not need this - its here for the JS stuff
import java.lang.IllegalStateException
private var _asserter: Asserter? = null
public var asserter: Asserter
get() {
if (_asserter == null) {
val klass = javaClass<Asserter>()
val loader = ServiceLoader.load(klass)
for (a in loader) {
if (a != null) {
_asserter = a
break
}
}
if (_asserter == null) {
_asserter = DefaultAsserter()
}
//debug("using asserter $_asserter")
}
return _asserter.sure()
}
set(value) {
_asserter = value
}
/** Asserts that the given block returns true */ /** Asserts that the given block returns true */
public inline fun assertTrue(message: String, block: ()-> Boolean) { public inline fun assertTrue(message: String, block: ()-> Boolean) {
@@ -122,14 +98,6 @@ public fun <T: Throwable> failsWith(block: ()-> Any): T {
} }
} }
/**
* Comments out a block of test code until it is implemented while keeping a link to the code
* to implement in your unit test output
*/
public inline fun todo(block: ()-> Any) {
println("TODO at " + (Exception() as java.lang.Throwable).getStackTrace()?.get(1) + " for " + block)
}
/** /**
* A plugin for performing assertions which can reuse JUnit or TestNG * A plugin for performing assertions which can reuse JUnit or TestNG
*/ */
@@ -144,37 +112,3 @@ trait Asserter {
public fun fail(message: String): Unit public fun fail(message: String): Unit
} }
/**
* Default implementation to avoid dependency on JUnit or TestNG
*/
class DefaultAsserter() : Asserter {
public override fun assertTrue(message : String, actual : Boolean) {
if (!actual) {
fail(message)
}
}
public override fun assertEquals(message : String, expected : Any?, actual : Any?) {
if (expected != actual) {
fail("$message. Expected <$expected> actual <$actual>")
}
}
public override fun assertNotNull(message : String, actual : Any?) {
if (actual == null) {
fail(message)
}
}
public override fun assertNull(message : String, actual : Any?) {
if (actual != null) {
fail(message)
}
}
public override fun fail(message : String) {
// TODO work around compiler bug as it should never try call the private constructor
throw AssertionError(message as Object)
}
}
@@ -0,0 +1,71 @@
package kotlin.test
import java.util.ServiceLoader
/**
* Comments out a block of test code until it is implemented while keeping a link to the code
* to implement in your unit test output
*/
public inline fun todo(block: ()-> Any) {
println("TODO at " + (Exception() as java.lang.Throwable).getStackTrace()?.get(1) + " for " + block)
}
private var _asserter: Asserter? = null
public var asserter: Asserter
get() {
if (_asserter == null) {
val klass = javaClass<Asserter>()
val loader = ServiceLoader.load(klass)
for (a in loader) {
if (a != null) {
_asserter = a
break
}
}
if (_asserter == null) {
_asserter = DefaultAsserter()
}
//debug("using asserter $_asserter")
}
return _asserter.sure()
}
set(value) {
_asserter = value
}
/**
* Default implementation to avoid dependency on JUnit or TestNG
*/
class DefaultAsserter() : Asserter {
public override fun assertTrue(message : String, actual : Boolean) {
if (!actual) {
fail(message)
}
}
public override fun assertEquals(message : String, expected : Any?, actual : Any?) {
if (expected != actual) {
fail("$message. Expected <$expected> actual <$actual>")
}
}
public override fun assertNotNull(message : String, actual : Any?) {
if (actual == null) {
fail(message)
}
}
public override fun assertNull(message : String, actual : Any?) {
if (actual != null) {
fail(message)
}
}
public override fun fail(message : String) {
// TODO work around compiler bug as it should never try call the private constructor
throw AssertionError(message as Any)
}
}
+3
View File
@@ -1,16 +1,19 @@
package testPackage package testPackage
import org.junit.Test as test import org.junit.Test as test
import kotlin.test.*
class SimpleTest { class SimpleTest {
public fun testFoo() { public fun testFoo() {
val name = "world" val name = "world"
val message = "hello $name!" val message = "hello $name!"
assertEquals("hello world", message)
} }
test fun cheese() { test fun cheese() {
val name = "world" val name = "world"
val message = "bye $name!" val message = "bye $name!"
assertEquals("bye world!", message)
} }
} }