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-debug/test/astwalker.js

62 lines
2.0 KiB

8 years ago
'use strict'
const tape = require('tape')
const AstWalker = require('../src/source/astWalker')
const node = require('./resources/ast')
8 years ago
tape('ASTWalker', function (t) {
t.test('ASTWalker.walk', function (st) {
st.plan(24)
const astwalker = new AstWalker()
8 years ago
astwalker.walk(node.ast.ast, function (node) {
if (node.nodeType === 'ContractDefinition') {
8 years ago
checkContract(st, node)
}
if (node.nodeType === 'FunctionDefinition') {
8 years ago
checkSetFunction(st, node)
}
return true
})
const callback = {}
8 years ago
callback.FunctionDefinition = function (node) {
st.equal(node.nodeType, 'FunctionDefinition')
st.equal(node.name === 'set' || node.name === 'get', true)
8 years ago
return true
}
astwalker.walk(node.ast.ast, callback)
8 years ago
})
})
function checkContract (st, node) {
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')
8 years ago
}
function checkSetFunction (st, node) {
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])
8 years ago
}
}
function checkExpressionStatement (st, node) {
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')
8 years ago
}