vm.js 825 B

123456789101112131415161718192021222324252627282930313233343536
  1. var test = require('tape');
  2. var vm = require('../');
  3. test('vmRunInNewContext', function (t) {
  4. t.plan(6);
  5. t.equal(vm.runInNewContext('a + 5', { a : 100 }), 105);
  6. (function () {
  7. var vars = { x : 10 };
  8. t.equal(vm.runInNewContext('x++', vars), 10);
  9. t.equal(vars.x, 11);
  10. })();
  11. (function () {
  12. var vars = { x : 10 };
  13. t.equal(vm.runInNewContext('var y = 3; y + x++', vars), 13);
  14. t.equal(vars.x, 11);
  15. t.equal(vars.y, 3);
  16. })();
  17. t.end();
  18. });
  19. test('vmRunInContext', function (t) {
  20. t.plan(2);
  21. var context = vm.createContext({ foo: 1 });
  22. vm.runInContext('var x = 1', context);
  23. t.deepEqual(context, { foo: 1, x: 1 });
  24. vm.runInContext('var y = 1', context);
  25. t.deepEqual(context, { foo: 1, x: 1, y: 1 });
  26. });