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-time reader accepts, and what it refuses.
//!
//! The CROSS-CRATE half of these assertions is not here — `ingraph-derive`'s `duration_parity`
//! suite is where one corpus goes through both entries and the table's bytes are compared. What is
//! here is this entry's own contract, so a failure reads as *this crate's reader is wrong* rather
//! than as *the two disagree*.

use super::parse;

/// jiff answers everything at hours and below, in both of its notations and with a sign.
#[test]
fn jiff_answers_the_sub_day_units() {
  for (text, seconds) in [
    ("PT1H30M30S", 5_430),
    ("1h 30m 30s", 5_430),
    ("1h30m30s", 5_430),
    ("10m", 600),
    ("-PT2S", -2),
    ("0s", 0),
  ] {
    assert_eq!(
      parse(text)
        .unwrap_or_else(|e| panic!("{text}: {e}"))
        .as_secs(),
      seconds,
      "{text}"
    );
  }
}

/// The table answers the four calendar units jiff declines, at their stated lengths.
#[test]
fn the_table_answers_the_calendar_units() {
  for (text, seconds) in [
    ("1d", 86_400),
    ("2w", 1_209_600),
    ("1month", 2_630_016),
    ("1M", 2_630_016),
    ("1y", 31_557_600),
  ] {
    assert_eq!(
      parse(text)
        .unwrap_or_else(|e| panic!("{text}: {e}"))
        .as_secs(),
      seconds,
      "{text}"
    );
  }
}

/// `1M` is a MONTH and `1m` is a minute — the sharpest edge in the grammar, kept.
#[test]
fn the_month_and_the_minute_differ_by_case() {
  assert_eq!(parse("1M").expect("a month").as_secs(), 2_630_016);
  assert_eq!(parse("1m").expect("a minute").as_secs(), 60);
}

/// A whole string the table reads, in any unit order — which is why the sub-day rows are in it.
#[test]
fn the_table_reads_a_compound_in_any_order() {
  assert_eq!(parse("1s 1h").expect("a compound").as_secs(), 3_601);
  assert_eq!(parse("1d 1hrs").expect("a compound").as_secs(), 90_000);
}

/// A bare number is refused by both grammars rather than guessed — `0` alone excepted.
#[test]
fn a_unitless_number_is_refused_and_zero_is_not() {
  assert!(parse("5400").is_err());
  assert!(parse("").is_err());
  assert_eq!(parse("0").expect("bare zero").as_secs(), 0);
}

/// A value the grammar reads and `SignedDuration` cannot hold refuses at the crossing.
#[test]
fn a_value_past_the_carrier_refuses_rather_than_wrapping() {
  assert!(parse("292471208677y").is_err());
}