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
# This is a combination of 50 commits. # This is the 1st commit message: executors # This is the commit message #2: libs # This is the commit message #3: remixd # This is the commit message #4: add react app # This is the commit message #5: debugging # This is the commit message #6: debug + sol # This is the commit message #7: fixes # This is the commit message #8: tsconfig # This is the commit message #9: ast walker # This is the commit message #10: as walker # This is the commit message #11: remixd etc # This is the commit message #12: commander # This is the commit message #13: remove jest # This is the commit message #14: rm ui files # This is the commit message #15: rm reserved keywords # This is the commit message #16: testrunner # This is the commit message #17: compiler # This is the commit message #18: production build # This is the commit message #19: config # This is the commit message #20: config # This is the commit message #21: web types # This is the commit message #22: update react # This is the commit message #23: add vm # This is the commit message #24: add workers # This is the commit message #25: worker2 # This is the commit message #26: rm react app # This is the commit message #27: remixd # This is the commit message #28: worker fix # This is the commit message #29: fix detection # This is the commit message #30: revert react # This is the commit message #31: rename type # This is the commit message #32: loading handler # This is the commit message #33: remove import # This is the commit message #34: rename # This is the commit message #35: local plugin # This is the commit message #36: etherscan # This is the commit message #37: revert react # This is the commit message #38: port # This is the commit message #39: rm worker # This is the commit message #40: publicpath # This is the commit message #41: fix test # This is the commit message #42: 112 # This is the commit message #43: show version # This is the commit message #44: "axios": "1.1.2", # This is the commit message #45: config # This is the commit message #46: lint # This is the commit message #47: fix build # This is the commit message #48: lint # This is the commit message #49: error on purpose # This is the commit message #50: test error
2 years ago
const 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
# This is a combination of 50 commits. # This is the 1st commit message: executors # This is the commit message #2: libs # This is the commit message #3: remixd # This is the commit message #4: add react app # This is the commit message #5: debugging # This is the commit message #6: debug + sol # This is the commit message #7: fixes # This is the commit message #8: tsconfig # This is the commit message #9: ast walker # This is the commit message #10: as walker # This is the commit message #11: remixd etc # This is the commit message #12: commander # This is the commit message #13: remove jest # This is the commit message #14: rm ui files # This is the commit message #15: rm reserved keywords # This is the commit message #16: testrunner # This is the commit message #17: compiler # This is the commit message #18: production build # This is the commit message #19: config # This is the commit message #20: config # This is the commit message #21: web types # This is the commit message #22: update react # This is the commit message #23: add vm # This is the commit message #24: add workers # This is the commit message #25: worker2 # This is the commit message #26: rm react app # This is the commit message #27: remixd # This is the commit message #28: worker fix # This is the commit message #29: fix detection # This is the commit message #30: revert react # This is the commit message #31: rename type # This is the commit message #32: loading handler # This is the commit message #33: remove import # This is the commit message #34: rename # This is the commit message #35: local plugin # This is the commit message #36: etherscan # This is the commit message #37: revert react # This is the commit message #38: port # This is the commit message #39: rm worker # This is the commit message #40: publicpath # This is the commit message #41: fix test # This is the commit message #42: 112 # This is the commit message #43: show version # This is the commit message #44: "axios": "1.1.2", # This is the commit message #45: config # This is the commit message #46: lint # This is the commit message #47: fix build # This is the commit message #48: lint # This is the commit message #49: error on purpose # This is the commit message #50: test error
2 years ago
const 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')
# This is a combination of 50 commits. # This is the 1st commit message: executors # This is the commit message #2: libs # This is the commit message #3: remixd # This is the commit message #4: add react app # This is the commit message #5: debugging # This is the commit message #6: debug + sol # This is the commit message #7: fixes # This is the commit message #8: tsconfig # This is the commit message #9: ast walker # This is the commit message #10: as walker # This is the commit message #11: remixd etc # This is the commit message #12: commander # This is the commit message #13: remove jest # This is the commit message #14: rm ui files # This is the commit message #15: rm reserved keywords # This is the commit message #16: testrunner # This is the commit message #17: compiler # This is the commit message #18: production build # This is the commit message #19: config # This is the commit message #20: config # This is the commit message #21: web types # This is the commit message #22: update react # This is the commit message #23: add vm # This is the commit message #24: add workers # This is the commit message #25: worker2 # This is the commit message #26: rm react app # This is the commit message #27: remixd # This is the commit message #28: worker fix # This is the commit message #29: fix detection # This is the commit message #30: revert react # This is the commit message #31: rename type # This is the commit message #32: loading handler # This is the commit message #33: remove import # This is the commit message #34: rename # This is the commit message #35: local plugin # This is the commit message #36: etherscan # This is the commit message #37: revert react # This is the commit message #38: port # This is the commit message #39: rm worker # This is the commit message #40: publicpath # This is the commit message #41: fix test # This is the commit message #42: 112 # This is the commit message #43: show version # This is the commit message #44: "axios": "1.1.2", # This is the commit message #45: config # This is the commit message #46: lint # This is the commit message #47: fix build # This is the commit message #48: lint # This is the commit message #49: error on purpose # This is the commit message #50: test error
2 years ago
const subNodes5 = astWalker.getASTNodeChildren(subNodes4[0])
st.equal(subNodes5.length, 1)
st.equal(subNodes5[0].nodeType, 'Assignment')
# This is a combination of 50 commits. # This is the 1st commit message: executors # This is the commit message #2: libs # This is the commit message #3: remixd # This is the commit message #4: add react app # This is the commit message #5: debugging # This is the commit message #6: debug + sol # This is the commit message #7: fixes # This is the commit message #8: tsconfig # This is the commit message #9: ast walker # This is the commit message #10: as walker # This is the commit message #11: remixd etc # This is the commit message #12: commander # This is the commit message #13: remove jest # This is the commit message #14: rm ui files # This is the commit message #15: rm reserved keywords # This is the commit message #16: testrunner # This is the commit message #17: compiler # This is the commit message #18: production build # This is the commit message #19: config # This is the commit message #20: config # This is the commit message #21: web types # This is the commit message #22: update react # This is the commit message #23: add vm # This is the commit message #24: add workers # This is the commit message #25: worker2 # This is the commit message #26: rm react app # This is the commit message #27: remixd # This is the commit message #28: worker fix # This is the commit message #29: fix detection # This is the commit message #30: revert react # This is the commit message #31: rename type # This is the commit message #32: loading handler # This is the commit message #33: remove import # This is the commit message #34: rename # This is the commit message #35: local plugin # This is the commit message #36: etherscan # This is the commit message #37: revert react # This is the commit message #38: port # This is the commit message #39: rm worker # This is the commit message #40: publicpath # This is the commit message #41: fix test # This is the commit message #42: 112 # This is the commit message #43: show version # This is the commit message #44: "axios": "1.1.2", # This is the commit message #45: config # This is the commit message #46: lint # This is the commit message #47: fix build # This is the commit message #48: lint # This is the commit message #49: error on purpose # This is the commit message #50: test error
2 years ago
const 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;
# This is a combination of 50 commits. # This is the 1st commit message: executors # This is the commit message #2: libs # This is the commit message #3: remixd # This is the commit message #4: add react app # This is the commit message #5: debugging # This is the commit message #6: debug + sol # This is the commit message #7: fixes # This is the commit message #8: tsconfig # This is the commit message #9: ast walker # This is the commit message #10: as walker # This is the commit message #11: remixd etc # This is the commit message #12: commander # This is the commit message #13: remove jest # This is the commit message #14: rm ui files # This is the commit message #15: rm reserved keywords # This is the commit message #16: testrunner # This is the commit message #17: compiler # This is the commit message #18: production build # This is the commit message #19: config # This is the commit message #20: config # This is the commit message #21: web types # This is the commit message #22: update react # This is the commit message #23: add vm # This is the commit message #24: add workers # This is the commit message #25: worker2 # This is the commit message #26: rm react app # This is the commit message #27: remixd # This is the commit message #28: worker fix # This is the commit message #29: fix detection # This is the commit message #30: revert react # This is the commit message #31: rename type # This is the commit message #32: loading handler # This is the commit message #33: remove import # This is the commit message #34: rename # This is the commit message #35: local plugin # This is the commit message #36: etherscan # This is the commit message #37: revert react # This is the commit message #38: port # This is the commit message #39: rm worker # This is the commit message #40: publicpath # This is the commit message #41: fix test # This is the commit message #42: 112 # This is the commit message #43: show version # This is the commit message #44: "axios": "1.1.2", # This is the commit message #45: config # This is the commit message #46: lint # This is the commit message #47: fix build # This is the commit message #48: lint # This is the commit message #49: error on purpose # This is the commit message #50: test error
2 years ago
const 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");
}