blockworx_doc/fixtures.rs
1//! Test builders shared across the module's suites.
2//!
3//! Only what is genuinely identical everywhere lives here — typed ids, the
4//! commit constructor, the visible projection. Per-suite fixtures that
5//! encode a *test's* intent (which rect, which init values) stay with
6//! their tests, where the values can be read next to the assertions they
7//! feed.
8
9use crate::{
10 commit::Commit,
11 document::Document,
12 geometry::GridRect,
13 id::{AreaId, BlockId, ImageId, PinId, RouteId, RouteLabelId, TextId},
14 opcode::OpCodes,
15};
16
17pub mod scale;
18macro_rules! id_fixtures {
19 ($( $name:ident -> $ty:ty ),* $(,)?) => {
20 $(
21 /// The id numbered `n` — the door around the
22 /// [`Allocator`](crate::id::Allocator) a fixture needs to name
23 /// an entity before it builds one.
24 pub fn $name(n: u32) -> $ty {
25 <$ty>::from_raw(n)
26 }
27 )*
28 };
29}
30
31id_fixtures! {
32 block_id -> BlockId,
33 pin_id -> PinId,
34 route_id -> RouteId,
35 route_label_id -> RouteLabelId,
36 text_id -> TextId,
37 area_id -> AreaId,
38 image_id -> ImageId,
39}
40
41/// A commit's identity, for a suite that keeps its own history beside a
42/// repo's. Real ones are assigned by `Repo::submit`; naming a rev no repo
43/// assigned is exactly what a history step's check refuses, so this is also
44/// how that refusal is tested.
45#[must_use]
46pub fn rev(n: u64) -> crate::rev::Rev {
47 crate::rev::Rev::new(n)
48}
49
50pub fn commit(label: &str, ops: Vec<OpCodes>) -> Commit {
51 Commit::new(label.into(), ops)
52}
53
54/// The user-visible projection: the blocks and their values, without revs
55/// or write orders. What an undo must restore, and no more.
56pub fn projection(doc: &Document) -> Vec<(BlockId, BlockId, GridRect, String)> {
57 let mut blocks: Vec<_> = doc
58 .blocks()
59 .map(|(id, block)| (id, block.parent, block.rect, block.title.name.clone()))
60 .collect();
61 blocks.sort_by_key(|(id, ..)| *id);
62 blocks
63}