"use strict";

var _vitest = require("vitest");
var _index = _interopRequireDefault(require("../../lib/stream/index"));
var _index2 = require("../../lib/parsec/index");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
(0, _vitest.describe)('Parser Core Tests', function () {
  (0, _vitest.it)('expect val to be a nice shortcut', function () {
    var parser = _index2.C.string('xyz');
    var val = parser.val('xyz');
    (0, _vitest.expect)(val).toBe('xyz');
  });
  (0, _vitest.it)('expect (map) to be accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').map(function (a) {
      return a + 'b';
    }).parse(_index["default"].ofString('a'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (map) to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').map(function (a) {
      return a + 'b';
    }).parse(_index["default"].ofString('b'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (map) to be return ab', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').map(function (a) {
      return a + 'b';
    }).parse(_index["default"].ofString('a'), 0).value).toBe('ab');
  });
  (0, _vitest.it)('expect (map) to be return 5x8', function () {
    var st = _index["default"].ofString('5x8');
    var combinator = _index2.N.integer().then(_index2.C["char"]('x').drop()).then(_index2.N.integer()).array().map(function (values) {
      return values[0] * values[1];
    });
    (0, _vitest.expect)(combinator.parse(st).value).toBe(40);
  });
  (0, _vitest.it)('expect (flatMap) to be accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').flatMap(function () {
      return _index2.F.returns('b');
    }).parse(_index["default"].ofString('a'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (flatMap) to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').flatMap(function () {
      return _index2.F.returns('b');
    }).parse(_index["default"].ofString('b'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (flatMap) to be return ab', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').flatMap(function () {
      return _index2.C["char"]('b');
    }).parse(_index["default"].ofString('ab'), 0).value).toBe('b');
  });
  (0, _vitest.it)('expect (filter) to be accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').filter(function (a) {
      return a === 'a';
    }).parse(_index["default"].ofString('a'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (filter) to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').filter(function (a) {
      return a === 'b';
    }).parse(_index["default"].ofString('a'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (match) to be accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').match('a').parse(_index["default"].ofString('a'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (match) to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').match('b').parse(_index["default"].ofString('a'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (then) to be accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').then(_index2.C["char"]('b')).parse(_index["default"].ofString('ab'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (then) left to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').then(_index2.C["char"]('b')).parse(_index["default"].ofString('cb'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (then) right to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').then(_index2.C["char"]('b')).parse(_index["default"].ofString('ac'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (then) to return [a,b]', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').then(_index2.C["char"]('b')).array().parse(_index["default"].ofString('ab'), 0).value).toEqual(['a', 'b']);
  });
  (0, _vitest.it)('expect (then) to return [a,b,d]', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').then(_index2.C["char"]('b').then(_index2.C["char"]('c').drop()).then(_index2.C["char"]('d'))).array().parse(_index["default"].ofString('abcd'), 0).value).toEqual(['a', 'b', 'd']);
  });
  (0, _vitest.it)('expect (then) to be empty with two drops', function () {
    var parser = _index2.C["char"]('a').drop().then(_index2.C["char"]('b').drop());
    var value = parser.parse(_index["default"].ofString('ab')).value.value;
    (0, _vitest.expect)(Array.isArray(value)).toBe(true);
    (0, _vitest.expect)(value.length).toBe(0);
  });
  (0, _vitest.it)('expect (then) to be associative', function () {
    var first = _index2.C["char"]('a').then(_index2.C["char"]('b')).then(_index2.C["char"]('c').drop()).then(_index2.C["char"]('d')).array();
    var second = _index2.C["char"]('a').then(_index2.C["char"]('b')).then(_index2.C["char"]('c').drop().then(_index2.C["char"]('d'))).array();
    (0, _vitest.expect)(first.parse(_index["default"].ofString('abcd')).value).toEqual(second.parse(_index["default"].ofString('abcd')).value);
  });
  (0, _vitest.it)('expect (then) to be replaced by concat', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').concat(_index2.C["char"]('b')).then(_index2.C["char"]('c').drop()).concat(_index2.C["char"]('d')).array().parse(_index["default"].ofString('abcd'), 0).value).toEqual(['a', 'b', 'd']);
  });
  (0, _vitest.it)('expect (thenLeft) to be accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').thenLeft(_index2.C["char"]('b')).parse(_index["default"].ofString('ab'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (thenLeft) to return a', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').thenLeft(_index2.C["char"]('b')).single().parse(_index["default"].ofString('ab'), 0).value).toBe('a');
  });
  (0, _vitest.it)('expect (thenLeft) to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').thenLeft(_index2.C["char"]('b')).parse(_index["default"].ofString('b'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (thenRight) to be accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').thenRight(_index2.C["char"]('b')).parse(_index["default"].ofString('ab'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (thenRight) to return a', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').thenRight(_index2.C["char"]('b')).single().parse(_index["default"].ofString('ab'), 0).value).toBe('b');
  });
  (0, _vitest.it)('expect (thenRight) to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').thenRight(_index2.C["char"]('b')).parse(_index["default"].ofString('b'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (drop/then) to be accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').drop().then(_index2.C["char"]('b')).parse(_index["default"].ofString('ab'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (drop/then) to be return b', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').drop().then(_index2.C["char"]('b')).single().parse(_index["default"].ofString('ab'), 0).value).toBe('b');
  });
  (0, _vitest.it)('expect (then/drop) to be accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').then(_index2.C["char"]('b').drop()).parse(_index["default"].ofString('ab'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (drop/then) to be return a', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').then(_index2.C["char"]('b').drop()).single().parse(_index["default"].ofString('ab'), 0).value).toBe('a');
  });
  (0, _vitest.it)('expect (eos) to be accepted at the end', function () {
    var parser = _index2.C.string('abc').eos();
    var response = parser.parse(_index["default"].ofString('abc'));
    (0, _vitest.expect)(response.isAccepted()).toBe(true);
    (0, _vitest.expect)(response.isEos()).toBe(true);
    (0, _vitest.expect)(response.value).toBe('abc');
  });
  (0, _vitest.it)('expect (eos) to be rejected without eating char', function () {
    var parser = _index2.C["char"]('a').eos();
    var response = parser.parse(_index["default"].ofString('ab'));
    (0, _vitest.expect)(response.isAccepted()).toBe(false);
    (0, _vitest.expect)(response.isEos()).toBe(false);
    (0, _vitest.expect)(response.offset).toBe(1);
    (0, _vitest.expect)(response.value).toBeUndefined();
  });
  (0, _vitest.it)('expect rejected (eos) to be rejected keeping previous offset', function () {
    var parser = _index2.C["char"]('a').then(_index2.C["char"]('a')).eos();
    var response = parser.parse(_index["default"].ofString('ab is ending at 1'));
    (0, _vitest.expect)(response.isAccepted()).toBe(false);
    (0, _vitest.expect)(response.isEos()).toBe(false);
    (0, _vitest.expect)(response.offset).toBe(1);
    (0, _vitest.expect)(response.value).toBeUndefined();
  });
  (0, _vitest.it)('expect (thenEos) to be accepted at the end', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').thenEos().parse(_index["default"].ofString('a')).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (thenEos) to be rejected if not the end', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').thenEos().parse(_index["default"].ofString('abc')).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (returns) to be accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').returns('b').parse(_index["default"].ofString('ab'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (returns) to return b', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').returns('b').parse(_index["default"].ofString('ab'), 0).value).toBe('b');
  });
  (0, _vitest.it)('expect (returns) not to eat char', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').returns('X').then(_index2.C["char"]('b')).array().parse(_index["default"].ofString('ab'), 0).value).toEqual(['X', 'b']);
  });
  (0, _vitest.it)('expect (returns) to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').returns('b').parse(_index["default"].ofString('b'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (or) to be accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').or(_index2.C["char"]('b')).parse(_index["default"].ofString('a'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (or) to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').or(_index2.C["char"]('b')).parse(_index["default"].ofString('c'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (or) LL(1) to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').then(_index2.C["char"]('b')).or(_index2.C["char"]('a')).parse(_index["default"].ofString('ac'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (or) to return a', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').or(_index2.C["char"]('b')).parse(_index["default"].ofString('a'), 0).value).toBe('a');
  });
  (0, _vitest.it)('expect (or) to return b', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').or(_index2.C["char"]('b')).parse(_index["default"].ofString('b'), 0).value).toBe('b');
  });
  (0, _vitest.it)('expect (then.or) left to be rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').then(_index2.C["char"]('b').or(_index2.C["char"]('c'))).parse(_index["default"].ofString('ad'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (then.or) left to be consumed', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').then(_index2.C["char"]('b').or(_index2.C["char"]('c'))).parse(_index["default"].ofString('ad'), 0).consumed).toBe(true);
  });
  (0, _vitest.it)('expect (opt) some to accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').opt().parse(_index["default"].ofString('a'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (opt) some to return some a', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').opt().parse(_index["default"].ofString('a'), 0).value.get()).toBe('a');
  });
  (0, _vitest.it)('expect (opt) none to accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').opt().parse(_index["default"].ofString('b'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (opt) none to return none', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').opt().parse(_index["default"].ofString('b'), 0).value.isPresent()).toBe(false);
  });
  (0, _vitest.it)('expect (rep) to accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').rep().parse(_index["default"].ofString('a'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (rep) to rejected', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').rep().parse(_index["default"].ofString('b'), 0).isAccepted()).toBe(false);
  });
  (0, _vitest.it)('expect (rep) mutiple to accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').rep().parse(_index["default"].ofString('aaaabbb'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (rep) mutiple to return [a,a,a,a]', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').rep().parse(_index["default"].ofString('aaaabbb'), 0).value.array()).toEqual(['a', 'a', 'a', 'a']);
  });
  (0, _vitest.it)('expect (optrep) to accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').optrep().parse(_index["default"].ofString('a'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (optrep) none to accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').optrep().parse(_index["default"].ofString('b'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (optrep) mutiple to accepted', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').optrep().parse(_index["default"].ofString('aaaabbb'), 0).isAccepted()).toBe(true);
  });
  (0, _vitest.it)('expect (optrep) mutiple to return some [a,a,a,a]', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').optrep().parse(_index["default"].ofString('aaaabbb'), 0).value.array()).toEqual(['a', 'a', 'a', 'a']);
  });
  (0, _vitest.it)('expect (optrep) to return none', function () {
    (0, _vitest.expect)(_index2.C["char"]('a').optrep().parse(_index["default"].ofString('bbb'), 0).value.array()).toEqual([]);
  });
  (0, _vitest.it)('expect (optrep) to return [b,b,b]', function () {
    (0, _vitest.expect)(_index2.C.notChar('a').optrep().parse(_index["default"].ofString('bbba'), 0).value.array()).toEqual(['b', 'b', 'b']);
  });
  (0, _vitest.it)('expect two (optrep) to be merged as [b,b,b,a]', function () {
    (0, _vitest.expect)(_index2.C["char"]('b').optrep().then(_index2.C["char"]('a').optrep()).array().parse(_index["default"].ofString('bbba'), 0).value).toEqual(['b', 'b', 'b', 'a']);
  });
  (0, _vitest.it)('expect two (optrep) to return [[b,b,b],[a]] using array()', function () {
    (0, _vitest.expect)(_index2.C["char"]('b').optrep().array().then(_index2.C["char"]('a').optrep().array()).array().parse(_index["default"].ofString('bbba'), 0).value).toEqual([['b', 'b', 'b'], ['a']]);
  });
  (0, _vitest.it)('expect debug() to make side effect', function () {
    var original = console.log;
    var sideEffect = false;
    console.log = function () {
      sideEffect = true;
    };
    _index2.C["char"]('a').debug('found').optrep().parse(_index["default"].ofString('aaa'), 0);
    console.log = original;
    (0, _vitest.expect)(sideEffect).toBe(true);
  });
  (0, _vitest.it)('expect debug(param, false) to make side effect', function () {
    var original = console.log;
    var sideEffect = false;
    console.log = function () {
      sideEffect = true;
    };
    _index2.C["char"]('a').debug('found', false).optrep().parse(_index["default"].ofString('aaa'), 0);
    console.log = original;
    (0, _vitest.expect)(sideEffect).toBe(true);
  });
  (0, _vitest.it)('expect (debug) to not make side effect', function () {
    var original = console.log;
    var sideEffect = false;
    console.log = function () {
      sideEffect = true;
    };
    _index2.C["char"]('a').debug('found').optrep().parse(_index["default"].ofString('xxxx'), 0);
    console.log = original;
    (0, _vitest.expect)(sideEffect).toBe(false);
  });
});
//# sourceMappingURL=parser_core_test.spec.js.map