-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestParser.js
More file actions
155 lines (134 loc) · 3.24 KB
/
TestParser.js
File metadata and controls
155 lines (134 loc) · 3.24 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
/**
* Read test cases, parse and return them as JSON
*/
const fs = require('fs');
const config = require(__dirname + "/config.json");
class TestParser {
constructor() {
};
static getTests() {
this.data = {
cycle : config.cycle,
};
this.readDir(__dirname + config.testFolder, this.data);
return this.data;
};
static stringToName(string) {
return string.replace(/[\d\.]+_(.*)/gm, "$1").replace(/_/g, " ");
};
static stringToID(string) {
return string.replace(/([\d\.]+)_.*/gm, "$1");
};
static readDir(dirname, target) {
fs.readdirSync(dirname).forEach(file => {
if(fs.statSync(dirname + '/' + file).isDirectory()) {
if(file.indexOf("shared") !== -1) {
//return;
}
let folder = {
id : this.stringToID(file),
name : this.stringToName(file)
};
this.readDir(dirname + '/' + file, folder);
if(!target.collection) {
target.collection = [];
}
target.collection.push(folder);
} else if(file == "testCases.ct") {
target.tests = this.getData(dirname + '/' + file);
}
});
};
// Read one file
static getData(file) {
let content = fs.readFileSync(file, 'utf8');
if(content) {
let parseContent = this.parse(content);
if(parseContent.length > 0) {
return parseContent;
}
}
return false;
};
/**
*
* @param {string} content
* @return {Object}
*/
static parse(content) {
var tests = [];
var tmp = [];
var lines = content.split("\n");
var regext_t = /[ ;]t:/;
for(let i = 0; i < lines.length; i++) {
let line = lines[i].replace(/\r/, "");
if(line.trim() == "") {
continue;
}
if(line.substr(0, 2) == "##") {
if(Object.keys(tmp).length > 0) {
tests.push(tmp);
}
tmp = {
'name' : line,
'id' : line.replace(/#[\#\s]*([\d\.]+).*/gm, "$1"),
'steps' : [],
};
continue;
}
if(/s-max/.test(line) === true) {
tmp.sharedCache = true;
}
if(line.substr(0, 1) != "#") {
let step = this.parseStep(line);
if(step.params && step.params.s && (regext_t.test(" " + step.params.s))) {
tmp.hasTerminate = true;
}
tmp.steps.push(step);
}
}
if(Object.keys(tmp).length > 0) {
tests.push(tmp);
tmp = [];
}
return tests;
};
static parseStep(step) {
// extract params from url
var [tmp, method, url, params_string] = step.match(/([A-Z]+)\s*([^ ]+)(.*)/);
var params = {};
// split by receiver
var matches = params_string.split(/ (-\w{1,2})/);
for(let i = 0; i < matches.length; i++) {
let key = matches[i].trim();
let name = key.substr(1);
if((name.length == 1 || name.length == 2) && key.substr(0, 1) == "-" && (i + 1) < matches.length) {
let value = matches[i + 1].trim();
if (value != "") {
if(name === "e" || name === "ep") {
params[name] = (params[name]) ? params[name] : [];
params[name].push(value.replace(/['"](.*)['"]/, "$1"));
} else {
if(!params[name]) {
params[name] = "";
} else {
params[name] += ";";
}
params[name] += value.replace(/['"](.*)['"]/, "$1");
}
}
i++;
}
}
// expection for private caches
if(params['ep']) {
params['e'] = params['ep'];
}
return {
url : url,
method : method,
params : params,
};
};
}
exports.TestParser = TestParser;