test.rs

 1use gpui::{Entity, ModelHandle};
 2use smol::channel;
 3use std::marker::PhantomData;
 4
 5#[cfg(test)]
 6#[ctor::ctor]
 7fn init_logger() {
 8    env_logger::init();
 9}
10
11pub fn sample_text(rows: usize, cols: usize) -> String {
12    let mut text = String::new();
13    for row in 0..rows {
14        let c: char = ('a' as u32 + row as u32) as u8 as char;
15        let mut line = c.to_string().repeat(cols);
16        if row < rows - 1 {
17            line.push('\n');
18        }
19        text += &line;
20    }
21    text
22}
23
24pub struct Observer<T>(PhantomData<T>);
25
26impl<T: 'static> Entity for Observer<T> {
27    type Event = ();
28}
29
30impl<T: Entity> Observer<T> {
31    pub fn new(
32        handle: &ModelHandle<T>,
33        cx: &mut gpui::TestAppContext,
34    ) -> (ModelHandle<Self>, channel::Receiver<()>) {
35        let (notify_tx, notify_rx) = channel::unbounded();
36        let observer = cx.add_model(|cx| {
37            cx.observe(handle, move |_, _, _| {
38                let _ = notify_tx.try_send(());
39            })
40            .detach();
41            Observer(PhantomData)
42        });
43        (observer, notify_rx)
44    }
45}