use ingraph_codegen::prelude::NodeKind;
use crate::{index, interface, parse, standalone, value};
fn record(input: syn::DeriveInput) -> String {
let (host, arguments) = crate::host::split(&input);
parse::parse(&host, &arguments, NodeKind::Record)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\n")
}
fn derived(input: syn::DeriveInput, kind: NodeKind) -> String {
let (host, arguments) = crate::host::split(&input);
index::parse(&host, &arguments, kind)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\n")
}
fn contract(input: syn::DeriveInput) -> String {
let (host, arguments) = crate::host::split(&input);
interface::parse(&host, &arguments)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\n")
}
fn suggests(word: &str) -> String {
format!(" — did you mean `{word}`?")
}
#[test]
fn a_near_miss_at_the_records_head_is_named_and_the_sentence_survives() {
let refused = record(syn::parse_quote! {
#[ingraph(tabel = "s")]
struct S {
#[ingraph(id)] id: Uuid7,
}
});
assert!(
refused.contains("unknown `ingraph` record attribute — a record takes its STORE WORDS bare"),
"the teaching sentence is still there: {refused}"
);
assert!(refused.contains(&suggests("table")), "{refused}");
}
#[test]
fn a_near_miss_of_the_options_word_is_named_and_the_sentence_teaches_it() {
let refused = record(syn::parse_quote! {
#[ingraph(option = "app::Knobs")]
struct S {
#[ingraph(id)] id: Uuid7,
}
});
assert!(refused.contains(&suggests("options")), "{refused}");
assert!(
refused.contains("`options = \"…\"`"),
"the sentence names the word it suggests: {refused}"
);
}
#[test]
fn a_near_miss_of_a_field_marker_is_named() {
let refused = record(syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(optionel)] a: String,
}
});
assert!(refused.contains(&suggests("optional")), "{refused}");
}
#[test]
fn a_near_miss_of_a_label_word_at_the_field_head_is_named() {
let refused = record(syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(durtion)] a: Duration,
}
});
assert!(refused.contains(&suggests("duration")), "{refused}");
}
#[test]
fn the_rulings_own_example_suggests_sortable() {
let refused = record(syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(list(string, sortble))] a: Vec<String>,
}
});
assert!(
refused.contains("is not an interpretation this framework names"),
"the element sentence is still there: {refused}"
);
assert!(refused.contains(&suggests("sortable")), "{refused}");
}
#[test]
fn a_near_miss_of_a_database_word_is_named() {
let refused = record(syn::parse_quote! {
#[ingraph(postgress)]
struct S {
#[ingraph(id)] id: Uuid7,
}
});
assert!(refused.contains(&suggests("postgres")), "{refused}");
}
#[test]
fn a_near_miss_inside_a_columns_database_block_is_named() {
let refused = record(syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(sqlite(indexd))] a: String,
}
});
assert!(refused.contains(&suggests("indexed")), "{refused}");
}
#[test]
fn a_near_miss_of_the_unique_word_is_named() {
let refused = record(syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(sqlite(uniqe))] a: String,
}
});
assert!(refused.contains(&suggests("unique")), "{refused}");
}
#[test]
fn a_near_miss_at_an_index_head_is_offered_only_the_words_that_kind_takes() {
let searchable = derived(
syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment", body = "text"), dedupe(hash))]
struct D {
#[ingraph(searchable)] body: String,
}
},
NodeKind::Fulltext,
);
assert!(searchable.contains(&suggests("dedup")), "{searchable}");
}
#[test]
fn an_engine_position_has_no_vocabulary_to_be_near_because_it_is_open() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(upstream(node = "video::Keyframe", payload = "pixels"), dedupe(hash))]
struct B {
#[ingraph(repeated)] payload: Vec<u8>,
}
};
let (host, arguments) = crate::host::split(&input);
let model = index::parse(&host, &arguments, NodeKind::Blob)
.expect("a word the framework does not own is an ENGINE, however it was spelled");
let stores: Vec<String> = model
.stores
.iter()
.map(|engine| engine.word.to_string())
.collect();
assert_eq!(stores, ["dedupe"], "the typo became an engine word");
}
#[test]
fn a_near_miss_at_an_interfaces_head_is_named() {
let refused = contract(syn::parse_quote! {
#[ingraph(feilds(id: Uuid7))]
enum Media {
Video(Video),
}
});
assert!(refused.contains(&suggests("fields")), "{refused}");
}
#[test]
fn a_word_near_nothing_adds_no_line() {
let refused = record(syn::parse_quote! {
#[ingraph(hippopotamus)]
struct S {
#[ingraph(id)] id: Uuid7,
}
});
assert!(
refused.contains("unknown `ingraph` record attribute"),
"{refused}"
);
assert!(!refused.contains("did you mean"), "{refused}");
}
#[test]
fn a_position_with_no_vocabulary_adds_no_line() {
let refused = record(syn::parse_quote! {
#[ingraph(sqlite, implements(media::Media(feild = "x")))]
struct S {
#[ingraph(id)] id: Uuid7,
}
});
assert!(!refused.contains("did you mean"), "{refused}");
}
#[test]
fn a_near_miss_of_a_tracing_level_is_named() {
let refused = record(syn::parse_quote! {
#[ingraph(sqlite, tracing(level = debgu))]
struct S {
#[ingraph(id)] id: Uuid7,
}
});
assert!(
refused.contains("is not a level this framework names"),
"the teaching sentence is still there: {refused}"
);
assert!(refused.contains(&suggests("debug")), "{refused}");
}
#[test]
fn a_near_miss_of_a_dedup_mechanism_is_named() {
let refused = derived(
syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment", body = "text"), dedup(hasg))]
struct D {
#[ingraph(searchable)] body: String,
}
},
NodeKind::Fulltext,
);
assert!(refused.contains("is not a dedup mechanism"), "{refused}");
assert!(refused.contains(&suggests("hash")), "{refused}");
}
#[test]
fn a_near_miss_of_an_element_family_is_named() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(operator(strng))]
struct Volume(u8);
};
let Err(refused) = standalone::parse(&input, standalone::Face::Filter) else {
panic!("the declaration is refused");
};
let refused = refused
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\n");
assert!(
refused.contains("is not a family an ELEMENT may be read under"),
"{refused}"
);
assert!(refused.contains(&suggests("string")), "{refused}");
}
#[test]
fn a_near_miss_of_a_member_variant_is_named() {
let refused = contract(syn::parse_quote! {
#[ingraph(fields(title(type = "String", members(Vide(field = "name")))))]
enum Media {
Video(Video),
Audio(Audio),
}
});
assert!(
refused.contains("is not a member variant of this interface"),
"{refused}"
);
assert!(refused.contains(&suggests("Video")), "{refused}");
}
#[test]
fn a_keyword_column_is_nameable_under_both_spellings() {
for written in ["type", "r#type"] {
let literal = syn::LitStr::new(written, proc_macro2::Span::call_site());
let read = value::named_field(&literal, "unreachable").expect("a keyword names a field");
assert_eq!(read.to_string(), "r#type", "written as {written:?}");
assert_eq!(
read,
syn::Ident::new_raw("type", proc_macro2::Span::call_site()),
"written as {written:?}"
);
}
}
#[test]
fn an_ordinary_column_stays_an_ordinary_identifier() {
let literal = syn::LitStr::new("format", proc_macro2::Span::call_site());
let read = value::named_field(&literal, "unreachable").expect("a word names a field");
assert_eq!(read.to_string(), "format");
}
#[test]
fn a_value_that_names_no_field_is_still_refused() {
for written in ["", "9lives", "a::b", "self", "crate"] {
let literal = syn::LitStr::new(written, proc_macro2::Span::call_site());
let refused = value::named_field(&literal, "the position's own sentence")
.expect_err("no spelling makes this a field name");
assert_eq!(refused.to_string(), "the position's own sentence");
}
}
#[test]
fn an_artifact_familys_suffix_column_may_be_a_keyword() {
for written in [r#"from = "type""#, r#"from = "r#type""#] {
let head: proc_macro2::TokenStream =
format!(r#"fs, extension({written}, any_of = ["png", "jpg"])"#)
.parse()
.expect("a head");
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(upstream(node = "video::Keyframe", payload = "pixels"), #head)]
struct Thumbnail {
#[ingraph(repeated)] payload: Vec<u8>,
}
};
let (host, arguments) = crate::host::split(&input);
let model = index::parse(&host, &arguments, NodeKind::Blob)
.unwrap_or_else(|e| panic!("`{written}` names the column: {e}"));
let column = match model.extension.as_ref().expect("an extension") {
ingraph_codegen::extension::Extension::Column(column) => column,
other => panic!("a column road, not {other:?}"),
};
let field = match &column.source {
ingraph_codegen::extension::ExtensionSource::Text { field } => field,
other => panic!("the string road, not {other:?}"),
};
assert_eq!(field.to_string(), "r#type", "written as {written}");
}
}
#[test]
fn an_enum_roads_column_may_be_a_keyword() {
let input: syn::DeriveInput = syn::parse_quote! {
#[ingraph(
upstream(node = "video::Keyframe", payload = "pixels"),
fs,
extension(from(field = "type", type = "app::Format"))
)]
struct Thumbnail {
#[ingraph(repeated)] payload: Vec<u8>,
}
};
let (host, arguments) = crate::host::split(&input);
let model =
index::parse(&host, &arguments, NodeKind::Blob).expect("a keyword names the enum column");
let column = match model.extension.as_ref().expect("an extension") {
ingraph_codegen::extension::Extension::Column(column) => column,
other => panic!("a column road, not {other:?}"),
};
match &column.source {
ingraph_codegen::extension::ExtensionSource::Enumerated { field, .. } => {
assert_eq!(field.to_string(), "r#type");
}
other => panic!("the enum road, not {other:?}"),
}
}