'use strict';
const common = require('../common');
// The doctool currently uses js-yaml from the tool/node_modules/eslint/ tree.
try {
require('../../tools/node_modules/eslint/node_modules/js-yaml');
} catch (e) {
common.skip('missing js-yaml (eslint not present)');
}
const assert = require('assert');
const { readFile } = require('fs');
const fixtures = require('../common/fixtures');
const toHTML = require('../../tools/doc/html.js');
// Test data is a list of objects with two properties.
// The file property is the file path.
// The html property is some HTML which will be generated by the doctool.
// This HTML will be stripped of all whitespace because we don't currently
// have an HTML parser.
const testData = [
{
file: fixtures.path('sample_document.md'),
html: '
- fish
- fish
' +
''
},
{
file: fixtures.path('order_of_end_tags_5873.md'),
html: 'ClassMethod: Buffer.from(array) ' +
'#
' +
''
},
{
file: fixtures.path('doc_with_yaml.md'),
html: 'Sample Markdown with YAML info' +
'#
' +
'Foobar#
' +
'Added in: v1.0.0
' +
'Describe Foobar
in more detail here.
' +
'Foobar II#
' +
'Describe Foobar II
in more detail here.' +
'fg(1)' +
'
Deprecated thingy#' +
'
Added in: v1.0.0' +
'Deprecated since: v2.0.0
Describe ' +
'Deprecated thingy
in more detail here.' +
'fg(1p)' +
'
Something#
' +
' ' +
'Describe Something
in more detail here.
'
},
{
file: fixtures.path('sample_document.md'),
html: '- fish
- fish
' +
'',
analyticsId: 'UA-67020396-1'
},
];
const spaces = /\s/g;
testData.forEach(({ file, html, analyticsId }) => {
// Normalize expected data by stripping whitespace.
const expected = html.replace(spaces, '');
const includeAnalytics = typeof analyticsId !== 'undefined';
readFile(file, 'utf8', common.mustCall((err, input) => {
assert.ifError(err);
toHTML(
{
input: input,
filename: 'foo',
nodeVersion: process.version,
analytics: analyticsId,
},
common.mustCall((err, output) => {
assert.ifError(err);
const actual = output.replace(spaces, '');
// Assert that the input stripped of all whitespace contains the
// expected markup.
assert(actual.includes(expected));
// Testing the insertion of Google Analytics script when
// an analytics id is provided. Should not be present by default.
const scriptDomain = 'google-analytics.com';
if (includeAnalytics) {
assert(actual.includes(scriptDomain),
`Google Analytics script was not present in "${actual}"`);
} else {
assert.strictEqual(actual.includes(scriptDomain), false,
'Google Analytics script was present in ' +
`"${actual}"`);
}
})
);
}));
});