use crate::graph::GraphModel;
pub(super) fn app() -> GraphModel {
GraphModel {
krate: syn::parse_quote!(::ingraph),
vis: syn::parse_quote!(pub),
name: syn::parse_quote!(App),
nodes: vec![
syn::parse_quote!(media::Video),
syn::parse_quote!(Scene),
syn::parse_quote!(TranscriptText),
],
mounts: vec![
syn::parse_quote!(videos),
syn::parse_quote!(scenes),
syn::parse_quote!(search),
],
queried: Vec::new(),
interfaces: Vec::new(),
fused: Vec::new(),
resources: Vec::new(),
origins: Vec::new(),
engines: Vec::new(),
retired: None,
generations: None,
}
}
pub(super) fn compact(model: &GraphModel) -> String {
rendered(model).split_whitespace().collect()
}
pub(super) fn rendered(model: &GraphModel) -> String {
prettyplease::unparse(&syn::parse2(model.options().emit()).expect("the spine parses as a file"))
}
#[test]
fn the_spine_is_named_after_the_root() {
assert_eq!(
super::default_options_name(&syn::parse_quote!(App)),
"AppOptions"
);
assert!(
compact(&app()).contains("structAppOptions{"),
"{}",
rendered(&app())
);
}
#[test]
fn every_mount_gets_a_field_holding_that_nodes_own_options() {
let model = app();
let emitted = compact(&model);
for (mount, node) in [
("videos", "media::Video"),
("scenes", "Scene"),
("search", "TranscriptText"),
] {
assert!(
emitted.contains(&format!(
"pub(crate){mount}:::core::option::Option<::ingraph::__private::drive::NodeSection<<{node}\
as::ingraph::__private::drive::AsyncConstructor>::Options,<{node}as::ingraph::__private::drive\
::Declared>::Channels>>"
)),
"`{mount}` holds `{node}`'s own options and its own shelf: {}",
rendered(&model)
);
}
}
#[test]
fn every_section_is_optional() {
let model = app();
let emitted = compact(&model);
assert_eq!(
emitted
.matches("::core::option::Option<::ingraph::__private::drive::NodeSection<")
.count(),
model.mounts.len() * 2,
"{}",
rendered(&model)
);
}
#[test]
fn every_field_defaults_so_a_partial_document_parses() {
let model = app();
let emitted = compact(&model);
assert_eq!(
emitted.matches("#[serde(default)]").count(),
model.mounts.len() + 1,
"{}",
rendered(&model)
);
}
#[test]
fn the_spine_denies_what_it_does_not_declare() {
let model = app();
let emitted = compact(&model);
assert!(
emitted.contains(r#"#[serde(crate="::ingraph::__private::serde",deny_unknown_fields)]"#),
"{}",
rendered(&model)
);
}
#[test]
fn the_declaration_is_written_once_inside_each_half_of_the_serde_gate() {
let model = app();
let emitted = compact(&model);
let with = emitted
.find("if_serde!")
.expect("the deserialized half rides the gate");
let without = emitted
.find("if_no_serde!")
.expect("the plain half rides the complement");
assert!(with < without, "{}", rendered(&model));
assert_eq!(
emitted.matches("structAppOptions{").count(),
2,
"one declaration per half: {}",
rendered(&model)
);
assert_eq!(
emitted.matches("#[serde(").count(),
1 + model.mounts.len() + 1
);
assert!(emitted.rfind("#[serde(").unwrap_or(usize::MAX) < without);
}
#[test]
fn the_accessors_are_emitted_once_for_both_halves() {
let model = app();
let emitted = compact(&model);
assert_eq!(emitted.matches("implAppOptions{").count(), 1);
for mount in ["videos", "scenes", "search"] {
assert_eq!(
emitted.matches(&format!("pubfnwith_{mount}(")).count(),
1,
"{}",
rendered(&model)
);
assert_eq!(
emitted.matches(&format!("pubfn{mount}(&self")).count(),
1,
"{}",
rendered(&model)
);
}
}
#[test]
fn the_debug_reports_presence_rather_than_the_options() {
let model = app();
let emitted = compact(&model);
assert!(
emitted.contains("impl::core::fmt::DebugforAppOptions{"),
"{}",
rendered(&model)
);
for mount in ["videos", "scenes", "search"] {
assert!(
emitted.contains(&format!(
r#".field("{mount}",&::core::option::Option::is_some(&self.{mount}))"#
)),
"`{mount}` reports its presence: {}",
rendered(&model)
);
}
assert!(
!emitted.contains("::core::fmt::Debug>"),
"no options type is bounded: {}",
rendered(&model)
);
}
#[test]
fn the_bridge_reads_every_mount_and_not_only_the_ones_a_document_carried() {
let model = app();
let emitted = compact(&model);
assert_eq!(
emitted
.matches("pubfnenablement(&self)->::ingraph::__private::drive::Enablement{")
.count(),
1,
"{}",
rendered(&model)
);
for mount in ["videos", "scenes", "search"] {
assert!(
emitted.contains(&format!(
r#".mounted("{mount}",::core::option::Option::as_ref(&self.{mount}))"#
)),
"`{mount}` reaches the bridge: {}",
rendered(&model)
);
}
assert!(
emitted.contains("::ingraph::__private::drive::Enablement::new()."),
"{}",
rendered(&model)
);
let empty = GraphModel {
nodes: Vec::new(),
mounts: Vec::new(),
..app()
};
assert!(
compact(&empty).contains("Enablement{::ingraph::__private::drive::Enablement::new()}"),
"{}",
rendered(&empty)
);
}
#[test]
fn the_spine_takes_the_roots_visibility() {
let mut private = app();
private.vis = syn::Visibility::Inherited;
let emitted = compact(&private);
assert!(
!emitted.contains("pubstructAppOptions") && emitted.contains("structAppOptions"),
"{}",
rendered(&private)
);
assert!(
!emitted.contains("pubfnwith_videos") && emitted.contains("fnwith_videos"),
"{}",
rendered(&private)
);
}
#[test]
fn the_registry_seat_is_on_every_root() {
let model = app();
let emitted = compact(&model);
assert!(
emitted.contains("pub(crate)registry:::ingraph::__private::drive::RegistryOptions,"),
"{}",
rendered(&model)
);
assert!(
emitted.contains("pubconstfnregistry(&self)->&::ingraph::__private::drive::RegistryOptions{"),
"{}",
rendered(&model)
);
assert!(
emitted.contains(
"pubfnwith_registry(mutself,registry:::ingraph::__private::drive::RegistryOptions,)->Self{"
),
"{}",
rendered(&model)
);
let empty = GraphModel {
nodes: Vec::new(),
mounts: Vec::new(),
..app()
};
assert!(
compact(&empty).contains("pub(crate)registry:::ingraph::__private::drive::RegistryOptions,"),
"{}",
rendered(&empty)
);
}
#[test]
fn the_registry_seat_reports_its_value_in_debug() {
let model = app();
let emitted = compact(&model);
assert!(
emitted.contains(r#".field("registry",&self.registry)"#),
"{}",
rendered(&model)
);
}
#[test]
fn a_root_that_declared_no_seat_grows_no_generations_key() {
let emitted = compact(&app());
assert!(!emitted.contains("generations"), "{}", rendered(&app()));
}
#[test]
fn the_word_grows_the_field_the_accessors_and_the_engine_const() {
let model = GraphModel {
generations: Some(crate::graph::GenerationsSeat {
engine: crate::model::DatabaseKind::Postgres,
}),
..app()
};
let emitted = compact(&model);
for expected in [
"pub(crate)generations:::ingraph::__private::drive::GenerationsOptions,",
"pubconstGENERATIONS_ENGINE:::ingraph::__private::drive::GenerationsEngine=::ingraph::\
__private::drive::GenerationsEngine::Postgres;",
"pubconstfngenerations(&self)->&::ingraph::__private::drive::GenerationsOptions{",
"pubfnwith_generations(mutself,generations:::ingraph::__private::drive::\
GenerationsOptions,)->Self{",
r#".field("generations",&self.generations)"#,
] {
assert!(
emitted.contains(expected),
"{expected}\n{}",
rendered(&model)
);
}
}
#[test]
fn the_engine_arm_is_the_runtime_enums_own_spelling() {
for (kind, arm) in [
(crate::model::DatabaseKind::Sqlite, "Sqlite"),
(crate::model::DatabaseKind::Postgres, "Postgres"),
(crate::model::DatabaseKind::Mysql, "Mysql"),
] {
assert_eq!(kind.generations_arm(), Some(arm));
}
assert_eq!(crate::model::DatabaseKind::Mongo.generations_arm(), None);
}
#[test]
fn the_crate_path_reaches_every_framework_name() {
let mut vendored = app();
vendored.krate = syn::parse_quote!(shim::vendored::ingraph);
let emitted = compact(&vendored);
assert_eq!(
emitted.matches("__private").count(),
emitted
.matches("shim::vendored::ingraph::__private")
.count(),
"every shelf reference is under the declared root, and none keeps the default: {}",
rendered(&vendored)
);
assert!(emitted.contains(r#"crate="shim::vendored::ingraph::__private::serde""#));
}
#[test]
fn the_retired_seat_is_absent_where_no_ledger_is_declared() {
let emitted = compact(&app());
assert!(
!emitted.contains("pub(crate)retired:"),
"{}",
rendered(&app())
);
assert!(!emitted.contains("RETIRED_ENGINE"), "{}", rendered(&app()));
assert!(
!emitted.contains(r#".field("retired""#),
"{}",
rendered(&app())
);
}
#[test]
fn a_declared_ledger_grows_the_section_and_names_its_engine() {
let declared = GraphModel {
retired: Some(crate::graph::RetiredLedger {
engine: crate::model::DatabaseKind::Postgres,
}),
..app()
};
let emitted = compact(&declared);
assert!(
emitted.contains("pub(crate)retired:::ingraph::__private::drive::RetiredOptions,"),
"{}",
rendered(&declared)
);
assert!(
emitted.contains(
"pubconstRETIRED_ENGINE:::ingraph::__private::drive::RetiredEngine=::ingraph::__private::\
drive::RetiredEngine::Postgres;"
),
"{}",
rendered(&declared)
);
assert!(
emitted.contains("pubconstfnretired(&self)->&::ingraph::__private::drive::RetiredOptions{"),
"{}",
rendered(&declared)
);
assert!(
emitted.contains(
"pubfnwith_retired(mutself,retired:::ingraph::__private::drive::RetiredOptions,)->Self{"
),
"{}",
rendered(&declared)
);
assert!(
emitted.contains(r#".field("retired",&self.retired)"#),
"{}",
rendered(&declared)
);
}
#[test]
fn every_engine_word_emits_its_own_arm() {
for (kind, arm) in [
(crate::model::DatabaseKind::Sqlite, "Sqlite"),
(crate::model::DatabaseKind::Postgres, "Postgres"),
(crate::model::DatabaseKind::Mysql, "Mysql"),
(crate::model::DatabaseKind::Mongo, "Mongo"),
] {
let declared = GraphModel {
retired: Some(crate::graph::RetiredLedger { engine: kind }),
..app()
};
assert!(
compact(&declared).contains(&format!(
"=::ingraph::__private::drive::RetiredEngine::{arm};"
)),
"{}",
rendered(&declared)
);
}
}
#[test]
fn the_retired_section_defaults() {
let declared = GraphModel {
retired: Some(crate::graph::RetiredLedger {
engine: crate::model::DatabaseKind::Sqlite,
}),
..app()
};
let emitted = compact(&declared);
assert!(
emitted.contains("#[serde(default)]pub(crate)retired:"),
"{}",
rendered(&declared)
);
}