2022-08-23 22:01:44 +00:00
|
|
|
//! Test that `DeriveWithDb` is correctly derived.
|
2022-08-23 21:54:18 +00:00
|
|
|
|
|
|
|
use expect_test::expect;
|
2024-07-27 12:29:41 +00:00
|
|
|
use salsa::{Database, Setter};
|
2022-08-23 21:54:18 +00:00
|
|
|
|
|
|
|
#[salsa::input]
|
|
|
|
struct MyInput {
|
|
|
|
field: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
|
|
|
struct NotSalsa {
|
|
|
|
field: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::input]
|
|
|
|
struct ComplexStruct {
|
|
|
|
my_input: MyInput,
|
|
|
|
not_salsa: NotSalsa,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-08-23 22:01:44 +00:00
|
|
|
fn input() {
|
2024-07-27 12:29:41 +00:00
|
|
|
salsa::DatabaseImpl::new().attach(|db| {
|
2024-07-17 12:56:08 +00:00
|
|
|
let input = MyInput::new(db, 22);
|
|
|
|
let not_salsa = NotSalsa {
|
|
|
|
field: "it's salsa time".to_string(),
|
|
|
|
};
|
|
|
|
let complex_struct = ComplexStruct::new(db, input, not_salsa);
|
2024-07-19 13:08:51 +00:00
|
|
|
|
2024-07-17 12:56:08 +00:00
|
|
|
// debug includes all fields
|
|
|
|
let actual = format!("{complex_struct:?}");
|
2024-08-14 05:07:28 +00:00
|
|
|
let expected = expect![[r#"ComplexStruct { [salsa id]: Id(400), my_input: MyInput { [salsa id]: Id(0), field: 22 }, not_salsa: NotSalsa { field: "it's salsa time" } }"#]];
|
2024-07-17 12:56:08 +00:00
|
|
|
expected.assert_eq(&actual);
|
|
|
|
})
|
2022-08-23 21:54:18 +00:00
|
|
|
}
|
2024-04-03 09:59:11 +00:00
|
|
|
|
|
|
|
#[salsa::tracked]
|
2024-07-19 09:55:40 +00:00
|
|
|
fn leak_debug_string(_db: &dyn salsa::Database, input: MyInput) -> String {
|
2024-07-17 12:56:08 +00:00
|
|
|
format!("{input:?}")
|
2024-04-03 09:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Test that field reads that occur as part of `Debug` are not tracked.
|
|
|
|
/// Intentionally leaks the debug string.
|
|
|
|
/// Don't try this at home, kids.
|
|
|
|
#[test]
|
|
|
|
fn untracked_dependencies() {
|
2024-07-27 12:29:41 +00:00
|
|
|
let mut db = salsa::DatabaseImpl::new();
|
2024-04-03 09:59:11 +00:00
|
|
|
|
|
|
|
let input = MyInput::new(&db, 22);
|
|
|
|
|
|
|
|
let s = leak_debug_string(&db, input);
|
|
|
|
expect![[r#"
|
2024-06-20 12:45:38 +00:00
|
|
|
"MyInput { [salsa id]: Id(0), field: 22 }"
|
2024-04-03 09:59:11 +00:00
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&s);
|
|
|
|
|
|
|
|
input.set_field(&mut db).to(23);
|
|
|
|
|
|
|
|
// check that we reuse the cached result for debug string
|
|
|
|
// even though the dependency changed.
|
|
|
|
let s = leak_debug_string(&db, input);
|
|
|
|
assert!(s.contains(", field: 22 }"));
|
|
|
|
}
|
2024-04-03 10:23:43 +00:00
|
|
|
|
2024-07-17 10:08:34 +00:00
|
|
|
#[salsa::tracked(no_debug)]
|
2024-05-24 01:16:30 +00:00
|
|
|
struct DerivedCustom<'db> {
|
2024-04-03 10:23:43 +00:00
|
|
|
my_input: MyInput,
|
|
|
|
value: u32,
|
|
|
|
}
|
|
|
|
|
2024-07-15 11:46:23 +00:00
|
|
|
impl std::fmt::Debug for DerivedCustom<'_> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-07-17 12:56:08 +00:00
|
|
|
salsa::with_attached_database(|db| {
|
|
|
|
write!(f, "{:?} / {:?}", self.my_input(db), self.value(db))
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| f.debug_tuple("DerivedCustom").finish())
|
2024-04-03 10:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
2024-07-17 12:56:08 +00:00
|
|
|
fn leak_derived_custom(db: &dyn salsa::Database, input: MyInput, value: u32) -> String {
|
2024-04-03 10:23:43 +00:00
|
|
|
let c = DerivedCustom::new(db, input, value);
|
2024-07-17 12:56:08 +00:00
|
|
|
format!("{c:?}")
|
2024-04-03 10:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_debug_impl() {
|
2024-07-27 12:29:41 +00:00
|
|
|
let db = salsa::DatabaseImpl::new();
|
2024-04-03 10:23:43 +00:00
|
|
|
|
|
|
|
let input = MyInput::new(&db, 22);
|
|
|
|
|
|
|
|
let s = leak_derived_custom(&db, input, 23);
|
|
|
|
expect![[r#"
|
2024-06-20 12:45:38 +00:00
|
|
|
"MyInput { [salsa id]: Id(0), field: 22 } / 23"
|
2024-04-03 10:23:43 +00:00
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&s);
|
|
|
|
}
|