"use strict";
var match_error_1 = require("./errors/match-error");
require("reflect-metadata");
var TestRunner = (function () {
function TestRunner() {
var _this = this;
this._currentTestId = 0;
this._currentTestFixtureIndex = 0;
this._currentTestIndex = 0;
this._currentTestCaseIndex = 0;
this._getTestDescription = function (test, testCaseArguments) {
var testDescription = _this._currentTestId + " - " + test.description;
if (testCaseArguments !== undefined) {
testDescription += " [ " + testCaseArguments.map(function (x) { return JSON.stringify(x) || "undefined"; }).join(", ") + " ]";
}
return testDescription;
};
this._runNextTestCase = function () {
_this._currentTestCaseIndex++;
if (!_this._currentTestSet.testFixtures[_this._currentTestFixtureIndex].tests[_this._currentTestIndex].testCases[_this._currentTestCaseIndex]) {
_this._runNextTest();
}
else {
_this._runTest(_this._currentTestSet.testFixtures[_this._currentTestFixtureIndex], _this._currentTestSet.testFixtures[_this._currentTestFixtureIndex].tests[_this._currentTestIndex], _this._currentTestSet.testFixtures[_this._currentTestFixtureIndex].tests[_this._currentTestIndex].testCases[_this._currentTestCaseIndex].arguments);
}
};
}
TestRunner.prototype.run = function (testSet) {
this._currentTestSet = testSet;
if (this._currentTestSet.testFixtures.length === 0) {
process.stderr.write("no tests to run\n");
process.exit(1);
}
else {
var totalTestCount = this._currentTestSet.testFixtures.map(function (x) { return x.tests.map(function (y) { return y.testCases.length; }).reduce(function (a, b) { return a + b; }); }).reduce(function (a, b) { return a + b; });
process.stdout.write("TAP version 13\n");
process.stdout.write("1.." + totalTestCount + "\n");
this._runTest(this._currentTestSet.testFixtures[this._currentTestFixtureIndex], this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex], this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments);
}
};
TestRunner.prototype._runTest = function (testFixture, test, testCaseArguments) {
var _this = this;
this._currentTestId++;
var setupFunctions = Reflect.getMetadata("alsatian:setup", testFixture.fixture);
if (setupFunctions) {
setupFunctions.forEach(function (setupFunction) {
testFixture.fixture[setupFunction].call(testFixture.fixture);
});
}
try {
if (test.isAsync) {
var timeout_1 = false;
var promise = testFixture.fixture[test.key].apply(testFixture.fixture, testCaseArguments);
promise.then(function () {
if (!timeout_1) {
_this._notifySuccess(test, testCaseArguments);
}
})
.catch(function (error) {
_this._handleError(error, test, testCaseArguments);
});
var timeoutCheck = setTimeout(function () {
timeout_1 = true;
_this._handleError(new Error("The test exceeded the given timeout."), test, testCaseArguments);
}, 500);
}
else {
testFixture.fixture[test.key].apply(testFixture, testCaseArguments);
this._notifySuccess(test, testCaseArguments);
}
}
catch (error) {
this._handleError(error, test, testCaseArguments);
}
};
TestRunner.prototype._handleError = function (error, test, testCaseArguments) {
process.stdout.write("not ok " + this._getTestDescription(test, testCaseArguments) + "\n");
if (error instanceof match_error_1.MatchError) {
process.stdout.write(" ---\n message: \"" + error.message + "\"\n severity: fail\n data:\n got: " + JSON.stringify(error.actualValue) + "\n expect: " + JSON.stringify(error.expectedValue) + "\n ...\n");
}
else {
console.log(error);
process.stdout.write("# Unknown Error\n");
}
this._teardown();
};
TestRunner.prototype._notifySuccess = function (test, testCaseArguments) {
process.stdout.write("ok " + this._getTestDescription(test, testCaseArguments) + "\n");
this._teardown();
};
TestRunner.prototype._teardown = function () {
var testFixture = this._currentTestSet.testFixtures[this._currentTestFixtureIndex];
var teardownFunctions = Reflect.getMetadata("alsatian:teardown", testFixture.fixture);
if (teardownFunctions) {
teardownFunctions.forEach(function (teardownFunction) {
testFixture.fixture[teardownFunction].call(testFixture.fixture);
});
}
this._runNextTestCase();
};
TestRunner.prototype._exit = function () {
process.exit(0);
};
TestRunner.prototype._runNextTestFixture = function () {
this._currentTestFixtureIndex++;
this._currentTestIndex = 0;
this._currentTestCaseIndex = 0;
if (!this._currentTestSet.testFixtures[this._currentTestFixtureIndex]) {
this._exit();
}
else {
this._runTest(this._currentTestSet.testFixtures[this._currentTestFixtureIndex], this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex], this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments);
}
};
TestRunner.prototype._runNextTest = function () {
this._currentTestIndex++;
this._currentTestCaseIndex = 0;
if (!this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex]) {
this._runNextTestFixture();
}
else {
this._runTest(this._currentTestSet.testFixtures[this._currentTestFixtureIndex], this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex], this._currentTestSet.testFixtures[this._currentTestFixtureIndex].tests[this._currentTestIndex].testCases[this._currentTestCaseIndex].arguments);
}
};
return TestRunner;
}());
exports.TestRunner = TestRunner;
//# sourceMappingURL=test-runner.js.map |