1 // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
9 // This script loads the test file in the virtual machine and runs it in a
10 // context that only exposes a test variable with methods for testing and to
13 // Note: an important part of this script is to keep nodejs-isms away from test
14 // code and isolate it from implementation details.
15 var fs = require('fs');
16 var vm = require('vm');
17 var BotManager = require('./botmanager.js');
19 function Test(botType) {
20 // TODO(houssainy) set the time out.
21 this.timeout_ = setTimeout(
22 this.fail.bind(this, "Test timeout!"),
24 this.botType_ = botType;
29 console.log.apply(console.log, arguments);
32 abort: function (error) {
33 var error = new Error(error || "Test aborted");
34 console.log(error.stack);
38 assert: function (value, message) {
40 this.abort(message || "Assert failed.");
45 this.assert(false, "Test failed.");
49 clearTimeout(this.timeout_);
50 console.log("Test succeeded");
54 // Utility method to wait for multiple callbacks to be executed.
55 // functions - array of functions to call with a callback.
56 // doneCallback - called when all callbacks on the array have completed.
57 wait: function (functions, doneCallback) {
58 var result = new Array(functions.length);
59 var missingResult = functions.length;
60 for (var i = 0; i != functions.length; ++i)
61 functions[i](complete.bind(this, i));
63 function complete(index, value) {
65 result[index] = value;
66 if (missingResult == 0)
67 doneCallback.apply(null, result);
71 spawnBot: function (name, doneCallback) {
72 // Lazy initialization of botmanager.
73 if (!this.botManager_)
74 this.botManager_ = new BotManager();
75 this.botManager_.spawnNewBot(name, this.botType_, doneCallback);
79 function runTest(botType, testfile) {
80 console.log("Running test: " + testfile);
81 var script = vm.createScript(fs.readFileSync(testfile), testfile);
82 script.runInNewContext({ test: new Test(botType) });
85 runTest(process.argv[2], process.argv[3]);