salsa/salsa-2022-tests/tests/singleton.rs
Niko Matsakis 389aa66bcf print all fields in debug() but ignore deps
In a previous PR we added the `include_all_fields`
parameter to `DebugWithDb` to allow it to
not create spurious dependencies.

This PR takes a different approach: we simply
ignore the dependencies created during debug
operations. This is risky as it can create
incorrect dependencies, but it is way more
convenient and seems like what users probably
want.

It also means that `DebugWithDb` has a simpler
signature that matches the `Debug` trait again,
which seems good to me.
2024-04-03 05:59:11 -04:00

72 lines
1.4 KiB
Rust

//! Basic Singleton struct test:
//!
//! Singleton structs are created only once. Subsequent `get`s and `new`s after creation return the same `Id`.
use expect_test::expect;
use salsa::DebugWithDb;
use salsa_2022_tests::{HasLogger, Logger};
use test_log::test;
#[salsa::jar(db = Db)]
struct Jar(MyInput);
trait Db: salsa::DbWithJar<Jar> + HasLogger {}
#[salsa::input(singleton)]
struct MyInput {
field: u32,
#[id]
id_field: u16,
}
#[salsa::db(Jar)]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
logger: Logger,
}
impl salsa::Database for Database {}
impl Db for Database {}
impl HasLogger for Database {
fn logger(&self) -> &Logger {
&self.logger
}
}
#[test]
fn basic() {
let db = Database::default();
let input1 = MyInput::new(&db, 3, 4);
let input2 = MyInput::get(&db);
assert_eq!(input1, input2);
let input3 = MyInput::try_get(&db);
assert_eq!(Some(input1), input3);
}
#[test]
#[should_panic]
fn twice() {
let db = Database::default();
let input1 = MyInput::new(&db, 3, 4);
let input2 = MyInput::get(&db);
assert_eq!(input1, input2);
// should panic here
_ = MyInput::new(&db, 3, 5);
}
#[test]
fn debug() {
let db = Database::default();
let input = MyInput::new(&db, 3, 4);
let actual = format!("{:?}", input.debug(&db));
let expected = expect!["MyInput { [salsa id]: 0, field: 3, id_field: 4 }"];
expected.assert_eq(&actual);
}