I am working on a project which teaches JavaScript techniques (like Code Academy). Mainly I do this for self education purposes.
I have difficulties with one important part: As the whole project is a web project, the user types in his code directly into the web site. My problem is, how do I check if the submitted code is passes all tests for the current exercise? My approach would be to eval the code and run my unit tests against it. The thing here is, how do I limit the scope for the eval'ed code and how can I test it? I already found a solution here about how to limit the scope:
function maskedEval(scr)
{
// set up an object to serve as the context for the code
// being evaluated.
var mask = {};
// mask global properties
for (p in this)
mask[p] = undefined;
// execute script in private context
(new Function( "with(this) { " + scr + "}")).call(mask);
}
But I don't have a solution how to run my test against the evaluated code. Is this the correct way to do this or should I do it completely different?