salsa/tests/interned-struct-with-lifetime.rs

60 lines
1.7 KiB
Rust

//! Test that a `tracked` fn on a `salsa::input`
//! compiles and executes successfully.
use expect_test::expect;
use test_log::test;
#[salsa::interned]
struct InternedString<'db> {
data: String,
}
#[salsa::interned]
struct InternedPair<'db> {
data: (InternedString<'db>, InternedString<'db>),
}
#[salsa::interned]
struct InternedTwoFields<'db> {
data1: String,
data2: String,
}
#[salsa::tracked]
fn intern_stuff(db: &dyn salsa::Database) -> String {
let s1 = InternedString::new(db, "Hello, ".to_string());
let s2 = InternedString::new(db, "World, ");
let s3 = InternedPair::new(db, (s1, s2));
format!("{s3:?}")
}
#[test]
fn execute() {
let db = salsa::DatabaseImpl::new();
expect![[r#"
"InternedPair { data: (InternedString { data: \"Hello, \" }, InternedString { data: \"World, \" }) }"
"#]].assert_debug_eq(&intern_stuff(&db));
}
#[test]
fn interning_returns_equal_keys_for_equal_data() {
let db = salsa::DatabaseImpl::new();
let s1 = InternedString::new(&db, "Hello, ".to_string());
let s2 = InternedString::new(&db, "World, ".to_string());
let s1_2 = InternedString::new(&db, "Hello, ");
let s2_2 = InternedString::new(&db, "World, ");
assert_eq!(s1, s1_2);
assert_eq!(s2, s2_2);
}
#[test]
fn interning_returns_equal_keys_for_equal_data_multi_field() {
let db = salsa::DatabaseImpl::new();
let s1 = InternedTwoFields::new(&db, "Hello, ".to_string(), "World");
let s2 = InternedTwoFields::new(&db, "World, ", "Hello".to_string());
let s1_2 = InternedTwoFields::new(&db, "Hello, ", "World");
let s2_2 = InternedTwoFields::new(&db, "World, ", "Hello");
assert_eq!(s1, s1_2);
assert_eq!(s2, s2_2);
}