ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
//! What the sink promises a converter: every push is its own segment, and no two of them merge.
//!
//! The property under test is the one the type exists for — a boundary the author drew is a boundary
//! the reader sees — so each test reads through [`BoxWriter::segments`] and never through the buffer,
//! which nothing hands out.

use super::BoxWriter;

/// Every push is one segment, in order. The whole contract, and the reason the converter takes a sink
/// instead of returning a string: a returned string is ONE segment however many values built it.
#[test]
fn each_push_is_its_own_segment() {
  let mut out = BoxWriter::new();
  out.push("red car");
  out.push(String::from("blue sky"));
  out.push("green");
  assert_eq!(
    out.segments().collect::<Vec<_>>(),
    ["red car", "blue sky", "green"]
  );
}

/// The boundary is not a character in the text, so nothing a converter pushes can forge one and
/// nothing joins two segments into a phrase that neither contains. Pinned with a value that CONTAINS
/// the separator a joined implementation would have used.
#[test]
fn segments_never_merge_and_no_separator_exists_to_forge() {
  let mut out = BoxWriter::new();
  out.push("red");
  out.push("car");
  assert_eq!(out.segments().collect::<Vec<_>>(), ["red", "car"]);

  let mut spaced = BoxWriter::new();
  spaced.push("red car");
  assert_eq!(
    spaced.segments().collect::<Vec<_>>(),
    ["red car"],
    "one push is one segment whatever it holds"
  );
}

/// An empty push is a segment. Dropping it would silently change the segmentation the converter chose
/// — a converter that means to skip a value does not push it.
#[test]
fn an_empty_push_is_still_a_segment() {
  let mut out = BoxWriter::new();
  out.push("");
  assert!(!out.is_empty(), "one segment was recorded");
  assert_eq!(out.segments().collect::<Vec<_>>(), [""]);
  assert_eq!(out.segments().len(), 1);
}

/// A writer nothing was pushed into has no segments — the shape a converter over an empty collection
/// leaves behind.
#[test]
fn a_writer_nothing_pushed_into_is_empty() {
  let out = BoxWriter::new();
  assert!(out.is_empty());
  assert_eq!(out.segments().len(), 0);
  assert_eq!(out.segments().next(), None);
  assert!(BoxWriter::default().is_empty(), "`default` is `new`");
}

/// The count comes off the iterator rather than a second accessor, so there is one authority on it and
/// it cannot disagree with what the walk yields.
#[test]
fn the_segment_count_is_the_iterators() {
  let mut out = BoxWriter::new();
  for text in ["a", "bb", "ccc"] {
    out.push(text);
  }
  assert_eq!(out.segments().len(), out.segments().count());
  assert_eq!(out.segments().len(), 3);
}

/// Multi-byte text pushes and reads back whole: the offsets are byte offsets into one buffer, so a
/// boundary landing mid-character would panic rather than corrupt — asserted, because the buffer is
/// sliced by index.
#[test]
fn multi_byte_segments_slice_on_their_own_boundaries() {
  let mut out = BoxWriter::new();
  out.push("日本語");
  out.push("café");
  assert_eq!(out.segments().collect::<Vec<_>>(), ["日本語", "café"]);
}

/// The two builtins ON THE SAME VALUES, side by side, because the difference between them IS the
/// declaration: `each` leaves two segments a phrase cannot span, `join` leaves one it can.
#[test]
fn the_builtins_differ_in_exactly_one_thing_and_it_is_the_boundary() {
  let values = ["red car", "blue sky"];

  let mut separated = BoxWriter::new();
  separated.push_each(values);
  assert_eq!(
    separated.segments().collect::<Vec<_>>(),
    ["red car", "blue sky"],
    "one append per value"
  );

  let mut joined = BoxWriter::new();
  joined.push_joined(values, ", ");
  assert_eq!(
    joined.segments().collect::<Vec<_>>(),
    ["red car, blue sky"],
    "one append, and `car, blue` is now a phrase the index will match"
  );
}

/// Both take an `IntoIterator` of anything renderable, so a collection of `String` and a slice of
/// `&str` reach them the same way — which is what lets a call write `push_each(&self.field)` without
/// knowing the field's exact spelling.
#[test]
fn the_builtins_take_the_field_however_it_is_spelled() {
  let owned = vec![String::from("a"), String::from("b")];
  let borrowed: &[&str] = &["a", "b"];

  let mut from_owned = BoxWriter::new();
  from_owned.push_each(&owned);
  let mut from_borrowed = BoxWriter::new();
  from_borrowed.push_each(borrowed);
  assert_eq!(
    from_owned.segments().collect::<Vec<_>>(),
    from_borrowed.segments().collect::<Vec<_>>()
  );

  let mut joined = BoxWriter::new();
  joined.push_joined(&owned, "-");
  assert_eq!(joined.segments().collect::<Vec<_>>(), ["a-b"]);
}

/// The empty collection, where the two builtins DISAGREE and each is right by its own definition:
/// "one segment per value" over nothing is nothing, and "one append of the joined values" over
/// nothing is one append of the empty string — `[].join(sep)` in std's own terms.
#[test]
fn the_builtins_read_an_empty_collection_by_their_own_definitions() {
  let none: [&str; 0] = [];

  let mut separated = BoxWriter::new();
  separated.push_each(none);
  assert!(separated.is_empty(), "no values, no appends");

  let mut joined = BoxWriter::new();
  joined.push_joined(none, ", ");
  assert_eq!(
    joined.segments().collect::<Vec<_>>(),
    [""],
    "one append either way — and `push`'s rule is that an empty one is still a segment"
  );
}

/// A join lands in the buffer as ONE segment however many values built it, and a segment already
/// there is untouched — the separator is written between values, never before the first.
#[test]
fn a_join_is_one_segment_beside_whatever_was_already_pushed() {
  let mut out = BoxWriter::new();
  out.push("a caption");
  out.push_joined(["red", "blue"], " / ");
  out.push_joined(["only"], " / ");
  assert_eq!(
    out.segments().collect::<Vec<_>>(),
    ["a caption", "red / blue", "only"],
    "three appends: one push and two joins, and no separator leaks to an edge"
  );
}