1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
mod common;

use treaty::{
    any::OwnedStatic,
    effect::blocking::Blocking,
    protocol::{
        visitor::{request_hint, ValueProto},
        DynWalker,
    },
    BuildExt as _, Builder as _, Flow,
};

use crate::common::protocol::hint::MockHintWalker;

#[test]
fn value_builder_gives_value_protocol_as_hint() {
    // Create a builder for a i32.
    let mut builder = i32::new_builder();

    // Expect the value builder to hint the value protocol with a owned i32.
    let mut walker = MockHintWalker::<ValueProto<OwnedStatic<i32>, Blocking>>::new();
    walker.expect_hint().once().returning(|visitor, ()| {
        // Fulfill the hint by visiting with a i32 value.
        assert_eq!(visitor.visit(OwnedStatic(42)).value(), Flow::Done.into());

        // We are done as the walker.
        Flow::Done
    });

    // Request a hint from the i32 builder for what protocol to use.
    assert_eq!(
        request_hint::<Blocking>(builder.as_visitor(), DynWalker(&mut walker)).value(),
        Flow::Done.into()
    );

    // The builder should have the value.
    assert_eq!(builder.build().value().unwrap(), 42);
}