1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
/* __ *\
** ________ ___ / / ___ __ ____ Scala.js Test Suite **
** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ **
** /____/\___/_/ |_/____/_/ | |__/ /____/ **
** |/____/ **
\* */
package scala.scalajs.testsuite.jsinterop
import scala.scalajs.js
import org.scalajs.jasminetest.JasmineTest
object ThisFunctionTest extends JasmineTest {
describe("scala.scalajs.js.ThisFunctionN") {
it("should provide an implicit conversion from Scala function to js.ThisFunction") {
val g = js.eval("""
var g = function(f, x) { return f.call(x, 42, x.foo); }; g;
""").asInstanceOf[js.Function2[js.ThisFunction2[
js.Dynamic, Int, String, String], js.Dynamic, String]]
val f = { (thiz: js.Dynamic, v: Int, u: String) =>
expect(thiz).toBeTruthy()
expect(thiz.foobar).toEqual("foobar")
u + v
}
val obj = js.Object().asInstanceOf[js.Dynamic]
obj.foo = "foo"
obj.foobar = "foobar"
expect(g(f, obj)).toEqual("foo42")
}
it("should accept a lambda where a js.ThisFunction is expected") {
val g = js.eval("""
var g = function(f, x) { return f.call(x, 42, x.foo); }; g;
""").asInstanceOf[js.Function2[js.ThisFunction2[
js.Dynamic, Int, String, String], js.Dynamic, String]]
val obj = js.Object().asInstanceOf[js.Dynamic]
obj.foo = "foo"
obj.foobar = "foobar"
expect(g({ (thiz: js.Dynamic, v: Int, u: String) =>
expect(thiz).toBeTruthy()
expect(thiz.foobar).toEqual("foobar")
u + v
}, obj)).toEqual("foo42")
}
it("should bind the first argument to this when applying js.ThisFunctionN") {
val g = js.eval("""
var g = function(x) { return this.foo + ":" + x; }; g;
""").asInstanceOf[js.ThisFunction1[js.Dynamic, Int, String]]
val obj = js.Object().asInstanceOf[js.Dynamic]
obj.foo = "foo"
expect(g(obj, 42)).toEqual("foo:42")
}
it("should provide an implicit conversion from js.ThisFunction to Scala function") {
val g = js.eval("""
var g = function(x) { return this.foo + ":" + x; }; g;
""").asInstanceOf[js.ThisFunction1[js.Dynamic, Int, String]]
val f: scala.Function2[js.Dynamic, Int, String] = g
val obj = js.Object().asInstanceOf[js.Dynamic]
obj.foo = "foo"
expect(f(obj, 42)).toEqual("foo:42")
}
}
}
|