use ingraph_codegen::{
aggregate::Measure,
element::ElementFamily,
enumeration::{Namespace, StorageForm},
index::{Dedup, Fold},
prelude::{DatabaseKind, Label, TracingLevel},
};
use super::{did_you_mean, word};
#[test]
fn a_near_word_is_suggested() {
assert_eq!(
did_you_mean("sortble", ["scalar", "string", "bytes", "enum", "sortable"]),
" — did you mean `sortable`?"
);
}
#[test]
fn a_far_word_stays_silent() {
assert_eq!(
did_you_mean("hippopotamus", ["scalar", "string", "bytes", "enum"]),
""
);
assert_eq!(did_you_mean("own", ["ask", "key", "out", "set"]), "");
}
#[test]
fn a_position_with_no_vocabulary_says_nothing() {
let none: [&str; 0] = [];
assert_eq!(did_you_mean("anything", none), "");
}
#[test]
fn the_raw_spelling_of_a_word_the_position_takes_is_suggested() {
assert_eq!(
did_you_mean("r#type", ["field", "type"]),
" — did you mean `type`?"
);
let raw: syn::Path = syn::parse_quote!(r#type);
assert_eq!(word(&raw).as_deref(), Some("type"));
let escaped: syn::Path = syn::parse_quote!(r#enum);
assert_eq!(word(&escaped).as_deref(), Some("enum"));
}
#[test]
fn a_path_of_several_segments_names_no_word() {
let path: syn::Path = syn::parse_quote!(app::hnsw::Hnsw);
assert_eq!(word(&path), None);
}
#[test]
fn the_cutoff_is_two_edits_and_half_the_word() {
assert_eq!(did_you_mean("asl", ["ask"]), " — did you mean `ask`?");
assert_eq!(did_you_mean("owk", ["ask"]), "");
assert_eq!(did_you_mean("lixt", ["list"]), " — did you mean `list`?");
assert_eq!(did_you_mean("lixx", ["list"]), " — did you mean `list`?");
assert_eq!(did_you_mean("lixxx", ["list"]), "");
assert_eq!(did_you_mean("sortabxxx", ["sortable"]), "");
}
#[test]
fn the_closest_word_wins_and_a_tie_goes_to_the_first() {
assert_eq!(
did_you_mean("orde_by", ["order", "order_by"]),
" — did you mean `order_by`?"
);
assert_eq!(
did_you_mean("copx", ["copy", "cop_"]),
" — did you mean `copy`?"
);
}
#[test]
fn the_cutoff_keeps_every_shipped_vocabulary_apart() {
let vocabularies: [Vec<&str>; 15] = [
Label::ALL.into_iter().map(Label::as_str).collect(),
DatabaseKind::ALL
.into_iter()
.map(DatabaseKind::as_str)
.collect(),
TracingLevel::ALL
.into_iter()
.map(TracingLevel::as_str)
.collect(),
StorageForm::ALL
.into_iter()
.map(StorageForm::as_str)
.collect(),
Namespace::ALL.into_iter().map(Namespace::as_str).collect(),
ElementFamily::ALL
.into_iter()
.map(ElementFamily::word)
.collect(),
Fold::WORDS.to_vec(),
Dedup::WORDS.to_vec(),
Measure::SHIPPED.to_vec(),
vec![
"key",
"lineage",
"persist",
"graphql",
"derived",
"implements",
"source",
"subscribe",
"emits",
"poll",
"tracing",
"teardown",
"async",
"crate",
],
vec![
"id", "copy", "repeated", "optional", "persist", "graphql", "ref", "key", "lineage", "label",
],
vec![
"rename", "indexed", "unique", "copy", "flatten", "type", "edge", "from", "try_from", "into",
"try_into",
],
crate::parse::BARE_POLL_WORDS.to_vec(),
crate::aggregate::head_vocabulary(),
crate::aggregate::field_vocabulary(),
];
for vocabulary in &vocabularies {
for word in vocabulary {
let others = vocabulary.iter().filter(|other| *other != word);
assert_eq!(
did_you_mean(word, others),
"",
"`{word}` is a near miss of another word of its own position: {vocabulary:?}"
);
}
}
}