use ingraph_codegen::prelude::*;
use crate::host;
fn model(block: &str) -> Result<RecordModel, Vec<String>> {
let input: syn::DeriveInput = syn::parse_str(&format!(
"#[ingraph(sqlite, graphql({block}))]
struct Reel {{
#[ingraph(id)] id: i64,
#[ingraph(string)] title: String,
}}"
))
.expect("a parseable declaration");
let (host, options) = host::split(&input);
crate::parse::parse(&host, &options, NodeKind::Record)
.map_err(|e| e.into_iter().map(|e| e.to_string()).collect())
}
fn words(block: &str) -> (Option<String>, Option<String>) {
let model = model(block).expect("the declaration settles");
let graphql = model.graphql.expect("the wire face");
(graphql.connection, graphql.edge)
}
fn refusal(block: &str) -> String {
model(block)
.expect_err("the declaration is refused")
.join(" | ")
}
#[test]
fn a_rust_keyword_is_an_ordinary_page_word() {
assert_eq!(
words(r#"async_graphql, connection(rename = "type"), edge(rename = "where")"#),
(Some("type".to_owned()), Some("where".to_owned()))
);
}
#[test]
fn every_shape_of_rust_keyword_is_accepted() {
for word in [
"type", "where", "fn", "impl", "move", "enum", "input", "union", "crate", "self", "Self",
"super", "become", "typeof", "async", "dyn", "_",
] {
assert_eq!(
words(&format!(r#"async_graphql, connection(rename = "{word}")"#)).0,
Some(word.to_owned()),
"`{word}` names a schema type and nothing in Rust"
);
}
}
#[test]
fn a_rust_types_rename_is_still_held_to_rusts_own_alphabet() {
for block in ["output", "key", "filter", "order_by"] {
let refused = refusal(&format!(r#"async_graphql, {block}(rename = "type")"#));
assert!(
refused.contains("must be a valid identifier"),
"`{block}(rename = \"type\")` names a Rust type: {refused}"
);
}
let refused = refusal(r#"async_graphql, rename = "type""#);
assert!(refused.contains("must be a valid identifier"), "{refused}");
}
#[test]
fn the_graphql_name_grammar_is_the_only_fence_and_it_still_bites() {
for word in ["débit", "", "9lives", "bit-rate", "bit rate", "$where"] {
let refused = refusal(&format!(r#"async_graphql, connection(rename = "{word}")"#));
assert!(
refused.contains("is not a GraphQL name"),
"`{word}`: {refused}"
);
}
}
#[test]
fn the_edge_word_is_fenced_by_the_same_grammar() {
let refused = refusal(r#"async_graphql, edge(rename = "débit")"#);
assert!(refused.contains("is not a GraphQL name"), "{refused}");
}
#[test]
fn a_bare_word_is_still_refused_and_the_sentence_does_not_say_identifier() {
let refused = refusal("async_graphql, connection(rename = Page)");
assert!(
refused.contains("string literal"),
"the shape is still a literal: {refused}"
);
assert!(
!refused.contains("valid identifier"),
"and the sentence does not teach the fence this round removed: {refused}"
);
}