Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #17 from tanishiking/test-start-function
Browse files Browse the repository at this point in the history
test: Remove `@JSExportTopLevel` from test-suites functions
  • Loading branch information
sjrd authored Mar 15, 2024
2 parents a03e78b + 7d78e4e commit 025fddc
Show file tree
Hide file tree
Showing 16 changed files with 272 additions and 229 deletions.
10 changes: 2 additions & 8 deletions cli/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@ object Main {

val result =
if (mode == "testsuite") {
val className = TestSuites.suites.map(_.className)
val moduleInitializers = className
.map { clazz =>
ModuleInitializer.mainMethod(clazz, "main")
}
.zip(className)

for {
irFiles <- new CliReader(classpath).irFiles
_ <- Future.sequence {
moduleInitializers.map { case (moduleInitializer, className) =>
TestSuites.suites.map { case TestSuites.TestSuite(className, methodName) =>
val moduleInitializer = ModuleInitializer.mainMethod(className, methodName)
Compiler.compileIRFiles(
irFiles,
List(moduleInitializer),
Expand Down
23 changes: 11 additions & 12 deletions cli/src/main/scala/TestSuites.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package cli

object TestSuites {
case class TestSuite(className: String, methodName: String)
case class TestSuite(className: String, methodName: String = "main")
val suites = List(
TestSuite("testsuite.core.simple.Simple", "simple"),
TestSuite("testsuite.core.add.Add", "add"),
TestSuite("testsuite.core.add.Add", "add"),
TestSuite("testsuite.core.virtualdispatch.VirtualDispatch", "virtualDispatch"),
TestSuite("testsuite.core.interfacecall.InterfaceCall", "interfaceCall"),
TestSuite("testsuite.core.asinstanceof.AsInstanceOfTest", "asInstanceOf"),
TestSuite("testsuite.core.jsinterop.JSInteropTest", "jsInterop"),
TestSuite("testsuite.core.hijackedclassesdispatch.HijackedClassesDispatchTest", "hijackedClassesDispatch"),
TestSuite("testsuite.core.hijackedclassesmono.HijackedClassesMonoTest", "hijackedClassesMono"),
TestSuite("testsuite.core.hijackedclassesupcast.HijackedClassesUpcastTest", "hijackedClassesUpcast"),
TestSuite("testsuite.core.tostring.ToStringTest", "toStringConversions")
TestSuite("testsuite.core.Simple"),
TestSuite("testsuite.core.Add"),
TestSuite("testsuite.core.VirtualDispatch"),
TestSuite("testsuite.core.InterfaceCall"),
TestSuite("testsuite.core.AsInstanceOfTest"),
TestSuite("testsuite.core.JSInteropTest"),
TestSuite("testsuite.core.HijackedClassesDispatchTest"),
TestSuite("testsuite.core.HijackedClassesMonoTest"),
TestSuite("testsuite.core.HijackedClassesUpcastTest"),
TestSuite("testsuite.core.ToStringTest")
)
}
17 changes: 17 additions & 0 deletions test-suite/src/main/scala/testsuite/Assert.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package testsuite

/** Temporary assertion method on Scala for Wasm. `ok` method generates `unreachable` if the given
* condition is false, trapping at runtime.
*
* While it's desirable to eventually utilize Scala's assertion, it's currently unavailable because
* we cannot compile Throwable to wasm yet, thus throw new (Throwable) is unusable. and making
* assert unavailable as well.
*
* Using JS's assert isn't feasible either; `console.assert` merely displays a message when
* assertion failure, and Node's assert module is unsupported for Wasm due to current
* unavailability of `JSImport` and module.
*/
object Assert {
def ok(cond: Boolean): Unit =
if (!cond) null.toString() // Apply to Null should compile to unreachable
}
10 changes: 4 additions & 6 deletions test-suite/src/main/scala/testsuite/core/Add.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package testsuite.core.add
package testsuite.core

import scala.scalajs.js.annotation._
import testsuite.Assert.ok

object Add {
def main(): Unit = { val _ = test() }
@JSExportTopLevel("add")
def test(): Boolean = {
1 + 1 == 2
def main(): Unit = {
ok(1 + 1 == 2)
}
}
17 changes: 8 additions & 9 deletions test-suite/src/main/scala/testsuite/core/AsInstanceOfTest.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package testsuite.core.asinstanceof
package testsuite.core

import scala.scalajs.js.annotation._
import testsuite.Assert.ok

object AsInstanceOfTest {
def main(): Unit = { val _ = test() }

@JSExportTopLevel("asInstanceOf")
def test(): Boolean = {
testInt(5) &&
testClasses(new Child()) &&
testString("foo", true)
def main(): Unit = {
ok(
testInt(5) &&
testClasses(new Child()) &&
testString("foo", true)
)
}

def testClasses(c: Child): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
package testsuite.core.hijackedclassesdispatch
package testsuite.core

import scala.scalajs.js.annotation._
import testsuite.Assert.ok

object HijackedClassesDispatchTest {
def main(): Unit = { val _ = test() }

@JSExportTopLevel("hijackedClassesDispatch")
def test(): Boolean = {
def main(): Unit = {
val obj = new Test()
val otherObj = new Test()
val obj2 = new Test2()
val otherObj2 = new Test2()
testToString(true, "true") &&
testToString(54321, "54321") &&
testToString(obj, "Test class") &&
testToString(obj2, "[object]") &&
testToString('A', "A") &&
testHashCode(true, 1231) &&
testHashCode(54321, 54321) &&
testHashCode("foo", 101574) &&
testHashCode(obj, 123) &&
testHashCode(obj2, 42) &&
testHashCode('A', 65) &&
testIntValue(Int.box(5), 5) &&
testIntValue(Long.box(6L), 6) &&
testIntValue(Double.box(7.5), 7) &&
testIntValue(new CustomNumber(), 789) &&
testLength("foo", 3) &&
testLength(new CustomCharSeq(), 54) &&
testCharAt("foobar", 3, 'b') &&
testCharAt(new CustomCharSeq(), 3, 'A') &&
testEquals(true, 1, false) &&
testEquals(1.0, 1, true) &&
testEquals("foo", "foo", true) &&
testEquals("foo", "bar", false) &&
testEquals(obj, obj2, false) &&
testEquals(obj, otherObj, true) &&
testEquals(obj2, otherObj2, false) &&
testNotifyAll(true) &&
testNotifyAll(obj)
ok(
testToString(true, "true") &&
testToString(54321, "54321") &&
testToString(obj, "Test class") &&
testToString(obj2, "[object]") &&
testToString('A', "A") &&
testHashCode(true, 1231) &&
testHashCode(54321, 54321) &&
testHashCode("foo", 101574) &&
testHashCode(obj, 123) &&
testHashCode(obj2, 42) &&
testHashCode('A', 65) &&
testIntValue(Int.box(5), 5) &&
testIntValue(Long.box(6L), 6) &&
testIntValue(Double.box(7.5), 7) &&
testIntValue(new CustomNumber(), 789) &&
testLength("foo", 3) &&
testLength(new CustomCharSeq(), 54) &&
testCharAt("foobar", 3, 'b') &&
testCharAt(new CustomCharSeq(), 3, 'A') &&
testEquals(true, 1, false) &&
testEquals(1.0, 1, true) &&
testEquals("foo", "foo", true) &&
testEquals("foo", "bar", false) &&
testEquals(obj, obj2, false) &&
testEquals(obj, otherObj, true) &&
testEquals(obj2, otherObj2, false) &&
testNotifyAll(true) &&
testNotifyAll(obj)
)
}

def testToString(x: Any, expected: String): Boolean =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package testsuite.core.hijackedclassesmono
package testsuite.core

import scala.scalajs.js.annotation._
import testsuite.Assert.ok

object HijackedClassesMonoTest {
def main(): Unit = { val _ = test() }

@JSExportTopLevel("hijackedClassesMono")
def test(): Boolean = {
testInteger(5) &&
testString("foo")
def main(): Unit = {
ok(
testInteger(5) &&
testString("foo")
)
}

def testInteger(x: Int): Boolean = {
Expand All @@ -17,6 +16,6 @@ object HijackedClassesMonoTest {

def testString(foo: String): Boolean = {
foo.length() == 3 &&
foo.hashCode() == 101574
foo.hashCode() == 101574
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package testsuite.core.hijackedclassesupcast
package testsuite.core

import scala.scalajs.js.annotation._
import testsuite.Assert.ok

object HijackedClassesUpcastTest {
def main(): Unit = { val _ = test() }
def main(): Unit = {
ok(
testBoolean(true) &&
testInteger(5) &&
testIntegerNull(null) &&
testString("foo") &&
testStringNull(null) &&
testCharacter('A')
)

@JSExportTopLevel("hijackedClassesUpcast")
def test(): Boolean = {
testBoolean(true) &&
testInteger(5) &&
testIntegerNull(null) &&
testString("foo") &&
testStringNull(null) &&
testCharacter('A')
}

def testBoolean(x: Boolean): Boolean = {
Expand All @@ -39,11 +39,11 @@ object HijackedClassesUpcastTest {

def testIntegerNull(x: Any): Boolean = {
!x.isInstanceOf[Int] &&
!x.isInstanceOf[java.lang.Integer] &&
(x.asInstanceOf[Int] == 0) && {
val x2 = x.asInstanceOf[java.lang.Integer]
x2 == null
}
!x.isInstanceOf[java.lang.Integer] &&
(x.asInstanceOf[Int] == 0) && {
val x2 = x.asInstanceOf[java.lang.Integer]
x2 == null
}
}

def testString(x: String): Boolean = {
Expand Down
11 changes: 4 additions & 7 deletions test-suite/src/main/scala/testsuite/core/InterfaceCall.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package testsuite.core.interfacecall
package testsuite.core

import scala.scalajs.js.annotation._
import testsuite.Assert.ok

object InterfaceCall {
def main(): Unit = { val _ = test() }

@JSExportTopLevel("interfaceCall")
def test(): Boolean = {
def main(): Unit = {
val c = new Concrete()
c.plus(c.zero, 1) == 1 && c.minus(1, c.zero) == 1
ok(c.plus(c.zero, 1) == 1 && c.minus(1, c.zero) == 1)
}

class Concrete extends AddSub with Zero {
Expand Down
Loading

0 comments on commit 025fddc

Please sign in to comment.