"use strict";

var _vitest = require("vitest");
var _index = _interopRequireDefault(require("../../lib/stream/index"));
var _index2 = require("../../lib/parsec/index");
var _unit = _interopRequireDefault(require("../../lib/data/unit"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function spaces() {
  return _index2.C.charIn(' \r\n\f\t').optrep().map(function () {
    return _unit["default"];
  });
}
(0, _vitest.describe)('Parser Chain Tests', function () {
  (0, _vitest.it)('expect (chain) to be accepted', function () {
    var lower = _index2.C["char"]('x');
    var upper = _index2.F.satisfy(function (val) {
      return val === 'x';
    });
    var parser = lower.chain(upper);
    var response = parser.parse(_index["default"].ofString('x'));
    (0, _vitest.expect)(response.isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (chain) to be rejected', function () {
    var lower = _index2.C["char"]('x');
    var upper = _index2.F.satisfy(function (val) {
      return val === 'y';
    });
    var parser = lower.chain(upper);
    var response = parser.parse(_index["default"].ofString('x'));
    (0, _vitest.expect)(response.isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (chain) to be accepted and offset to have move', function () {
    var lower = _index2.C["char"]('x');
    var upper = _index2.F.satisfy(function (val) {
      return val === 'x';
    });
    var parser = lower.chain(upper);
    var response = parser.parse(_index["default"].ofString('x'));
    (0, _vitest.expect)(response.offset).toBe(1);
  });
  (0, _vitest.it)('expect (chain) to be accepted and offset to have move more', function () {
    var lower = _index2.C.string('xyz');
    var upper = _index2.F.satisfy(function (val) {
      return val === 'xyz';
    });
    var parser = lower.chain(upper.then(upper));
    var response = parser.parse(_index["default"].ofString('xyzxyz'));
    (0, _vitest.expect)(response.offset).toBe(2);
  });
  (0, _vitest.it)('expect (chain) to be find back the source offset', function () {
    var lower = _index2.C.string('xyz');
    var upper = _index2.F.satisfy(function (val) {
      return val === 'xyz';
    });
    var parser = lower.chain(upper.then(upper));
    var response = parser.parse(_index["default"].ofString('xyzxyz'));
    (0, _vitest.expect)(response.input.source.offsets[response.offset]).toBe(6);
  });
  (0, _vitest.it)('expect (chain) to be accepted again', function () {
    var p1 = _index2.N.number().thenLeft(_index2.C["char"](' ').opt());
    var p2 = _index2.F.any().then(_index2.F.any()).thenLeft(_index2.F.eos()).map(function (r) {
      return r[0] + r[1];
    });
    (0, _vitest.expect)(p1.chain(p2).parse(_index["default"].ofString('12 34'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (chain) to return 46', function () {
    var p1 = _index2.N.number().thenLeft(_index2.C["char"](' ').opt());
    var p2 = _index2.F.any().then(_index2.F.any()).thenLeft(_index2.F.eos()).array().map(function (r) {
      return r[0] + r[1];
    });
    (0, _vitest.expect)(p1.chain(p2).parse(_index["default"].ofString('12 34'), 0).value).toBe(46);
  });
  (0, _vitest.it)('expect (chain) to add multiple numbers', function () {
    var token = _index2.N.number().then(spaces().opt().drop()).single();
    var lex = _index2.F.satisfy(function (number) {
      return number > 0;
    }).rep().map(function (values) {
      return values.array().reduce(function (acc, n) {
        return acc + n;
      }, 0);
    });
    var parsing = token.chain(lex).parse(_index["default"].ofString('10 12 44'), 0);
    (0, _vitest.expect)(parsing.isEos()).toBe(true);
    (0, _vitest.expect)(parsing.value).toBe(66);
  });
  (0, _vitest.it)('expect (chain) to be not satisfied by upper level', function () {
    var token = _index2.N.number().then(spaces().opt().drop());
    var lex = _index2.F.satisfy(function (number) {
      return number > 0;
    }).rep().map(function (values) {
      return values.array().reduce(function (acc, n) {
        return acc + n;
      }, 0);
    });
    var parsing = token.chain(lex).parse(_index["default"].ofString('10 -12 44'), 0);
    (0, _vitest.expect)(parsing.isEos()).toBe(false);
  });
});
//# sourceMappingURL=parser_chain_test.spec.js.map