CultLib/packages/cultcache-rs/tests/first_call.rs
Libby 533b492af2
Some checks failed
CultMesh QUIC native / Build win32-x64 (pull_request) Has been skipped
CultMesh QUIC native / Build linux-x64 (pull_request) Has been skipped
CultMesh QUIC native / Release native binaries (pull_request) Has been skipped
CultMesh portability and performance / ubuntu-latest one-command multiplayer onboarding (pull_request) Has been cancelled
CultMesh portability and performance / windows-latest one-command multiplayer onboarding (pull_request) Has been cancelled
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Has been cancelled
CultMesh portability and performance / windows-latest reactive semantics and leases (pull_request) Has been cancelled
CultNet cross-runtime interop / C# / TypeScript / Rust / Python / Kotlin (pull_request) Has been cancelled
CultMesh QUIC native / Verify committed binaries match SHA256SUMS (pull_request) Has been cancelled
docs/start: tested Rust and Python CultCache first calls
Each test file carries the snippet between markers; the docs show it verbatim and a second test in the file fails if the README block drifts. Matrix cells for Rust and Python CultCache now link them.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 20:20:33 +00:00

56 lines
2.1 KiB
Rust

//! The Rust CultCache first call from docs/start/README.md. The docs show the region between the
//! `snippet` markers verbatim, and `docs_show_the_tested_snippet` fails when they differ.
use cultcache_rs::{CultCache, DatabaseEntry, SingleFileMessagePackBackingStore};
// snippet:begin
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "item")]
struct Item {
#[cultcache(key = 0)]
name: String,
#[cultcache(key = 1)]
category: String,
#[cultcache(key = 2, default)]
value: u32,
}
fn first_call() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
let path = dir.path().join("first-call.cc");
let mut cache = CultCache::new();
cache.register_entry_type::<Item>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path));
cache.pull_all_backing_stores()?;
cache.put("item:potion", &Item {
name: "Potion".to_string(),
category: "Consumable".to_string(),
value: 50,
})?; // written to the .cc file before this returns
let mut reopened = CultCache::new();
reopened.register_entry_type::<Item>()?;
reopened.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path));
reopened.pull_all_backing_stores()?; // read it back
let potion = reopened.get_required::<Item>("item:potion")?;
assert_eq!(potion.name, "Potion");
assert_eq!(potion.value, 50);
Ok(())
}
// snippet:end
#[test]
fn first_call_round_trips_through_a_second_cache() {
first_call().unwrap();
}
#[test]
fn docs_show_the_tested_snippet() {
let source = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/first_call.rs")).unwrap();
let begin = source.find("// snippet:begin\n").unwrap() + "// snippet:begin\n".len();
let end = source.find("// snippet:end\n").unwrap();
let docs = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/../../docs/start/README.md")).unwrap();
let block = format!("```rust\n{}```", &source[begin..end]);
assert!(docs.contains(&block), "docs/start/README.md no longer shows tests/first_call.rs verbatim");
}