Skip to content

Commit 6c64f0b

Browse files
Rollup merge of rust-lang#47250 - GuillaumeGomez:test-rustdoc-js, r=Mark-Simulacrum
Test rustdoc js Add tests for the rustdoc search. It was heavily required because of all the recent breaking changes that happened while I went through improvements in doc search (add search in/for generic search for example).
2 parents 0f9c784 + 3a7e247 commit 6c64f0b

File tree

12 files changed

+435
-47
lines changed

12 files changed

+435
-47
lines changed

src/bootstrap/builder.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ impl<'a> Builder<'a> {
254254
Kind::Test => describe!(check::Tidy, check::Bootstrap, check::DefaultCompiletest,
255255
check::HostCompiletest, check::Crate, check::CrateLibrustc, check::Rustdoc,
256256
check::Linkcheck, check::Cargotest, check::Cargo, check::Rls, check::Docs,
257-
check::ErrorIndex, check::Distcheck, check::Rustfmt, check::Miri, check::Clippy),
257+
check::ErrorIndex, check::Distcheck, check::Rustfmt, check::Miri, check::Clippy,
258+
check::RustdocJS),
259+
258260
Kind::Bench => describe!(check::Crate, check::CrateLibrustc),
259261
Kind::Doc => describe!(doc::UnstableBook, doc::UnstableBookGen, doc::TheBook,
260262
doc::Standalone, doc::Std, doc::Test, doc::Rustc, doc::ErrorIndex, doc::Nomicon,
@@ -443,7 +445,8 @@ impl<'a> Builder<'a> {
443445
let out_dir = self.stage_out(compiler, mode);
444446
cargo.env("CARGO_TARGET_DIR", out_dir)
445447
.arg(cmd)
446-
.arg("--target").arg(target);
448+
.arg("--target")
449+
.arg(target);
447450

448451
// If we were invoked from `make` then that's already got a jobserver
449452
// set up for us so no need to tell Cargo about jobs all over again.

src/bootstrap/check.rs

+37
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,43 @@ fn path_for_cargo(builder: &Builder, compiler: Compiler) -> OsString {
424424
env::join_paths(iter::once(path).chain(env::split_paths(&old_path))).expect("")
425425
}
426426

427+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
428+
pub struct RustdocJS {
429+
pub host: Interned<String>,
430+
pub target: Interned<String>,
431+
}
432+
433+
impl Step for RustdocJS {
434+
type Output = ();
435+
const DEFAULT: bool = true;
436+
const ONLY_HOSTS: bool = true;
437+
438+
fn should_run(run: ShouldRun) -> ShouldRun {
439+
run.path("src/test/rustdoc-js")
440+
}
441+
442+
fn make_run(run: RunConfig) {
443+
run.builder.ensure(RustdocJS {
444+
host: run.host,
445+
target: run.target,
446+
});
447+
}
448+
449+
fn run(self, builder: &Builder) {
450+
if let Some(ref nodejs) = builder.config.nodejs {
451+
let mut command = Command::new(nodejs);
452+
command.args(&["src/tools/rustdoc-js/tester.js", &*self.host]);
453+
builder.ensure(::doc::Std {
454+
target: self.target,
455+
stage: builder.top_stage,
456+
});
457+
builder.run(&mut command);
458+
} else {
459+
println!("No nodejs found, skipping \"src/test/rustdoc-js\" tests");
460+
}
461+
}
462+
}
463+
427464
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
428465
pub struct Tidy {
429466
host: Interned<String>,

src/bootstrap/doc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,8 @@ impl Step for Standalone {
419419

420420
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
421421
pub struct Std {
422-
stage: u32,
423-
target: Interned<String>,
422+
pub stage: u32,
423+
pub target: Interned<String>,
424424
}
425425

426426
impl Step for Std {

src/librustdoc/html/static/main.js

+39-43
Original file line numberDiff line numberDiff line change
@@ -353,35 +353,33 @@
353353
* This code is an unmodified version of the code written by Marco de Wit
354354
* and was found at http://stackoverflow.com/a/18514751/745719
355355
*/
356-
var levenshtein = (function() {
357-
var row2 = [];
358-
return function(s1, s2) {
359-
if (s1 === s2) {
360-
return 0;
356+
var levenshtein_row2 = [];
357+
function levenshtein(s1, s2) {
358+
if (s1 === s2) {
359+
return 0;
360+
}
361+
var s1_len = s1.length, s2_len = s2.length;
362+
if (s1_len && s2_len) {
363+
var i1 = 0, i2 = 0, a, b, c, c2, row = levenshtein_row2;
364+
while (i1 < s1_len) {
365+
row[i1] = ++i1;
361366
}
362-
var s1_len = s1.length, s2_len = s2.length;
363-
if (s1_len && s2_len) {
364-
var i1 = 0, i2 = 0, a, b, c, c2, row = row2;
365-
while (i1 < s1_len) {
366-
row[i1] = ++i1;
367-
}
368-
while (i2 < s2_len) {
369-
c2 = s2.charCodeAt(i2);
370-
a = i2;
371-
++i2;
372-
b = i2;
373-
for (i1 = 0; i1 < s1_len; ++i1) {
374-
c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
375-
a = row[i1];
376-
b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
377-
row[i1] = b;
378-
}
367+
while (i2 < s2_len) {
368+
c2 = s2.charCodeAt(i2);
369+
a = i2;
370+
++i2;
371+
b = i2;
372+
for (i1 = 0; i1 < s1_len; ++i1) {
373+
c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
374+
a = row[i1];
375+
b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
376+
row[i1] = b;
379377
}
380-
return b;
381378
}
382-
return s1_len + s2_len;
383-
};
384-
})();
379+
return b;
380+
}
381+
return s1_len + s2_len;
382+
}
385383

386384
function initSearch(rawSearchIndex) {
387385
var currentResults, index, searchIndex;
@@ -400,12 +398,20 @@
400398
/**
401399
* Executes the query and builds an index of results
402400
* @param {[Object]} query [The user query]
403-
* @param {[type]} max [The maximum results returned]
404401
* @param {[type]} searchWords [The list of search words to query
405402
* against]
406403
* @return {[type]} [A search index of results]
407404
*/
408-
function execQuery(query, max, searchWords) {
405+
function execQuery(query, searchWords) {
406+
function itemTypeFromName(typename) {
407+
for (var i = 0; i < itemTypes.length; ++i) {
408+
if (itemTypes[i] === typename) {
409+
return i;
410+
}
411+
}
412+
return -1;
413+
}
414+
409415
var valLower = query.query.toLowerCase(),
410416
val = valLower,
411417
typeFilter = itemTypeFromName(query.type),
@@ -1021,9 +1027,8 @@
10211027
return true;
10221028
}
10231029

1024-
function getQuery() {
1025-
var matches, type, query, raw =
1026-
document.getElementsByClassName('search-input')[0].value;
1030+
function getQuery(raw) {
1031+
var matches, type, query;
10271032
query = raw;
10281033

10291034
matches = query.match(/^(fn|mod|struct|enum|trait|type|const|macro)\s*:\s*/i);
@@ -1227,7 +1232,7 @@
12271232
}
12281233

12291234
function showResults(results) {
1230-
var output, query = getQuery();
1235+
var output, query = getQuery(document.getElementsByClassName('search-input')[0].value);
12311236

12321237
currentResults = query.id;
12331238
output = '<h1>Results for ' + escape(query.query) +
@@ -1271,7 +1276,7 @@
12711276
resultIndex;
12721277
var params = getQueryStringParams();
12731278

1274-
query = getQuery();
1279+
query = getQuery(document.getElementsByClassName('search-input')[0].value);
12751280
if (e) {
12761281
e.preventDefault();
12771282
}
@@ -1293,19 +1298,10 @@
12931298
}
12941299
}
12951300

1296-
results = execQuery(query, 20000, index);
1301+
results = execQuery(query, index);
12971302
showResults(results);
12981303
}
12991304

1300-
function itemTypeFromName(typename) {
1301-
for (var i = 0; i < itemTypes.length; ++i) {
1302-
if (itemTypes[i] === typename) {
1303-
return i;
1304-
}
1305-
}
1306-
return -1;
1307-
}
1308-
13091305
function buildIndex(rawSearchIndex) {
13101306
searchIndex = [];
13111307
var searchWords = [];

src/test/rustdoc-js/basic.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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';
12+
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+
};

0 commit comments

Comments
 (0)