Browse Source

test,vm: enable strict mode for vm tests

Some vm tests are not in strict mode because they need to create and use
global variables. By using `global.foo` instead of just `foo`, we can
still enable strict mode.

PR-URL: https://github.com/nodejs/node/pull/6209
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
process-exit-stdio-flushing
Rich Trott 9 years ago
parent
commit
15f13cd74a
  1. 40
      test/parallel/test-vm-new-script-new-context.js
  2. 36
      test/parallel/test-vm-new-script-this-context.js
  3. 36
      test/parallel/test-vm-run-in-new-context.js

40
test/parallel/test-vm-new-script-new-context.js

@ -1,15 +1,15 @@
/* eslint-disable strict */ 'use strict';
var common = require('../common'); const common = require('../common');
var assert = require('assert'); const assert = require('assert');
var Script = require('vm').Script; const Script = require('vm').Script;
common.globalCheck = false; common.globalCheck = false;
console.error('run a string'); console.error('run a string');
var script = new Script('\'passed\';'); var script = new Script('\'passed\';');
console.error('script created'); console.error('script created');
var result1 = script.runInNewContext(); const result1 = script.runInNewContext();
var result2 = script.runInNewContext(); const result2 = script.runInNewContext();
assert.equal('passed', result1); assert.equal('passed', result1);
assert.equal('passed', result2); assert.equal('passed', result2);
@ -27,31 +27,31 @@ assert.throws(function() {
}, /not defined/); }, /not defined/);
hello = 5; global.hello = 5;
script = new Script('hello = 2'); script = new Script('hello = 2');
script.runInNewContext(); script.runInNewContext();
assert.equal(5, hello); assert.equal(5, global.hello);
console.error('pass values in and out'); console.error('pass values in and out');
code = 'foo = 1;' + global.code = 'foo = 1;' +
'bar = 2;' + 'bar = 2;' +
'if (baz !== 3) throw new Error(\'test fail\');'; 'if (baz !== 3) throw new Error(\'test fail\');';
foo = 2; global.foo = 2;
obj = { foo: 0, baz: 3 }; global.obj = { foo: 0, baz: 3 };
script = new Script(code); script = new Script(global.code);
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
var baz = script.runInNewContext(obj); var baz = script.runInNewContext(global.obj);
/* eslint-enable no-unused-vars */ /* eslint-enable no-unused-vars */
assert.equal(1, obj.foo); assert.equal(1, global.obj.foo);
assert.equal(2, obj.bar); assert.equal(2, global.obj.bar);
assert.equal(2, foo); assert.equal(2, global.foo);
console.error('call a function by reference'); console.error('call a function by reference');
script = new Script('f()'); script = new Script('f()');
function changeFoo() { foo = 100; } function changeFoo() { global.foo = 100; }
script.runInNewContext({ f: changeFoo }); script.runInNewContext({ f: changeFoo });
assert.equal(foo, 100); assert.equal(global.foo, 100);
console.error('modify an object by reference'); console.error('modify an object by reference');
script = new Script('f.a = 2'); script = new Script('f.a = 2');

36
test/parallel/test-vm-new-script-this-context.js

@ -1,13 +1,13 @@
/* eslint-disable strict */ 'use strict';
var common = require('../common'); const common = require('../common');
var assert = require('assert'); const assert = require('assert');
var Script = require('vm').Script; const Script = require('vm').Script;
common.globalCheck = false; common.globalCheck = false;
console.error('run a string'); console.error('run a string');
var script = new Script('\'passed\';'); var script = new Script('\'passed\';');
var result = script.runInThisContext(script); const result = script.runInThisContext(script);
assert.equal('passed', result); assert.equal('passed', result);
console.error('thrown error'); console.error('thrown error');
@ -16,26 +16,26 @@ assert.throws(function() {
script.runInThisContext(script); script.runInThisContext(script);
}); });
hello = 5; global.hello = 5;
script = new Script('hello = 2'); script = new Script('hello = 2');
script.runInThisContext(script); script.runInThisContext(script);
assert.equal(2, hello); assert.equal(2, global.hello);
console.error('pass values'); console.error('pass values');
code = 'foo = 1;' + global.code = 'foo = 1;' +
'bar = 2;' + 'bar = 2;' +
'if (typeof baz !== \'undefined\') throw new Error(\'test fail\');'; 'if (typeof baz !== "undefined") throw new Error("test fail");';
foo = 2; global.foo = 2;
obj = { foo: 0, baz: 3 }; global.obj = { foo: 0, baz: 3 };
script = new Script(code); script = new Script(global.code);
script.runInThisContext(script); script.runInThisContext(script);
assert.equal(0, obj.foo); assert.equal(0, global.obj.foo);
assert.equal(2, bar); assert.equal(2, global.bar);
assert.equal(1, foo); assert.equal(1, global.foo);
console.error('call a function'); console.error('call a function');
f = function() { foo = 100; }; global.f = function() { global.foo = 100; };
script = new Script('f()'); script = new Script('f()');
script.runInThisContext(script); script.runInThisContext(script);
assert.equal(100, foo); assert.equal(100, global.foo);

36
test/parallel/test-vm-run-in-new-context.js

@ -1,16 +1,16 @@
/* eslint-disable strict */ 'use strict';
// Flags: --expose-gc // Flags: --expose-gc
var common = require('../common'); const common = require('../common');
var assert = require('assert'); const assert = require('assert');
var vm = require('vm'); const vm = require('vm');
assert.equal(typeof gc, 'function', 'Run this test with --expose-gc'); assert.equal(typeof gc, 'function', 'Run this test with --expose-gc');
common.globalCheck = false; common.globalCheck = false;
console.error('run a string'); console.error('run a string');
var result = vm.runInNewContext('\'passed\';'); const result = vm.runInNewContext('\'passed\';');
assert.equal('passed', result); assert.equal('passed', result);
console.error('thrown error'); console.error('thrown error');
@ -18,28 +18,28 @@ assert.throws(function() {
vm.runInNewContext('throw new Error(\'test\');'); vm.runInNewContext('throw new Error(\'test\');');
}); });
hello = 5; global.hello = 5;
vm.runInNewContext('hello = 2'); vm.runInNewContext('hello = 2');
assert.equal(5, hello); assert.equal(5, global.hello);
console.error('pass values in and out'); console.error('pass values in and out');
code = 'foo = 1;' + global.code = 'foo = 1;' +
'bar = 2;' + 'bar = 2;' +
'if (baz !== 3) throw new Error(\'test fail\');'; 'if (baz !== 3) throw new Error(\'test fail\');';
foo = 2; global.foo = 2;
obj = { foo: 0, baz: 3 }; global.obj = { foo: 0, baz: 3 };
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
var baz = vm.runInNewContext(code, obj); var baz = vm.runInNewContext(global.code, global.obj);
/* eslint-enable no-unused-vars */ /* eslint-enable no-unused-vars */
assert.equal(1, obj.foo); assert.equal(1, global.obj.foo);
assert.equal(2, obj.bar); assert.equal(2, global.obj.bar);
assert.equal(2, foo); assert.equal(2, global.foo);
console.error('call a function by reference'); console.error('call a function by reference');
function changeFoo() { foo = 100; } function changeFoo() { global.foo = 100; }
vm.runInNewContext('f()', { f: changeFoo }); vm.runInNewContext('f()', { f: changeFoo });
assert.equal(foo, 100); assert.equal(global.foo, 100);
console.error('modify an object by reference'); console.error('modify an object by reference');
var f = { a: 1 }; var f = { a: 1 };

Loading…
Cancel
Save