"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
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 }; }
var _default = exports["default"] = {
  setUp: function setUp(done) {
    done();
  },
  'endOfStream for empty stream': function endOfStream_for_empty_stream(test) {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number());
    test.ok(_index["default"].ofParser(p, _index["default"].ofString('')).endOfStream(0), 'should be endOfStream.');
    test.done();
  },
  'endOfStream for non empty stream': function endOfStream_for_non_empty_stream(test) {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number());
    test.ok(_index["default"].ofParser(p, _index["default"].ofString('1')).endOfStream(1), 'should be endOfStream.');
    test.done();
  },
  'no endOfStream for non empty stream': function no_endOfStream_for_non_empty_stream(test) {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number());
    test.equal(_index["default"].ofParser(p, _index["default"].ofString('1')).endOfStream(0), false, 'should be endOfStream.');
    test.done();
  },
  'get from stream': function (test) {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number());
    test.equal(_index["default"].ofParser(p, _index["default"].ofString('1')).get(0).isSuccess(), true, 'should be a success.');
    test.done();
  },
  'do not get from empty stream': function do_not_get_from_empty_stream(test) {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number());
    test.equal(_index["default"].ofParser(p, _index["default"].ofString('1')).get(1).isSuccess(), false, 'should be a failure.');
    test.done();
  },
  'get from stream number 123': function (test) {
    var p = _charsBundle["default"]["char"](' ').optrep().thenRight(_numbersBundle["default"].number()).single();
    test.equal(_index["default"].ofParser(p, _index["default"].ofString('123')).get(0).success(), 123, 'should be a 123.');
    test.done();
  },
  'Offset are found in series of numbers': function Offset_are_found_in_series_of_numbers(test) {
    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
    test.equal(first, 123);
    var second = parserStream.get(1).success(); //>> 114
    test.equal(second, 14137);
    test.deepEqual(parserStream.offsets, [0, 6, 11]);

    // Offset after reading start (1) is related to index after 123 (3)
    // Offset after reading at 6 (7) is related to index after '123   14137'(11)

    test.done();
  },
  'failing series of numbers': function failing_series_of_numbers(test) {
    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
    test.equal(first, 123);
    test.deepEqual(parserStream.offsets, [0, 6]);
    var second = parserStream.get(1); // try 'a'
    test.ok(second.isFailure());
    test.deepEqual(parserStream.offsets, [0, 6]);
    test.done();
  },
  'having correct location when success': function having_correct_location_when_success(test) {
    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
    test.equal(first, 123);
    test.equal(0, parserStream.location(0));
    var second = parserStream.get(1).success(); //>> 114
    test.equal(second, 14137);
    test.equal(6, parserStream.location(1));
    test.done();
  },
  'searching illegal location will fail': function searching_illegal_location_will_fail(test) {
    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
    test.equal(first, 123);
    var foundError = false;
    try {
      test.equal(0, parserStream.location(4));
    } catch (_unused) {
      foundError = true;
    }
    test.ok(foundError);
    test.done();
  },
  'having correct location when fail': function having_correct_location_when_fail(test) {
    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
    test.equal(first, 1234);
    test.equal(0, parserStream.location(0));
    parserStream.get(1); //>> fail
    test.equal(4, parserStream.location(1));
    test.done();
  },
  'unsafe_get can see next element': function unsafe_get_can_see_next_element(test) {
    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);
    test.equal(12, value);
    test.done();
  },
  'unsafe_get cannot see beyond next element': function unsafe_get_cannot_see_beyond_next_element(test) {
    var lower = _numbersBundle["default"].number().then(spaces().opt().drop());
    var lowerStream = _stream["default"].ofString('10 12 44');
    var parserStream = _stream["default"].ofParser(lower, lowerStream);
    var found = false;
    try {
      parserStream.unsafeGet(1);
    } catch (_unused2) {
      found = true;
    }
    test.ok(found);
    test.done();
  }
};
function spaces() {
  return _charsBundle["default"].charIn(' \r\n\f\t').optrep().map(function () {
    return _unit["default"];
  });
}
//# sourceMappingURL=parser_stream_test.js.map