Skip to main content

Module test

Module test 

Source
Expand description

Support for the #[gpui::test] macro, and streams for observing entities in tests.

#[gpui::test] runs a test function once per seed. Each &TestAppContext parameter gets its own app, and all the apps share one deterministic scheduler, so a failing seed replays the same interleaving. The functions in this module are used by the macro; you rarely need to call them directly. See the testing guide.

§Example

use gpui::{AppContext, Global, TestAppContext};

struct Name(&'static str);
impl Global for Name {}

#[gpui::test]
fn test_counts_are_positive(cx: &mut TestAppContext) {
    let count = cx.new(|_| 1usize);
    cx.run_until_parked();
    cx.read(|cx| assert!(*count.read(cx) > 0));
}

#[gpui::test]
fn test_each_app_has_its_own_globals(cx_a: &mut TestAppContext, cx_b: &mut TestAppContext) {
    cx_a.update(|cx| cx.set_global(Name("alice")));
    cx_b.update(|cx| cx.set_global(Name("bob")));
    cx_a.read_global::<Name, _>(|name, _| assert_eq!(name.0, "alice"));
    cx_b.read_global::<Name, _>(|name, _| assert_eq!(name.0, "bob"));
}

Structs§

Observation
A stream of unit values from an entity observation, created by observe.

Functions§

apply_seed_to_proptest_config
Sets the RNG seed of a proptest config from SEED, or from 0 when it’s unset.
observe
Returns a stream that yields () each time entity notifies.
run_test
Runs test_fn for each seed, retrying a failing seed up to max_retries times.
run_test_once
Runs test_fn once on a dispatcher for seed, and returns its result.
seed_strategy
Returns a proptest strategy for scheduler seeds.