2022-09-02 12:09:28 +00:00
|
|
|
//! Basic Singleton struct test:
|
2022-09-02 01:54:21 +00:00
|
|
|
//!
|
2022-09-02 12:09:28 +00:00
|
|
|
//! Singleton structs are created only once. Subsequent `get`s and `new`s after creation return the same `Id`.
|
2022-09-02 01:54:21 +00:00
|
|
|
|
2022-09-16 13:53:56 +00:00
|
|
|
use expect_test::expect;
|
2022-09-02 01:54:21 +00:00
|
|
|
|
2024-07-17 10:41:56 +00:00
|
|
|
use salsa::Database as _;
|
2022-09-02 01:54:21 +00:00
|
|
|
use test_log::test;
|
|
|
|
|
|
|
|
#[salsa::input(singleton)]
|
|
|
|
struct MyInput {
|
|
|
|
field: u32,
|
2022-09-16 13:53:56 +00:00
|
|
|
id_field: u16,
|
2022-09-02 01:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
2024-07-27 12:29:41 +00:00
|
|
|
let db = salsa::DatabaseImpl::new();
|
2022-09-25 01:23:36 +00:00
|
|
|
let input1 = MyInput::new(&db, 3, 4);
|
2022-09-02 01:54:21 +00:00
|
|
|
let input2 = MyInput::get(&db);
|
|
|
|
|
|
|
|
assert_eq!(input1, input2);
|
|
|
|
|
|
|
|
let input3 = MyInput::try_get(&db);
|
|
|
|
assert_eq!(Some(input1), input3);
|
2022-09-16 13:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn twice() {
|
2024-07-27 12:29:41 +00:00
|
|
|
let db = salsa::DatabaseImpl::new();
|
2022-09-25 01:23:36 +00:00
|
|
|
let input1 = MyInput::new(&db, 3, 4);
|
2022-09-16 13:53:56 +00:00
|
|
|
let input2 = MyInput::get(&db);
|
2022-09-02 01:54:21 +00:00
|
|
|
|
2022-09-16 13:53:56 +00:00
|
|
|
assert_eq!(input1, input2);
|
|
|
|
|
|
|
|
// should panic here
|
2022-09-25 01:23:36 +00:00
|
|
|
_ = MyInput::new(&db, 3, 5);
|
2022-09-16 13:53:56 +00:00
|
|
|
}
|
2022-09-02 01:54:21 +00:00
|
|
|
|
2022-09-16 13:53:56 +00:00
|
|
|
#[test]
|
|
|
|
fn debug() {
|
2024-07-27 12:29:41 +00:00
|
|
|
salsa::DatabaseImpl::new().attach(|db| {
|
2024-07-17 10:41:56 +00:00
|
|
|
let input = MyInput::new(db, 3, 4);
|
|
|
|
let actual = format!("{:?}", input);
|
2024-06-20 12:45:38 +00:00
|
|
|
let expected = expect!["MyInput { [salsa id]: Id(0), field: 3, id_field: 4 }"];
|
2024-07-17 10:41:56 +00:00
|
|
|
expected.assert_eq(&actual);
|
|
|
|
});
|
2022-09-02 01:54:21 +00:00
|
|
|
}
|