ingraph-derive 0.1.0

The attribute and derive macros for ingraph: the words a record, node, key, filter and vocabulary declaration is written in.
Documentation
//! The WIRE BLOCK's axis — a DOMAIN block, then a BACKEND block, then that cell's entries.
//!
//! The union device wrote this axis first and its note is the charter: *a grammar that grows a
//! second consumer writes the same two words at the same depth*. This is that second consumer, and
//! what these tests pin is that it writes them at the same depth rather than at a convenient one.

use ingraph_codegen::{model::GraphqlKind, newtype::Gate};

/// A newtype wearing whatever struct-level block a case hands it.
fn declared(
  block: proc_macro2::TokenStream,
) -> syn::Result<ingraph_codegen::newtype::NewTypeModel> {
  super::parse(&syn::parse_quote! {
    #[ingraph(#block)]
    pub struct Tag(String);
  })
}

/// Every refusal a block earns, joined.
fn refusals(block: proc_macro2::TokenStream) -> String {
  let Err(refused) = declared(block) else {
    panic!("the block is refused")
  };
  refused
    .into_iter()
    .map(|error| error.to_string())
    .collect::<Vec<_>>()
    .join(" | ")
}

/// **THE FLAT SPELLING IS REFUSED**, and it is refused as what it is: `scalar` is not a BACKEND.
///
/// There is no special-case check for it. A wire word one level up falls through the
/// unknown-backend road, and the empty guard then prints the nested form — so a flat writer gets two
/// sentences that together teach the axis.
#[test]
fn a_wire_word_written_one_level_up_is_an_unknown_backend() {
  let refused = refusals(quote::quote!(graphql(scalar)));
  assert!(
    refused.contains("unknown backend in `graphql(…)`"),
    "{refused}"
  );
  assert!(refused.contains("one level down"), "{refused}");
  assert!(
    refused.contains("graphql(async_graphql(scalar))"),
    "the refusal spells the nested form: {refused}"
  );
}

/// **A backend this framework does not ship names no face**, and the near miss is measured against
/// the roster rather than against the words one level down.
#[test]
fn a_backend_this_framework_does_not_ship_is_refused() {
  let refused = refusals(quote::quote!(graphql(juniper(scalar))));
  assert!(
    refused.contains("unknown backend in `graphql(…)`"),
    "{refused}"
  );
}

/// **ONE BLOCK holds every backend**, and one word per backend inside it.
#[test]
fn the_domain_block_and_its_cells_are_each_declared_once() {
  let domain = refusals(quote::quote!(
    graphql(async_graphql(scalar)),
    graphql(async_graphql(input))
  ));
  assert!(
    domain.contains("`graphql(…)` is declared more than once"),
    "{domain}"
  );

  let backend = refusals(quote::quote!(graphql(
    async_graphql(scalar),
    async_graphql(input)
  )));
  assert!(
    backend.contains("is declared more than once in `graphql(…)`"),
    "{backend}"
  );

  let face = refusals(quote::quote!(graphql(async_graphql(input, input))));
  assert!(
    face.contains("`input` is declared more than once in this backend's block"),
    "{face}"
  );
}

/// **A block naming no backend, and a backend dressing the type for no face**, are the two empty
/// shapes — each refused with the form to write instead.
#[test]
fn an_empty_block_at_either_depth_is_refused() {
  let no_backend = refusals(quote::quote!(graphql));
  assert!(no_backend.contains("names no backend"), "{no_backend}");
  assert!(
    no_backend.contains(&format!(
      "graphql({first}(scalar))",
      first = GraphqlKind::ALL[0].as_str()
    )),
    "{no_backend}"
  );

  let no_face = refusals(quote::quote!(graphql(async_graphql)));
  assert!(
    no_face.contains("dresses this newtype for no face"),
    "{no_face}"
  );
}

/// **The three faces a schema has for a declared value**, and a word naming none of them is refused
/// with the list.
#[test]
fn the_backend_block_takes_the_three_faces() {
  let model = declared(quote::quote!(graphql(async_graphql(scalar, input, output))))
    .expect("the three faces");
  assert!(model.kits.scalar.is_some());
  assert!(model.kits.input);
  assert!(model.kits.output);

  let refused = refusals(quote::quote!(graphql(async_graphql(operand))));
  assert!(
    refused.contains("unknown entry in a newtype's `async_graphql` block"),
    "{refused}"
  );
}

/// **`nullable` is the roster's word and this round does not build it** — refused by name, with the
/// measurement that says why a pass-through would add nothing.
#[test]
fn the_nullable_word_refuses_by_name_and_says_where_the_face_already_comes_from() {
  let refused = refusals(quote::quote!(graphql(async_graphql(nullable))));
  assert!(
    refused.contains("`nullable` is a word this roster carries"),
    "{refused}"
  );
  assert!(refused.contains("FILTER KINDS"), "{refused}");
  assert!(
    !refused.contains("did you mean"),
    "a word the sentence refuses by name earns no near miss: {refused}"
  );
}

/// **`scalar` is the ONE cell with options**, and both of its words reach the model.
#[test]
fn the_scalar_cell_carries_the_schema_name_and_the_door() {
  let model = declared(quote::quote!(graphql(async_graphql(scalar(
    rename = "Subtag",
    parse = "door"
  )))))
  .expect("a renamed scalar with a text door");
  let scalar = model.kits.scalar.as_ref().expect("the kit");
  assert_eq!(scalar.rename.as_deref(), Some("Subtag"));
  assert!(matches!(scalar.gate, Some(Gate::Parse(_))));

  let bare = declared(quote::quote!(graphql(async_graphql(scalar)))).expect("a bare scalar");
  let kit = bare.kits.scalar.as_ref().expect("the kit");
  assert!(kit.rename.is_none());
  assert!(kit.gate.is_none());
}

/// **A schema name is GraphQL's Name grammar**, checked at the literal where the word settles.
#[test]
fn a_schema_name_outside_graphqls_grammar_is_refused() {
  let refused = refusals(quote::quote!(graphql(async_graphql(scalar(
    rename = "Sub Tag"
  )))));
  assert!(refused.contains("Sub Tag"), "{refused}");
}

/// **`input` and `output` take nothing** — every part of the face they install is the wrapped type's
/// own answer.
#[test]
fn the_two_crossings_take_no_options() {
  let refused = refusals(quote::quote!(graphql(async_graphql(output(rename = "X")))));
  assert!(refused.contains("`output` takes no options"), "{refused}");
}