parser-combinator
Version:
Parser combinators
207 lines (169 loc) • 7.42 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _markdownParser = require('../../../lib/standard/markdown/markdown-parser');
var _markdownParser2 = _interopRequireDefault(_markdownParser);
var _index = require('../../../lib/stream/index');
var _index2 = _interopRequireDefault(_index);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Created by Nicolas Zozol on 15/12/2016.
*/
var value = undefined;
function testBlock(block) {
var parsing = _markdownParser2.default.parse(_index2.default.ofString(block));
value = parsing.value;
}
var standardParagraph = 'Their job is to destroy\nDeath Stars.';
var expectedStandardParagraph = {
paragraph: [{ text: 'Their job is to destroy Death Stars.' }]
};
var lev1Title = '# Star Wars Rocks';
var otherLev1Title = '# Star Trek also Rocks';
var lev1AltTitle = 'Star Wars Rocks\n======';
var lev2AltTitle = 'C3PO and R2D2\n----';
var expectedLev1Title = { title: { level: 1, text: 'Star Wars Rocks' } };
var expectedOtherLev1Title = {
title: { level: 1, text: 'Star Trek also Rocks' }
};
var expectedLev2Title = { title: { level: 2, text: 'C3PO and R2D2' } };
var complexParagraph = 'They know how to *use the force*\n , speed up, and destroy **Death Stars**.';
var expectedComplexParagraph = {
paragraph: [{ text: 'They know how to ' }, { italic: 'use the force' }, { text: ' , speed up, and destroy ' }, { bold: 'Death Stars' }, { text: '.' }]
};
var bullet1 = '* Look for plans';
var bullet2 = '* Find an **unexpensive** youngster';
var bullet3 = '- Kill an *old man*';
var bullet4level2 = ' - Use the `force`';
var bullet5level2 = '\t* Destroy Death Star ';
var bullets = bullet1 + '\n' + bullet2 + '\n' + bullet3 + '\n' + bullet4level2 + '\n' + bullet5level2;
var expectedBullet1 = {
bullet: { level: 1, content: [{ text: 'Look for plans' }] }
};
var expectedBullet2 = {
bullet: {
level: 1,
content: [{ text: 'Find an ' }, { bold: 'unexpensive' }, { text: ' youngster' }]
}
};
var expectedBullet3 = {
bullet: {
level: 1,
content: [{ text: 'Kill an ' }, { italic: 'old man' }]
}
};
var expectedBullet4level2 = {
bullet: {
level: 2,
content: [{ text: 'Use the ' }, { code: 'force' }]
}
};
var expectedBullet5level2 = {
bullet: {
level: 2,
content: [{ text: 'Destroy Death Star ' }]
}
};
var expectedBullets = [expectedBullet1, expectedBullet2, expectedBullet3, expectedBullet4level2, expectedBullet5level2];
exports.default = {
setUp: function setUp(done) {
done();
},
'Multilines lines paragraph should be accepted': function MultilinesLinesParagraphShouldBeAccepted(test) {
var block = '' + standardParagraph;
testBlock(block);
var expected = [expectedStandardParagraph];
test.deepEqual(expected, value, 'bad value for multiline');
test.done();
},
'Complex Multilines lines should be accepted': function ComplexMultilinesLinesShouldBeAccepted(test) {
var block = '' + complexParagraph;
testBlock(block);
var expected = [expectedComplexParagraph];
test.deepEqual(expected, value, 'bad value for complex paragraph');
test.done();
},
'Read title and its paragraphs': function ReadTitleAndItsParagraphs(test) {
var block = lev1Title + '\n' + standardParagraph + '\n\n' + complexParagraph;
testBlock(block);
var expected = [expectedLev1Title, expectedStandardParagraph, expectedComplexParagraph];
test.deepEqual(expected, value, 'bad value for title & paragraphs');
test.done();
},
'Read bullets': function ReadBullets(test) {
//test.expect(2);
var block = '' + bullets;
testBlock(block);
//test.ok(accepted, 'should be accepted.');
test.deepEqual(expectedBullets, value, 'bad value for bullets');
test.done();
},
'Read title, paragraph & bullets': function ReadTitleParagraphBullets(test) {
//test.expect(2);
var block = lev1Title + '\n' + bullets + '\n' + standardParagraph;
testBlock(block);
var expected = [expectedLev1Title].concat(expectedBullets).concat([expectedStandardParagraph]);
test.deepEqual(expected, value, 'bad value for bullets');
test.done();
},
'Read multilevel chapters': function ReadMultilevelChapters(test) {
var block = lev1AltTitle + '\n' + bullets + '\n ' + lev2AltTitle + '\n ' + standardParagraph + '\n \n' + otherLev1Title + '\n ' + complexParagraph;
testBlock(block);
//test.ok(accepted, 'should be accepted.');
var expected = [expectedLev1Title].concat(expectedBullets).concat([expectedLev2Title, expectedStandardParagraph, expectedOtherLev1Title, expectedComplexParagraph]);
test.deepEqual(value, expected, 'bad value for complex text');
test.done();
},
/*
'Read multilevel with initial \n ': function (test) {
//test.expect(2);
const block = `
${lev1AltTitle}
${bullets}
${lev2AltTitle}
${standardParagraph}
${otherLev1Title}
${complexParagraph}
`;
testBlock(block);
//test.ok(accepted, 'should be accepted.');
const expected= [expectedLev1Title, expectedBullets,
expectedLev2Title, expectedStandardParagraph,
expectedOtherLev1Title, expectedComplexParagraph];
test.deepEqual(value, expected, 'bad value for complex text');
test.done();
} */
'Read space code line': function ReadSpaceCodeLine(test) {
var spaceCodeLine = ' Star Wars is an Atari game';
var expectedSpaceCodeLine = [{ code: 'Star Wars is an Atari game' }];
testBlock(spaceCodeLine);
test.deepEqual(value, expectedSpaceCodeLine, 'bad value for space code line');
test.done();
},
'Read tab code line': function ReadTabCodeLine(test) {
var tabCodeLine = '\t\tStar Wars is an Amstrad game';
var expectedTabCodeLine = [{ code: 'Star Wars is an Amstrad game' }];
testBlock(tabCodeLine);
test.deepEqual(value, expectedTabCodeLine, 'bad value for space code line');
test.done();
},
'Read tab code blocks': function ReadTabCodeBlocks(test) {
//test.expect(2);
var haskellCode = '\t\timport Control.Monad\n\t\t\n\t\tsolveTable :: [String] -> [String] -> [(String, Integer)]';
var expectedHaskellCode = [{ code: 'import Control.Monad' }, { code: '' }, { code: 'solveTable :: [String] -> [String] -> [(String, Integer)]' }];
testBlock(haskellCode);
test.deepEqual(value, expectedHaskellCode, 'bad value for bullets');
test.done();
},
'Read space code blocks': function ReadSpaceCodeBlocks(test) {
//test.expect(2);
// there are 8 spaces for first line, then 10 for the second line
var spaceCodeBloc = ' Star Wars is an Amstrad game\n Star Wars is an Atari game';
var expectedSpaceCodeBloc = [{ code: 'Star Wars is an Amstrad game' }, { code: ' Star Wars is an Atari game' }];
testBlock(spaceCodeBloc);
test.deepEqual(expectedSpaceCodeBloc, value, 'bad value for bullets');
test.done();
}
};
//# sourceMappingURL=document-parser-test.js.map