Skip to content

Commit 63dd4f9

Browse files
authored
Merge pull request #9951 from adpi2/sjs-top-level-native-defs
Fix #9755: Add tests for Scala.js top-level native members
2 parents 47cd8d4 + e583ee8 commit 63dd4f9

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.scalajs.testsuite.jsinterop
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
6+
import scala.scalajs.js
7+
import scala.scalajs.js.annotation._
8+
9+
@js.native
10+
@JSGlobal("interoperabilityTestGlobalValDefConstant")
11+
val interoperabilityTestGlobalValDefConstant: Int = js.native
12+
13+
@js.native
14+
@JSGlobal("interoperabilityTestGlobalValDefVariable")
15+
def interoperabilityTestGlobalValDefVariable: Int = js.native
16+
17+
@js.native
18+
@JSGlobal("interoperabilityTestGlobalValDefGetVariable")
19+
def interoperabilityTestGlobalValDefGetVariable(): Int = js.native
20+
21+
@js.native
22+
@JSGlobal("interoperabilityTestGlobalValDefSetVariable")
23+
def interoperabilityTestGlobalValDefSetVariable(x: Int): Unit = js.native
24+
25+
@js.native
26+
@JSGlobal("interoperabilityTestGlobalValDefFunction")
27+
def interoperabilityTestGlobalValDefFunction(x: Int): Int = js.native
28+
29+
@js.native
30+
@JSImport("querystring", "stringify")
31+
def stringify(obj: js.Dictionary[String], sep: String = "&", eq: String = "="): String = js.native
32+
33+
@js.native
34+
@JSImport("os", "EOL")
35+
val EOL: String = js.native
36+
37+
@js.native
38+
@JSImport("os", "EOL")
39+
def EOLAsDef: String = js.native
40+
41+
class TopLevelNativeJSMembersTestScala3 {
42+
@Test def should_access_top_level_JS_vars_and_functions_with_top_level_native_vals_and_defs(): Unit = {
43+
js.eval("""
44+
var interoperabilityTestGlobalValDefConstant = 654321;
45+
var interoperabilityTestGlobalValDefVariable = 7357;
46+
var interoperabilityTestGlobalValDefGetVariable = function() {
47+
return interoperabilityTestGlobalValDefVariable;
48+
}
49+
var interoperabilityTestGlobalValDefSetVariable = function(x) {
50+
interoperabilityTestGlobalValDefVariable = x;
51+
}
52+
var interoperabilityTestGlobalValDefFunction = function(x) {
53+
return interoperabilityTestGlobalValDefVariable + x;
54+
};
55+
""")
56+
57+
assertEquals(654321, interoperabilityTestGlobalValDefConstant)
58+
59+
assertEquals(7357, interoperabilityTestGlobalValDefVariable)
60+
assertEquals(7357, interoperabilityTestGlobalValDefGetVariable())
61+
assertEquals(7360, interoperabilityTestGlobalValDefFunction(3))
62+
63+
interoperabilityTestGlobalValDefSetVariable(123)
64+
assertEquals(123, interoperabilityTestGlobalValDefGetVariable())
65+
assertEquals(126, interoperabilityTestGlobalValDefFunction(3))
66+
}
67+
68+
/*
69+
Ideally we would like to add those two tests as well.
70+
However, we do not have infrastructure yet in the Dotty build to test Scala.js
71+
with module support enabled, so the linker will refuse to link the @JSImports.
72+
73+
@Test def testImportFunctionInModule(): Unit = {
74+
val dict = js.Dictionary("foo" -> "bar", "baz" -> "qux")
75+
76+
assertEquals("foo=bar&baz=qux", stringify(dict))
77+
assertEquals("foo:bar;baz:qux", stringify(dict, ";", ":"))
78+
}
79+
80+
@Test def testImportFieldInModule(): Unit = {
81+
assertEquals("string", js.typeOf(EOL))
82+
assertEquals("string", js.typeOf(EOLAsDef))
83+
}
84+
*/
85+
}

0 commit comments

Comments
 (0)