forked from aca-mobile/ti-unit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockcontroller.js
More file actions
95 lines (71 loc) · 2.73 KB
/
mockcontroller.js
File metadata and controls
95 lines (71 loc) · 2.73 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
function mockController(controllerPath){
if(!controllerPath){
console.warn('Please specify path to a controller');
return;
}
var fs = require('fs');
var controllerSourceCode = fs.readFileSync(controllerPath);
var dollar;
dollar = {};
if(false) {
console.info(`Current directory: ${process.cwd()}`);
console.info('This is the source code of the controller: ' + controllerSourceCode);
}
// matches all functions only
// \$(.[a-zA-Z.]+[(]+)+
// matches all properties (followed by space or =) only
// \$(.[a-zA-Z.]+[\s=])+
// original regex
// var re = /\$(.[a-zA-Z]+)+/g;
var re = /\$(.[a-zA-Z]+)+/g;
var reFunctions = /\$(.[a-zA-Z.]+[(]+)+/g ;
var reProperties = /\$(.[a-zA-Z.]+[\s=])+/g ;
var usedProperties = _findAndReturnMatchesArray(controllerSourceCode, reProperties);
var usedFunctions = _findAndReturnMatchesArray(controllerSourceCode, reFunctions);
usedProperties.forEach(function(item){
if (item.indexOf('$.') == -1) return;
var name = item.slice(2);
var namespaces = name.split('.');
var firstNamespace = namespaces.shift();
if(firstNamespace != undefined){
_pushNamespaces(dollar, firstNamespace, namespaces, true);
}
});
usedFunctions.forEach(function(item){
if (item.indexOf('$.') == -1) return;
var name = item.slice(2);
var namespaces = name.split('.');
var firstNamespace = namespaces.shift();
if(firstNamespace != undefined){
_pushNamespaces(dollar, firstNamespace, namespaces, false);
}
});
function _pushNamespaces(parentNamespace, namespace, list, isProperty){
// we need to execute them at least twice, code could be improved though
// eg. $.dateFoo.setValue(moment(selectedDate --> $.dateFoo.setValue
namespace = namespace.replace(/\[.*$/g, '');
namespace = namespace.replace(/\[.*$/g, '');
namespace = namespace.replace(/\([a-zA-Z.]*/g, '');
namespace = namespace.replace(/\([a-zA-Z.]*/g, '');
if(!parentNamespace[namespace]){
if(isProperty){
parentNamespace[namespace] = {};
} else {
parentNamespace[namespace] = function(){};
}
}
var shift = list.shift();
if(shift != undefined){
_pushNamespaces(parentNamespace[namespace], shift, list, isProperty);
}
}
function _findAndReturnMatchesArray(source, regEx){
var matches = new String(source).match(regEx);
matches = _.isNull(matches) ? [] : matches;
return matches;
}
return dollar;
}
module.exports = {
createControllerMock: mockController
}