use core::{ cell::RefCell, fmt::{Display, Error as fmtError, Formatter}, }; use rhizome::{error::Error, extensions::*, Node, Provision, TypeKey}; use std::error::Error as stdError; type V = RefCell; #[test] fn shared_at_root() { let root = Node::::new_for::().into_arc(); let branch_a = root.derive_for::(); let branch_b = root.derive_for::(); *branch_a.extract_with::().unwrap().borrow_mut() += 1; assert_eq!(*branch_b.extract_with::().unwrap().borrow(), 1); } #[test] fn only_at_a() { let root = Node::::new_for::().into_arc(); let branch_a = root.derive_for::(); let branch_b = root.derive_for::(); assert_eq!(*branch_a.extract_with::().unwrap().borrow(), 0); assert_eq!( branch_b.extract_with::().unwrap_err(), Error::NoTagMatched ); } #[test] fn not_shared() { let root = Node::::new_for::().into_arc(); let branch_a = root.derive_for::(); let branch_b = root.derive_for::(); *branch_a.extract_with::().unwrap().borrow_mut() += 1; assert_eq!(*branch_b.extract_with::().unwrap().borrow(), 0); } #[test] fn manual_provision() { let mut root = Node::::new_for::(); assert_eq!( root.extract_with::().unwrap_err(), Error::NoDefault ); root.provide(KManual::key(), V::default()).unwrap(); root.extract_with::().unwrap(); } struct RootOwner; struct AOwner; struct BOwner; #[derive(Debug)] enum Never {} impl stdError for Never {} impl Display for Never { fn fmt(&self, _: &mut Formatter<'_>) -> Result<(), fmtError> { todo!() } } enum K {} impl TypeKey for K { fn provision() -> Provision { Provision::at_root(factory) } } enum KA {} impl TypeKey for KA { fn provision() -> Provision { Provision::at_owner::(factory) } } enum KManual {} impl TypeKey for KManual {} fn factory(_: &Node) -> Result { Ok(V::default()) }