From 40fcada889ac9462bb0184c224df413c2b6d961c Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Wed, 2 Sep 2020 20:47:48 +0530 Subject: [PATCH] astwalker tests fixed --- libs/remix-debug/test/astwalker.js | 56 ++++---- libs/remix-debug/test/resources/ast.js | 179 +++++++++++++++++++++++++ 2 files changed, 207 insertions(+), 28 deletions(-) diff --git a/libs/remix-debug/test/astwalker.js b/libs/remix-debug/test/astwalker.js index 0d8d34f193..6e9f705762 100644 --- a/libs/remix-debug/test/astwalker.js +++ b/libs/remix-debug/test/astwalker.js @@ -8,11 +8,11 @@ tape('ASTWalker', function (t) { st.plan(24) const astwalker = new AstWalker() - astwalker.walk(node.ast.legacyAST, function (node) { - if (node.name === 'ContractDefinition') { + astwalker.walk(node.ast.ast, function (node) { + if (node.nodeType === 'ContractDefinition') { checkContract(st, node) } - if (node.name === 'FunctionDefinition') { + if (node.nodeType === 'FunctionDefinition') { checkSetFunction(st, node) } return true @@ -20,42 +20,42 @@ tape('ASTWalker', function (t) { const callback = {} callback.FunctionDefinition = function (node) { - st.equal(node.name, 'FunctionDefinition') - st.equal(node.attributes.name === 'set' || node.attributes.name === 'get', true) + st.equal(node.nodeType, 'FunctionDefinition') + st.equal(node.name === 'set' || node.name === 'get', true) return true } - astwalker.walk(node.ast.legacyAST, callback) + astwalker.walk(node.ast.ast, callback) }) }) function checkContract (st, node) { - st.equal(node.attributes.name, 'test') - st.equal(node.children[0].attributes.name, 'x') - st.equal(node.children[0].attributes.type, 'int256') - st.equal(node.children[1].attributes.name, 'y') - st.equal(node.children[1].attributes.type, 'int256') - st.equal(node.children[2].name, 'FunctionDefinition') - st.equal(node.children[2].attributes.constant, false) - st.equal(node.children[2].attributes.name, 'set') - st.equal(node.children[2].attributes.public, true) + st.equal(node.name, 'test') + st.equal(node.nodes[0].name, 'x') + st.equal(node.nodes[0].typeDescriptions.typeString, 'int256') + st.equal(node.nodes[1].name, 'y') + st.equal(node.nodes[1].typeDescriptions.typeString, 'int256') + st.equal(node.nodes[2].nodeType, 'FunctionDefinition') + st.equal(node.nodes[2].stateMutability, 'nonpayable') + st.equal(node.nodes[2].name, 'set') + st.equal(node.nodes[2].visibility, 'public') } function checkSetFunction (st, node) { - if (node.attributes.name === 'set') { - st.equal(node.children[0].name, 'ParameterList') - st.equal(node.children[1].name, 'ParameterList') - st.equal(node.children[2].name, 'Block') - st.equal(node.children[2].children[1].name, 'ExpressionStatement') - checkExpressionStatement(st, node.children[2].children[0]) + if (node.name === 'set') { + st.equal(node.parameters.nodeType, 'ParameterList') + st.equal(node.returnParameters.nodeType, 'ParameterList') + st.equal(node.body.nodeType, 'Block') + st.equal(node.body.statements[0].nodeType, 'ExpressionStatement') + checkExpressionStatement(st, node.body.statements[0]) } } function checkExpressionStatement (st, node) { - st.equal(node.children[0].name, 'Assignment') - st.equal(node.children[0].attributes.operator, '=') - st.equal(node.children[0].attributes.type, 'int256') - st.equal(node.children[0].children[0].name, 'Identifier') - st.equal(node.children[0].children[0].attributes.value, 'x') - st.equal(node.children[0].children[1].name, 'Identifier') - st.equal(node.children[0].children[1].attributes.value, '_x') + st.equal(node.expression.nodeType, 'Assignment') + st.equal(node.expression.operator, '=') + st.equal(node.expression.typeDescriptions.typeString, 'int256') + st.equal(node.expression.leftHandSide.nodeType, 'Identifier') + st.equal(node.expression.leftHandSide.name, 'x') + st.equal(node.expression.rightHandSide.nodeType, 'Identifier') + st.equal(node.expression.rightHandSide.name, '_x') } diff --git a/libs/remix-debug/test/resources/ast.js b/libs/remix-debug/test/resources/ast.js index b8a4650b44..c3912bb9d1 100644 --- a/libs/remix-debug/test/resources/ast.js +++ b/libs/remix-debug/test/resources/ast.js @@ -2,6 +2,185 @@ const node = {} node.ast = {"legacyAST":{"children":[{"attributes":{"fullyImplemented":true,"isLibrary":false,"linearizedBaseContracts":[5640396],"name":"test"},"children":[{"attributes":{"name":"x","type":"int256"},"children":[{"attributes":{"name":"int"},"id":5657860,"name":"ElementaryTypeName","src":"21:3:11"}],"id":5658100,"name":"VariableDeclaration","src":"21:5:11"},{"attributes":{"name":"y","type":"int256"},"children":[{"attributes":{"name":"int"},"id":5658180,"name":"ElementaryTypeName","src":"38:3:11"}],"id":5658268,"name":"VariableDeclaration","src":"38:5:11"},{"attributes":{"constant":false,"name":"set","public":true},"children":[{"children":[{"attributes":{"name":"_x","type":"int256"},"children":[{"attributes":{"name":"int"},"id":5658404,"name":"ElementaryTypeName","src":"68:3:11"}],"id":5658492,"name":"VariableDeclaration","src":"68:6:11"}],"id":5658572,"name":"ParameterList","src":"67:8:11"},{"children":[{"attributes":{"name":"_r","type":"int256"},"children":[{"attributes":{"name":"int"},"id":5658628,"name":"ElementaryTypeName","src":"85:3:11"}],"id":5658716,"name":"VariableDeclaration","src":"85:6:11"}],"id":5658796,"name":"ParameterList","src":"84:8:11"},{"children":[{"children":[{"attributes":{"operator":"=","type":"int256"},"children":[{"attributes":{"type":"int256","value":"x"},"id":5658900,"name":"Identifier","src":"108:1:11"},{"attributes":{"type":"int256","value":"_x"},"id":5658980,"name":"Identifier","src":"112:2:11"}],"id":5657492,"name":"Assignment","src":"108:6:11"}],"id":5659028,"name":"ExpressionStatement","src":"108:6:11"},{"children":[{"attributes":{"operator":"=","type":"int256"},"children":[{"attributes":{"type":"int256","value":"y"},"id":5659116,"name":"Identifier","src":"125:1:11"},{"attributes":{"string":null,"type":"int_const 10","value":"10"},"id":5659196,"name":"Literal","src":"129:2:11"}],"id":5659252,"name":"Assignment","src":"125:6:11"}],"id":5659316,"name":"ExpressionStatement","src":"125:6:11"},{"children":[{"attributes":{"operator":"=","type":"int256"},"children":[{"attributes":{"type":"int256","value":"_r"},"id":5659428,"name":"Identifier","src":"141:2:11"},{"attributes":{"type":"int256","value":"x"},"id":5639308,"name":"Identifier","src":"146:1:11"}],"id":5639356,"name":"Assignment","src":"141:6:11"}],"id":5639420,"name":"ExpressionStatement","src":"141:6:11"}],"id":5639516,"name":"Block","src":"97:57:11"}],"id":5639612,"name":"FunctionDefinition","src":"55:99:11"},{"attributes":{"constant":false,"name":"get","public":true},"children":[{"children":[],"id":5639764,"name":"ParameterList","src":"179:2:11"},{"children":[{"attributes":{"name":"x","type":"uint256"},"children":[{"attributes":{"name":"uint"},"id":5639820,"name":"ElementaryTypeName","src":"191:4:11"}],"id":5639908,"name":"VariableDeclaration","src":"191:6:11"},{"attributes":{"name":"y","type":"uint256"},"children":[{"attributes":{"name":"uint"},"id":5639988,"name":"ElementaryTypeName","src":"199:4:11"}],"id":5640076,"name":"VariableDeclaration","src":"199:6:11"}],"id":5640156,"name":"ParameterList","src":"190:16:11"},{"children":[],"id":5640212,"name":"Block","src":"212:17:11"}],"id":5640276,"name":"FunctionDefinition","src":"167:62:11"}],"id":5640396,"name":"ContractDefinition","src":"0:231:11"}],"name":"SourceUnit"}} +node.ast.ast = { + absolutePath: 'sample.sol', + exportedSymbols: { test: [ 33 ] }, + id: 34, + nodeType: 'SourceUnit', + nodes: + [ { + abstract: false, + baseContracts: [], + contractDependencies: [], + contractKind: 'contract', + documentation: null, + fullyImplemented: true, + id: 33, + linearizedBaseContracts: [ 33 ], + name: 'test', + nodeType: 'ContractDefinition', + nodes: + [ { constant: false, + id: 2, + name: 'x', + nodeType: 'VariableDeclaration', + overrides: null, + scope: 33, + src: '20:5:0', + stateVariable: true, + storageLocation: 'default', + typeDescriptions: { typeIdentifier: 't_int256', typeString: 'int256' }, + typeName: { + id: 1, + name: 'int', + nodeType: 'ElementaryTypeName', + src: '20:3:0', + typeDescriptions: [Object] + }, + value: null, + visibility: 'internal' }, + { constant: false, + id: 4, + name: 'y', + nodeType: 'VariableDeclaration', + overrides: null, + scope: 33, + src: '31:5:0', + stateVariable: true, + storageLocation: 'default', + typeDescriptions: { typeIdentifier: 't_int256', typeString: 'int256' }, + typeName: + { id: 3, + name: 'int', + nodeType: 'ElementaryTypeName', + src: '31:3:0', + typeDescriptions: [Object] }, + value: null, + visibility: 'internal' }, + { body: { id: 23, + nodeType: 'Block', + src: '96:55:0', + statements: + [ { expression: + { argumentTypes: null, + id: 13, + isConstant: false, + isLValue: false, + isPure: false, + lValueRequested: false, + leftHandSide: + { argumentTypes: null, + id: 11, + name: 'x', + nodeType: 'Identifier', + overloadedDeclarations: [], + referencedDeclaration: 2, + src: '106:1:0', + typeDescriptions: { typeIdentifier: 't_int256', typeString: 'int256' } }, + nodeType: 'Assignment', + operator: '=', + rightHandSide: + { argumentTypes: null, + id: 12, + name: '_x', + nodeType: 'Identifier', + overloadedDeclarations: [], + referencedDeclaration: 6, + src: '110:2:0', + typeDescriptions: { typeIdentifier: 't_int256', typeString: 'int256' } }, + src: '106:6:0', + typeDescriptions: { typeIdentifier: 't_int256', typeString: 'int256' } + }, + id: 14, + nodeType: 'ExpressionStatement', + src: '106:6:0' }, + { expression: + { argumentTypes: null, + id: 17, + isConstant: false, + isLValue: false, + isPure: false, + lValueRequested: false, + leftHandSide: [Object], + nodeType: 'Assignment', + operator: '=', + rightHandSide: [Object], + src: '122:6:0', + typeDescriptions: [Object] }, + id: 18, + nodeType: 'ExpressionStatement', + src: '122:6:0' }, + { expression: + { argumentTypes: null, + id: 21, + isConstant: false, + isLValue: false, + isPure: false, + lValueRequested: false, + leftHandSide: [Object], + nodeType: 'Assignment', + operator: '=', + rightHandSide: [Object], + src: '138:6:0', + typeDescriptions: [Object] }, + id: 22, + nodeType: 'ExpressionStatement', + src: '138:6:0' } ] + }, + documentation: null, + functionSelector: 'e5c19b2d', + id: 24, + implemented: true, + kind: 'function', + modifiers: [], + name: 'set', + nodeType: 'FunctionDefinition', + overrides: null, + parameters: { id: 7, + nodeType: 'ParameterList', + parameters: [Array], + src: '59:8:0' }, + returnParameters: { id: 10, + nodeType: 'ParameterList', + parameters: [Array], + src: '83:8:0' }, + scope: 33, + src: '47:104:0', + stateMutability: 'nonpayable', + virtual: false, + visibility: 'public' }, + { body: { id: 31, nodeType: 'Block', src: '214:17:0', statements: [] }, + documentation: null, + functionSelector: '6d4ce63c', + id: 32, + implemented: true, + kind: 'function', + modifiers: [], + name: 'get', + nodeType: 'FunctionDefinition', + overrides: null, + parameters: + { id: 25, + nodeType: 'ParameterList', + parameters: [], + src: '175:2:0' }, + returnParameters: + { id: 30, + nodeType: 'ParameterList', + parameters: [Array], + src: '193:16:0' }, + scope: 33, + src: '163:68:0', + stateMutability: 'nonpayable', + virtual: false, + visibility: 'public' } ], + scope: 34, + src: '0:233:0' + } ], + src: '0:233:0' +} + + node.source = `contract test { int x;