use super::*;
const SRC: &str = "\
#[cfg(test)]
mod testonly;
pub(crate) mod internal;
mod args;
pub mod bpe;
mod capcmd;
pub mod cli;
mod inline { fn x() {} }
use crate::render::Line;
use crate::units;
use std::path::Path;
";
#[test]
fn pub_and_pub_crate_and_private_are_three_different_things() {
let mods = mod_decls(SRC);
assert_eq!(mods.len(), 5, "{mods:?}");
assert!(mods.iter().any(|m| m.name == "bpe" && m.is_pub));
assert!(mods.iter().any(|m| m.name == "args" && !m.is_pub));
assert!(
mods.iter().any(|m| m.name == "internal" && !m.is_pub),
"pub(crate) is not published: {mods:?}"
);
}
#[test]
fn an_inline_or_test_only_module_is_not_a_candidate() {
let mods = mod_decls(SRC);
assert!(!mods.iter().any(|m| m.name == "inline"));
assert!(!mods.iter().any(|m| m.name == "testonly"));
}
#[test]
fn crate_uses_names_internal_modules_and_ignores_the_rest() {
assert_eq!(crate_uses(SRC), vec!["render", "units"]);
}
#[test]
fn a_file_reaching_for_nothing_internal_yields_nothing() {
assert!(crate_uses("use std::path::Path;\nfn main() {}\n").is_empty());
}
#[test]
fn a_brace_group_names_every_module_in_it() {
let src = "use crate::{render, units};\nuse crate::bpe;\n\
use std::path::Path;\n";
assert_eq!(crate_uses(src), vec!["bpe", "render", "units"]);
}
#[test]
fn a_single_member_group_is_still_a_group() {
assert_eq!(crate_uses("use crate::{bpe};\n"), vec!["bpe"]);
assert_eq!(crate_uses("use crate::{bpe, };\n"), vec!["bpe"]);
}
#[test]
fn an_import_carries_the_items_it_names() {
let one = |src: &str| -> (String, Vec<String>) {
let i = crate_imports(src).first().cloned().unwrap_or(Import {
module: String::new(),
items: vec![],
});
(i.module, i.items)
};
assert_eq!(
one("use crate::lint::Level;\n"),
("lint".to_string(), vec!["Level".to_string()])
);
assert_eq!(
one("use crate::lint::{Level, Rule};\n"),
("lint".into(), vec!["Level".into(), "Rule".into()])
);
assert_eq!(
one("use crate::lint::rules::Rule;\n"),
("lint".into(), vec!["Rule".into()])
);
assert_eq!(one("use crate::lint;\n"), ("lint".into(), vec![]));
let many = crate_imports("use crate::{lint::Level, render};\n");
assert_eq!(many.len(), 2, "{many:?}");
assert_eq!(
many.iter().map(|i| i.module.clone()).collect::<Vec<_>>(),
vec!["lint", "render"]
);
assert_eq!(
many.first().map(|i| i.items.clone()),
Some(vec!["Level".into()])
);
assert_eq!(many.get(1).map(|i| i.items.clone()), Some(vec![]));
}
#[test]
fn types_across_sources_are_pooled_and_deduplicated() {
let sources = vec![
"pub struct Edge;\npub enum Kind { A }\n".to_string(),
"pub struct Edge;\npub trait Reader {}\n".to_string(),
];
let names: Vec<String> =
types_in(&sources).into_iter().map(|t| t.name).collect();
assert_eq!(names, vec!["Edge", "Kind", "Reader"]);
}
const TYPES: &str = "pub struct Edge {\n pub dir: String,\n}\
\npub enum Verdict { Fits, Over }\
\npub trait Transport {}\
\npub type Rows = Vec<Edge>;\
\nstruct Hidden;\npub(crate) struct Internal;\
\npub fn go() {}\npub const N: u8 = 1;\n";
#[test]
fn every_public_type_form_is_named_with_the_word_that_declared_it() {
let t = public_types(TYPES);
let of = |n: &str| t.iter().find(|p| p.name == n).map(|p| &p.kind);
assert_eq!(of("Edge"), Some(&"struct".to_string()));
assert_eq!(of("Verdict"), Some(&"enum".to_string()));
assert_eq!(of("Transport"), Some(&"trait".to_string()));
assert_eq!(of("Rows"), Some(&"type".to_string()));
assert_eq!(t.len(), 4, "a fn and a const are not types: {t:?}");
}
#[test]
fn a_private_or_crate_visible_type_is_not_vocabulary() {
let named: Vec<String> =
public_types(TYPES).into_iter().map(|p| p.name).collect();
assert!(!named.contains(&"Hidden".to_string()), "{named:?}");
assert!(
!named.contains(&"Internal".to_string()),
"pub(crate) is not published: {named:?}"
);
}
#[test]
fn a_source_with_no_types_yields_an_empty_vocabulary() {
assert!(public_types("pub fn a() {}\n").is_empty());
assert!(public_types("").is_empty());
}
#[test]
fn a_word_beginning_with_a_keyword_is_not_a_declaration() {
assert!(public_types("pub structure_of(x: u8) {}\n").is_empty());
}