"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
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); }
var value = undefined;
var accepted = undefined;
function testParser(parser, string) {
  var stream = _index["default"].ofString(string);
  var parsing = parser.parse(stream);
  value = parsing.value;
  accepted = parsing.isAccepted();
}
var _default = exports["default"] = {
  setUp: function setUp(done) {
    done();
  },
  'subStream is ok on string stream': function subStream_is_ok_on_string_stream(test) {
    var text = 'Hello World';
    var parser = _index2.F.subStream(6).then(_index2.C.string('World'));
    var response = parser.parse(_index["default"].ofString(text));
    test.ok(response.isAccepted());
    test.equal(7, response.value.size()); // 6 for the stream, one for World

    test.done();
  },
  'subStream is ok on genlex stream': function subStream_is_ok_on_genlex_stream(test) {
    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));
    test.ok(response.isAccepted());
    test.equal(2, response.value.size());
    test.done();
  },
  'not parser should not eat offset': function not_parser_should_not_eat_offset(test) {
    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));
    test.ok(response.isAccepted());
    test.equal(text.length, response.offset);
    var withParser = _index2.F.not(eol).rep().then(eol);
    response = withParser.parse(_index["default"].ofString(line));
    test.ok(response.isAccepted());
    test.equal(line.length, response.offset);
    test.done();
  },
  'expect flatten result to be ok': function expect_flatten_result_to_be_ok(test) {
    var string = 'foobar';
    // tests here
    var parser = _index2.C["char"]('f').then(_index2.C["char"]('o')).then(_index2.C["char"]('o')).then(_index2.C.string('bar')).array();
    testParser(parser, string);
    test.deepEqual(value, ['f', 'o', 'o', 'bar'], 'flatten result not ok');
    test.done();
  },
  'expect returns to be ok when empty': function expect_returns_to_be_ok_when_empty(test) {
    var string = 'some';
    // tests here
    var parser = _index2.F.any().rep().then(_index2.F.eos()).returns([]);
    testParser(parser, string);
    test.ok(accepted);
    test.deepEqual(value, [], 'flatten result not ok');
    test.done();
  },
  'expect startWith to start': function expect_startWith_to_start(test) {
    var string = ' world';
    var object = 'hello';
    // tests here
    var parser = _index2.F.startWith(object).then(_index2.C.string(' world')).then(_index2.F.eos().drop());
    testParser(parser, string);
    test.ok(accepted);
    test.equals(value.join(''), 'hello world');
    test.done();
  },
  'test moveUntilFast string': function test_moveUntilFast_string(test) {
    var line = _index["default"].ofString('soXYZso');
    var combinator = _index2.F.moveUntil('XYZ');
    var parser = combinator.parse(line);
    var value = parser.value;
    var offset = parser.offset;
    test.equals(value, 'so');
    test.equals(offset, 2);
    test.done();
  },
  'test moveUntilFast string with continuation': function test_moveUntilFast_string_with_continuation(test) {
    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);
    var value = parser.value;
    var offset = parser.offset;
    test.equals(value, 'detect-');
    test.equals(offset, document.length);
    test.done();
  },
  'test moveUntilFast array of string with continuation': function test_moveUntilFast_array_of_string_with_continuation(test) {
    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);
    var value = parser.value;
    var offset = parser.offset;
    test.equals(value, 'detect-');
    test.equals(offset, document.length);
    test.done();
  },
  'test moveUntilFast string fails': function test_moveUntilFast_string_fails(test) {
    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);
    test.ok(!parsing.isAccepted());
    test.done();
  },
  'test moveUntilFast array of string fails': function test_moveUntilFast_array_of_string_fails(test) {
    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);
    test.ok(!parsing.isAccepted());
    test.done();
  },
  'test moveUntilFast fails if array stream': function test_moveUntilFast_fails_if_array_stream(test) {
    var document = ['More', 'XYZ'];
    var line = _index["default"].ofArray(document);
    var combinator = _index2.F.moveUntil(['ABC', 'ZE', 'XYZ']);
    var found = false;
    try {
      combinator.parse(line);
    } catch (e) {
      if (e === 'Input source must be a String') {
        found = true;
      }
    }
    test.ok(found);
    test.done();
  },
  'test moveUntilFastString fails if array stream': function test_moveUntilFastString_fails_if_array_stream(test) {
    var document = ['More', 'XYZ'];
    var line = _index["default"].ofArray(document);
    var combinator = _index2.F.moveUntil('XYZ');
    var found = false;
    try {
      combinator.parse(line);
    } catch (e) {
      if (e === 'Input source must be a String') {
        found = true;
      }
    }
    test.ok(found);
    test.done();
  },
  'test moveUntil': function test_moveUntil(test) {
    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;
    test.equals(value, 'I write until ');
    test.done();
  },
  'test moveUntil Not found': function test_moveUntil_Not_found(test) {
    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();
    test.ok(!accepted);
    test.done();
  },
  'test moveUntil  found with failing parser': function test_moveUntil__found_with_failing_parser(test) {
    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();
    test.ok(!accepted);
    test.done();
  },
  'test dropTo with string': function test_dropTo_with_string(test) {
    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();
    test.ok(accepted);
    test.done();
  },
  'test dropTo with string fail': function test_dropTo_with_string_fail(test) {
    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();
    test.ok(accepted);
    test.done();
  },
  'test dropTo with parser': function test_dropTo_with_parser(test) {
    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();
    test.ok(accepted);
    test.done();
  },
  'test moveUntil found with more parsers': function test_moveUntil_found_with_more_parsers(test) {
    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;
    test.equals(value, 'I write until ');
    test.done();
  },
  'lazy with a class': function lazy_with_a_class(test) {
    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;
    test.equals(value.join(''), 'ababa');
    test.done();
  }
};
//# sourceMappingURL=flow-bundle-test.js.map