mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-15 01:39:25 +00:00
make salsa-2022 tests into independent files
also add a few new tests
This commit is contained in:
parent
e0c3109d6a
commit
3b3e0be981
8 changed files with 120 additions and 27 deletions
|
@ -9,6 +9,3 @@ edition = "2021"
|
||||||
salsa = { path = "../components/salsa-2022", package = "salsa-2022" }
|
salsa = { path = "../components/salsa-2022", package = "salsa-2022" }
|
||||||
expect-test = "1.4.0"
|
expect-test = "1.4.0"
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "salsa-2022-tests"
|
|
||||||
path = "main.rs"
|
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
//! This crate has the beginning of various unit tests on salsa 2022
|
|
||||||
//! code.
|
|
||||||
|
|
||||||
mod tracked_fn_on_input;
|
|
||||||
mod tracked_fn_on_tracked;
|
|
||||||
mod tracked_fn_on_tracked_specify;
|
|
||||||
mod tracked_fn_rev;
|
|
||||||
|
|
||||||
fn main() {}
|
|
24
salsa-2022-tests/src/lib.rs
Normal file
24
salsa-2022-tests/src/lib.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
/// Utility for tests that lets us log when notable events happen.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Logger {
|
||||||
|
logs: std::sync::Mutex<Vec<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait implemented by databases that lets them log events.
|
||||||
|
pub trait HasLogger {
|
||||||
|
/// Return a reference to the logger from the database.
|
||||||
|
fn logger(&self) -> &Logger;
|
||||||
|
|
||||||
|
/// Log an event from inside a tracked function.
|
||||||
|
fn push_log(&self, string: String) {
|
||||||
|
self.logger().logs.lock().unwrap().push(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Asserts what the (formatted) logs should look like,
|
||||||
|
/// clearing the logged events. This takes `&mut self` because
|
||||||
|
/// it is meant to be run from outside any tracked functions.
|
||||||
|
fn assert_logs(&mut self, expected: expect_test::Expect) {
|
||||||
|
let logs = std::mem::replace(&mut *self.logger().logs.lock().unwrap(), vec![]);
|
||||||
|
expected.assert_eq(&format!("{:#?}", logs));
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,15 +2,14 @@
|
||||||
//! compiles and executes successfully.
|
//! compiles and executes successfully.
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use salsa_2022_tests::{HasLogger, Logger};
|
||||||
|
|
||||||
use expect_test::expect;
|
use expect_test::expect;
|
||||||
use std::cell::RefCell;
|
|
||||||
|
|
||||||
#[salsa::jar(db = Db)]
|
#[salsa::jar(db = Db)]
|
||||||
struct Jar(MyInput, MyTracked, final_result, intermediate_result);
|
struct Jar(MyInput, MyTracked, final_result, intermediate_result);
|
||||||
|
|
||||||
trait Db: salsa::DbWithJar<Jar> {
|
trait Db: salsa::DbWithJar<Jar> + HasLogger {}
|
||||||
fn push_log(&self, message: String);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[salsa::input(jar = Jar)]
|
#[salsa::input(jar = Jar)]
|
||||||
struct MyInput {
|
struct MyInput {
|
||||||
|
@ -38,14 +37,7 @@ fn intermediate_result(db: &dyn Db, input: MyInput) -> MyTracked {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct Database {
|
struct Database {
|
||||||
storage: salsa::Storage<Self>,
|
storage: salsa::Storage<Self>,
|
||||||
log: RefCell<Vec<String>>,
|
logger: Logger,
|
||||||
}
|
|
||||||
|
|
||||||
impl Database {
|
|
||||||
fn assert_logs(&mut self, expected: expect_test::Expect) {
|
|
||||||
let logs = std::mem::replace(&mut *self.log.borrow_mut(), vec![]);
|
|
||||||
expected.assert_eq(&format!("{:#?}", logs));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl salsa::Database for Database {
|
impl salsa::Database for Database {
|
||||||
|
@ -54,9 +46,11 @@ impl salsa::Database for Database {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Db for Database {
|
impl Db for Database {}
|
||||||
fn push_log(&self, message: String) {
|
|
||||||
self.log.borrow_mut().push(message);
|
impl HasLogger for Database {
|
||||||
|
fn logger(&self) -> &Logger {
|
||||||
|
&self.logger
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
87
salsa-2022-tests/tests/tracked_fn_read_own_entity.rs
Normal file
87
salsa-2022-tests/tests/tracked_fn_read_own_entity.rs
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
//! Test that a `tracked` fn on a `salsa::input`
|
||||||
|
//! compiles and executes successfully.
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use salsa_2022_tests::{HasLogger, Logger};
|
||||||
|
|
||||||
|
use expect_test::expect;
|
||||||
|
|
||||||
|
#[salsa::jar(db = Db)]
|
||||||
|
struct Jar(MyInput, MyTracked, final_result, intermediate_result);
|
||||||
|
|
||||||
|
trait Db: salsa::DbWithJar<Jar> + HasLogger {}
|
||||||
|
|
||||||
|
#[salsa::input(jar = Jar)]
|
||||||
|
struct MyInput {
|
||||||
|
field: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[salsa::tracked(jar = Jar)]
|
||||||
|
fn final_result(db: &dyn Db, input: MyInput) -> u32 {
|
||||||
|
db.push_log(format!("final_result({:?})", input));
|
||||||
|
intermediate_result(db, input).field(db) * 2
|
||||||
|
}
|
||||||
|
|
||||||
|
#[salsa::tracked(jar = Jar)]
|
||||||
|
struct MyTracked {
|
||||||
|
field: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[salsa::tracked(jar = Jar)]
|
||||||
|
fn intermediate_result(db: &dyn Db, input: MyInput) -> MyTracked {
|
||||||
|
db.push_log(format!("intermediate_result({:?})", input));
|
||||||
|
let tracked = MyTracked::new(db, input.field(db) / 2);
|
||||||
|
let _ = tracked.field(db); // read the field of an entity we created
|
||||||
|
tracked
|
||||||
|
}
|
||||||
|
|
||||||
|
#[salsa::db(Jar)]
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Database {
|
||||||
|
storage: salsa::Storage<Self>,
|
||||||
|
logger: Logger,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl salsa::Database for Database {
|
||||||
|
fn salsa_runtime(&self) -> &salsa::Runtime {
|
||||||
|
self.storage.runtime()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Db for Database {}
|
||||||
|
|
||||||
|
impl HasLogger for Database {
|
||||||
|
fn logger(&self) -> &Logger {
|
||||||
|
&self.logger
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn execute() {
|
||||||
|
let mut db = Database::default();
|
||||||
|
|
||||||
|
let input = MyInput::new(&mut db, 22);
|
||||||
|
assert_eq!(final_result(&db, input), 22);
|
||||||
|
db.assert_logs(expect![[r#"
|
||||||
|
[
|
||||||
|
"final_result(MyInput(Id { value: 1 }))",
|
||||||
|
"intermediate_result(MyInput(Id { value: 1 }))",
|
||||||
|
]"#]]);
|
||||||
|
|
||||||
|
// Intermediate result is the same, so final result does
|
||||||
|
// not need to be recomputed:
|
||||||
|
input.set_field(&mut db, 23);
|
||||||
|
assert_eq!(final_result(&db, input), 22);
|
||||||
|
db.assert_logs(expect![[r#"
|
||||||
|
[
|
||||||
|
"intermediate_result(MyInput(Id { value: 1 }))",
|
||||||
|
]"#]]);
|
||||||
|
|
||||||
|
input.set_field(&mut db, 24);
|
||||||
|
assert_eq!(final_result(&db, input), 24);
|
||||||
|
db.assert_logs(expect![[r#"
|
||||||
|
[
|
||||||
|
"intermediate_result(MyInput(Id { value: 1 }))",
|
||||||
|
"final_result(MyInput(Id { value: 1 }))",
|
||||||
|
]"#]]);
|
||||||
|
}
|
Loading…
Reference in a new issue