ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! What the expansion-side seam takes and what it refuses.

use super::check;

/// The classic five fields, in the shapes a schedule actually gets written in.
#[test]
fn the_classic_five_fields_are_read() {
  for expression in [
    "0 3 * * *",
    "*/15 * * * *",
    "0 3 1 * MON",
    "30 2 * * SUN",
    "0 0 1 1 *",
    "0 9-17 * * 1-5",
  ] {
    assert_eq!(check(expression, None), Ok(()), "{expression}");
  }
}

/// A timezone is the `tz` word, never a sixth field — the crate would take one, and this framework
/// does not.
#[test]
fn a_sixth_field_is_refused_by_the_count() {
  let refusal = check("0 3 * * * Asia/Shanghai", None).expect_err("six fields");
  assert!(refusal.contains("classic 5 fields"), "{refusal}");
  assert!(refusal.contains("and this one has 6"), "{refusal}");
  assert!(refusal.contains("`tz = \"<IANA>\"`"), "{refusal}");
}

/// Too FEW fields is the same rule, said with the same sentence.
#[test]
fn a_short_expression_is_refused_by_the_count() {
  let refusal = check("0 3 * *", None).expect_err("four fields");
  assert!(refusal.contains("and this one has 4"), "{refusal}");
}

/// Garbage carries the crate's own diagnosis, which names the offending field.
#[test]
fn garbage_carries_the_offending_field() {
  let refusal = check("99 3 * * *", None).expect_err("a minute past 59");
  assert!(refusal.contains("minute"), "{refusal}");
  assert!(refusal.contains("99"), "{refusal}");

  let refusal = check("0 3 * * MONDAY", None).expect_err("not a day name");
  assert!(refusal.contains("day of week"), "{refusal}");
}

/// The refusal QUOTES THE EXPRESSION with a caret on the run that broke it — the span spent, rather
/// than the crate's sentence passed through on its own.
///
/// It is the TWIN of `ingraph`'s `drive::poll::calendar::caret`, written out twice because the
/// build order gives the two crates no way to share one. `the_two_halves_render_one_caret`, one
/// crate over, asserts the same three lines from the other side, which is what keeps them one
/// rendering.
#[test]
fn a_refusal_quotes_the_expression_under_a_caret() {
  let refusal = check("99 3 * * *", None).expect_err("a minute past 59");
  let mut lines = refusal.lines();
  let sentence = lines.next().expect("a sentence");
  assert!(sentence.contains("minute"), "{refusal}");
  assert_eq!(lines.next(), Some("  99 3 * * *"), "{refusal}");
  assert_eq!(lines.next(), Some("  ^^"), "{refusal}");
  assert_eq!(lines.next(), None, "the rendering is three lines");
}

/// A caret is at least ONE column wide, because a parser that ran out of input points AT the end and
/// an empty span would otherwise mark nothing.
#[test]
fn an_empty_span_still_marks_a_column() {
  let refusal = check("0 3 * * ", None).expect_err("four fields and a space");
  // The count rule answers first here, which is itself the ordering test below — so the caret is
  // exercised through an expression whose shape is right and whose last field is empty.
  assert!(refusal.contains("has 4"), "{refusal}");

  let refusal = check("0 3 * * -", None).expect_err("a field that ends too early");
  let marked = refusal.lines().next_back().expect("a caret line");
  // The whole marker, indent stripped: it is carets and nothing else, and there is at least one of
  // them. A span that ended where it started would otherwise render an empty line under the
  // expression, which points at nothing while looking like it points at something.
  let carets = marked.trim_start();
  assert!(!carets.is_empty(), "{refusal}");
  assert!(
    carets.chars().all(|marker| marker == '^'),
    "a caret line is carets: {refusal}"
  );
}

/// A zone that exists passes; a typo does not.
#[test]
fn the_zone_word_is_a_typo_check() {
  assert_eq!(check("0 3 * * *", Some("America/New_York")), Ok(()));
  assert_eq!(check("0 3 * * *", Some("Asia/Shanghai")), Ok(()));

  // Guarded exactly as the check itself is: on a machine with no database, nothing is refused, so
  // the assertion is made only where there is something to assert.
  if jiff::tz::db().available().next().is_some() {
    let refusal = check("0 3 * * *", Some("Not/AZone")).expect_err("not a zone");
    assert!(refusal.contains("Not/AZone"), "{refusal}");
    assert!(refusal.contains("IANA"), "{refusal}");
  }
}

/// The field check runs BEFORE the zone check, so a declaration with both wrong hears about its
/// shape first — the one it has to fix before the other question means anything.
#[test]
fn the_shape_is_answered_before_the_zone() {
  let refusal = check("0 3 * *", Some("Not/AZone")).expect_err("both wrong");
  assert!(refusal.contains("and this one has 4"), "{refusal}");
}