/* eslint-disable max-lines */ const dedent = require('dedent-tabs').default; const Markdown = require('naturalcrit/markdown.js'); // Marked.js adds line returns after closing tags on some default tokens. // This removes those line returns for comparison sake. String.prototype.trimReturns = function(){ return this.replace(/\r?\n|\r/g, '').trim(); }; // Adding `.failing()` method to `describe` or `it` will make failing tests "pass" as long as they continue to fail. // Remove the `.failing()` method once you have fixed the issue. describe('Block-level variables', ()=>{ it('Handles variable assignment and recall with simple text', function() { const source = dedent` [var]: string $[var] `; const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('
string
'); }); it('Handles variable assignment and recall with multiline string', function() { const source = dedent` [var]: string across multiple lines $[var]`; const rendered = Markdown.render(source).replace(/\s/g,' ').trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('string across multiple lines
'); }); it('Handles variable assignment and recall with tables', function() { const source = dedent` [var]: ##### Title | H1 | H2 | |:---|:--:| | A | B | | C | D | $[var]`; const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(dedent`| H1 | H2 |
|---|---|
| A | B |
| C | D |
string
'); }); it('Hoists last instance of variable', function() { const source = dedent` $[var] [var]: string [var]: new string`; const rendered = Markdown.render(source).replace(/\s/g,' ').trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('new string
'); }); it('Handles complex hoisting', function() { const source = dedent` $[titleAndName]: $[title] $[fullName] $[title]: Mr. $[fullName]: $[firstName] $[lastName] [firstName]: Bob Welcome, $[titleAndName]! [lastName]: Jacob [lastName]: $[lastName]son `; const rendered = Markdown.render(source).replace(/\s/g,' ').trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('Welcome, Mr. Bob Jacobson!
'); }); }); describe('Inline-level variables', ()=>{ it('Handles variable assignment and recall with simple text', function() { const source = dedent` $[var](string) $[var] `; const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('string
string
'); }); it('Hoists undefined variables', function() { const source = dedent` $[var](My name is $[name] Jones) [name]: Bob`; const rendered = Markdown.render(source).replace(/\s/g,' ').trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('My name is Bob Jones
'); }); it('Hoists last instance of variable', function() { const source = dedent` $[var](My name is $[name] Jones) $[name](Bob) [name]: Bill`; const rendered = Markdown.render(source).replace(/\s/g,' ').trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`My name is Bill Jones
Bob
`.trimReturns()); }); }); describe('Math', ()=>{ it('Handles simple math using numbers only', function() { const source = dedent` $[1 + 3 * 5 - (1 / 4)] `; const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('15.75
'); }); it('Handles round function', function() { const source = dedent` $[round(1/4)]`; const rendered = Markdown.render(source).replace(/\s/g,' ').trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('0
'); }); it('Handles floor function', function() { const source = dedent` $[floor(0.6)]`; const rendered = Markdown.render(source).replace(/\s/g,' ').trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('0
'); }); it('Handles ceil function', function() { const source = dedent` $[ceil(0.2)]`; const rendered = Markdown.render(source).replace(/\s/g,' ').trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('1
'); }); it('Handles nested functions', function() { const source = dedent` $[ceil(floor(round(0.6)))]`; const rendered = Markdown.render(source).replace(/\s/g,' ').trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('1
'); }); it('Handles simple math with variables', function() { const source = dedent` $[num1]: 5 $[num2]: 4 Answer is $[answer]($[1 + 3 * num1 - (1 / num2)]). `; const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('Answer is 15.75.
'); }); it('Handles variable incrementing', function() { const source = dedent` $[num1]: 5 Increment num1 to get $[num1]($[num1 + 1]) and again to $[num1]($[num1 + 1]). `; const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('Increment num1 to get 6 and again to 7.
'); }); }); describe('Code blocks', ()=>{ it('Ignores all variables in fenced code blocks', function() { const source = dedent` \`\`\` [var]: string $[var] $[var](new string) \`\`\` `; const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(dedent`
[var]: string
$[var]
$[var](new string)
`.trimReturns());
});
it('Ignores all variables in indented code blocks', function() {
const source = dedent`
test
[var]: string
$[var]
$[var](new string)
`;
const rendered = Markdown.render(source).trimReturns();
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(dedent`
test
[var]: string
$[var]
$[var](new string)
`.trimReturns());
});
it('Ignores all variables in inline code blocks', function() {
const source = '[var](Hello) `[link](url)`. This `[var] does not work`';
const rendered = Markdown.render(source).trimReturns();
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(dedent`
var [link](url). This [var] does not work
An image !
A Link to my website!
`.trimReturns()); }); it('Renders normal links with a title', function() { const source = 'A Link to my [website](url "and title")!'; const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(dedent`A Link to my website!
`.trimReturns()); }); }); // TODO: add tests for ID with accordance to CSS spec: // // From https://drafts.csswg.org/selectors/#id-selectors: // // > An ID selector consists of a “number sign” (U+0023, #) immediately followed by the ID value, which must be a CSS identifier. // // From: https://www.w3.org/TR/CSS21/syndata.html#value-def-identifier: // // > In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] // > and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); // > they cannot start with a digit, two hyphens, or a hyphen followed by a digit. // > Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). // > For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F". // > Note that Unicode is code-by-code equivalent to ISO 10646 (see [UNICODE] and [ISO10646]). // TODO: add tests for class with accordance to CSS spec: // // From: https://drafts.csswg.org/selectors/#class-html: // // > The class selector is given as a full stop (. U+002E) immediately followed by an identifier.