UNPKG

parser-combinator

Version:
599 lines (510 loc) 23.6 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) 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 (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 (lowerCase) to be accepted': function expectLowerCaseToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.lowerCase.parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (lowerCase) to be rejected': function expectLowerCaseToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.C.lowerCase.parse(_index2.default.ofString('A'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (upperCase) to be accepted': function expectUpperCaseToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.upperCase.parse(_index2.default.ofString('A'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (upperCase) to be rejected': function expectUpperCaseToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.C.upperCase.parse(_index2.default.ofString('z'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect upper (letter) to be accepted': function expectUpperLetterToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.letter.parse(_index2.default.ofString('A'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect lower (letter) to be accepted': function expectLowerLetterToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.letter.parse(_index2.default.ofString('z'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect space (letter) to be rejected': function expectSpaceLetterToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.C.letter.parse(_index2.default.ofString(' '), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect non (letter) to be rejected': function expectNonLetterToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.C.letter.parse(_index2.default.ofString('0'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (letters) to be accepted': function expectLettersToBeAccepted(test) { test.expect(2); // tests here var parsing = _index3.C.letters.thenLeft(_index3.F.eos).parse(_index2.default.ofString('someLetters'), 0); test.equal(parsing.isAccepted(), true, 'should be accepted.'); test.deepEqual(parsing.value, 'someLetters', 'should be equal.'); test.done(); }, 'expect (letters) with space to be rejected': function expectLettersWithSpaceToBeRejected(test) { test.expect(2); // tests here var parsing = _index3.C.letters.then(_index3.F.eos).parse(_index2.default.ofString('some Letters'), 0); test.equal(parsing.isAccepted(), false, 'should be rejected.'); test.notDeepEqual(parsing.value, 'some Letters', 'should be equal.'); test.done(); }, 'expect (letters) with number to be rejected': function expectLettersWithNumberToBeRejected(test) { test.expect(1); // tests here var parsing = _index3.C.letters.then(_index3.F.eos).parse(_index2.default.ofString('some2Letters'), 0); test.equal(parsing.isAccepted(), false, 'should be accepted.'); test.done(); }, 'expect (letters) to return a string, not an array of letters': function expectLettersToReturnAStringNotAnArrayOfLetters(test) { test.expect(1); // tests here var parsing = _index3.C.letters.thenLeft(_index3.F.eos).parse(_index2.default.ofString('someLetters'), 0); test.equal(parsing.value, 'someLetters', 'not a string'); test.done(); }, 'expect (char) to be accepted': function expectCharToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.char('a').parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (char) to be rejected': function expectCharToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.C.char('a').parse(_index2.default.ofString('b'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (char) to be refused': function expectCharToBeRefused(test) { test.expect(1); // tests here test.throws(function () { _index3.C.char('aa'); }); test.done(); }, 'expect (notChar) to be accepted': function expectNotCharToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.notChar('a').parse(_index2.default.ofString('b'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (notChar) to be rejected': function expectNotCharToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.C.notChar('a').parse(_index2.default.ofString('a'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (notChar) to be refused': function expectNotCharToBeRefused(test) { test.expect(1); // tests here test.throws(function () { _index3.C.notChar('aa'); }); test.done(); }, 'expect (charNotIn) to be accepted': function expectCharNotInToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.charNotIn('a').parse(_index2.default.ofString('b'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (charNotIn) to be rejected': function expectCharNotInToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.C.charNotIn('a').parse(_index2.default.ofString('a'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (charIn) to be accepted': function expectCharInToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.charIn('a').parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (charIn) to be rejected': function expectCharInToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.C.charIn('a').parse(_index2.default.ofString('b'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (string) to be accepted': function expectStringToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.string('Hello').parse(_index2.default.ofString('Hello'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (string) to be rejected': function expectStringToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.C.string('hello').parse(_index2.default.ofString('hell'), 0).isAccepted(), false, 'should be rejected.'); test.done(); }, 'expect (notString) to be accepted': function expectNotStringToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.notString('**').parse(_index2.default.ofString('hello'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (notString) to be h': function expectNotStringToBeH(test) { test.expect(1); // tests here test.equal(_index3.C.notString('**').parse(_index2.default.ofString('hello'), 0).value, 'h', 'should be h.'); test.done(); }, 'expect (notString) to be rejected': function expectNotStringToBeRejected(test) { test.expect(1); // tests here test.equal(_index3.C.notString('**').parse(_index2.default.ofString('**hello'), 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.numberLiteral.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.numberLiteral.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.numberLiteral.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.numberLiteral.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.numberLiteral.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.numberLiteral.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 test.equal(_index3.C.stringLiteral.parse(_index2.default.ofString('"a"'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (stringLiteral) to return abc': function expectStringLiteralToReturnAbc(test) { test.expect(1); // tests here test.equal(_index3.C.stringLiteral.parse(_index2.default.ofString('"abc"'), 0).value, 'abc', 'should be accepted.'); test.done(); }, 'expect (stringLiteral) empty to be accepted': function expectStringLiteralEmptyToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.C.stringLiteral.parse(_index2.default.ofString('""'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (occurence 1) to be accepted': function expectOccurence1ToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.F.any.occurrence(1).parse(_index2.default.ofString('a'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (occurence 1) to return [a]': function expectOccurence1ToReturnA(test) { test.expect(1); // tests here test.deepEqual(_index3.F.any.occurrence(1).parse(_index2.default.ofString('a'), 0).value.array(), ['a'], 'should be accepted.'); test.done(); }, 'expect (occurence 2) to be accepted': function expectOccurence2ToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.F.any.occurrence(1).parse(_index2.default.ofString('aa'), 0).isAccepted(), true, 'should be accepted.'); test.done(); }, 'expect (occurence 2) to return [a,a]': function expectOccurence2ToReturnAA(test) { test.expect(1); // tests here test.deepEqual(_index3.F.any.occurrence(2).parse(_index2.default.ofString('aa'), 0).value.array(), ['a', 'a'], 'should be accepted.'); test.done(); }, 'expect (occurence 3) to return [a,a,a]': function expectOccurence3ToReturnAAA(test) { test.expect(1); // tests here test.deepEqual(_index3.F.any.occurrence(3).parse(_index2.default.ofString('aaa'), 0).value.array(), ['a', 'a', 'a'], 'should be accepted.'); test.done(); }, 'expect (occurence 0) to be accepted': function expectOccurence0ToBeAccepted(test) { test.expect(1); // tests here test.equal(_index3.F.any.occurrence(0).parse(_index2.default.ofString('aa'), 0).isAccepted(), true, 'should be accepter.'); test.done(); }, 'expect (occurence 0) to return []': function expectOccurence0ToReturn(test) { test.expect(1); // tests here test.deepEqual(_index3.F.any.occurrence(0).parse(_index2.default.ofString('aa'), 0).value.array(), [], 'should be accepter.'); test.done(); }, "expect sequence ( '(',text(), ')' ) to return ['(', text, ')']": function expectSequenceTextToReturnText(test) { test.expect(1); // tests here var string = '(Hello)'; var expected = ['(', 'Hello', ')']; var parsing = _index3.F.sequence(_index3.C.char('('), _index3.C.charNotIn(')').rep().map(function (v) { return v.join(''); }), _index3.C.char(')')).parse(_index2.default.ofString(string), 0); test.deepEqual(parsing.value, expected, 'should be equal'); test.done(); }, "expect sequence ( 2+2) to return [2,'+' ,2]": function expectSequence22ToReturn22(test) { test.expect(1); // tests here var string = '2+2'; var expected = [2, '+', 2]; var parsing = _index3.F.sequence(_index3.N.numberLiteral, _index3.C.char('+'), _index3.N.numberLiteral).parse(_index2.default.ofString(string), 0); test.deepEqual(parsing.value, expected, 'should be equal'); test.done(); }, "expect chained then (2+2) to return [2,'+' ,2]": function expectChainedThen22ToReturn22(test) { // Main difference with sequence, is that a sequence element could be an array test.expect(1); // tests here var string = '2+2'; var expected = [2, '+', 2]; var parsing = _index3.N.numberLiteral.then(_index3.C.char('+')).then(_index3.N.numberLiteral).parse(_index2.default.ofString(string), 0); test.deepEqual(parsing.value, expected, 'should be equal'); test.done(); }, 'expect chained then (2+2) to return []': function expectChainedThen22ToReturn(test) { // Main difference with sequence, is that a sequence element could be an array test.expect(1); // tests here var string = '2+2'; var expected = []; var parsing = _index3.N.numberLiteral.then(_index3.C.char('+')).then(_index3.N.numberLiteral).thenReturns([]).parse(_index2.default.ofString(string), 0); test.deepEqual(parsing.value, expected, 'should be equal'); test.done(); }, 'export subStream(4) to return [h,e,l,l]': function exportSubStream4ToReturnHELL(test) { test.expect(1); // tests here var string = 'hello'; var expected = ['h', 'e', 'l', 'l']; var parsing = _index3.F.subStream(4).parse(_index2.default.ofString(string), 0); test.deepEqual(parsing.value.array(), expected, 'should be equal'); test.done(); }, 'export subString(4) to return hell': function exportSubString4ToReturnHell(test) { test.expect(1); // tests here var string = 'hello'; var expected = 'hell'; var parsing = _index3.C.subString(4).parse(_index2.default.ofString(string), 0); test.deepEqual(parsing.value, expected, 'should be equal'); test.done(); } }; //# sourceMappingURL=parser_extensions_test.js.map