Skip to content

Fix #9755: Add tests for Scala.js top-level native members #9951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.scalajs.testsuite.jsinterop

import org.junit.Assert._
import org.junit.Test

import scala.scalajs.js
import scala.scalajs.js.annotation._

@js.native
@JSGlobal("interoperabilityTestGlobalValDefConstant")
val interoperabilityTestGlobalValDefConstant: Int = js.native

@js.native
@JSGlobal("interoperabilityTestGlobalValDefVariable")
def interoperabilityTestGlobalValDefVariable: Int = js.native

@js.native
@JSGlobal("interoperabilityTestGlobalValDefGetVariable")
def interoperabilityTestGlobalValDefGetVariable(): Int = js.native

@js.native
@JSGlobal("interoperabilityTestGlobalValDefSetVariable")
def interoperabilityTestGlobalValDefSetVariable(x: Int): Unit = js.native

@js.native
@JSGlobal("interoperabilityTestGlobalValDefFunction")
def interoperabilityTestGlobalValDefFunction(x: Int): Int = js.native

@js.native
@JSImport("querystring", "stringify")
def stringify(obj: js.Dictionary[String], sep: String = "&", eq: String = "="): String = js.native

@js.native
@JSImport("os", "EOL")
val EOL: String = js.native

@js.native
@JSImport("os", "EOL")
def EOLAsDef: String = js.native

class TopLevelNativeJSMembersTestScala3 {
@Test def should_access_top_level_JS_vars_and_functions_with_top_level_native_vals_and_defs(): Unit = {
js.eval("""
var interoperabilityTestGlobalValDefConstant = 654321;
var interoperabilityTestGlobalValDefVariable = 7357;
var interoperabilityTestGlobalValDefGetVariable = function() {
return interoperabilityTestGlobalValDefVariable;
}
var interoperabilityTestGlobalValDefSetVariable = function(x) {
interoperabilityTestGlobalValDefVariable = x;
}
var interoperabilityTestGlobalValDefFunction = function(x) {
return interoperabilityTestGlobalValDefVariable + x;
};
""")

assertEquals(654321, interoperabilityTestGlobalValDefConstant)

assertEquals(7357, interoperabilityTestGlobalValDefVariable)
assertEquals(7357, interoperabilityTestGlobalValDefGetVariable())
assertEquals(7360, interoperabilityTestGlobalValDefFunction(3))

interoperabilityTestGlobalValDefSetVariable(123)
assertEquals(123, interoperabilityTestGlobalValDefGetVariable())
assertEquals(126, interoperabilityTestGlobalValDefFunction(3))
}

/*
Ideally we would like to add those two tests as well.
However, we do not have infrastructure yet in the Dotty build to test Scala.js
with module support enabled, so the linker will refuse to link the @JSImports.

@Test def testImportFunctionInModule(): Unit = {
val dict = js.Dictionary("foo" -> "bar", "baz" -> "qux")

assertEquals("foo=bar&baz=qux", stringify(dict))
assertEquals("foo:bar;baz:qux", stringify(dict, ";", ":"))
}

@Test def testImportFieldInModule(): Unit = {
assertEquals("string", js.typeOf(EOL))
assertEquals("string", js.typeOf(EOLAsDef))
}
*/
}