@masala/parser
Version:
141 lines (104 loc) • 4.94 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _index = require('../../lib/stream/index');
var _index2 = _interopRequireDefault(_index);
var _index3 = require('../../lib/parsec/index');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.default = {
setUp: function setUp(done) {
done();
},
'expect F.layer(parser) to work as parser with backtrak on success': function expectFLayerParserToWorkAsParserWithBacktrakOnSuccess(test) {
var parser = _index3.C.char('a').thenEos();
var successInput = 'a';
var failInput = 'b';
var layer = _index3.F.layer(parser);
var response = layer.parse(_index2.default.ofString(successInput));
test.ok(response.isAccepted());
test.equal(response.offset, 0);
// Fail test
response = layer.parse(_index2.default.ofString(failInput));
test.ok(!response.isAccepted());
test.equal(response.offset, 0);
test.done();
},
'expect F.layer(parser).and(other) to succeed': function expectFLayerParserAndOtherToSucceed(test) {
try {
var first = _index3.C.char('a').then(_index3.C.char('a')).thenEos().array().map(function (r) {
return r.length;
});
var second = _index3.C.string('aa').thenEos();
var successInput = 'aa';
var layer = _index3.F.layer(first).and(second).and(second).array();
var response = layer.parse(_index2.default.ofString(successInput));
test.ok(response.isAccepted());
test.deepEqual(response.value, [2, 'aa', 'aa']);
test.equal(response.offset, 2);
} catch (e) {
console.log(e);
}
test.done();
},
'expect F.layer(first).and(second).and(third) to be associative': function expectFLayerFirstAndSecondAndThirdToBeAssociative(test) {
var first = _index3.C.char('a').then(_index3.C.char('a')).thenEos().array().map(function (r) {
return r.length;
});
var second = _index3.C.char('a').then(_index3.C.char('a')).thenEos().array().map(function (r) {
return r.join('-');
});
var third = _index3.C.string('aa').thenEos();
var input = 'aa';
var layer = _index3.F.layer(first).and(second).and(third).array();
var response = layer.parse(_index2.default.ofString(input));
test.ok(response.isAccepted());
test.deepEqual(response.value, [2, 'a-a', 'aa']);
test.equal(response.offset, 2);
test.done();
},
'expect F.layer(parser).and(other) to fail with second': function expectFLayerParserAndOtherToFailWithSecond(test) {
var first = _index3.C.char('a').then(_index3.C.char('a')).array().thenEos().map(function (r) {
return r.length;
});
var second = _index3.C.string('aaFAIL').thenEos();
var successInput = 'aa';
var layer = _index3.F.layer(first).and(second).array();
var response = layer.parse(_index2.default.ofString(successInput));
test.ok(!response.isAccepted());
test.equal(response.offset, 0);
test.equal(response.value, undefined);
test.done();
},
'expect F.layer(parser).and(other) to fail with first': function expectFLayerParserAndOtherToFailWithFirst(test) {
var first = _index3.C.char('a').then(_index3.C.char('a')).thenEos().map(function (r) {
return r.length;
});
var second = _index3.C.string('aaSUCCESS').thenEos();
var successInput = 'aaSUCCESS'; // first fail by not meeting Eos
var layer = _index3.F.layer(first).and(second);
var response = layer.parse(_index2.default.ofString(successInput));
test.ok(!response.isAccepted());
test.equal(response.offset, 2); // stop at first because could not find the Eos
test.equal(response.value, undefined);
test.done();
},
'expect F.layer(parser).and(other) to not move on the second after first fails': function expectFLayerParserAndOtherToNotMoveOnTheSecondAfterFirstFails(test) {
var first = _index3.C.char('a').then(_index3.C.char('a')).thenEos().map(function (r) {
return r.length;
});
var found = false;
var second = _index3.C.string('aaSUCCESS').thenEos().map(function (x) {
found = true;
return x;
});
var successInput = 'aaSUCCESS'; // first fail by not meeting Eos
var layer = _index3.F.layer(first).and(second);
var response = layer.parse(_index2.default.ofString(successInput));
test.ok(!response.isAccepted());
test.equal(response.offset, 2);
test.equal(false, found); // second was not even tried
test.done();
}
};
//# sourceMappingURL=f-layer-test.js.map