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 ORIGIN TIER's word**, and the rules it composes under.
//!
//! Beside the source suite rather than inside it, for [`super::interface_tests`]'s reason: a word
//! that arrives after a grammar has been proven reads as one file, and the roster stays the one it
//! was.

use super::tests::{accepted, refused};

/// **THE TIER WORD PUTS THE FIELD ON ITS OWN LIST**, and off the node one — which is the whole of
/// what it buys: the storage walk, the root query and the create pass's node predicates are each
/// written about a member an origin is not.
#[test]
fn an_origin_field_is_no_node_and_no_resource() {
  let model = accepted(quote::quote! {
    pub struct App {
      videos: media::Video,
      #[ingraph(origin)]
      watch: app::Watch,
    }
  });

  assert_eq!(model.nodes.len(), 1, "one field of two is a node");
  assert_eq!(model.mounts.len(), 1);
  assert!(model.resources.is_empty());
  assert!(model.interfaces.is_empty());
  assert!(model.fused.is_empty());
  assert_eq!(model.origins.len(), 1);
  assert_eq!(model.origins[0].mount, "watch");
  assert_eq!(
    quote::ToTokens::to_token_stream(&model.origins[0].ty).to_string(),
    "app :: Watch"
  );
}

/// **THE WORD IS BARE**, in both shapes a non-bare one could take: WHICH origin this is is the
/// field's own type, and its face, its command node and what it produces are written in that
/// source's own declaration.
#[test]
fn the_tier_word_takes_no_value_and_no_block() {
  for written in [
    quote::quote!(#[ingraph(origin = "app::Watch")]),
    quote::quote!(#[ingraph(origin(async))]),
  ] {
    let refusal = refused(quote::quote! {
      pub struct App {
        videos: media::Video,
        #written
        watch: app::Watch,
      }
    });

    assert!(refusal.contains("BARE word"), "{refusal}");
  }
}

/// **AND IT IS WRITTEN ONCE**, for the reason every set-once word in this grammar is.
#[test]
fn the_tier_word_is_declared_once() {
  let refusal = refused(quote::quote! {
    pub struct App {
      videos: media::Video,
      #[ingraph(origin, origin)]
      watch: app::Watch,
    }
  });

  assert!(refusal.contains("more than once"), "{refusal}");
}

/// The UNKNOWN sentence names the word, so a reader who misspells it is told the spelling that
/// exists rather than only that theirs does not.
#[test]
fn the_unknown_refusal_names_the_tier_word() {
  let refusal = refused(quote::quote! {
    pub struct App {
      videos: media::Video,
      #[ingraph(origins)]
      watch: app::Watch,
    }
  });

  assert!(refusal.contains("`origin`"), "{refusal}");
}

/// **A MEMBER IS ON ONE TIER**, and each pairing gets its own sentence because each has its own fix.
#[test]
fn an_origin_is_on_no_other_tier() {
  for (written, said) in [
    (quote::quote!(#[ingraph(origin, resource)]), "hands out"),
    (quote::quote!(#[ingraph(origin, interface)]), "contract"),
    (quote::quote!(#[ingraph(origin, fuse)]), "corpus"),
  ] {
    let refusal = refused(quote::quote! {
      pub struct App {
        videos: media::Video,
        #written
        watch: app::Watch,
      }
    });

    assert!(refusal.contains("ONE tier"), "{refusal}");
    assert!(refusal.contains(said), "{refusal}");
  }
}

/// **AN ORIGIN ANSWERS NOBODY.** A root query folds the fields its members answer, and a source
/// declaration emits no query unit at all — so the word would be a claim with nothing behind it, and
/// the sentence says where the face an origin DOES publish is written.
#[test]
fn an_origin_field_answers_no_root_query() {
  let refusal = refused(quote::quote! {
    pub struct App {
      videos: media::Video,
      #[ingraph(origin, query)]
      watch: app::Watch,
    }
  });

  assert!(refusal.contains("takes no `query`"), "{refusal}");
  assert!(refusal.contains("mutation(node"), "{refusal}");
}

/// **THE ONE HALF OF *AN ORIGIN NOBODY LISTENS TO* THIS PASS CAN DECIDE**: only the node tier
/// declares upstreams, so a root with no node has an origin whose consumer could not exist.
///
/// One row PER ORIGIN, because each field is a thing an author fixes.
#[test]
fn an_origin_on_a_root_with_no_node_can_have_no_consumer() {
  let refusal = refused(quote::quote! {
    pub struct App {
      #[ingraph(origin)]
      watch: app::Watch,
      #[ingraph(origin)]
      poll: app::Poll,
      #[ingraph(resource)]
      egemma: model::Egemma,
    }
  });

  assert_eq!(
    refusal.matches("can have no consumer").count(),
    2,
    "one row per origin field: {refusal}"
  );
}

/// And a root that DOES mount a node says nothing of the kind — whether that node listens is written
/// in its own declaration, which this pass cannot read and does not pretend to.
#[test]
fn a_root_that_mounts_a_node_is_left_to_the_launch() {
  let model = accepted(quote::quote! {
    pub struct App {
      #[ingraph(origin)]
      watch: app::Watch,
      discover: app::Discover,
    }
  });

  assert_eq!(model.origins.len(), 1);
  assert_eq!(model.nodes.len(), 1);
}