tap-parser
Version:
parse the test anything protocol
132 lines • 5.05 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringify = exports.parse = void 0;
/**
* Static `parse()` and `stringify()` methods
*/
const events_to_array_1 = __importDefault(require("events-to-array"));
const index_js_1 = require("./index.js");
const escape_js_1 = require("./escape.js");
const tap_yaml_1 = __importDefault(require("tap-yaml"));
const brace_patterns_js_1 = require("./brace-patterns.js");
// used in flattening mode
const getId = () => {
const id = () => id.current++;
id.current = 1;
return id;
};
/**
* Parse a TAP text stream into a log of all the events encountered
*/
const parse = (str, options) => {
const { flat = false } = options;
const ignore = ['line', 'pass', 'fail', 'todo', 'skip', 'result'];
if (flat)
ignore.push('assert', 'child', 'plan', 'complete');
const parser = new index_js_1.Parser(options);
const events = (0, events_to_array_1.default)(parser, ignore);
if (flat) {
const id = getId();
parser.on('result', res => {
res.name = res.fullname;
res.id = id();
events.push(['assert', res]);
});
parser.on('complete', res => {
if (!res.bailout)
events.push(['plan', { end: id.current - 1, start: 1 }]);
events.push(['complete', res]);
});
}
parser.end(str);
return events;
};
exports.parse = parse;
/**
* Turn an EventLog from {@link tap-parser!statics.parse} into a TAP string
*/
const stringify = (msg, { flat = false, indent = '', id = getId() }) => {
const ind = flat ? '' : indent;
return (ind +
msg
.map(item => {
switch (item[0]) {
case 'child':
const comment = item[1][0];
const child = item[1].slice(1);
return (index_js_1.Parser.stringify([comment], {
flat,
indent: '',
id,
}) +
index_js_1.Parser.stringify(child, {
flat,
indent: ' ',
id,
}));
case 'version':
return 'TAP version ' + item[1] + '\n';
case 'plan':
if (flat) {
if (indent !== '')
return '';
item[1].start = 1;
item[1].end = id.current - 1;
}
return (item[1].start +
'..' +
item[1].end +
(item[1].comment ? ' # ' + (0, escape_js_1.esc)(item[1].comment) : '') +
'\n');
case 'pragma':
return 'pragma ' + (item[2] ? '+' : '-') + item[1] + '\n';
case 'bailout':
return ('Bail out!' + (item[1] ? ' ' + (0, escape_js_1.esc)(item[1]) : '') + '\n');
case 'assert':
const res = item[1];
if (flat) {
res.id = id();
res.name = res.fullname;
}
return ((res.ok ? '' : 'not ') +
'ok' +
(res.id ? ' ' + res.id : '') +
(res.name ?
' - ' +
(0, escape_js_1.esc)(res.name).replace(brace_patterns_js_1.SPACE_OPEN_BRACE_EOL, '')
: '') +
(res.skip ?
' # SKIP' +
(res.skip === true ? '' : ' ' + (0, escape_js_1.esc)(res.skip))
: '') +
(res.todo ?
' # TODO' +
(res.todo === true ? '' : ' ' + (0, escape_js_1.esc)(res.todo))
: '') +
(res.time ? ' # time=' + res.time + 'ms' : '') +
'\n' +
(res.diag ?
' ---\n ' +
tap_yaml_1.default
.stringify(res.diag)
.split('\n')
.join('\n ')
.trim() +
'\n ...\n'
: ''));
case 'extra':
case 'comment':
return item[1];
}
})
.join('')
.split('\n')
.join('\n' + ind)
.trim() +
'\n');
};
exports.stringify = stringify;
//# sourceMappingURL=statics.js.map