"use strict";

var _vitest = require("vitest");
var _index = _interopRequireDefault(require("../../lib/stream/index"));
var _index2 = require("../../lib/parsec/index");
var _lib = require("../../lib");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function testParser(parser, string) {
  var stream = _index["default"].ofString(string);
  var parsing = parser.parse(stream);
  return parsing;
}
(0, _vitest.describe)('Flow Bundle Tests', function () {
  (0, _vitest.it)('subStream is ok on string stream', function () {
    var text = 'Hello World';
    var parser = _index2.F.subStream(6).then(_index2.C.string('World'));
    var response = parser.parse(_index["default"].ofString(text));
    (0, _vitest.expect)(response.isAccepted()).toBe(true);
    (0, _vitest.expect)(response.value.size()).toBe(7);
  });
  (0, _vitest.it)('subStream is ok on genlex stream', function () {
    var genlex = new _lib.GenLex();
    genlex.setSeparatorsParser(_index2.F.not(_index2.C.charIn('+-<>[],.')));
    genlex.keywords(['+', '-', '<', '>', '[', ']', ',', '.']);
    var grammar = _index2.F.subStream(4).drop().then(_index2.F.any().rep());
    var parser = genlex.use(grammar);
    var text = '++++ and then >>';
    var response = parser.parse(_index["default"].ofString(text));
    (0, _vitest.expect)(response.isAccepted()).toBe(true);
    (0, _vitest.expect)(response.value.size()).toBe(2);
  });
  (0, _vitest.it)('not parser should not eat offset', function () {
    var text = 'this is a line';
    var line = text + '\n';
    var eol = _index2.C["char"]('\n');
    var parser = _index2.F.not(eol).rep();
    var response = parser.parse(_index["default"].ofString(line));
    (0, _vitest.expect)(response.isAccepted()).toBe(true);
    (0, _vitest.expect)(response.offset).toBe(text.length);
    var withParser = _index2.F.not(eol).rep().then(eol);
    response = withParser.parse(_index["default"].ofString(line));
    (0, _vitest.expect)(response.isAccepted()).toBe(true);
    (0, _vitest.expect)(response.offset).toBe(line.length);
  });
  (0, _vitest.it)('expect flatten result to be ok', function () {
    var string = 'foobar';
    var parser = _index2.C["char"]('f').then(_index2.C["char"]('o')).then(_index2.C["char"]('o')).then(_index2.C.string('bar')).array();
    var parsing = testParser(parser, string);
    (0, _vitest.expect)(parsing.value).toEqual(['f', 'o', 'o', 'bar']);
  });
  (0, _vitest.it)('expect returns to be ok when empty', function () {
    var string = 'some';
    var parser = _index2.F.any().rep().then(_index2.F.eos()).returns([]);
    var parsing = testParser(parser, string);
    (0, _vitest.expect)(parsing.isAccepted()).toBe(true);
    (0, _vitest.expect)(parsing.value).toEqual([]);
  });
  (0, _vitest.it)('expect startWith to start', function () {
    var string = ' world';
    var object = 'hello';
    var parser = _index2.F.startWith(object).then(_index2.C.string(' world')).then(_index2.F.eos().drop());
    var parsing = testParser(parser, string);
    (0, _vitest.expect)(parsing.isAccepted()).toBe(true);
    (0, _vitest.expect)(parsing.value.join('')).toBe('hello world');
  });
  (0, _vitest.it)('test moveUntilFast string', function () {
    var line = _index["default"].ofString('soXYZso');
    var combinator = _index2.F.moveUntil('XYZ');
    var parser = combinator.parse(line);
    (0, _vitest.expect)(parser.value).toBe('so');
    (0, _vitest.expect)(parser.offset).toBe(2);
  });
  (0, _vitest.it)('test moveUntilFast string with continuation', function () {
    var document = 'start-detect-XYZ-continues';
    var line = _index["default"].ofString(document);
    var start = _index2.C.string('start-');
    var combinator = start.drop().then(_index2.F.moveUntil('XYZ')).then(_index2.C.string('XYZ-continues').drop()).single();
    var parser = combinator.parse(line);
    (0, _vitest.expect)(parser.value).toBe('detect-');
    (0, _vitest.expect)(parser.offset).toBe(document.length);
  });
  (0, _vitest.it)('test moveUntilFast array of string with continuation', function () {
    var document = 'start-detect-XYZ-continues';
    var line = _index["default"].ofString(document);
    var start = _index2.C.string('start-');
    var combinator = start.drop().then(_index2.F.moveUntil(['ABC', 'ZE', 'XYZ'])).then(_index2.C.string('XYZ-continues').drop()).single();
    var parser = combinator.parse(line);
    (0, _vitest.expect)(parser.value).toBe('detect-');
    (0, _vitest.expect)(parser.offset).toBe(document.length);
  });
  (0, _vitest.it)('test moveUntilFast string fails', function () {
    var document = 'start-detect-XYZ-continues';
    var line = _index["default"].ofString(document);
    var start = _index2.C.string('start-');
    var combinator = start.drop().then(_index2.F.moveUntil('EEE')).then(_index2.C.string('XYZ-continues').drop());
    var parsing = combinator.parse(line);
    (0, _vitest.expect)(parsing.isAccepted()).toBe(false);
  });
  (0, _vitest.it)('test moveUntilFast array of string fails', function () {
    var document = 'start-detect-XYZ-continues';
    var line = _index["default"].ofString(document);
    var start = _index2.C.string('start-');
    var combinator = start.drop().then(_index2.F.moveUntil(['ABC', 'ZE', 'EEE'])).then(_index2.C.string('XYZ-continues').drop());
    var parsing = combinator.parse(line);
    (0, _vitest.expect)(parsing.isAccepted()).toBe(false);
  });
  (0, _vitest.it)('test moveUntilFast fails if array stream', function () {
    var document = ['More', 'XYZ'];
    var line = _index["default"].ofArray(document);
    var combinator = _index2.F.moveUntil(['ABC', 'ZE', 'XYZ']);
    (0, _vitest.expect)(function () {
      return combinator.parse(line);
    }).toThrow('Input source must be a String');
  });
  (0, _vitest.it)('test moveUntilFastString fails if array stream', function () {
    var document = ['More', 'XYZ'];
    var line = _index["default"].ofArray(document);
    var combinator = _index2.F.moveUntil('XYZ');
    (0, _vitest.expect)(function () {
      return combinator.parse(line);
    }).toThrow('Input source must be a String');
  });
  (0, _vitest.it)('test moveUntil', function () {
    var line = _index["default"].ofString('I write until James appears');
    var combinator = _index2.F.moveUntil(_index2.C.string('James')).then(_index2.F.any().drop()).single();
    var value = combinator.parse(line).value;
    (0, _vitest.expect)(value).toBe('I write until ');
  });
  (0, _vitest.it)('test moveUntil Not found', function () {
    var line = _index["default"].ofString('I write until James appears');
    var combinator = _index2.F.moveUntil(_index2.C.string('Indiana')).then(_index2.C.string('I')).then(_index2.F.any().drop());
    var accepted = combinator.parse(line).isAccepted();
    (0, _vitest.expect)(accepted).toBe(false);
  });
  (0, _vitest.it)('test moveUntil found with failing parser', function () {
    var line = _index["default"].ofString('I write until James Bond appears');
    var combinator = _index2.F.moveUntil(_index2.C.string('James')).then(_index2.F.dropTo(_index2.F.eos()));
    var accepted = combinator.parse(line).isAccepted();
    (0, _vitest.expect)(accepted).toBe(false);
  });
  (0, _vitest.it)('test dropTo with string', function () {
    var line = _index["default"].ofString('I write until James Bond appears');
    var combinator = _index2.F.dropTo('James').then(_index2.C.string(' Bond appears')).then(_index2.F.eos());
    var accepted = combinator.parse(line).isAccepted();
    (0, _vitest.expect)(accepted).toBe(true);
  });
  (0, _vitest.it)('test dropTo with string fail', function () {
    var line = _index["default"].ofString('I write until James Bond appears');
    var combinator = _index2.F.dropTo('James').then(_index2.C.string(' Bond appears')).then(_index2.F.eos());
    var accepted = combinator.parse(line).isAccepted();
    (0, _vitest.expect)(accepted).toBe(true);
  });
  (0, _vitest.it)('test dropTo with parser', function () {
    var line = _index["default"].ofString('I write until James Bond appears');
    var combinator = _index2.F.dropTo(_index2.C.string('James')).then(_index2.C.string(' Bond appears')).then(_index2.F.eos());
    var accepted = combinator.parse(line).isAccepted();
    (0, _vitest.expect)(accepted).toBe(true);
  });
  (0, _vitest.it)('test moveUntil found with more parsers', function () {
    var line = _index["default"].ofString('I write until James Bond appears');
    var combinator = _index2.F.moveUntil(_index2.C.string('James')).then(_index2.F.dropTo('appears')).then(_index2.F.eos().drop()).single();
    var value = combinator.parse(line).value;
    (0, _vitest.expect)(value).toBe('I write until ');
  });
  (0, _vitest.it)('lazy with a class', function () {
    var SomeLazyParser = /*#__PURE__*/function () {
      function SomeLazyParser(_char) {
        _classCallCheck(this, SomeLazyParser);
        this["char"] = _char;
      }
      return _createClass(SomeLazyParser, [{
        key: "first",
        value: function first() {
          return _index2.C["char"](this["char"]).then(this.second().opt().map(function (opt) {
            return opt.orElse('');
          }));
        }
      }, {
        key: "second",
        value: function second() {
          return _index2.C["char"]('b').then(_index2.F.lazy(this.first, ['a'], this));
        }
      }]);
    }();
    var line = _index["default"].ofString('ababa');
    var combinator = new SomeLazyParser('a').first().then(_index2.F.eos().drop());
    var value = combinator.parse(line).value;
    (0, _vitest.expect)(value.join('')).toBe('ababa');
  });
});
//# sourceMappingURL=flow-bundle-test.spec.js.map