use crate::order;
#[test]
fn the_index_path_is_the_leaf_name_inside_the_directory_it_orders() {
assert_eq!(order::order_file_path(""), ".order.json");
assert_eq!(order::order_file_path("docs"), "docs/.order.json");
assert_eq!(
order::order_file_path("docs/guides"),
"docs/guides/.order.json"
);
}
#[test]
fn an_index_is_recognised_by_its_leaf_name_at_any_depth() {
assert!(order::is_order_file(".order.json"));
assert!(order::is_order_file("docs/.order.json"));
assert!(order::is_order_file("a/b/c/.order.json"));
assert!(!order::is_order_file("order.json"));
assert!(!order::is_order_file("docs/.order.json.md"));
assert!(!order::is_order_file("docs/intro.md"));
}
#[test]
fn the_directory_of_an_index_is_its_parent_with_the_root_spelled_empty() {
assert_eq!(order::directory_of_order_file(".order.json"), Some(""));
assert_eq!(
order::directory_of_order_file("docs/.order.json"),
Some("docs")
);
assert_eq!(order::directory_of_order_file("docs/intro.md"), None);
}
#[test]
fn split_parent_puts_a_root_level_path_in_the_empty_directory() {
assert_eq!(order::split_parent("intro.md"), ("", "intro.md"));
assert_eq!(order::split_parent("docs/intro.md"), ("docs", "intro.md"));
assert_eq!(
order::split_parent("docs/guides/intro.md"),
("docs/guides", "intro.md")
);
}
#[test]
fn two_spellings_of_one_entry_compare_equal() {
assert_eq!(order::entry_name("guides/"), "guides");
assert_eq!(order::entry_name("guides"), "guides");
assert_eq!(order::directory_entry("guides"), "guides/");
}
#[test]
fn hiddenness_is_judged_on_the_name_not_the_slash() {
assert!(order::is_hidden(".templates/"));
assert!(order::is_hidden(".gitignore"));
assert!(!order::is_hidden("docs/"));
assert!(!order::is_hidden("intro.md"));
}
#[test]
fn the_repository_root_displays_as_a_slash() {
assert_eq!(order::display_directory(""), "/");
assert_eq!(order::display_directory("docs"), "docs");
}
#[test]
fn a_stored_index_round_trips() {
let order = vec![
"intro.md".to_string(),
"getting-started/".to_string(),
"advanced.mdx".to_string(),
];
let serialized = order::serialize(&order);
assert!(serialized.ends_with('\n'));
assert_eq!(order::parse(&serialized), Some(order));
}
#[test]
fn a_malformed_index_parses_as_no_index_rather_than_an_error() {
assert_eq!(order::parse("not json"), None);
assert_eq!(order::parse("[]"), None);
assert_eq!(order::parse(r#"{"order": "nope"}"#), None);
assert_eq!(order::parse(r#"{"order": []}"#), Some(vec![]));
}
#[test]
fn a_valid_order_passes() {
let order = vec!["intro.md".to_string(), "guides/".to_string()];
assert!(order::validate_order(&order, false).is_ok());
}
#[test]
fn an_empty_order_is_rejected_because_delete_is_what_it_means() {
let reason = order::validate_order(&[], false)
.expect_err("an empty order should be rejected")
.to_string();
assert!(reason.contains("delete the index"), "{}", reason);
}
#[test]
fn order_entries_must_be_leaf_names() {
let error = order::validate_order(&["docs/intro.md".to_string()], false)
.expect_err("a nested path should be rejected");
assert!(error.to_string().contains("leaf names"), "{}", error);
}
#[test]
fn order_entries_reject_relative_references_git_and_the_index_itself() {
for entry in [".", "..", ".git", ".order.json", ""] {
assert!(
order::validate_order(&[entry.to_string()], true).is_err(),
"accepted: {:?}",
entry
);
}
}
#[test]
fn the_index_itself_stays_rejected_even_with_hidden_entries_allowed() {
let error = order::validate_order(&[".order.json".to_string()], true)
.expect_err("the index should never order itself");
assert!(error.to_string().contains("the index itself"), "{}", error);
}
#[test]
fn hidden_entries_are_rejected_unless_asked_for() {
let error = order::validate_order(&[".gitignore".to_string()], false)
.expect_err("a hidden entry should be rejected by default");
assert!(
error.to_string().contains("allow_hidden_files"),
"{}",
error
);
assert!(order::validate_order(&[".gitignore".to_string()], true).is_ok());
assert!(order::validate_order(&[".templates/".to_string()], true).is_ok());
}
#[test]
fn duplicate_entries_are_rejected_across_both_spellings() {
let error = order::validate_order(&["guides".to_string(), "guides/".to_string()], false)
.expect_err("two spellings of one entry are still a duplicate");
assert!(error.to_string().contains("unique"), "{}", error);
}