"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 _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();
  },
  'expect (chain) to be accepted': function expect_Chain_to_be_accepted(test) {
    var lower = _index2.C["char"]('x');
    var upper = _index2.F.satisfy(function (val) {
      return val === 'x';
    });
    var parser = lower.chain(upper);
    var response = parser.parse(_index["default"].ofString('x'));
    test.ok(response.isAccepted(), 'Should be accepted');
    test.done();
  },
  'expect (chain) to be rejected': function expect_Chain_to_be_rejected(test) {
    var lower = _index2.C["char"]('x');
    var upper = _index2.F.satisfy(function (val) {
      return val === 'y';
    });
    var parser = lower.chain(upper);
    var response = parser.parse(_index["default"].ofString('x'));
    test.ok(!response.isAccepted(), 'Should be rejected');
    test.done();
  },
  'expect (chain) to be accepted and offset to have move': function expect_Chain_to_be_accepted_and_offset_to_have_move(test) {
    var lower = _index2.C["char"]('x');

    // satisfy makes the stuff move only if accepted
    var upper = _index2.F.satisfy(function (val) {
      return val === 'x';
    });
    var parser = lower.chain(upper);
    var response = parser.parse(_index["default"].ofString('x'));
    test.ok(response.offset === 1, 'Should have moved');
    test.done();
  },
  'expect (chain) to be accepted and offset to have move more': function expect_Chain_to_be_accepted_and_offset_to_have_move_more(test) {
    var lower = _index2.C.string('xyz');

    // satisfy makes the stuff move only if accepted
    var upper = _index2.F.satisfy(function (val) {
      return val === 'xyz';
    });
    var parser = lower.chain(upper.then(upper));
    var response = parser.parse(_index["default"].ofString('xyzxyz'));
    test.equal(response.offset, 2, 'Should have moved more');
    test.done();
  },
  'expect (chain) to be find back the source offset': function expect_Chain_to_be_find_back_the_source_offset(test) {
    var lower = _index2.C.string('xyz');

    // satisfy makes the stuff move only if accepted
    var upper = _index2.F.satisfy(function (val) {
      return val === 'xyz';
    });
    var parser = lower.chain(upper.then(upper));
    var response = parser.parse(_index["default"].ofString('xyzxyz'));
    test.equal(response.input.source.offsets[response.offset], 6, 'Should have find stringStream offset');
    test.done();
  },
  'expect (chain) to be accepted again': function expect_Chain_to_be_accepted_again(test) {
    test.expect(1);
    var p1 = _index2.N.number().thenLeft(_index2.C["char"](' ').opt()),
      p2 = _index2.F.any().then(_index2.F.any()).thenLeft(_index2.F.eos()).map(function (r) {
        return r[0] + r[1];
      });
    test.equal(p1.chain(p2).parse(_index["default"].ofString('12 34'), 0).isAccepted(), true, 'should be accepted.');
    test.done();
  },
  'expect (chain) to return 46': function expect_Chain_to_return_46(test) {
    test.expect(1);
    // tests here
    var p1 = _index2.N.number().thenLeft(_index2.C["char"](' ').opt()),
      p2 = _index2.F.any().then(_index2.F.any()).thenLeft(_index2.F.eos()).array().map(function (r) {
        return r[0] + r[1];
      });
    test.equal(p1.chain(p2).parse(_index["default"].ofString('12 34'), 0).value, 46);
    test.done();
  },
  'expect (chain) to add multiple numbers ': function expect_Chain_to_add_multiple_numbers_(test) {
    var token = _index2.N.number().then(spaces().opt().drop()).single();
    var lex = _index2.F.satisfy(function (number) {
      return number > 0;
    }).rep().map(function (values) {
      return values.array().reduce(function (acc, n) {
        return acc + n;
      }, 0);
    });
    var parsing = token.chain(lex).parse(_index["default"].ofString('10 12 44'), 0);
    test.ok(parsing.isEos(), 'should have been consumed');
    test.equal(parsing.value, 66);
    test.done();
  },
  'expect (chain) to be not satisfied by upper level ': function expect_Chain_to_be_not_satisfied_by_upper_level_(test) {
    var token = _index2.N.number().then(spaces().opt().drop());
    var lex = _index2.F.satisfy(function (number) {
      return number > 0;
    }).rep().map(function (values) {
      return values.array().reduce(function (acc, n) {
        return acc + n;
      }, 0);
    });
    var parsing = token.chain(lex).parse(_index["default"].ofString('10 -12 44'), 0);
    test.ok(!parsing.isEos(), 'should have been consumed');
    test.done();
  }
};
function spaces() {
  return _index2.C.charIn(' \r\n\f\t').optrep().map(function () {
    return _unit["default"];
  });
}
//# sourceMappingURL=parser_chain_test.js.map