forked from wolfgangmm/xqlint
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_tests.js
156 lines (136 loc) · 4.33 KB
/
run_tests.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
var walk = require('walk');
var fs = require('fs');
var requirejs = require('./r');
var Compiler = requirejs('./lib/Compiler').Compiler;
var SyntaxHighlighter = requirejs('./lib/visitors/SyntaxHighlighter').SyntaxHighlighter;
var JSONParseTreeHandler = requirejs('./lib/JSONParseTreeHandler').JSONParseTreeHandler;
var XQueryParser = requirejs('./lib/XQueryParser').XQueryParser;
var CodeFormatter = requirejs('./lib/visitors/CodeFormatter').CodeFormatter;
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
var successes = [];
var failures = [];
var testHighlighter = false;
var testMarkers = true;
var testCodeFormatter = false;
var showAST = false;
var writeBack = false;
var testFileExtensions = ['.xq'];
function getCode(tokens)
{
var value = null;
for(var i in tokens.lines) {
var line = tokens.lines[i];
value = value === null ? "" : value + "\n";
for(var j in line) {
var token = line[j];
value += token.value;
}
}
return value;
}
function parseFile(filename, failOnError)
{
console.log("Parse: " + filename);
var code = fs.readFileSync(filename, "UTF-8");
code = code.replace(/^(#!.*\n)/, "(:$1:)");
var c = new Compiler();
if (showAST){
c.showAST = true;
}
var ast = c.compile(code);
var fail = ast.error !== undefined;
fail ? failures.push(filename) : successes.push(filename);
if (testMarkers){
console.log(ast.markers);
}
if(testHighlighter) {
console.log("Test syntax highlighter");
var visitor = new SyntaxHighlighter(ast);
var tokens = visitor.getTokens();
if(code !== getCode(tokens)) throw "Syntax Highlighter issue with " + filename;
}
var green = '\x1b[32m';
var red = '\x1b[31m';
var reset = '\x1b[0m';
if(testCodeFormatter){
console.log("Test code formatter");
console.log(red + "Code preformat:\n\"\n" + code + "\"\n" + reset);
var h = new JSONParseTreeHandler(code);
var parser = new XQueryParser(code, h);
parser.parse_XQuery();
ast = h.getParseTree();
//console.log(ast);
var codeFormatter = new CodeFormatter(ast);
var formatted = codeFormatter.format();
console.log(green + "Code postformat:\n\"");
console.log(formatted + "\n\"" + reset);
if (writeBack) {
fs.writeFile(filename, formatted, function(err) {
if(err) {
console.log(err);
} else {
console.log("Wrote formatted code to " + filename);
}
});
}
}
}
function main(args) {
var keepGoing = args.indexOf("--keep-going") != -1;
var file = args.indexOf("-f");
var dir = args.indexOf("-d");
testHighlighter = args.indexOf("--test-highlighter") != -1;
testCodeFormatter = args.indexOf("--test-formatter") != -1;
showAST = args.indexOf("--ast") != -1;
writeBack = args.indexOf("-w") != -1;
var path = "./tests/queries";
if(file != -1)
{
if(args.length <= (file + 1)) {
throw "Missing argument to -f: -f <filename>";
}
var filename = args[file + 1];
path = "./" + filename;
parseFile(filename, keepGoing);
} else if (dir != -1){
if (args.length <= (dir + 1)) {
throw "Missing argument to -d: -d <dirname>";
}
var dirName = args[dir + 1];
if (dirName.charAt(dirName.length - 1) !== '/'){
dirName += '/';
}
var testFiles = fs.readdirSync(dirName).sort();
for (var i = 0; i < testFiles.length; i++){
var testFile = dirName + testFiles[i];
for (var j = 0; j < testFileExtensions.length; j++){
if (testFile.endsWith(testFileExtensions[j])){
parseFile(testFile, keepGoing);
break;
}
}
}
}else {
var walker = walk.walk(path, { followLinks: false });
walker.on('file', function(root, stat, next) {
// Add this file to the list of files
var filename = root + '/' + stat.name;
if(filename.endsWith(".xq")) {
parseFile(filename, keepGoing);
}
next();
});
walker.on('end', function() {
console.log("Parsed " + (failures.length + successes.length) + " files.");
console.log(successes.length + " succeeded, " + failures.length + " failed.");
console.log("The following files didn't parse: ");
var i;
for(i in failures) {
console.log(failures[i]);
}
});
}
};
main(process.argv);