'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _helpers = require('../formatter/helpers');

var _stack_trace_filter = require('./stack_trace_filter');

var _stack_trace_filter2 = _interopRequireDefault(_stack_trace_filter);

var _status = require('../status');

var _status2 = _interopRequireDefault(_status);

var _test_case_runner = require('./test_case_runner');

var _test_case_runner2 = _interopRequireDefault(_test_case_runner);

var _user_code_runner = require('../user_code_runner');

var _user_code_runner2 = _interopRequireDefault(_user_code_runner);

var _verror = require('verror');

var _verror2 = _interopRequireDefault(_verror);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Runtime = function () {
  // options - {dryRun, failFast, filterStacktraces, strict}
  function Runtime(_ref) {
    var eventBroadcaster = _ref.eventBroadcaster,
        options = _ref.options,
        supportCodeLibrary = _ref.supportCodeLibrary,
        testCases = _ref.testCases;
    (0, _classCallCheck3.default)(this, Runtime);

    this.eventBroadcaster = eventBroadcaster;
    this.options = options || {};
    this.stackTraceFilter = new _stack_trace_filter2.default();
    this.supportCodeLibrary = supportCodeLibrary;
    this.testCases = testCases || [];
    this.result = {
      duration: 0,
      success: true
    };
  }

  (0, _createClass3.default)(Runtime, [{
    key: 'runTestRunHooks',
    value: function () {
      var _ref2 = (0, _bluebird.coroutine)(function* (key, name) {
        var _this = this;

        yield _bluebird2.default.each(this.supportCodeLibrary[key], function () {
          var _ref3 = (0, _bluebird.coroutine)(function* (hookDefinition) {
            var _ref4 = yield _user_code_runner2.default.run({
              argsArray: [],
              fn: hookDefinition.code,
              thisArg: null,
              timeoutInMilliseconds: hookDefinition.options.timeout || _this.supportCodeLibrary.defaultTimeout
            }),
                error = _ref4.error;

            if (error) {
              var location = (0, _helpers.formatLocation)(hookDefinition);
              throw new _verror2.default(error, name + ' hook errored, process exiting: ' + location);
            }
          });

          return function (_x3) {
            return _ref3.apply(this, arguments);
          };
        }());
      });

      function runTestRunHooks(_x, _x2) {
        return _ref2.apply(this, arguments);
      }

      return runTestRunHooks;
    }()
  }, {
    key: 'runTestCase',
    value: function () {
      var _ref5 = (0, _bluebird.coroutine)(function* (testCase) {
        var skip = this.options.dryRun || this.options.failFast && !this.result.success;
        var testCaseRunner = new _test_case_runner2.default({
          eventBroadcaster: this.eventBroadcaster,
          skip: skip,
          supportCodeLibrary: this.supportCodeLibrary,
          testCase: testCase,
          worldParameters: this.options.worldParameters
        });
        var testCaseResult = yield testCaseRunner.run();
        if (testCaseResult.duration) {
          this.result.duration += testCaseResult.duration;
        }
        if (this.shouldCauseFailure(testCaseResult.status)) {
          this.result.success = false;
        }
      });

      function runTestCase(_x4) {
        return _ref5.apply(this, arguments);
      }

      return runTestCase;
    }()
  }, {
    key: 'start',
    value: function () {
      var _ref6 = (0, _bluebird.coroutine)(function* () {
        if (this.options.filterStacktraces) {
          this.stackTraceFilter.filter();
        }
        this.eventBroadcaster.emit('test-run-started');
        yield this.runTestRunHooks('beforeTestRunHookDefinitions', 'a BeforeAll');
        yield _bluebird2.default.each(this.testCases, this.runTestCase.bind(this));
        yield this.runTestRunHooks('afterTestRunHookDefinitions', 'an AfterAll');
        this.eventBroadcaster.emit('test-run-finished', { result: this.result });
        if (this.options.filterStacktraces) {
          this.stackTraceFilter.unfilter();
        }
        return this.result.success;
      });

      function start() {
        return _ref6.apply(this, arguments);
      }

      return start;
    }()
  }, {
    key: 'shouldCauseFailure',
    value: function shouldCauseFailure(status) {
      return _lodash2.default.includes([_status2.default.AMBIGUOUS, _status2.default.FAILED, _status2.default.UNDEFINED], status) || status === _status2.default.PENDING && this.options.strict;
    }
  }]);
  return Runtime;
}();

exports.default = Runtime;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7552 characters]