Skip to content

Commit 43acd23

Browse files
Add tester and a few tests
1 parent 53d8ec0 commit 43acd23

File tree

8 files changed

+324
-13
lines changed

8 files changed

+324
-13
lines changed

src/test/rustdoc-js/basic.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -10,6 +10,16 @@
1010

1111
const QUERY = 'String';
1212

13-
const EXPECTED = [
14-
{'all': ['std::string::String']},
15-
];
13+
const EXPECTED = {
14+
'others': [
15+
{ 'path': 'std::string', 'name': 'String' },
16+
{ 'path': 'std::ffi', 'name': 'OsString' },
17+
{ 'path': 'std::ffi', 'name': 'CString' },
18+
],
19+
'in_args': [
20+
{ 'path': 'std::str', 'name': 'eq' },
21+
],
22+
'returned': [
23+
{ 'path': 'std::string::String', 'name': 'add' },
24+
],
25+
};

src/test/rustdoc-js/enum-option.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const QUERY = 'enum:Option';
12+
13+
const EXPECTED = {
14+
'others': [
15+
{ 'path': 'std::option', 'name': 'Option' },
16+
],
17+
};

src/test/rustdoc-js/fn-forget.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const QUERY = 'fn:forget';
12+
13+
const EXPECTED = {
14+
'others': [
15+
{ 'path': 'std::mem', 'name': 'forget' },
16+
{ 'path': 'std::fmt', 'name': 'format' },
17+
],
18+
};

src/test/rustdoc-js/from_u.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const QUERY = 'from_u';
12+
13+
const EXPECTED = {
14+
'others': [
15+
{ 'path': 'std::char', 'name': 'from_u32' },
16+
{ 'path': 'std::str', 'name': 'from_utf8' },
17+
{ 'path': 'std::string::String', 'name': 'from_utf8' },
18+
{ 'path': 'std::boxed::Box', 'name': 'from_unique' },
19+
{ 'path': 'std::i32', 'name': 'from_unsigned' },
20+
{ 'path': 'std::i128', 'name': 'from_unsigned' },
21+
],
22+
};

src/test/rustdoc-js/macro-print.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const QUERY = 'macro:print';
12+
13+
const EXPECTED = {
14+
'others': [
15+
{ 'path': 'std', 'name': 'print' },
16+
{ 'path': 'std', 'name': 'eprint' },
17+
{ 'path': 'std', 'name': 'println' },
18+
{ 'path': 'std', 'name': 'eprintln' },
19+
],
20+
};

src/test/rustdoc-js/string-from_ut.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const QUERY = 'String::from_ut';
12+
13+
const EXPECTED = {
14+
'others': [
15+
{ 'path': 'std::string::String', 'name': 'from_utf8' },
16+
{ 'path': 'std::string::String', 'name': 'from_utf8' },
17+
{ 'path': 'std::string::String', 'name': 'from_utf8_lossy' },
18+
{ 'path': 'std::string::String', 'name': 'from_utf16_lossy' },
19+
{ 'path': 'std::string::String', 'name': 'from_utf8_unchecked' },
20+
],
21+
};

src/test/rustdoc-js/struct-vec.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const QUERY = 'struct:Vec';
12+
13+
const EXPECTED = {
14+
'others': [
15+
{ 'path': 'std::vec', 'name': 'Vec' },
16+
{ 'path': 'std::collections', 'name': 'VecDeque' },
17+
{ 'path': 'alloc::raw_vec', 'name': 'RawVec' },
18+
],
19+
};

src/tools/rustdoc-js/tester.js

+193-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -12,15 +12,199 @@ const fs = require('fs');
1212

1313
const TEST_FOLDER = 'src/test/rustdoc-js/';
1414

