-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-tdd.js
More file actions
81 lines (68 loc) · 2.39 KB
/
debug-tdd.js
File metadata and controls
81 lines (68 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function extractFunctionNames(code) {
const functionRegex =
/(?:async\s+)?function\s*\*?\s*(\w+)|const\s+(\w+)\s*=\s*(?:async\s+)?(?:\([^)]*\)\s*=>|function\s*\*?)|(\w+)\s*:\s*(?:\([^)]*\)\s*=>|function)/g;
const names = [];
let match;
while ((match = functionRegex.exec(code)) !== null) {
const name = match[1] || match[2] || match[3];
if (name) names.push(name);
}
return names;
}
// eslint-disable-next-line @typescript-eslint/no-require-imports
const problem = require('./lib/problems/test-driven-development.ts').problem;
const code = problem.solution;
const functionNames = extractFunctionNames(code);
console.log('Extracted function names:', functionNames);
// Simulate what the test runner does
const mockConsole = { log: () => {}, error: () => {}, warn: () => {} };
try {
const safeEval = new Function(
'console',
`
try {
${code}
} catch (e) {}
const functions = {};
${functionNames
.map(
(name) =>
`try {
if (typeof ${name} !== 'undefined') {
functions['${name}'] = ${name};
} else {
functions['${name}'] = null;
}
} catch(e) {
functions['${name}'] = null;
}`
)
.join('\n')}
return functions;
`
);
const functions = safeEval(mockConsole);
console.log('\nFunctions object keys:', Object.keys(functions));
const availableFunctions = Object.keys(functions).filter(
(name) => functions[name] !== null && typeof functions[name] === 'function'
);
console.log('\nAvailable functions (actually callable):', availableFunctions);
// Check which function would be matched for description "testToBeTruthy returns true for truthy value"
const testDescription = 'testToBeTruthy returns true for truthy value';
const matchingFunction = availableFunctions.find((fn) =>
testDescription.toLowerCase().startsWith(fn.toLowerCase())
);
console.log('\nFor description "' + testDescription + '":');
console.log(' Matching function:', matchingFunction);
if (matchingFunction && functions[matchingFunction]) {
console.log(' Function exists:', typeof functions[matchingFunction]);
try {
const result = functions[matchingFunction](1);
console.log(' Result of calling with (1):', result);
} catch (e) {
console.log(' Error calling function:', e.message);
}
}
} catch (e) {
console.log('Error:', e.message);
}