use quote::ToTokens as _;
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 minted(model: &RecordModel) -> String {
let graphql = model.graphql.as_ref().expect("the wire face");
model
.page_names(graphql, true)
.to_token_stream()
.to_string()
}
fn unpaged(model: &RecordModel) -> String {
let graphql = model.graphql.as_ref().expect("the wire face");
model
.page_names(graphql, false)
.to_token_stream()
.to_string()
}
fn closed(model: &RecordModel) -> (String, String) {
let (connection, edge) = model.page_name_arguments();
(connection.to_string(), edge.to_string())
}
#[test]
fn a_record_that_renamed_nothing_takes_the_plural_base() {
let model = record(|_| {});
let emitted = minted(&model);
assert!(
emitted.contains("\"VideoTracksConnection\""),
"the page is the record's PLURAL, not `VideoTrackConnection`:\n{emitted}"
);
assert!(
emitted.contains("\"VideoTracksEdge\""),
"and the edge rides the same tower:\n{emitted}"
);
assert!(
!emitted.contains("DefaultConnectionName") && !emitted.contains("DefaultEdgeName"),
"the library's singular-based defaults are named nowhere:\n{emitted}"
);
assert_eq!(
closed(&model),
(
"VideoTrackConnectionName".to_owned(),
"VideoTrackEdgeName".to_owned()
),
"and the page closes on this record's own markers"
);
}
#[test]
fn a_declared_connection_word_is_minted_and_returned() {
let model = record(|graphql| graphql.connection = Some("TrackReel".to_owned()));
let emitted = minted(&model);
assert!(
emitted.contains("struct VideoTrackConnectionName"),
"the marker is declared:\n{emitted}"
);
assert!(
emitted.contains("ConnectionNameType for VideoTrackConnectionName"),
"and implements the hook:\n{emitted}"
);
assert!(
emitted.contains("\"TrackReel\""),
"the word is a string the body hands back, never an ident:\n{emitted}"
);
assert_eq!(
closed(&model),
(
"VideoTrackConnectionName".to_owned(),
"VideoTrackEdgeName".to_owned()
),
"the page takes the pair whichever tier decided each word"
);
assert!(
emitted.contains("\"VideoTracksEdge\""),
"and the edge, unasked, keeps the tower's base:\n{emitted}"
);
}
#[test]
fn an_edge_word_alone_moves_only_the_edge() {
let model = record(|graphql| graphql.edge = Some("TrackItem".to_owned()));
let emitted = minted(&model);
assert!(
emitted.contains("struct VideoTrackEdgeName"),
"the edge marker:\n{emitted}"
);
assert!(
emitted.contains("EdgeNameType for VideoTrackEdgeName"),
"and its hook:\n{emitted}"
);
assert!(
emitted.contains("\"TrackItem\""),
"carrying the declared word:\n{emitted}"
);
assert!(
emitted.contains("\"VideoTracksConnection\"") && !emitted.contains("\"TrackItemConnection\""),
"and the page keeps the plural, derived from NOTHING the edge wrote:\n{emitted}"
);
}
#[test]
fn both_words_mint_two_markers_and_close_both_positions() {
let model = record(|graphql| {
graphql.connection = Some("TrackReel".to_owned());
graphql.edge = Some("SplicePoint".to_owned());
});
let emitted = minted(&model);
assert!(emitted.contains("\"TrackReel\""), "{emitted}");
assert!(emitted.contains("\"SplicePoint\""), "{emitted}");
assert!(
!emitted.contains("VideoTracksConnection") && !emitted.contains("VideoTracksEdge"),
"and the tower's base is nowhere, both tiers having been covered:\n{emitted}"
);
assert_eq!(
closed(&model),
(
"VideoTrackConnectionName".to_owned(),
"VideoTrackEdgeName".to_owned()
)
);
}
#[test]
fn the_markers_rust_name_follows_the_record_and_not_the_word() {
let model = record(|graphql| graphql.connection = Some("TrackReel".to_owned()));
let emitted = minted(&model);
assert!(
emitted.contains("struct VideoTrackConnectionName"),
"{emitted}"
);
assert!(
!emitted.contains("struct TrackReel") && !emitted.contains("struct VideoTracksConnection"),
"the word names a schema type and never a Rust one:\n{emitted}"
);
}
#[test]
fn a_record_with_no_page_mints_nothing_even_having_declared_both_words() {
let model = record(|graphql| {
graphql.connection = Some("TrackReel".to_owned());
graphql.edge = Some("TrackItem".to_owned());
});
assert!(
unpaged(&model).is_empty(),
"not one token where there is no page to name:\n{}",
unpaged(&model)
);
assert!(
!minted(&model).is_empty(),
"and the same declaration WITH a page does mint"
);
}
#[test]
fn a_record_with_no_page_mints_nothing_having_declared_neither_word() {
let model = record(|_| {});
assert!(
unpaged(&model).is_empty(),
"the plural base is a word for a page, and there is no page:\n{}",
unpaged(&model)
);
}