diff options
Diffstat (limited to 'examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/ThisFunctionTest.scala')
-rw-r--r-- | examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/ThisFunctionTest.scala | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/ThisFunctionTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/ThisFunctionTest.scala new file mode 100644 index 0000000..4ac9058 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/jsinterop/ThisFunctionTest.scala @@ -0,0 +1,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") + } + + } +} |