UNPKG

@masala/parser

Version:
443 lines (402 loc) 15.9 kB
'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 }; } /* ======== A Handy Little Nodeunit Reference ======== https://github.com/caolan/nodeunit Test methods: test.expect(numAssertions) test.done() Test assertions: test.ok(value, [message]) test.equal(actual, expected, [message]) test.notEqual(actual, expected, [message]) test.deepEqual(actual, expected, [message]) test.notDeepEqual(actual, expected, [message]) test.strictEqual(actual, expected, [message]) test.notStrictEqual(actual, expected, [message]) test.throws(block, [error], [message]) test.doesNotThrow(block, [error], [message]) test.ifError(value) */ exports.default = { setUp: function setUp(done) { done(); }, 'expect (returns) to be accepted': function expectReturnsToBeAccepted(test) { test.expect(1); // tests here test.ok(_index3.F.returns().parse(_index2.default.ofString(''), 0).isAccepted(), 'should be accepted.'); test.done(); }, 'expect (returns) to return a given value': function expectReturnsToReturnAGivenValue(test) { test.expect(1); // tests here test.equal(_index3.F.returns(123).parse(_index2.default.ofString(''), 0).value, 123, 'should be accepted.'); test.done(); }, 'expect (returns) to be rejected': function expectReturnsToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.F.error().parse(_index2.default.ofString(''), 0).isAccepted(), false, 'should be accepted.'); test.done(); }, 'expect (lazy) to be accepted': function expectLazyToBeAccepted(test) { test.expect(1); // tests here test.ok(_index3.F.lazy(function () { return _index3.F.returns(); }).parse(_index2.default.ofString(''), 0).isAccepted(), 'should be accepted.'); test.done(); }, 'expect (lazy) to return a given value': function expectLazyToReturnAGivenValue(test) { test.expect(1); // tests here test.equal(_index3.F.lazy(function () { return _index3.F.returns(123); }).parse(_index2.default.ofString(''), 0).value, 123, 'should be accepted.'); test.done(); }, 'expect (lazy with empty params) to return a given value': function expectLazyWithEmptyParamsToReturnAGivenValue(test) { test.expect(1); // tests here test.equal(_index3.F.lazy(function () { return _index3.F.returns(123); }, []).parse(_index2.default.ofString(''), 0).value, 123, 'should be accepted.'); test.done(); }, 'expect (lazy) to be rejected': function expectLazyToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.F.lazy(function () { return _index3.F.error(); }).parse(_index2.default.ofString(''), 0).isAccepted(), false, 'should be accepted.'); test.done(); }, 'expect (lazy) with a parameter to return a given value': function expectLazyWithAParameterToReturnAGivenValue(test) { test.expect(1); // tests here test.equal(_index3.F.lazy(function (v) { return _index3.F.returns(v); }, [123]).parse(_index2.default.ofString(''), 0).value, 123, 'should be accepted.'); test.done(); }, 'expect (lazy) with multiple parameters to return a given value': function expectLazyWithMultipleParametersToReturnAGivenValue(test) { test.expect(1); // tests here test.equal(_index3.F.lazy(function (v1, v2) { return _index3.F.returns(v1 + v2); }, [10, 20]).parse(_index2.default.ofString(''), 0).value, 30, 'should be accepted.'); test.done(); }, 'expect (lazy) with unpacked parameters to fail': function expectLazyWithUnpackedParametersToFail(test) { test.expect(1); // tests here var found = false; try { var combinator = _index3.F.lazy(function (v1, v2) { return _index3.F.returns(v1 + v2); }, 10, 20); combinator.parse(_index2.default.ofString(''), 0); } catch (e) { if (e.includes('packed into an array')) { found = true; } } test.ok(found, 'should have catch an error'); test.done(); }, 'expect (error) to be rejected': function expectErrorToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.F.error().parse(_index2.default.ofString(''), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (eos) to be accepted': function expectEosToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.F.eos().parse(_index2.default.ofString(''), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (eos) to be rejected': function expectEosToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.F.eos().parse(_index2.default.ofString('a'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (satisfy) to be accepted': function expectSatisfyToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.F.satisfy(function (v) { return v === 'a'; }).parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (satisfy) to be return the right value': function expectSatisfyToBeReturnTheRightValue(test) { test.expect(1); // tests here test.equal(_index3.F.satisfy(function (v) { return v === 'a'; }).parse(_index2.default.ofString('a'), 0).value, 'a', 'should be the right value.'); test.done(); }, 'expect (satisfy) to be return the right offset': function expectSatisfyToBeReturnTheRightOffset(test) { test.expect(1); // tests here test.equal(_index3.F.satisfy(function (v) { return v === 'a'; }).parse(_index2.default.ofString('a'), 0).offset, 1, 'should be the right offset.'); test.done(); }, 'expect (satisfy) to be rejected': function expectSatisfyToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.F.satisfy(function (v) { return v === 'b'; }).parse(_index2.default.ofString('a'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (doTry satisfy) to be accepted': function expectDoTrySatisfyToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.F.try(_index3.F.satisfy(function (v) { return v === 'a'; })).parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (doTry satisfy) to be rejected': function expectDoTrySatisfyToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.F.try(_index3.F.satisfy(function (v) { return v === 'b'; })).parse(_index2.default.ofString('a'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (digit) to be accepted': function expectDigitToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.N.digit().parse(_index2.default.ofString('1'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (digit) to be rejected': function expectDigitToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.N.digit().parse(_index2.default.ofString('a'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (number) to be accepted': function expectNumberToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.N.number().parse(_index2.default.ofString('123'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (number) to return 123': function expectNumberToReturn123(test) { test.expect(1); // tests here test.equal(_index3.N.number().parse(_index2.default.ofString('123'), 0).value, 123, 'should be accepted.'); test.done(); }, 'expect negative (number) to be accepted': function expectNegativeNumberToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.N.number().parse(_index2.default.ofString('-123'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect negative (number) to return -123': function expectNegativeNumberToReturn123(test) { test.expect(1); // tests here test.equal(_index3.N.number().parse(_index2.default.ofString('-123'), 0).value, -123, 'should be accepted.'); test.done(); }, 'expect float (number) to be accepted': function expectFloatNumberToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.N.number().parse(_index2.default.ofString('123.34e-34'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect float (number) to return 123.34e-34': function expectFloatNumberToReturn12334e34(test) { test.expect(1); // tests here test.equal(_index3.N.number().parse(_index2.default.ofString('123.34e-34'), 0).value, 123.34e-34, 'should be accepted.'); test.done(); }, 'expect (charLiteral) to be accepted': function expectCharLiteralToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.charLiteral().parse(_index2.default.ofString("'a'"), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (charLiteral) to return a': function expectCharLiteralToReturnA(test) { test.expect(1); // tests here test.equal(_index3.C.charLiteral().parse(_index2.default.ofString("'a'"), 0).value, 'a', 'should be accepted.'); test.done(); }, 'expect (charLiteral) quote to be accepted': function expectCharLiteralQuoteToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.charLiteral().parse(_index2.default.ofString("'\\''"), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (charLiteral) to be rejected': function expectCharLiteralToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.C.charLiteral().parse(_index2.default.ofString("''"), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (stringLiteral) to be accepted': function expectStringLiteralToBeAccepted(test) { test.expect(1); // tests here try { test.equal(_index3.C.stringLiteral().parse(_index2.default.ofString('"a"'), 0).isAccepted(), true, 'should be accepted.'); } catch (e) { console.log(e); } test.done(); } /* 'expect (stringLiteral) to return abc': function(test) { test.expect(1); // tests here test.equal( C.stringLiteral().parse(stream.ofString('"abc"'), 0).value, 'abc', 'should be accepted.' ); test.done(); }, 'expect (stringLiteral) empty to be accepted': function(test) { test.expect(1); // tests here test.equal( C.stringLiteral().parse(stream.ofString('""'), 0).isAccepted(), true, 'should be accepted.' ); test.done(); }, /* 'expect (occurence 1) to be accepted': function(test) { test.expect(1); // tests here test.equal( F.any().occurrence(1).parse(stream.ofString('a'), 0).isAccepted(), true, 'should be accepted.' ); test.done(); }, 'expect (occurence 1) to return [a]': function(test) { test.expect(1); // tests here test.deepEqual( F.any().occurrence(1).parse(stream.ofString('a'), 0).value.array(), ['a'], 'should be accepted.' ); test.done(); }, 'expect (occurence 2) to be accepted': function(test) { test.expect(1); // tests here test.equal( F.any().occurrence(1).parse(stream.ofString('aa'), 0).isAccepted(), true, 'should be accepted.' ); test.done(); }, 'expect (occurence 2) to return [a,a]': function(test) { test.expect(1); // tests here test.deepEqual( F.any().occurrence(2).parse(stream.ofString('aa'), 0).value.array(), ['a', 'a'], 'should be accepted.' ); test.done(); }, ///OK /* 'expect (occurence 3) to return [a,a,a]': function(test) { test.expect(1); // tests here test.deepEqual( F.any().occurrence(3).parse(stream.ofString('aaa'), 0).value.array(), ['a', 'a', 'a'], 'should be accepted.' ); test.done(); }, 'expect (occurence 0) to be accepted': function(test) { test.expect(1); // tests here test.equal( F.any().occurrence(0).parse(stream.ofString('aa'), 0).isAccepted(), true, 'should be accepter.' ); test.done(); }, 'expect (occurence 0) to return []': function(test) { test.expect(1); // tests here test.deepEqual( F.any().occurrence(0).parse(stream.ofString('aa'), 0).value.array(), [], 'should be accepter.' ); test.done(); }, "expect chained then (2+2) to return [2,'+' ,2]": function(test) { // Main difference with sequence, is that a sequence element could be an array test.expect(1); // tests here const string = '2+2'; const expected = [2, '+', 2]; const parsing = N.number() .then(C.char('+')) .then(N.number()).array() .parse(stream.ofString(string), 0); test.deepEqual(parsing.value, expected, 'should be equal'); test.done(); }, 'expect chained then (2+2) to return []': function(test) { // Main difference with sequence, is that a sequence element could be an array test.expect(1); // tests here const string = '2+2'; const expected = []; const parsing = N.number() .then(C.char('+')) .then(N.number()) .returns([]) .parse(stream.ofString(string), 0); test.deepEqual(parsing.value, expected, 'should be equal'); test.done(); }, 'export subStream(4) to return [h,e,l,l]': function(test) { test.expect(1); // tests here const string = 'hello'; const expected = ['h', 'e', 'l', 'l']; const parsing = F.subStream(4).parse(stream.ofString(string), 0); test.deepEqual(parsing.value.array(), expected, 'should be equal'); test.done(); }, 'export subString(4) to return hell': function(test) { test.expect(1); // tests here const string = 'hello'; const expected = 'hell'; const parsing = C.subString(4).parse(stream.ofString(string), 0); test.deepEqual(parsing.value, expected, 'should be equal'); test.done(); }*/ }; //# sourceMappingURL=parser_extensions_test.js.map