Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zacps committed Sep 3, 2020
1 parent f707a7c commit 71174ff
Showing 1 changed file with 94 additions and 2 deletions.
96 changes: 94 additions & 2 deletions crates/ide/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition)
// version of import map which follows the same process as rustdoc. Otherwise there'll always be some
// edge cases where we select the wrong import path.
fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> {
eprintln!("enter");
// Get the outermost definition for the moduledef. This is used to resolve the public path to the type,
// then we can join the method, field, etc onto it if required.
let target_def: ModuleDef = match definition {
Expand All @@ -88,8 +89,8 @@ fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> {
let module = definition.module(db)?;
let krate = module.krate();
let import_map = db.import_map(krate.into());
let base = once(krate.display_name(db).unwrap())
.chain(import_map.path_of(ns).unwrap().segments.iter().map(|name| format!("{}", name)))
let base = once(krate.display_name(db)?)
.chain(import_map.path_of(ns)?.segments.iter().map(|name| format!("{}", name)))
.join("/");

let filename = get_symbol_filename(db, &target_def);
Expand All @@ -109,6 +110,7 @@ fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> {
Definition::Field(field) => get_symbol_fragment(db, &FieldOrAssocItem::Field(field)),
_ => None,
};
eprintln!("end-ish");

get_doc_url(db, &krate)
.and_then(|url| url.join(&base).ok())
Expand Down Expand Up @@ -387,3 +389,93 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
}
}
}

#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};

use crate::mock_analysis::analysis_and_position;

fn check(ra_fixture: &str, expect: Expect) {
let (analysis, position) = analysis_and_position(ra_fixture);
let url = analysis.external_docs(position).unwrap().unwrap();

expect.assert_eq(&url)
}

#[test]
fn test_doc_url_struct() {
check(
r#"
pub struct Fo<|>o;
"#,
expect![[r#"https://docs.rs/test/*/test/struct.Foo.html"#]],
);
}

// TODO: Fix this test. Fails on `import_map.path_of(ns)`
#[test]
fn test_doc_url_fn() {
check(
r#"
pub fn fo<|>o() {}
"#,
expect![[r#""#]],
);
}

#[test]
fn test_doc_url_inherent_method() {
check(
r#"
pub struct Foo;
impl Foo {
pub fn met<|>hod() {}
}
"#,
expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#method.method"##]],
);
}

#[test]
fn test_doc_url_trait_provided_method() {
check(
r#"
pub trait Bar {
fn met<|>hod() {}
}
"#,
expect![[r##"https://docs.rs/test/*/test/trait.Bar.html#method.method"##]],
);
}

#[test]
fn test_doc_url_trait_required_method() {
check(
r#"
pub trait Foo {
fn met<|>hod();
}
"#,
expect![[r##"https://docs.rs/test/*/test/trait.Foo.html#tymethod.method"##]],
);
}


#[test]
fn test_doc_url_field() {
check(
r#"
pub struct Foo {
pub fie<|>ld: ()
}
"#,
expect![[r##"https://docs.rs/test/*/test/struct.Foo.html#structfield.field"##]],
);
}
}

0 comments on commit 71174ff

Please sign in to comment.