"use strict";

var _vitest = require("vitest");
var _index = _interopRequireDefault(require("../../lib/stream/index"));
var _charsBundle = _interopRequireDefault(require("../../lib/parsec/chars-bundle"));
var _numbersBundle = _interopRequireDefault(require("../../lib/parsec/numbers-bundle"));
var _stream = _interopRequireDefault(require("../../lib/stream"));
var _unit = _interopRequireDefault(require("../../lib/data/unit"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function spaces() {
  return _charsBundle["default"].charIn(' \r\n\f\t').optrep().map(function () {
    return _unit["default"];
  });
}
(0, _vitest.describe)('Parser Stream Tests', function () {
  (0, _vitest.it)('endOfStream for empty stream', function () {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number());
    (0, _vitest.expect)(_index["default"].ofParser(p, _index["default"].ofString('')).endOfStream(0)).toBe(true);
  });
  (0, _vitest.it)('endOfStream for non empty stream', function () {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number());
    (0, _vitest.expect)(_index["default"].ofParser(p, _index["default"].ofString('1')).endOfStream(1)).toBe(true);
  });
  (0, _vitest.it)('no endOfStream for non empty stream', function () {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number());
    (0, _vitest.expect)(_index["default"].ofParser(p, _index["default"].ofString('1')).endOfStream(0)).toBe(false);
  });
  (0, _vitest.it)('get from stream', function () {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number());
    (0, _vitest.expect)(_index["default"].ofParser(p, _index["default"].ofString('1')).get(0).isSuccess()).toBe(true);
  });
  (0, _vitest.it)('do not get from empty stream', function () {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number());
    (0, _vitest.expect)(_index["default"].ofParser(p, _index["default"].ofString('1')).get(1).isSuccess()).toBe(false);
  });
  (0, _vitest.it)('get from stream number 123', function () {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number()).single();
    (0, _vitest.expect)(_index["default"].ofParser(p, _index["default"].ofString('123')).get(0).success()).toBe(123);
  });
  (0, _vitest.it)('Offset are found in series of numbers', function () {
    var p = _numbersBundle["default"].number().then(_charsBundle["default"]["char"](' ').optrep().drop()).single();
    var parserStream = _index["default"].ofParser(p, _index["default"].ofString('123   14137'));
    // index: ^0    ^6

    var first = parserStream.get(0).success(); //>> 123
    (0, _vitest.expect)(first).toBe(123);
    var second = parserStream.get(1).success(); //>> 114
    (0, _vitest.expect)(second).toBe(14137);
    (0, _vitest.expect)(parserStream.offsets).toEqual([0, 6, 11]);
  });
  (0, _vitest.it)('failing series of numbers', function () {
    var p = _numbersBundle["default"].number().then(_charsBundle["default"]["char"](' ').optrep().drop()).single();
    var parserStream = _index["default"].ofParser(p, _index["default"].ofString('123   a'));
    //                                                index: ^0    ^6

    var first = parserStream.get(0).success(); //>> 123
    (0, _vitest.expect)(first).toBe(123);
    (0, _vitest.expect)(parserStream.offsets).toEqual([0, 6]);
    var second = parserStream.get(1); // try 'a'
    (0, _vitest.expect)(second.isFailure()).toBe(true);
    (0, _vitest.expect)(parserStream.offsets).toEqual([0, 6]);
  });
  (0, _vitest.it)('having correct location when success', function () {
    var p = _numbersBundle["default"].number().then(_charsBundle["default"]["char"](' ').optrep().drop()).single();
    var parserStream = _index["default"].ofParser(p, _index["default"].ofString('123   14137'));
    //                                                index: ^0    ^6

    var first = parserStream.get(0).success(); //>> 123
    (0, _vitest.expect)(first).toBe(123);
    (0, _vitest.expect)(parserStream.location(0)).toBe(0);
    var second = parserStream.get(1).success(); //>> 114
    (0, _vitest.expect)(second).toBe(14137);
    (0, _vitest.expect)(parserStream.location(1)).toBe(6);
  });
  (0, _vitest.it)('searching illegal location will fail', function () {
    var p = _numbersBundle["default"].number().then(_charsBundle["default"]["char"](' ').optrep().drop()).single();
    var parserStream = _index["default"].ofParser(p, _index["default"].ofString('123   14137'));
    //                                                index: ^0    ^6

    var first = parserStream.get(0).success(); //>> 123
    (0, _vitest.expect)(first).toBe(123);
    (0, _vitest.expect)(function () {
      return parserStream.location(4);
    }).toThrow();
  });
  (0, _vitest.it)('having correct location when fail', function () {
    var p = _numbersBundle["default"].number();
    var parserStream = _index["default"].ofParser(p, _index["default"].ofString('1234   14137'));
    //                                                index: ^0  ^4

    var first = parserStream.get(0).success(); //>> 123
    (0, _vitest.expect)(first).toBe(1234);
    (0, _vitest.expect)(parserStream.location(0)).toBe(0);
    parserStream.get(1); //>> fail
    (0, _vitest.expect)(parserStream.location(1)).toBe(4);
  });
  (0, _vitest.it)('unsafe_get can see next element', function () {
    var lower = _numbersBundle["default"].number().then(spaces().opt().drop()).single();
    var lowerStream = _stream["default"].ofString('10 12 44');
    var parserStream = _stream["default"].ofParser(lower, lowerStream);
    parserStream.unsafeGet(0);
    var value = parserStream.unsafeGet(1);
    (0, _vitest.expect)(value).toBe(12);
  });
  (0, _vitest.it)('unsafe_get cannot see beyond next element', function () {
    var lower = _numbersBundle["default"].number().then(spaces().opt().drop());
    var lowerStream = _stream["default"].ofString('10 12 44');
    var parserStream = _stream["default"].ofParser(lower, lowerStream);
    (0, _vitest.expect)(function () {
      return parserStream.unsafeGet(1);
    }).toThrow();
  });
});
//# sourceMappingURL=parser_stream_test.spec.js.map