// Test generated code
const testGeneratedCode = async (code, testCases) => {
const vm = new VM({
timeout: 5000,
sandbox: {}
});
const results = [];
for (const testCase of testCases) {
try {
// Create a function from the code
const fn = vm.run(`(function(input) { ${code} })`);
// Execute the function with the test case input
const result = fn(testCase.input);
// Check if the result matches the expected output
const passed = JSON.stringify(result) === JSON.stringify(testCase.expected);
results.push({
testCase,
result,
passed
});
} catch (error) {
results.push({
testCase,
error: error.message,
passed: false
});
}
}
return {
passed: results.every(r => r.passed),
results
};
};
// Example usage
const code = '// Generated code here';
const testCases = [
{ input: [1, 2, 3], expected: 6 },
{ input: [4, 5, 6], expected: 15 }
];
const testResults = await testGeneratedCode(code, testCases);