-
Notifications
You must be signed in to change notification settings - Fork 1
/
getNewVersionDTFile.js
363 lines (348 loc) · 16.3 KB
/
getNewVersionDTFile.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Created by Pankajan on 11/09/2016.
*/
var parser = require('./definitionParser');
var ranTestGen = require('./randomTestGenerator');
var ts = require('typescript');
var fs = require('fs');
var defFilePath;
var projectName;
var definitionStructure;
var source;
var removingIndexesMap = [];
var removingIndexes = [];
var addingMethods = {};
function checkMethod(object, method, isFunction) {
if(object!==undefined && method!==undefined) {
if (isFunction) {
if (!(typeof object === 'function' || object[method.name] !== undefined)) {
removeCodeSegment(method.pos, method.end);
}
} else if (object[method.name] === undefined) {
removeCodeSegment(method.pos, method.end);
}
}
}
function removeCodeSegment(start, end) {
if (removingIndexesMap.indexOf(start + '_' + end) === -1) {
removingIndexes.push(start);
removingIndexes.push(end);
removingIndexesMap.push(start + '_' + end);
}
}
function checkNewMethods(object, methods, isStatic) {
for (var prop in object) {
if(object.hasOwnProperty(prop)) {
currentProperty = object[prop];
if(typeof currentProperty === 'function') {
var methodName = currentProperty.name;
if(methodName===undefined || methodName==='') {methodName = prop;}
var variableName = {};
var methodExists = false;
for(var i=0; i<methods.length; i++) {
for(var j=0; j<methods[i].arguments.length;j++) {
variableName[methods[i].arguments[j].name] = methods[i].arguments[j].type.rawName
}
if(currentProperty.name===undefined || currentProperty.name==='') {
if(methods[i].name===prop) {
methodExists = true;
}
} else if(methods[i].name===currentProperty.name) {
methodExists = true;
}
}
if(!methodExists && methodName!==undefined) {
var lastMethodDetected = false;
j=methods.length-1;
while(!lastMethodDetected && j>=0){
if(removingIndexes.indexOf(methods[j].end)===-1) {
var curKey = source.substring(ts.skipTrivia(source, methods[j].pos), methods[j].end);
if(addingMethods.hasOwnProperty(curKey)) {
if(addingMethods[curKey].indexOf(methodName)===-1) {
addingMethods[curKey] = addingMethods[curKey] +
getNewMethod(methodName, currentProperty, variableName, isStatic);
}
} else {
addingMethods[curKey] =
getNewMethod(methodName, currentProperty, variableName, isStatic);
}
lastMethodDetected = true;
}
j--;
}
}
}
}
}
}
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var ARGUMENT_NAMES = /([^\s,]+)/g;
function getParamNames(func) {
var fnStr = func.toString().replace(STRIP_COMMENTS, '');
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
if(result === null)
result = [];
return result;
}
function getNewMethod(methodName, method, existingNames, isStatic) {
var params = getParamNames(method);
var methodBuilder = methodName + '(';
if(isStatic) {methodBuilder = 'static ' + methodBuilder};
for (var i=0; i<params.length;i++){
var name = existingNames.hasOwnProperty(params[i]) ? existingNames[params[i]] : 'any';
methodBuilder += params[i] + ': ' + name;
if(i<params.length-1) {methodBuilder += ','}
}
return '\n' + methodBuilder + '): any;\n';
}
function generateNewSource() {
var buildSource;
if(removingIndexes.length>0) {
removingIndexes.sort(function (a, b) {
return a - b;
});
buildSource = source.substring(0, removingIndexes[0]);
for (var i = 1; i < removingIndexes.length; i = i + 2) {
buildSource += source.substring(removingIndexes[i], removingIndexes[i + 1]);
}
} else {
buildSource = source;
}
for (var prop in addingMethods) {
if (addingMethods.hasOwnProperty(prop)) {
var index = buildSource.indexOf(prop);
if(index!==-1) {
index += prop.length;
buildSource = buildSource.substring(0, index) + addingMethods[prop] + buildSource.substring(index);
}
}
}
return buildSource;
}
function bootstrapRandomTesting(moduleName) {
var mName = moduleName.replace(/-/g, "").replace(/\./g, "");
var createdTestSource = '';
var executingModule = require(moduleName);
/**
* Check if current eecuting library is loaded as an object. In that case, all public api will be in that object and no need to create any objects
*/
if (typeof executingModule === 'object') {
//Library initialized a object so we can directly access methods
var methods = definitionStructure.methods;
if (methods === undefined) {
console.log();
} else {
for (var i = 0; i < methods.length; i++) {
if (ranTestGen.isValidArguments(methods[i].arguments)) {
var kind = undefined;
var isArray = undefined;
if (ranTestGen.validReturnType(methods[i].returnType)) {
kind = methods[i].returnType.properties.kind;
isArray = methods[i].returnType.array;
}
checkMethod(executingModule, methods[i]);
}
}
checkNewMethods(executingModule, methods);
}
}
/**
* If isMethod property is true in the retrieved structure of ts definition, then at least one method is available to call by default from library without using any method name (like library(params))
*/
else if (definitionStructure.isMethod && definitionStructure.methods !== undefined) {
//Library is either contains only one method which is exposed or should create an object using a construtor before using it.
methods = definitionStructure.methods;
for (i = 0; i < methods.length; i++) {
//TODO if new keyword is in method add it.
checkMethod(executingModule, methods[i], true);
}
checkNewMethods(executingModule, methods);
}
/**
* Constructors available means, we have to create an object before use the library
*/
else if (definitionStructure.constructors.length > 0) {
if (definitionStructure.methods.length > 0) {
for (i = 0; i < definitionStructure.constructors.length; i++) {
var curConst = definitionStructure.constructors[i];
if (ranTestGen.isValidArguments(curConst.arguments)) {
skipModules = ['big.js', 'axios', 'jquery', 'moment'];
var moduleObj;
var createdObj = true;
if (skipModules.indexOf(moduleName) !== -1) {
moduleObj = executingModule;
//createdTestSource += 'var '+ mName+ 'Obj = ' + mName + ';\n';
} else {
try {
moduleObj = executingModule.apply(ranTestGen.generateRandomArgumentsArray(curConst.arguments));
//createdTestSource += wrapTryCatch('var '+ mName+ 'Obj = new ' + mName + generateRandomArguments(curConst.arguments) + ";");
} catch (err) {
createdObj = false;
}
}
if (createdObj) {
/**
* load available methods to execute by looking into the retrieved definition structure.
*/
if (skipModules.indexOf(moduleName) !== -1 || curConst.returnType === 'void') {
var curMethods = definitionStructure.methods;
} else {
curMethods = [];
for (j = 0; j < definitionStructure.classes.length; j++) {
if (curConst.returnType.properties !== undefined &&
typeof curConst.returnType.properties.kind === "string" &&
curConst.returnType.properties.kind.toLowerCase() === definitionStructure.classes[j].name.toLowerCase()) {
curMethods = definitionStructure.classes[j].methods;
}
}
}
for (j = 0; j < curMethods.length; j++) {
if (ranTestGen.isValidArguments(curMethods[j].arguments)) {
methodName = curMethods[j].name;
if (curMethods[j].isStatic) {
kind = undefined;
isArray = undefined;
if (ranTestGen.validReturnType(curMethods[j].returnType)) {
kind = curMethods[j].returnType.properties.kind;
isArray = curMethods[j].returnType.array;
}
checkMethod(executingModule, curMethods[j]);
//createdTestSource += wrapTryCatch(kind, isArray, "variableName = " + mName + '.' + methodName + generateRandomArguments(curMethods[j].arguments) + ";");
} else {
kind = undefined;
isArray = undefined;
if (ranTestGen.validReturnType(curMethods[j].returnType)) {
kind = curMethods[j].returnType.properties.kind;
isArray = curMethods[j].returnType.array;
}
checkMethod(moduleObj, curMethods[j]);
//createdTestSource += wrapTryCatch(kind, isArray, "variableName = " + mName + 'Obj.' + methodName + generateRandomArguments(curMethods[j].arguments) + ";");
}
}
}
checkNewMethods(executingModule, curMethods, true);
checkNewMethods(moduleObj, curMethods);
}
}
}
}
} else if (definitionStructure.methods.length > 0) {
if (definitionStructure.methods.length === 1) {
kind = undefined;
isArray = undefined;
if (ranTestGen.validReturnType(definitionStructure.methods[0].returnType)) {
kind = definitionStructure.methods[0].returnType.properties.kind;
isArray = definitionStructure.methods[0].returnType.array;
}
checkMethod(executingModule, methods[0], true);
checkNewMethods(executingModule, methods);
//createdTestSource += wrapTryCatch(kind, isArray, "variableName = " + mName + generateRandomArguments(definitionStructure.methods[0].arguments) + ";");
} else {
for (j = 0; j < definitionStructure.methods.length; j++) {
kind = undefined;
isArray = undefined;
if (ranTestGen.validReturnType(definitionStructure.methods[j].returnType)) {
kind = definitionStructure.methods[j].returnType.properties.kind;
isArray = definitionStructure.methods[j].returnType.array;
}
checkMethod(executingModule, definitionStructure.methods[j]);
//createdTestSource += wrapTryCatch(kind, isArray, "variableName = " + mName + '.' + definitionStructure.methods[j].name + generateRandomArguments(definitionStructure.methods[j].arguments) + ";");
}
checkNewMethods(executingModule, definitionStructure.methods);
}
} else if (definitionStructure.classes.length === 1) {
processSingleClass(definitionStructure.classes[0], executingModule);
var selectedClass = definitionStructure.classes[0];
moduleObj = executingModule;
if(definitionStructure.classes[0].constructors.length > 0) {
moduleObj = executingModule.apply(ranTestGen.generateRandomArgumentsArray(definitionStructure.classes[0].constructors[0].arguments));
}
for (j = 0; j < selectedClass.methods.length; j++) {
checkMethod(moduleObj, selectedClass.methods[j]);
//generateRandomArguments(selectedClass.methods[j].arguments);
}
checkNewMethods(moduleObj, selectedClass.methods[j]);
} else {
var processed = false;
i = 0;
while (i < definitionStructure.classes.length && !processed) {
if (moduleName.toLowerCase() === definitionStructure.classes[i].name.toLowerCase()) {
selectedClass = definitionStructure.classes[i];
for (j = 0; j < selectedClass.methods.length; j++) {
processSingleClass(selectedClass, executingModule);
//generateRandomArguments(selectedClass.methods[j].arguments);
}
processed = true;
}
i++;
}
if (!processed) {
selectedClass = undefined;
var maxMethods = 0;
for (i = 0; i < definitionStructure.classes.length; i++) {
if (definitionStructure.classes[i].constructors.length > 0) {
if (definitionStructure.classes[i].methods.length > maxMethods) {
maxMethods = definitionStructure.classes[i].methods.length;
selectedClass = definitionStructure.classes[i];
}
processed = true;
}
}
if (selectedClass) {
processSingleClass(selectedClass, executingModule);
}
}
if (!processed) {
console.log();
}
}
return createdTestSource;
}
function processSingleClass(selectedClass, executingModule) {
var moduleObj = executingModule;
if(definitionStructure.classes[0].constructors.length > 0) {
moduleObj = executingModule.apply(ranTestGen.generateRandomArgumentsArray(definitionStructure.classes[0].constructors[0].arguments));
}
for (j = 0; j < selectedClass.methods.length; j++) {
checkMethod(moduleObj, selectedClass.methods[j]);
//generateRandomArguments(selectedClass.methods[j].arguments);
}
checkNewMethods(moduleObj, selectedClass.methods[j]);
}
function loadDefFile(moduleName, defFilePath) {
definitionStructure = parser.getFunctions(moduleName, defFilePath);
source = String(fs.readFileSync(defFilePath));
projectName = moduleName;
}
function saveNewSourceFile() {
console.log();
}
function createNewVersionDTFile(moduleName, dfPath) {
loadDefFile(moduleName, dfPath);
if (definitionStructure) {
ranTestGen.setDefinitionStructure(definitionStructure);
bootstrapRandomTesting(moduleName);
return generateNewSource();
}
}
module.exports = {
createNewVersionDTFile: createNewVersionDTFile
};
if (!module.parent) {
// this is the main module
var args = process.argv.slice(2);
if(args.length!==2 && args.length!==3) {
console.log('Usage: <moduleName> <definitionFilepath>');
} else {
var newSource = createNewVersionDTFile(args[0], args[1]);
var newFilePath;
if(args.length===2) {
var path = require('path');
var fileName = path.basename(args[1]);
fileName = fileName.substring(0, fileName.indexOf('.d.ts')) + '_new.d.ts';
newFilePath = path.join(path.dirname(args[1]), fileName);
} else
newFilePath = args[2];
fs.writeFileSync(newFilePath, newSource);
}
}