15-
function loadFile(filePath) {
16-
var src = fs.readFileSync(filePath, 'utf8').split('\n').slice(15, -10).join('\n');
15+
// Stupid function extractor based on indent.
16+
function extractFunction(content, functionName) {
17+
var x = content.split('\n');
18+
var in_func = false;
19+
var indent = 0;
20+
var lines = [];
21+
22+
for (var i = 0; i < x.length; ++i) {
23+
if (in_func === false) {
24+
var splitter = "function " + functionName + "(";
25+
if (x[i].trim().startsWith(splitter)) {
26+
in_func = true;
27+
indent = x[i].split(splitter)[0].length;
28+
lines.push(x[i]);
29+
}
30+
} else {
31+
lines.push(x[i]);
32+
if (x[i].trim() === "}" && x[i].split("}")[0].length === indent) {
33+
return lines.join("\n");
34+
}
35+
}
36+
}
37+
return null;
38+
}
39+
40+
// Stupid function extractor for array.
41+
function extractArrayVariable(content, arrayName) {
42+
var x = content.split('\n');
43+
var found_var = false;
44+
var lines = [];
45+
46+
for (var i = 0; i < x.length; ++i) {
47+
if (found_var === false) {
48+
var splitter = "var " + arrayName + " = [";
49+
if (x[i].trim().startsWith(splitter)) {
50+
found_var = true;
51+
i -= 1;
52+
}
53+
} else {
54+
lines.push(x[i]);
55+
if (x[i].endsWith('];')) {
56+
return lines.join("\n");
57+
}
58+
}
59+
}
60+
return null;
61+
}
62+
63+
// Stupid function extractor for variable.
64+
function extractVariable(content, varName) {
65+
var x = content.split('\n');
66+
var found_var = false;
67+
var lines = [];
68+
69+
for (var i = 0; i < x.length; ++i) {
70+
if (found_var === false) {
71+
var splitter = "var " + varName + " = ";
72+
if (x[i].trim().startsWith(splitter)) {
73+
found_var = true;
74+
i -= 1;
75+
}
76+
} else {
77+
lines.push(x[i]);
78+
if (x[i].endsWith(';')) {
79+
return lines.join("\n");
80+
}
81+
}
82+
}
83+
return null;
84+
}
85+
86+
function loadContent(content) {
1787
var Module = module.constructor;
1888
var m = new Module();
19-
m._compile(src, filePath);
20-
return m;
89+
m._compile(content, "tmp.js");
90+
return m.exports;
91+
}
92+
93+
function readFile(filePath) {
94+
return fs.readFileSync(filePath, 'utf8');
95+
}
96+
97+
function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
98+
var content = '';
99+
for (var i = 0; i < thingsToLoad.length; ++i) {
100+
var tmp = funcToCall(fileContent, thingsToLoad[i]);
101+
if (tmp === null) {
102+
console.error('enable to find ' + kindOfLoad + ' "' + thingsToLoad[i] + '"');
103+
process.exit(1);
104+
}
105+
content += tmp;
106+
content += 'exports.' + thingsToLoad[i] + ' = ' + thingsToLoad[i] + ';';
107+
}
108+
return content;
109+
}
110+
111+
function lookForEntry(entry, data) {
112+
for (var i = 0; i < data.length; ++i) {
113+
var allGood = true;
114+
for (var key in entry) {
115+
if (!entry.hasOwnProperty(key)) {
116+
continue;
117+
}
118+
let value = data[i][key];
119+
// To make our life easier, if there is a "parent" type, we add it to the path.
120+
if (key === 'path' && data[i]['parent'] !== undefined) {
121+
if (value.length > 0) {
122+
value += '::' + data[i]['parent']['name'];
123+
} else {
124+
value = data[i]['parent']['name'];
125+
}
126+
}
127+
if (value !== entry[key]) {
128+
allGood = false;
129+
break;
130+
}
131+
}
132+
if (allGood === true) {
133+
return true;
134+
}
135+
}
136+
return false;
137+
}
138+
139+
function main(argv) {
140+
if (argv.length !== 3) {
141+
console.error("Expected toolchain to check as argument (for example 'x86_64-apple-darwin'");
142+
return 1;
143+
}
144+
var toolchain = argv[2];
145+
146+
var mainJs = readFile("build/" + toolchain + "/doc/main.js");
147+
var searchIndex = readFile("build/" + toolchain + "/doc/search-index.js").split("\n");
148+
if (searchIndex[searchIndex.length - 1].length === 0) {
149+
searchIndex.pop();
150+
}
151+
searchIndex.pop();
152+
searchIndex = loadContent(searchIndex.join("\n") + '\nexports.searchIndex = searchIndex;');
153+
finalJS = "";
154+
155+
var arraysToLoad = ["itemTypes"];
156+
var variablesToLoad = ["MAX_LEV_DISTANCE", "MAX_RESULTS", "TY_PRIMITIVE", "levenshtein_row2"];
157+
// execQuery first parameter is built in getQuery (which takes in the search input).
158+
// execQuery last parameter is built in buildIndex.
159+
// buildIndex requires the hashmap from search-index.
160+
var functionsToLoad = ["levenshtein", "validateResult", "getQuery", "buildIndex", "execQuery"];
161+
162+
finalJS += 'window = { "currentCrate": "std" };\n';
163+
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
164+
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
165+
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
166+
167+
var loaded = loadContent(finalJS);
168+
var index = loaded.buildIndex(searchIndex.searchIndex);
169+
170+
var errors = 0;
171+
172+
fs.readdirSync(TEST_FOLDER).forEach(function(file) {
173+
var loadedFile = loadContent(readFile(TEST_FOLDER + file) +
174+
'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;');
175+
const expected = loadedFile.EXPECTED;
176+
const query = loadedFile.QUERY;
177+
var results = loaded.execQuery(loaded.getQuery(query), index);
178+
process.stdout.write('Checking "' + file + '" ... ');
179+
var error_text = [];
180+
for (var key in expected) {
181+
if (!expected.hasOwnProperty(key)) {
182+
continue;
183+
}
184+
if (!results.hasOwnProperty(key)) {
185+
error_text.push('==> Unknown key "' + key + '"');
186+
break;
187+
}
188+
var entry = expected[key];
189+
var found = false;
190+
for (var i = 0; i < entry.length; ++i) {
191+
if (lookForEntry(entry[i], results[key]) === true) {
192+
found = true;
193+
} else {
194+
error_text.push("==> Result not found in '" + key + "': '" +
195+
JSON.stringify(entry[i]) + "'");
196+
}
197+
}
198+
}
199+
if (error_text.length !== 0) {
200+
errors += 1;
201+
console.error("FAILED");
202+
console.error(error_text.join("\n"));
203+
} else {
204+
console.log("OK");
205+
}
206+
});
207+
return errors;
21208
}
22209

23-
fs.readdirSync(TEST_FOLDER).forEach(function(file) {
24-
var file = require(TEST_FOLDER + file);
25-
const expected = file.EXPECTED;
26-
});
210+
process.exit(main(process.argv));

0 commit comments

Comments
 (0)