"use strict";

var _vitest = require("vitest");
var _response = _interopRequireDefault(require("../../lib/parsec/response"));
var _index = _interopRequireDefault(require("../../lib/stream/index"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
(0, _vitest.describe)('Response Tests', function () {
  (0, _vitest.it)('response accepted', function () {
    (0, _vitest.expect)(_response["default"].accept().isAccepted()).toBe(true);
  });
  (0, _vitest.it)('response as a success', function () {
    (0, _vitest.expect)(_response["default"].accept().toTry().isSuccess()).toBe(true);
  });
  (0, _vitest.it)('response accepted map to accepted', function () {
    (0, _vitest.expect)(_response["default"].accept().map(function (a) {
      return a;
    }).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('response accepted map to return the value', function () {
    (0, _vitest.expect)(_response["default"].accept('a').map(function (a) {
      return a;
    }).value).toBe('a');
  });
  (0, _vitest.it)('response accepted map to pass in response as second argument', function () {
    (0, _vitest.expect)(_response["default"].accept('a').map(function (_, response) {
      return response;
    }).value).toEqual(_response["default"].accept('a'));
  });
  (0, _vitest.it)('response accepted flatMap to accepted', function () {
    (0, _vitest.expect)(_response["default"].accept('a').flatMap(function (a) {
      return _response["default"].accept(a);
    }).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('response accepted flatMap knows the offset', function () {
    var responseOffset = _response["default"].accept('a', 'abc', 1, false).flatMap(function (a, response) {
      return response.offset;
    });
    (0, _vitest.expect)(responseOffset).toBe(1);
  });
  (0, _vitest.it)('response accepted flatMap to return the value', function () {
    (0, _vitest.expect)(_response["default"].accept('a').flatMap(function (a) {
      return _response["default"].accept(a);
    }).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('response accepted flatMap to reject', function () {
    (0, _vitest.expect)(_response["default"].accept().flatMap(function () {
      return _response["default"].reject();
    }).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('response rejected map to rejected', function () {
    (0, _vitest.expect)(_response["default"].reject().map(function (t) {
      return t;
    }).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('response rejected map callback not called', function () {
    var calls = 0;
    _response["default"].reject().map(function () {
      calls++;
    });
    (0, _vitest.expect)(calls).toBe(0);
  });
  (0, _vitest.it)('response rejected flatMap to rejected', function () {
    (0, _vitest.expect)(_response["default"].reject().flatMap(function () {
      return _response["default"].accept();
    }).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('response rejected flatMap callback not called', function () {
    var calls = 0;
    _response["default"].reject().flatMap(function () {
      calls++;
    });
    (0, _vitest.expect)(calls).toBe(0);
  });
  (0, _vitest.it)('response accepted fold', function () {
    (0, _vitest.expect)(_response["default"].accept('a').fold(function (a) {
      return a.value;
    })).toBe('a');
  });
  (0, _vitest.it)('fold takes a function to map the value depending on result', function () {
    var value = _response["default"].accept('a').fold(function (accept) {
      return accept.value + '-potato';
    },
    // Accept has value, input, offset, consumed
    function (reject) {
      return reject.offset + '-tomato';
    }); // Reject has offset, consumed

    // we accept, so it should be a-potato
    (0, _vitest.expect)(value).toBe('a-potato');
    value = _response["default"].reject().fold(function (accept) {
      return accept.value + '-potato';
    }, function (reject) {
      return reject.offset + '-tomato';
    });

    // we reject, so it should use the second function
    // Offset is undefined because it's up to the parser to know which offset it's parsing
    (0, _vitest.expect)(value).toBe('undefined-tomato');
  });
  (0, _vitest.it)('response filter accepted', function () {
    (0, _vitest.expect)(_response["default"].accept('a').filter(function (a) {
      return a === 'a';
    }).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('response not filter accepted', function () {
    (0, _vitest.expect)(_response["default"].accept('a').filter(function (a) {
      return a !== 'a';
    }).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('accept can be consumed', function () {
    var myStream = _index["default"].ofString('abc');
    var acceptance = _response["default"].accept('c', myStream, 3, false);
    var consumed = acceptance.isEos();
    (0, _vitest.expect)(consumed).toBe(true);
  });
  (0, _vitest.it)('accept should not be yet consumed', function () {
    var myStream = _index["default"].ofString('abc');
    (0, _vitest.expect)(_response["default"].accept('b', myStream, 2, false).isEos()).toBe(false);
  });
  (0, _vitest.it)('response rejected', function () {
    (0, _vitest.expect)(_response["default"].reject().isAccepted()).toBe(false);
  });
  (0, _vitest.it)('response rejected should not be consumed', function () {
    (0, _vitest.expect)(_response["default"].reject().isEos()).toBe(false);
  });
  (0, _vitest.it)('response as a failure', function () {
    (0, _vitest.expect)(_response["default"].reject().toTry().isSuccess()).toBe(false);
  });
  (0, _vitest.it)('response rejected fold', function () {
    (0, _vitest.expect)(_response["default"].reject().fold(function (a) {
      return a.value;
    }, function () {
      return 'b';
    })).toBe('b');
  });
  (0, _vitest.it)('response filter rejected', function () {
    (0, _vitest.expect)(_response["default"].reject().filter(function () {
      return true;
    }).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('response not filter rejected', function () {
    (0, _vitest.expect)(_response["default"].reject().filter(function () {
      return false;
    }).isAccepted()).toBe(false);
  });
});
//# sourceMappingURL=response_test.spec.js.map