-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestFunctionCallsAppender.js
More file actions
182 lines (163 loc) · 4.98 KB
/
TestFunctionCallsAppender.js
File metadata and controls
182 lines (163 loc) · 4.98 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const fs = require("fs");
function generateFunctionCalls(testCase, functionSource) {
const functionName = testCase.name;
const condition = testCase.conditionName;
const trueValues = extractValues(testCase, "true");
const falseValues = extractValues(testCase, "false");
// Extracting variables from the condition
const variables = condition.match(/[a-zA-Z_]\w*/g);
// parameter names from the function signature
const parameterNames = functionSource
.toString()
.match(/\(([^)]+)\)/)[1]
.split(",")
.map((param) => param.trim());
// array to store the function calls
const functionCalls = [];
if (testCase.trueValue) {
var callString = generateCallString(
testCase,
functionName,
parameterNames,
variables,
trueValues
);
functionCalls.push(callString);
callString = generateCallString(
testCase,
functionName,
parameterNames,
variables,
falseValues
);
functionCalls.push(callString);
} else {
var callString = generateCallString(
testCase,
functionName,
parameterNames,
variables,
falseValues
);
functionCalls.push(callString);
callString = generateCallString(
testCase,
functionName,
parameterNames,
variables,
trueValues
// );
);
functionCalls.push(callString);
}
return functionCalls;
}
function extractValues(testCase, type) {
const values = [];
const suffixes = ["Left", "Right"];
suffixes.forEach((suffix) => {
const key = `${type}Value${suffix}`;
const simplekey = `${type}Value`;
if (testCase.hasOwnProperty(key)) {
values.push(testCase[key]);
complex = true;
} else if (testCase.hasOwnProperty(simplekey)) {
simple = true;
values.push(testCase[simplekey]);
}
});
return values;
}
function generateCallString(
testCase,
functionName,
parameterNames,
variables,
falseValues
) {
var f = 0;
var assign = false;
const paramValues = {};
for (let i = 0; i < parameterNames.length; i++) {
if (variables.includes(parameterNames[i])) {
assign = false;
for (let k = 0; k < variables.length; k++) {
console.log(falseValues);
if (parameterNames[i] === variables[k]) {
if (falseValues[f] && assign === false) {
if (typeof falseValues[f] === "string") {
paramValues[parameterNames[i]] = `"${falseValues[f]}"`;
} else {
paramValues[parameterNames[i]] = falseValues[f];
}
f++;
assign = true;
}
}
}
} else {
paramValues[parameterNames[i]] = Math.random();
}
}
// Generate the function call string
const callString = `${functionName}(${parameterNames
.map((param) => paramValues[param])
.join(", ")});`;
console.log(callString);
return callString;
}
// Reading the content of the file containing the code to be tested
const functions = require("./test");
// Reading test cases from the JSON file
const testCases = require("./testcases.json");
const fileName = "test.js";
let fileContent = "";
//return true if file exists
if (fs.existsSync(fileName)) {
fileContent = fs.readFileSync(fileName, "utf-8");
}
const delimiter = "//---------Test cases below---------//";
const delimiterIndex = fileContent.indexOf(delimiter);
// If the delimiter is found, remove everything below it
if (delimiterIndex !== -1) {
fileContent = fileContent.substring(0, delimiterIndex);
}
testCases.forEach((testCase) => {
// Find the function in the 'functions' object
const testFunction = functions[testCase.name];
if (testFunction) {
// Generating function calls for the test case
const functionCalls = generateFunctionCalls(testCase, testFunction);
// Append function calls to the existing fileContent
fileContent += `\n//---------Test cases below---------//\n${functionCalls.join(
"\n"
)}`;
} else {
console.error(`Function ${testCase.name} not found.`);
}
});
// Check for functions without test cases
Object.keys(functions).forEach((functionName) => {
const testFunction = functions[functionName];
if (!testCases.some((testCase) => testCase.name === functionName)) {
// No test case found for this function, assign random values to its parameters
const parameterNames = testFunction
.toString()
.match(/\(([^)]+)\)/)[1]
.split(",")
.map((param) => param.trim());
const randomValues = {};
parameterNames.forEach((param) => {
randomValues[param] = Math.random();
});
// Generate function call with random values
const callString = `${functionName}(${parameterNames
.map((param) => randomValues[param])
.join(", ")});`;
// Append the function call to the existing fileContent
fileContent += `\n//---------Test cases below---------//\n${callString}`;
}
});
// Writing the modified content back to the file
fs.writeFileSync(fileName, fileContent);
console.log(`Function calls have been appended to ${fileName}`);