Creates unit test harnesses for legacy code
npm install runtime-spiesjs
var helper1 = function (x) {
globalVar = 2 * x
return 2 * x
}
var helper2 = function (x) { return 3 * x }
var globalVar = 5
var globalVar2 = { 1: 6, 2: 2 }
var b = { 1: 1, 2: globalVar2 }
globalVar2['3'] = b
globalVar2['4'] = { 1: 4, 12: function (x) { return 5 * x } }
`
The function we want to harness:
`js
var testFunction = function (A) {
helper1(21)
var a = globalVar2['4']'12'
var result = a+helper1(A) + helper2(A) + globalVar + globalVar2['3']['2']['1'] + globalVar2['4']'12'
return result
}
`
We add our code to the function
`js
var testFunction = function (A) {
// We've added the following lines to generate the harness
var mySpy = new RuntimeSpy('mySpy') //the main spy object
mySpy.setTestFunctionCall("testFunction(A)") //setting how to call the test function
eval(mySpy.addGlobalVariablesSpies({ A: A, globalVar: globalVar, globalVar2: globalVar2, helper1: helper1, helper2: helper2 }).getCodeToEvalToSpyOnVariables()) //spying on variables
//end of setup
helper1(21)
var a = globalVar2['4']'12'
var result = a+helper1(A) + helper2(A) + globalVar + globalVar2['3']['2']['1'] + globalVar2['4']'12'
mySpy.addFinalResult(result) //here we tell the spy what is the end result so it can later assert on it
harness = mySpy.getHarness() //generating the harness
return result
}
`
Now we run the function with our code in it. And this is the harness the RuntimeSpy generates:
`js
var myHarness = new Harness('myHarness') //the main object managing the operation
var mockRepositoryData = {} //This is the "database" of the mock functions
mockRepositoryData['globalVar2[\'4\'][\'12\']'] = {input:[[3],[4]],output:[15,20]}
mockRepositoryData['helper1'] = {input:[[21],[5]],output:[42,10]}
//helper1 was called twice. At the first time there was one input, 21, and the output was 42. At the second time it was 5 and 10.
mockRepositoryData['helper2'] = {input:[[5]],output:[15]}
myHarness.setMockRepositoryData(mockRepositoryData)
//load the harness object with this information
A_DB = new Map([['Initial','A = 5']]) //"database" for global variable A. It had one value throughout the program's run: 5
var A
myHarness.addGlobalVariableMock('A',A_DB) //load A to the harness object
globalVar_DB = new Map([['Initial','globalVar = 5'],['helper1_0','globalVar = 42'],['helper1_1','globalVar = 10']])
var globalVar
myHarness.addGlobalVariableMock('globalVar',globalVar_DB)
globalVar2_DB = new Map([['Initial','globalVar2 = {1:6,2:2,3:{1:1},4:{1:4,12:function(){}}};globalVar2[\'3\'][\'2\']=globalVar2']])
var globalVar2
myHarness.addGlobalVariableMock('globalVar2',globalVar2_DB)
myHarness.updateVariablesByTag('Initial',function(codeToEval){eval(codeToEval)})
//Here, the global variables (A, globalVar and globalVar2 are updated to to have the first value (tag == "Initial"))
myHarness.addFunctionMock('globalVar2[\'4\'][\'12\']')
//loading the global function, globalVar2['4']['12'] to the harness object. The below is the definition of the function
globalVar2['4']['12']= function(){
var returnValue = myHarness.callFunctionSpy('globalVar2[\'4\'][\'12\']',arguments,function(codeToEval){eval(codeToEval)})
if (returnValue!='NOVALUERETURNED')return eval(returnValue)
}
myHarness.addFunctionMock('helper1')
helper1= function(){
var returnValue = myHarness.callFunctionSpy('helper1',arguments,function(codeToEval){eval(codeToEval)})
if (returnValue!='NOVALUERETURNED')return eval(returnValue)
}
myHarness.addFunctionMock('helper2')
helper2= function(){
var returnValue = myHarness.callFunctionSpy('helper2',arguments,function(codeToEval){eval(codeToEval)})
if (returnValue!='NOVALUERETURNED')return eval(returnValue)
}
expect(VariableLiteral.getVariableLiteral(testFunction(A)).getLiteralAndCyclicDefinition('result')).equals('result = 76')
//here the program is called and the result is asserted
``