diff --git a/js/js.libraries/src/core/javalang.kt b/js/js.libraries/src/core/javalang.kt index 36e6950f747..5f0325f9e63 100644 --- a/js/js.libraries/src/core/javalang.kt +++ b/js/js.libraries/src/core/javalang.kt @@ -1,6 +1,6 @@ package java.lang -import java.util.Iterator; +import java.util.Iterator import js.library library diff --git a/js/js.libraries/src/junit/core.kt b/js/js.libraries/src/junit/core.kt index 37aaa2e442d..09964020077 100644 --- a/js/js.libraries/src/junit/core.kt +++ b/js/js.libraries/src/junit/core.kt @@ -2,3 +2,4 @@ package org.junit; native annotation class Test(name : String = "") {} + diff --git a/js/js.libraries/src/qunit/core.kt b/js/js.libraries/src/qunit/core.kt new file mode 100644 index 00000000000..8f40a765363 --- /dev/null +++ b/js/js.libraries/src/qunit/core.kt @@ -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; + diff --git a/js/js.libraries/src/stdlib/test.kt b/js/js.libraries/src/stdlib/test.kt new file mode 100644 index 00000000000..80c9c2b660f --- /dev/null +++ b/js/js.libraries/src/stdlib/test.kt @@ -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) + } +} \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestSupport.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestSupport.java index c8b4d4e0aa6..6c2f14ba2ad 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestSupport.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestSupport.java @@ -17,6 +17,7 @@ */ package org.jetbrains.k2js.test.semantics; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.jetbrains.annotations.NotNull; @@ -57,13 +58,22 @@ abstract class StdLibTestSupport extends SingleFileTranslationTest { "/dom/html/window.kt"); // lets add the standard JS library files - for (String libFileName : Config.LIB_FILE_NAMES) { + Iterable names = Iterables.concat(Config.LIB_FILE_NAMES, Config.LIB_FILE_NAMES_DEPENDENT_ON_STDLIB); + for (String libFileName : names) { if (!ignoreFiles.contains(libFileName)) { System.out.println("Compiling " + 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 for (EcmaVersion version : ecmaVersions) { K2JSCompiler compiler = new K2JSCompiler(); diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java index 0cd9e1ab726..9dda8814647 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java @@ -39,6 +39,8 @@ public class StdLibToJSTest extends StdLibTestSupport { generateJavaScriptFiles(EcmaVersion.all(), "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"); } } diff --git a/js/js.translator/qunit/index.html b/js/js.translator/qunit/index.html index efa915718e6..908d96cc9d6 100644 --- a/js/js.translator/qunit/index.html +++ b/js/js.translator/qunit/index.html @@ -6,7 +6,8 @@ - + + diff --git a/js/js.translator/src/org/jetbrains/k2js/config/Config.java b/js/js.translator/src/org/jetbrains/k2js/config/Config.java index cbfc0b1b876..1d452b4dc91 100644 --- a/js/js.translator/src/org/jetbrains/k2js/config/Config.java +++ b/js/js.translator/src/org/jetbrains/k2js/config/Config.java @@ -69,11 +69,33 @@ public abstract class Config { "/dom/html/htmlcore.kt", "/dom/html5/canvas.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 LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList( + "/stdlib/test.kt" ); public static final String LIBRARIES_LOCATION = "js/js.libraries/src"; + /** + * The file names in the standard library to compile + */ + @NotNull + public static final List 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 private final Project project; @Nullable diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java b/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java index 1efc125f83d..5d2d64ca78c 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java @@ -236,14 +236,14 @@ public final class Translation { prefix = "var "; 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(" expect(0);"); + //rawStatements.add(" expect(0);"); rawStatements.add(" _testCase." + funName + "();"); } else { rawStatements.add("QUnit.test( \"" + funName + "()\", function() {"); - rawStatements.add(" expect(0);"); + //rawStatements.add(" expect(0);"); rawStatements.add(" " + funName + "();"); } rawStatements.add("});"); diff --git a/libraries/stdlib/src/kotlin/test/Test.kt b/libraries/stdlib/src/kotlin/test/Test.kt index 245f31104be..9880f1045ba 100644 --- a/libraries/stdlib/src/kotlin/test/Test.kt +++ b/libraries/stdlib/src/kotlin/test/Test.kt @@ -3,32 +3,8 @@ */ package kotlin.test -import java.util.ServiceLoader - -private var _asserter: Asserter? = null - -public var asserter: Asserter - get() { - if (_asserter == null) { - val klass = javaClass() - 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 - } +// TODO should not need this - its here for the JS stuff +import java.lang.IllegalStateException /** Asserts that the given block returns true */ public inline fun assertTrue(message: String, block: ()-> Boolean) { @@ -122,14 +98,6 @@ public fun 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 */ @@ -144,37 +112,3 @@ trait Asserter { 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) - } -} \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/test/TestJVM.kt b/libraries/stdlib/src/kotlin/test/TestJVM.kt new file mode 100644 index 00000000000..3229b72ee1a --- /dev/null +++ b/libraries/stdlib/src/kotlin/test/TestJVM.kt @@ -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() + 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) + } +} \ No newline at end of file diff --git a/libraries/stdlib/test/js/SimpleTest.kt b/libraries/stdlib/test/js/SimpleTest.kt index d9447d5bb59..6b5d52d8054 100644 --- a/libraries/stdlib/test/js/SimpleTest.kt +++ b/libraries/stdlib/test/js/SimpleTest.kt @@ -1,16 +1,19 @@ package testPackage import org.junit.Test as test +import kotlin.test.* class SimpleTest { public fun testFoo() { val name = "world" val message = "hello $name!" + assertEquals("hello world", message) } test fun cheese() { val name = "world" val message = "bye $name!" + assertEquals("bye world!", message) } }