From a4448736298e022b052fa05a8e587c13c2e5d2c4 Mon Sep 17 00:00:00 2001 From: gered Date: Thu, 20 Feb 2014 19:41:01 -0500 Subject: [PATCH] add tests --- test/clj_rhino/core_test.clj | 57 +++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/test/clj_rhino/core_test.clj b/test/clj_rhino/core_test.clj index f2f86c8..429643d 100644 --- a/test/clj_rhino/core_test.clj +++ b/test/clj_rhino/core_test.clj @@ -237,5 +237,60 @@ (is (= (js/eval scope "api.add(1, 2) + api.multiply(2, 3)") 9.0)) )))) -) + (testing "non-dynamic scoping - function cannot access variable set in a nested scope" + (is (= (js/toggle-dynamic-scope! false) false)) ; this is the default, but just being explicit about it + (let [scope (js/new-safe-scope)] + (js/eval scope "function getFoo() { return (typeof foo == 'undefined' ? undefined : foo); }") + + (is (= (type (js/eval scope "getFoo()")) org.mozilla.javascript.Undefined)) + + (let [nested-scope (js/new-scope nil scope)] + (js/set! nested-scope "foo" 1337) + (is (= (js/get nested-scope "foo") 1337)) + (is (= (type (js/eval nested-scope "getFoo()")) org.mozilla.javascript.Undefined))) + + (is (= (js/get scope "foo") org.mozilla.javascript.UniqueTag/NOT_FOUND)) + (is (= (type (js/eval scope "getFoo()")) org.mozilla.javascript.Undefined)))) + + (testing "dynamic scoping - function can access variable set in a nested scope" + (is (= (js/toggle-dynamic-scope! true) true)) + (let [scope (js/new-safe-scope)] + + (js/eval scope "function getFoo() { return (typeof foo == 'undefined' ? undefined : foo); }") + + (is (= (type (js/eval scope "getFoo()")) org.mozilla.javascript.Undefined)) + + (let [nested-scope (js/new-scope nil scope)] + (js/set! nested-scope "foo" 1337) + (is (= (js/get nested-scope "foo") 1337)) + (is (= (js/eval nested-scope "getFoo()") 1337))) + + (is (= (js/get scope "foo") org.mozilla.javascript.UniqueTag/NOT_FOUND)) + (is (= (type (js/eval scope "getFoo()")) org.mozilla.javascript.Undefined)) + + ; reset back to default now that the test is done + (is (= (js/toggle-dynamic-scope! false) false)))) + + (testing "dynamic scoping - function can access variable set in root scope and changed in nested scope" + (is (= (js/toggle-dynamic-scope! true) true)) + (let [scope (js/new-safe-scope)] + + (js/eval scope "function getFoo() { return (typeof foo == 'undefined' ? undefined : foo); }") + + (js/set! scope "foo" 42) + (is (= (js/get scope "foo") 42)) + (is (= (js/eval scope "getFoo()") 42)) + + (let [nested-scope (js/new-scope nil scope)] + (js/set! nested-scope "foo" 1337) + (is (= (js/get nested-scope "foo") 1337)) + (is (= (js/eval nested-scope "getFoo()") 1337))) + + (is (= (js/get scope "foo") 42)) + (is (= (js/eval scope "getFoo()") 42)) + + ; reset back to default now that the test is done + (is (= (js/toggle-dynamic-scope! false) false)))) + +)