Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/tools/jsondoclint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ impl<'a> Validator<'a> {

fn check_item(&mut self, id: &'a Id) {
if let Some(item) = &self.krate.index.get(id) {
item.links.values().for_each(|id| self.add_any_id(id));

match &item.inner {
ItemEnum::Import(x) => self.check_import(x),
ItemEnum::Union(x) => self.check_union(x),
Expand Down Expand Up @@ -376,6 +378,10 @@ impl<'a> Validator<'a> {
}
}

fn add_any_id(&mut self, id: &'a Id) {
self.add_id_checked(id, |_| true, "any kind of item");
}

fn add_field_id(&mut self, id: &'a Id) {
self.add_id_checked(id, Kind::is_struct_field, "StructField");
}
Expand Down Expand Up @@ -446,3 +452,6 @@ fn set_remove<T: Hash + Eq + Clone>(set: &mut HashSet<T>) -> Option<T> {
None
}
}

#[cfg(test)]
mod tests;
50 changes: 50 additions & 0 deletions src/tools/jsondoclint/src/validator/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::collections::HashMap;

use rustdoc_json_types::{Crate, Item, Visibility};

use super::*;

#[track_caller]
fn check(krate: &Crate, errs: &[Error]) {
let mut validator = Validator::new(krate);
validator.check_crate();

assert_eq!(errs, &validator.errs[..]);
}

fn id(s: &str) -> Id {
Id(s.to_owned())
}

#[test]
fn errors_on_missing_links() {
let k = Crate {
root: id("0"),
crate_version: None,
includes_private: false,
index: HashMap::from_iter([(
id("0"),
Item {
name: Some("root".to_owned()),
id: id(""),
crate_id: 0,
span: None,
visibility: Visibility::Public,
docs: None,
links: HashMap::from_iter([("Not Found".to_owned(), id("1"))]),
attrs: vec![],
deprecation: None,
inner: ItemEnum::Module(Module {
is_crate: true,
items: vec![],
is_stripped: false,
}),
},
)]),
paths: HashMap::new(),
external_crates: HashMap::new(),
format_version: rustdoc_json_types::FORMAT_VERSION,
};

check(&k, &[Error { kind: ErrorKind::NotFound, id: id("1") }]);
}