node.d.ts: Create tests for "vm" module

This commit is contained in:
rhysd
2016-03-30 14:58:30 +09:00
parent 984de2a7a7
commit 13bc8b8337
+61
View File
@@ -16,6 +16,7 @@ import * as path from "path";
import * as readline from "readline";
import * as childProcess from "child_process";
import * as os from "os";
import * as vm from "vm";
// Specifically test buffer module regression.
import {Buffer as ImportedBuffer, SlowBuffer as ImportedSlowBuffer} from "buffer";
@@ -679,3 +680,63 @@ namespace os_tests {
result = os.networkInterfaces();
}
}
////////////////////////////////////////////////////
/// vm tests : https://nodejs.org/api/vm.html
////////////////////////////////////////////////////
namespace vm_tests {
{
const sandbox = {
animal: 'cat',
count: 2
};
const context = new vm.createContext(sandbox);
console.log(vm.isContext(context));
const script = new vm.Script('count += 1; name = "kitty"');
for (let i = 0; i < 10; ++i) {
script.runInContext(context);
}
console.log(util.inspect(sandbox));
vm.runInNewContext('count += 1; name = "kitty"', sandbox);
console.log(util.inspect(sandbox));
}
{
const sandboxes = [{}, {}, {}];
const script = new vm.Script('globalVar = "set"');
sandboxes.forEach((sandbox) => {
script.runInNewContext(sandbox);
});
console.log(util.inspect(sandboxes));
}
{
global.globalVar = 0;
const script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' });
for (var i = 0; i < 1000; ++i) {
script.runInThisContext();
}
console.log(globalVar);
var localVar = 'initial value';
vm.runInThisContext('localVar = "vm";');
console.log(localVar);
}
{
const Debug = vm.runInDebugContext('Debug');
Debug.scripts().forEach(function(script) { console.log(script.name); });
}
}