test.rs

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