use quote::ToTokens as _;
use super::{page_type, root_field};
use crate::model::{Graphql, RecordModel};
fn record(knob: impl FnOnce(&mut Graphql)) -> RecordModel {
let mut model = crate::tests::wired();
knob(model.graphql.as_mut().expect("the fixture has a wire face"));
model
}
fn word(tokens: &str, marker: &str) -> String {
let at = tokens
.find(&format!("for {marker}"))
.unwrap_or_else(|| panic!("`{marker}` is minted:\n{tokens}"));
let body = &tokens[at..];
let opened = body
.find("to_owned (\"")
.unwrap_or_else(|| panic!("`{marker}`'s body hands a literal back:\n{body}"))
+ "to_owned (\"".len();
let rest = &body[opened..];
let closed = rest
.find('"')
.unwrap_or_else(|| panic!("`{marker}`'s literal closes:\n{rest}"));
rest[..closed].to_owned()
}
fn published(model: &RecordModel) -> (String, String) {
let graphql = model.graphql.as_ref().expect("the wire face");
let tokens = model
.page_names(graphql, true)
.to_token_stream()
.to_string();
(
word(&tokens, "VideoTrackConnectionName"),
word(&tokens, "VideoTrackEdgeName"),
)
}
fn list_field(model: &RecordModel) -> String {
let graphql = model.graphql.as_ref().expect("the wire face");
root_field(&crate::emit::wire::published_plural(graphql, model))
}
#[test]
fn a_page_type_is_a_root_fields_exact_inverse() {
assert_eq!(page_type("corpora"), "Corpora");
assert_eq!(page_type("Corpora"), "Corpora");
assert_eq!(page_type(&root_field("VideoTracks")), "VideoTracks");
assert_eq!(root_field(&page_type("videoTracks")), "videoTracks");
assert_eq!(page_type("IPTCRecords"), "IPTCRecords");
assert_eq!(page_type("xmlDocs"), "XmlDocs");
assert_eq!(page_type(""), "");
assert_eq!(page_type("\u{fb01}les"), "FIles");
}
#[test]
fn the_base_tier_is_the_records_own_plural() {
let model = record(|_| {});
assert_eq!(
published(&model),
(
"VideoTracksConnection".to_owned(),
"VideoTracksEdge".to_owned()
)
);
assert_eq!(
list_field(&model),
"videoTracks",
"the same word the field is named from"
);
}
#[test]
fn one_declared_plural_moves_the_field_the_page_and_the_edge() {
let model = record(|graphql| graphql.plural = Some("corpora".to_owned()));
assert_eq!(
(list_field(&model), published(&model).0, published(&model).1),
(
"corpora".to_owned(),
"CorporaConnection".to_owned(),
"CorporaEdge".to_owned()
),
"one declaration, one word, three names"
);
}
#[test]
fn a_declared_plural_reaches_one_pair_of_names_however_it_was_cased() {
let lower = record(|graphql| graphql.plural = Some("corpora".to_owned()));
let upper = record(|graphql| graphql.plural = Some("Corpora".to_owned()));
assert_eq!(published(&lower), published(&upper));
assert_eq!(list_field(&lower), list_field(&upper));
}
#[test]
fn a_rename_covers_the_plural_for_the_one_name_it_writes() {
let model = record(|graphql| {
graphql.plural = Some("corpora".to_owned());
graphql.connection = Some("CorpusPage".to_owned());
});
assert_eq!(
published(&model),
("CorpusPage".to_owned(), "CorporaEdge".to_owned()),
"the page is the user's word; the edge is still the plural's"
);
assert_eq!(
list_field(&model),
"corpora",
"and a page rename says nothing about the field"
);
}
#[test]
fn the_edge_rides_the_same_tower_the_page_does() {
let base = record(|_| {});
let plural = record(|graphql| graphql.plural = Some("corpora".to_owned()));
let renamed = record(|graphql| {
graphql.plural = Some("corpora".to_owned());
graphql.edge = Some("CorpusItem".to_owned());
});
assert_eq!(published(&base).1, "VideoTracksEdge");
assert_eq!(published(&plural).1, "CorporaEdge");
assert_eq!(published(&renamed).1, "CorpusItem");
assert_eq!(
published(&renamed).0,
"CorporaConnection",
"and an edge rename leaves the page on the tier below it"
);
}
#[test]
fn the_top_tier_wins_over_every_tier_below_it() {
let model = record(|graphql| {
graphql.plural = Some("corpora".to_owned());
graphql.connection = Some("CorpusPage".to_owned());
graphql.edge = Some("CorpusItem".to_owned());
});
assert_eq!(
published(&model),
("CorpusPage".to_owned(), "CorpusItem".to_owned())
);
let graphql = model.graphql.as_ref().expect("the wire face");
let tokens = model
.page_names(graphql, true)
.to_token_stream()
.to_string();
for covered in ["CorporaConnection", "CorporaEdge", "VideoTracksConnection"] {
assert!(
!tokens.contains(covered),
"`{covered}` is a tier the declaration covered, and it survives nowhere — not even in the \
prose:\n{tokens}"
);
}
}