This code is a JavaScript testing module that uses the esprima
and escodegen
libraries to parse and generate code, and the assert
library to make assertions on the code's syntax. The module exports two test functions, testProgram
and testSelect
, which can be used to test the code's structure and behavior.
var esprima = require('esprima');
var escodegen = require('escodegen');
var assert = require('assert');
var code = `
var importer = require('../Core');
function name(params) {
return importer.interpret('this is a describe request');
}
console.log()
`
function testProgram(ctx) {
var node = selectTree('Program', ctx);
assert(node.type === 'Program', 'just a single node not a list');
return node;
}
function testSelect(ctx) {
var node = selectTree([
'Program'
], ctx);
assert(node.length === 1, 'list of root node');
assert(node[0].type === 'Program');
// using the first query results as the context for the second query on each item
var node = selectTree([
'Identifier, CallExpression', // interchangable CSS selector
], ctx);
assert(node.length > 6, 'list of concatenated elements');
assert(typeof node[6].name !== 'undefined');
var node = selectTree([
'//*[@type="Identifier"]', // interchangable CSS selector
'./@name'
], ctx);
assert(node.length > 3, 'list of ids');
assert(node[3] === 'params');
}
module.exports = {
testProgram,
testSelect
}
// TODO: compare with acorn
if(typeof $ !== 'undefined') {
var ctx = esprima.parse(code);
var output = testProgram(ctx);
testSelect(ctx);
var output = escodegen.generate(output);
console.log(output)
}
const esprima = require('esprima');
const escodegen = require('escodegen');
const assert = require('assert');
const code = `
var importer = require('../Core');
function name(params) {
return importer.interpret('this is a describe request');
}
console.log()
`;
const parseCode = () => JSON.stringify(esprima.parse(code), null, 2);
const generateCode = (node) => escodegen.generate(node);
const testProgram = (ctx) => {
const programNode = selectTree('Program', ctx);
assert.strictEqual(programNode.type, 'Program', 'Just a single node, not a list');
return programNode;
};
const testSelect = (ctx) => {
const programNode = selectTree(['Program'], ctx);
assert.strictEqual(programNode.length, 1, 'A list with one root node');
assert.strictEqual(programNode[0].type, 'Program', 'Root node is Program');
const identifierCallExpressions = selectTree(['Identifier', 'CallExpression'], ctx);
assert(identifierCallExpressions.length > 6, 'List of concatenated elements');
assert(identifierCallExpressions[6].type === 'Identifier', '6th element is an Identifier');
const identifierNames = selectTree(['//[@type="Identifier"]', './@name'], ctx);
assert(identifierNames.length > 3, 'List of identifier names');
assert(identifierNames[3] === 'params', '4th element is \'params\'');
const callExpressions = selectTree(['CallExpression, Identifier'], ctx);
assert(callExpressions.length > 6, 'List of concatenated elements');
assert(callExpressions[6].type === 'Identifier', '7th element is an Identifier');
};
module.exports = {
testProgram,
testSelect
};
const $ = esprima.parse(code);
if ($!== undefined) {
const output = testProgram($);
testSelect($);
const generatedCode = generateCode(output);
console.log(generatedCode);
}
// TODO: Compare with acorn
var esprima = require('esprima');
var escodegen = require('escodegen');
var assert = require('assert');
esprima
is a JSON-based abstract syntax tree (AST) parser for JavaScript code.escodegen
is a code generator that converts AST into JavaScript code.assert
is a module for making assertions in the code.var code = `
var importer = require('../Core');
function name(params) {
return importer.interpret('this is a describe request');
}
console.log()
`
testProgram
function testProgram(ctx) {
var node = selectTree('Program', ctx);
assert(node.type === 'Program', 'just a single node not a list');
return node;
}
ctx
) and uses selectTree
to find the root node (Program
).testSelect
function testSelect(ctx) {
//...
}
ctx
) and performs multiple selectTree
queries to test its behavior.Identifier
or CallExpression
and assert that there are more than 6 nodes.Identifier
with @type="Identifier"
and assert that there are more than 3 nodes.Identifier
with @name
and assert that the third node is 'params'
.module.exports = {
testProgram,
testSelect
}
testProgram
and testSelect
.if(typeof $!== 'undefined') {
var ctx = esprima.parse(code);
var output = testProgram(ctx);
testSelect(ctx);
var output = escodegen.generate(output);
console.log(output)
}
$$
variable is defined.esprima
, runs the testProgram
and testSelect
functions, and generates the output code with escodegen
.