remix-project mirror
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
remix-project/libs/remix-astwalker/tests/newTests.ts

161 lines
5.1 KiB

import tape from "tape";
import { AstWalker, AstNode, isAstNode } from "../src";
import node from "./resources/newAST";
import legacyNode from "./resources/legacyAST";
tape("New ASTWalker", (t: tape.Test) => {
// New Ast Object
const astWalker = new AstWalker();
4 years ago
const latestASTNode = JSON.parse(JSON.stringify(node.ast))
t.test("ASTWalker.walk && .walkastList", (st: tape.Test) => {
4 years ago
const latestAST = JSON.parse(JSON.stringify(latestASTNode))
st.plan(24);
// EventListener
astWalker.on("node", node => {
if (node.nodeType === "ContractDefinition") {
checkContract(st, node);
}
if (node.nodeType === "PragmaDirective") {
checkProgramDirective(st, node);
}
});
// Callback pattern
4 years ago
astWalker.walk(latestAST, (node: AstNode) => {
if (node.nodeType === "ContractDefinition") {
checkContract(st, node);
}
if (node.nodeType === "PragmaDirective") {
checkProgramDirective(st, node);
}
});
// Callback Object
var callback: any = {};
callback.FunctionDefinition = function(node: AstNode): boolean {
st.equal(node.name, "FunctionDefinition");
return true;
};
// Calling walk function with cb
4 years ago
astWalker.walk(latestAST, callback);
// Calling walk function without cb
4 years ago
astWalker.walk(latestAST);
// Calling WALKASTLIST function
astWalker.walkAstList(node);
// Calling WALKASTLIST function with cb
astWalker.walkAstList(node, node => {
return true;
});
st.end();
});
4 years ago
4 years ago
t.test("ASTWalker.getASTNodeChildren", (st: tape.Test) => {
const latestAST = JSON.parse(JSON.stringify(latestASTNode))
st.plan(26);
4 years ago
st.equal(latestAST.nodeType, 'SourceUnit')
4 years ago
4 years ago
let subNodes1 = astWalker.getASTNodeChildren(latestAST)
4 years ago
4 years ago
st.equal(subNodes1.length, 3)
st.equal(subNodes1[0].nodeType, 'PragmaDirective')
st.equal(subNodes1[1].nodeType, 'ImportDirective')
st.equal(subNodes1[2].nodeType, 'ContractDefinition')
4 years ago
4 years ago
let subNodes2 = astWalker.getASTNodeChildren(subNodes1[0])
st.equal(subNodes2.length, 0)
4 years ago
4 years ago
subNodes2 = astWalker.getASTNodeChildren(subNodes1[1])
st.equal(subNodes2.length, 0)
4 years ago
4 years ago
subNodes2 = astWalker.getASTNodeChildren(subNodes1[2])
st.equal(subNodes2.length, 4)
st.equal(subNodes2[0].nodeType, 'VariableDeclaration')
st.equal(subNodes2[1].nodeType, 'FunctionDefinition')
st.equal(subNodes2[2].nodeType, 'FunctionDefinition')
st.equal(subNodes2[3].nodeType, 'InheritanceSpecifier')
4 years ago
4 years ago
let subNodes3 = astWalker.getASTNodeChildren(subNodes2[0])
st.equal(subNodes3.length, 1)
st.equal(subNodes3[0].nodeType, 'ElementaryTypeName')
let subNodes4 = astWalker.getASTNodeChildren(subNodes3[0])
st.equal(subNodes4.length, 0)
4 years ago
subNodes3 = astWalker.getASTNodeChildren(subNodes2[1])
st.equal(subNodes3.length, 1)
st.equal(subNodes3[0].nodeType, 'Block')
subNodes4 = astWalker.getASTNodeChildren(subNodes3[0])
st.equal(subNodes4.length, 1)
st.equal(subNodes4[0].nodeType, 'ExpressionStatement')
let subNodes5 = astWalker.getASTNodeChildren(subNodes4[0])
st.equal(subNodes5.length, 1)
st.equal(subNodes5[0].nodeType, 'Assignment')
let subNodes6 = astWalker.getASTNodeChildren(subNodes5[0])
st.equal(subNodes6.length, 2)
st.equal(subNodes6[0].nodeType, 'Identifier')
st.equal(subNodes6[1].nodeType, 'Identifier')
let subNodes7 = astWalker.getASTNodeChildren(subNodes6[0])
st.equal(subNodes7.length, 0)
subNodes7 = astWalker.getASTNodeChildren(subNodes6[1])
st.equal(subNodes7.length, 0)
4 years ago
st.end();
});
4 years ago
t.test("ASTWalkFull", (st: tape.Test) => {
4 years ago
const latestAST = JSON.parse(JSON.stringify(latestASTNode))
const astNodeCount = 26;
st.plan(2 + astNodeCount);
let count: number = 0;
4 years ago
astWalker.walkFull(latestAST, (node: AstNode) => {
st.ok(isAstNode(node), "passed an ast node");
count += 1;
});
st.equal(count, astNodeCount, "traverses all AST nodes");
count = 0;
let badCall = function() {
/* Typescript will keep us from calling walkFull with a legacyAST.
However, for non-typescript uses, we add this test which casts
to an AST to check that there is a run-time check in walkFull.
*/
astWalker.walkFull(<any>legacyNode, (node: AstNode) => {
count += 1;
});
}
// t.throws(badCall, /first argument should be an ast/,
// "passing legacyAST fails");
st.equal(count, 0, "traverses no AST nodes");
st.end();
});
});
function checkProgramDirective(st: tape.Test, node: AstNode) {
st.equal(node.id, 1);
st.equal(node.literals.length, 7);
}
function checkContract(st: tape.Test, node: AstNode) {
st.equal(node.name, "Greeter");
st.equal(node.nodes[0].name, "greeting");
st.equal(node.nodes[0].nodeType, "VariableDeclaration");
st.equal(node.nodes[0].name, "greeting");
st.equal(node.nodes[0].typeName.name, "string");
st.equal(node.nodes[1].nodeType, "FunctionDefinition");
st.equal(node.nodes[1].name, "");
st.equal(node.nodes[1].scope, 25);
st.equal(node.nodes[2].nodeType, "FunctionDefinition");
st.equal(node.nodes[2].name, "greet");
}