jeudi 31 décembre 2015

Local Variable Unit Testing

I am coding in C++, and am unit testing a function. This function has a set back though, as I am testing it from an outside source file using GoogleTest. I can't think of way of testing a certain local variable inside it. The function creates a class pointer variable inside itself and deletes it at the end of the function. If I move the local variable declaration into the .h for the overall class, it works just fine and I can do what ever I want to the local variable regardless of it being deleted in the end. So My question is, given that I am not allowed to alter the original setup, is there a way that anyone knows of to access the info from a local variable via some tricky logic or programming voodoo you guys know of? Any advice is appreciated! Also the pointer object in question points to methods in its class and does data manips via input. Ex: pointerA->setWidth(int, int); I need to get to this pointerA in general.

foo(int number, int num) {
   classA* pointerA = new classA;  
   pointerA->setWidth(number, num);  
   //other data manipulations  
   delete pointerA;
}

Example of Test:

EXPECT_EQ(pointerA->setWidth(returns width), 2); //checks to see if it returns correct value of 2

Also the reason I want to be able to do it without having to alter any code is because I'm hoping to let it be automated and run the test every time the code is compiled. It's how I have it set up for all my other tests, but they don't have local vars.

Aucun commentaire:

Enregistrer un commentaire