react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / contextify / test / contextify.js
81144 viewsvar Contextify = require('../lib/contextify.js');12exports['basic tests'] = {3// Creating a context shouldn't fail.4'blank context' : function (test) {5var ctx = Contextify({});6test.notEqual(ctx, null);7test.notEqual(ctx, undefined);8test.done();9},1011// Creating a context with sandbox shouldn't change existing sandbox12// properties.13'basic context' : function (test) {14var sandbox = {15prop1 : 'prop1',16prop2 : 'prop2'17};18Contextify(sandbox);19test.equal(sandbox.prop1, 'prop1');20test.equal(sandbox.prop2, 'prop2');21test.done();22},2324'basic createContext' : function (test) {25var sandbox = {26prop1: 'prop1',27prop2: 'prop2'28};29var context = Contextify.createContext(sandbox);30test.equal(sandbox.prop1, 'prop1');31test.equal(sandbox.prop2, 'prop2');32test.done();33},3435// Ensure that the correct properties exist on a wrapped sandbox.36'test contextified object extra properties' : function (test) {37var sandbox = Contextify({});38test.notEqual(sandbox.run, undefined);39test.notEqual(sandbox.getGlobal, undefined);40test.notEqual(sandbox.dispose, undefined);41test.done();42},4344'createContext should not modify the sandbox' : function (test) {45var sandbox = {};46Contextify.createContext(sandbox);47test.equal(sandbox.run, undefined);48test.equal(sandbox.getGlobal, undefined);49test.equal(sandbox.dispose, undefined);50test.done();51},5253// Passing undefined should create an empty context.54'test undefined sandbox' : function (test) {55// Should return an empty object.56test.notEqual(Contextify(undefined, undefined), undefined);57test.notEqual(Contextify(), undefined);58test.done();59},6061'sandbox prototype properties should be searched' : function (test) {62var sandbox = {};63sandbox.__proto__ = {64prop1 : 'test'65};66Contextify(sandbox);67test.equal(sandbox.getGlobal().prop1, 'test');68test.done();69},7071// Make sure properties that aren't there...aren't there.72'test for nonexistent properties' : function (test) {73var global = Contextify({}).getGlobal();74test.equal(global.test1, undefined);75test.done();76},7778// Make sure properties with value "undefined" are there.79'test for "undefined" properties' : function (test) {80var sandbox = { x: undefined };81Contextify(sandbox);82sandbox.run("_x = x");83test.equal(sandbox._x, undefined);84test.done();85},8687'test for "undefined" properties with createContext' : function (test) {88var sandbox = { x: undefined };89var context = Contextify.createContext(sandbox);90context.run("_x = x");91test.equal(sandbox._x, undefined);92test.done();93},9495'test for "undefined" variables' : function (test) {96var sandbox = { };97Contextify(sandbox);98// In JavaScript a declared variable is set to 'undefined'.99sandbox.run("var y; (function() { var _y ; y = _y })()");100test.equal(sandbox._y, undefined);101// This should apply to top-level variables (global properties).102sandbox.run("var z; _z = z");103test.equal(sandbox._z, undefined);104// Make sure nothing wacky happens when accessing global declared but105// undefined variables.106test.equal(sandbox.getGlobal().z, undefined);107test.done();108},109110// Make sure run can be called with a filename parameter.111'test run with filename' : function (test) {112var sandbox = Contextify();113sandbox.run('var x = 3', "test.js");114test.equal(sandbox.x, 3);115test.done();116},117118// Make sure run can be called on a context119'test run with createContext' : function (test) {120var sandbox = {};121var context = Contextify.createContext(sandbox);122context.run('var x = 3', "test.js");123test.equal(sandbox.x, 3);124test.done();125},126127// Make sure getters/setters on the sandbox object are used.128'test accessors on sandbox' : function (test) {129var sandbox = {};130sandbox.__defineGetter__('test', function () { return 3;});131sandbox.__defineSetter__('test2', function (val) { this.x = val;});132Contextify(sandbox);133var global = sandbox.getGlobal();134test.equal(global.test, 3);135sandbox.test2 = 5;136test.equal(sandbox.x, 5);137global.test2 = 7;138test.equal(global.x, 7);139test.equal(sandbox.x, 7);140test.done();141},142143// Make sure dispose cleans up the sandbox.144'test dispose' : function (test) {145var sandbox = Contextify();146test.notEqual(sandbox.run, undefined);147test.notEqual(sandbox.getGlobal, undefined);148test.notEqual(sandbox.dispose, undefined);149sandbox.dispose();150test.throws(function () {151sandbox.run();152}, Error);153test.throws(function () {154sandbox.getGlobal();155}, Error);156test.throws(function () {157sandbox.dispose();158}, Error);159test.done();160}161};162163exports['synchronous script tests'] = {164// Synchronous context script execution:165// Ensure that global variables are put on the sandbox object.166'global variables in scripts should go on sandbox' : function (test) {167var sandbox = {168prop1 : 'prop1',169prop2 : 'prop2'170};171Contextify(sandbox);172sandbox.run('x = 3');173test.equal(sandbox.x, 3);174test.done();175},176177// Synchronous context script execution:178// Ensure that sandbox properties can be accessed as global variables.179'sandbox properties should be globals' : function (test) {180var sandbox = {181prop1 : 'prop1',182prop2 : 'prop2'183};184Contextify(sandbox);185sandbox.run("test1 = (prop1 == 'prop1');" +186"test2 = (prop2 == 'prop2');");187test.ok(sandbox.test1);188test.ok(sandbox.test2);189test.done();190}191};192193exports['asynchronous script tests'] = {194// Asynchronous context script execution:195// Ensure that global variables are put on the sandbox object.196'global variables in scripts should go on sandbox' : function (test) {197var sandbox = {198setTimeout : setTimeout,199prop1 : 'prop1',200prop2 : 'prop2'201};202Contextify(sandbox);203sandbox.run('setTimeout(function () {x = 3}, 0);');204test.equal(sandbox.x, undefined);205setTimeout(function () {206test.equal(sandbox.x, 3);207test.done();208}, 0);209},210211// Asynchronous context script execution:212// Ensure that sandbox properties can be accessed as global variables.213'sandbox properties should be globals' : function (test) {214var sandbox = {215setTimeout : setTimeout,216prop1 : 'prop1',217prop2 : 'prop2'218};219Contextify(sandbox);220sandbox.run("setTimeout(function () {" +221"test1 = (prop1 == 'prop1');" +222"test2 = (prop2 == 'prop2');" +223"}, 0)");224test.equal(sandbox.test1, undefined);225test.equal(sandbox.test2, undefined);226setTimeout(function () {227test.ok(sandbox.test1);228test.ok(sandbox.test2);229test.done();230}, 0);231},232233// Asynchronous context script execution:234// Ensure that sandbox properties can be accessed as global variables.235'createContext: sandbox properties should be globals' : function (test) {236var sandbox = {237setTimeout : setTimeout,238prop1 : 'prop1',239prop2 : 'prop2'240};241var context = Contextify.createContext(sandbox);242context.run("setTimeout(function () {" +243"test1 = (prop1 == 'prop1');" +244"test2 = (prop2 == 'prop2');" +245"}, 0)");246test.equal(sandbox.test1, undefined);247test.equal(sandbox.test2, undefined);248setTimeout(function () {249test.ok(sandbox.test1);250test.ok(sandbox.test2);251test.done();252}, 0);253},254255// Asynchronous context script execution:256// Ensure that async execution is safely executed after dispose257'setTimeout should not fail after dispose' : function (test) {258var sandbox = {259test: test,260setTimeout : setTimeout,261prop1 : 'prop1',262prop2 : 'prop2'263};264Contextify(sandbox);265sandbox.run("setTimeout(function () {" +266"test.ok(prop1 == 'prop1');" +267"test.ok(prop2 == 'prop2');" +268"test.done();" +269"}, 10)");270test.equal(sandbox.test1, undefined);271test.equal(sandbox.test2, undefined);272sandbox.dispose();273}274};275276exports['test global'] = {277// Make sure getGlobal() works.278'basic test' : function (test) {279var sandbox = {280prop1 : 'prop1',281prop2 : 'prop2'282};283Contextify(sandbox);284var global = sandbox.getGlobal();285test.notDeepEqual(global, null);286test.notDeepEqual(global, undefined);287// Make sure global is forwarding properly.288test.equal(global.prop1, 'prop1');289test.equal(global.prop2, 'prop2');290global.prop3 = 'prop3';291test.equal(sandbox.prop3, 'prop3');292test.done();293},294295// Make sure that references to the global are correct.296'self references to the global object' : function (test) {297var sandbox = Contextify({});298var global = sandbox.getGlobal();299sandbox.ref1 = global;300sandbox.ref2 = {301ref2 : global302};303sandbox.run("test1 = (this == ref1);" +304"test2 = (this == ref2.ref2);");305test.ok(sandbox.test1);306test.ok(sandbox.test2);307test.done();308},309310// Make sure the enumerator is enumerating correctly.311'test enumerator' : function (test) {312var sandbox = {313prop1 : 'prop1',314prop2 : 'prop2'315};316var global = Contextify(sandbox).getGlobal();317var globalProps = Object.keys(global);318test.equal(globalProps.length, 5);319test.ok(globalProps.indexOf('prop1') != -1);320test.ok(globalProps.indexOf('prop2') != -1);321test.ok(globalProps.indexOf('run') != -1);322test.ok(globalProps.indexOf('getGlobal') != -1);323test.ok(globalProps.indexOf('dispose') != -1);324test.done();325},326327// Make sure deleter is working.328'test deleter' : function (test) {329var sandbox = {330prop1 : 'prop1',331prop2 : 'prop2'332};333var global = Contextify(sandbox).getGlobal();334test.equal(Object.keys(global).length, 5);335test.equal(Object.keys(sandbox).length, 5);336delete global.prop1;337test.equal(Object.keys(global).length, 4);338test.equal(Object.keys(sandbox).length, 4);339delete global.prop2;340test.equal(Object.keys(global).length, 3);341test.equal(Object.keys(sandbox).length, 3);342delete global.run;343test.equal(Object.keys(global).length, 2);344test.equal(Object.keys(sandbox).length, 2);345delete global.getGlobal;346test.equal(Object.keys(global).length, 1);347test.equal(Object.keys(sandbox).length, 1);348delete global.dispose;349test.equal(Object.keys(global).length, 0);350test.equal(Object.keys(sandbox).length, 0);351test.done();352},353354// Make sure the global's class name is the same as the sandbox.355'test global class name' : function (test) {356function DOMWindow () {};357var sandbox = Contextify(new DOMWindow());358var global = sandbox.getGlobal();359test.equal(sandbox.constructor.name, 'DOMWindow');360test.equal(sandbox.constructor.name, global.constructor.name);361sandbox.run('thisName = this.constructor.name');362test.equal(sandbox.thisName, sandbox.constructor.name);363test.done();364},365366// Make sure functions in global scope are accessible through global.367'test global functions' : function (test) {368var sandbox = Contextify();369var global = sandbox.getGlobal();370sandbox.run("function testing () {}");371test.notEqual(global.testing, undefined);372test.done();373},374375// Make sure global can be a receiver for run().376'test global.run()' : function (test) {377var global = Contextify().getGlobal();378global.run("x = 5");379test.equal(global.x, 5);380test.done();381},382383// Make sure global can be a receiver for getGlobal().384'test global.getGlobal()' : function (test) {385var global = Contextify().getGlobal();386test.deepEqual(global, global.getGlobal());387test.done();388},389390//Make sure global can be a receiver for dispose().391'test global.dispose()' : function (test) {392var sandbox = Contextify();393var global = sandbox.getGlobal();394test.notEqual(global.run, undefined);395test.notEqual(global.getGlobal, undefined);396test.notEqual(global.dispose, undefined);397global.dispose();398// It's not safe to use the global after disposing.399test.throws(function () {400sandbox.run();401}, Error);402test.throws(function () {403sandbox.getGlobal();404}, Error);405test.throws(function () {406sandbox.dispose();407}, Error);408test.done();409},410411'test context delete global' : function (test) {412var sandbox = Contextify({});413sandbox.global = sandbox.getGlobal();414sandbox.run('delete global.global;');415test.ok(!sandbox.global);416sandbox.dispose();417test.done();418},419420'test context delete unwritable global' : function (test) {421var sandbox = Contextify({});422Object.defineProperty(sandbox, 'global', {423enumerable: false,424configurable: false,425writable: false,426value: sandbox.getGlobal()427});428429sandbox.run('delete global.global;');430test.ok(sandbox.global);431test.ok(sandbox.global.global);432sandbox.dispose();433test.done();434}435};436437438// Test that multiple contexts don't interfere with each other.439exports['test multiple contexts'] = function (test) {440var sandbox1 = {441prop1 : 'prop1',442prop2 : 'prop2'443};444var sandbox2 = {445prop1 : 'prop1',446prop2 : 'prop2'447};448var global1 = Contextify(sandbox1).getGlobal();449var global2 = Contextify(sandbox2).getGlobal();450test.equal(global1.prop1, 'prop1');451test.equal(global2.prop1, 'prop1');452sandbox1.run('test = 3');453sandbox2.run('test = 4');454test.equal(sandbox1.test, 3);455test.equal(global1.test, 3);456test.equal(sandbox2.test, 4);457test.equal(global2.test, 4);458test.done();459};460461// Test console - segfaults in REPL.462exports['test console'] = function (test) {463var sandbox = {464console : console,465prop1 : 'prop1'466};467Contextify(sandbox);468test.doesNotThrow(function () {469sandbox.run('console.log(prop1);');470});471test.done();472};473474475// Test eval scope.476exports['test eval'] = {477'basic test' : function (test) {478var sandbox = Contextify();479sandbox.run('eval("test1 = 1")');480test.equal(sandbox.test1, 1);481sandbox.run('(function() { eval("test2 = 2") })()');482test.equal(sandbox.test2, 2);483test.done();484},485486'this test' : function (test) {487var sandbox = Contextify();488sandbox.run('e = eval ; e("test1 = 1")');489test.equal(sandbox.test1, 1);490sandbox.run('var t = 1 ; (function() { var t = 2; test2 = eval("t") })()');491test.equal(sandbox.test2, 2);492sandbox.run('t = 1 ; (function() { var t = 2; e = eval; test3 = e("t") })()');493test.equal(sandbox.test3, 1);494sandbox.run('var t = 1 ; global = this; (function() { var t = 2; e = eval; test4 = global.eval.call(global, "t") })()');495test.equal(sandbox.test4, 1);496test.done();497}498};499500501// Make sure exceptions get thrown for invalid scripts.502exports['test exceptions'] = {503'basic test' : function (test) {504var sandbox = Contextify();505// Exceptions thrown from "run" will be from the Contextified context.506var ReferenceError = sandbox.run('ReferenceError');507var SyntaxError = sandbox.run('SyntaxError');508test.throws(function () {509sandbox.run('doh');510}, ReferenceError);511test.throws(function () {512sandbox.run('x = y');513}, ReferenceError);514test.throws(function () {515sandbox.run('function ( { (( }{);');516}, SyntaxError);517test.done();518},519520'test double dispose() - sandbox' : function (test) {521var sandbox = Contextify();522test.doesNotThrow(function () {523sandbox.dispose();524});525test.throws(function () {526sandbox.dispose();527}, 'Called dispose() twice.');528test.done();529},530531'test double dispose - global' : function (test) {532var sandbox = Contextify();533var global = sandbox.getGlobal();534test.doesNotThrow(function () {535global.dispose();536});537test.throws(function () {538global.dispose();539}, 'Called dispose() twice.');540test.done();541},542543'test run() after dispose()' : function (test) {544var sandbox = Contextify();545test.doesNotThrow(function () {546sandbox.dispose();547});548test.throws(function () {549sandbox.run('var x = 3');550}, 'Called run() after dispose().');551test.done();552},553554'test getGlobal() after dispose()' : function (test) {555var sandbox = Contextify();556test.doesNotThrow(function () {557sandbox.dispose();558});559test.throws(function () {560var g = sandbox.getGlobal();561}, 'Called getGlobal() after dispose().');562test.done();563}564};565566exports['test scripts'] = {567'test createScript()' : function (test) {568var script = Contextify.createScript('var x = 3', 'test.js');569test.equal(typeof script.runInContext, 'function');570test.done();571},572573'test createScript() without code' : function (test) {574test.throws(function () {575Contextify.createScript();576});577test.throws(function () {578Contextify.createScript(true);579});580test.throws(function () {581Contextify.createScript(null);582});583test.throws(function () {584Contextify.createScript(1);585});586test.done();587},588589'test runInContext' : function (test) {590var sandbox = {};591var script = Contextify.createScript('var x = 3', 'test.js');592var context = Contextify.createContext(sandbox);593script.runInContext(context);594test.equal(sandbox.x, 3);595test.done();596}597};598599600