Adds more testing updates coverage
Build Successful The build was successful
Gitea/epoch_forge/pipeline/head This commit looks good
Details
Build Successful The build was successful
Gitea/epoch_forge/pipeline/head This commit looks good
Details
This commit is contained in:
parent
7050ec2c8e
commit
f05a6184f0
|
|
@ -1,8 +1,8 @@
|
|||
[last_run_clean]
|
||||
run_status = "Success"
|
||||
lines_parsed = 83
|
||||
coverage = 50.17
|
||||
lines_parsed = 98
|
||||
coverage = 52.9
|
||||
|
||||
[last_run_clean.run_duration]
|
||||
secs = 25
|
||||
nanos = 72743368
|
||||
nanos = 607715147
|
||||
|
|
|
|||
|
|
@ -317,6 +317,12 @@ dependencies = [
|
|||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fastrand"
|
||||
version = "2.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.3.4"
|
||||
|
|
@ -420,6 +426,7 @@ dependencies = [
|
|||
"rand",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"thiserror",
|
||||
"uuid",
|
||||
]
|
||||
|
|
@ -753,6 +760,19 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.23.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2d31c77bdf42a745371d260a26ca7163f1e0924b64afa0b688e61b5a9fa02f16"
|
||||
dependencies = [
|
||||
"fastrand",
|
||||
"getrandom",
|
||||
"once_cell",
|
||||
"rustix",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.4.1"
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="122" height="18" role="img" aria-label="test-coverage: 50%"><title>test-coverage: 50%</title><linearGradient id="bms-8fc2dc264327201c" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-8fc2dc264327201c"><rect width="122" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-8fc2dc264327201c)"><rect width="87" height="18" fill="#555"/><rect x="87" width="35" height="18" fill="#97ca00"/><rect width="122" height="18" fill="url(#bms-8fc2dc264327201c)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="445" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="770">test-coverage</text><text x="445" y="130" transform="scale(.1)" fill="#fff" textLength="770">test-coverage</text><text aria-hidden="true" x="1035" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">50%</text><text x="1035" y="130" transform="scale(.1)" fill="#fff" textLength="250">50%</text></g></svg>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="122" height="18" role="img" aria-label="test-coverage: 53%"><title>test-coverage: 53%</title><linearGradient id="bms-78c5016245f6e360" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-78c5016245f6e360"><rect width="122" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-78c5016245f6e360)"><rect width="87" height="18" fill="#555"/><rect x="87" width="35" height="18" fill="#97ca00"/><rect width="122" height="18" fill="url(#bms-78c5016245f6e360)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="445" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="770">test-coverage</text><text x="445" y="130" transform="scale(.1)" fill="#fff" textLength="770">test-coverage</text><text aria-hidden="true" x="1035" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">53%</text><text x="1035" y="130" transform="scale(.1)" fill="#fff" textLength="250">53%</text></g></svg>
|
||||
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
|
|
@ -20,4 +20,4 @@ once_cell = "1.21.3"
|
|||
|
||||
[dev-dependencies]
|
||||
insta = {workspace = true}
|
||||
|
||||
tempfile = "3.23.0"
|
||||
|
|
|
|||
|
|
@ -165,3 +165,82 @@ impl FromStr for EpochIdentifier {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
mod test {
|
||||
use insta::assert_debug_snapshot;
|
||||
// tests/epoch.rs
|
||||
use serde_json::to_string_pretty;
|
||||
|
||||
use crate::epoch::{EpochId, EpochIdentifier, SaveMode, TagList};
|
||||
|
||||
#[test]
|
||||
fn epoch_id_arithmetic() {
|
||||
let id = EpochId::new(42);
|
||||
assert_eq!(*id, 42);
|
||||
|
||||
let next = id.next();
|
||||
assert_eq!(*next, 43);
|
||||
|
||||
let added = id.add_to_epoch(5);
|
||||
assert_eq!(*added, 47);
|
||||
|
||||
let negative = id.add_to_epoch(-10);
|
||||
assert_eq!(*negative, 32);
|
||||
|
||||
// saturating behaviour
|
||||
let zero = EpochId::new(0);
|
||||
let under = zero.add_to_epoch(-1);
|
||||
assert_eq!(*under, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn epoch_identifier_parsing() {
|
||||
// numeric string → Id
|
||||
let parsed: EpochIdentifier = "7".parse().unwrap();
|
||||
assert!(matches!(parsed, EpochIdentifier::Id(_)));
|
||||
|
||||
// non‑empty string → Tag
|
||||
let parsed: EpochIdentifier = "winter-b".parse().unwrap();
|
||||
assert!(matches!(parsed, EpochIdentifier::Tag(_)));
|
||||
|
||||
// empty string → error
|
||||
assert!("".parse::<EpochIdentifier>().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn epoch_identifier_serialisation() {
|
||||
// Id variant should serialize to a number
|
||||
let id = EpochIdentifier::Id(EpochId::new(13));
|
||||
let json = to_string_pretty(&id).unwrap();
|
||||
assert_debug_snapshot!("epoch_identifier_id", json);
|
||||
|
||||
// Tag variant should serialize to a string
|
||||
let tag = EpochIdentifier::Tag("alpha".into());
|
||||
let json = to_string_pretty(&tag).unwrap();
|
||||
assert_debug_snapshot!("epoch_identifier_tag", json);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tag_list_deref_and_mut() {
|
||||
let mut list = TagList::new();
|
||||
list.push("one".into());
|
||||
list.push("two".into());
|
||||
|
||||
// Deref to Vec<String>
|
||||
assert_eq!(list[0], "one");
|
||||
assert_eq!(list[1], "two");
|
||||
|
||||
// Mutating through DerefMut
|
||||
list[1] = "TWO".into();
|
||||
assert_eq!(list[1], "TWO");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_mode_variants() {
|
||||
// Just a quick check that the variants exist and can be constructed
|
||||
let _do_not = SaveMode::DoNotErase;
|
||||
let _erase = SaveMode::Erase;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -108,3 +108,127 @@ pub trait ConfigWritable: Serialize {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::unwrap_used)]
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use derive_more::Display;
|
||||
use insta::assert_snapshot;
|
||||
use tempfile::tempdir;
|
||||
|
||||
/// A tiny helper struct that implements every trait you defined.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Table, Display)]
|
||||
#[display("{id}:{value}")]
|
||||
struct DummyItem {
|
||||
id: Uuid,
|
||||
value: i32,
|
||||
#[serde(skip)]
|
||||
#[table(skip)]
|
||||
save: String,
|
||||
}
|
||||
|
||||
impl DummyItem {
|
||||
fn new(value: i32) -> Self {
|
||||
Self {
|
||||
id: Uuid::nil(),
|
||||
value,
|
||||
save: tempdir()
|
||||
.expect("Tempdir")
|
||||
.path()
|
||||
.join("dummy.json")
|
||||
.to_string_lossy()
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Diffable for DummyItem {
|
||||
fn delta(&self, other: &Self) -> Self {
|
||||
Self {
|
||||
id: self.id, // keep the same id
|
||||
value: self.value - other.value,
|
||||
save: self.save.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// impl Applyable for DummyItem {}
|
||||
// impl Nextable for DummyItem {}
|
||||
impl Prettiable for DummyItem {}
|
||||
|
||||
/* ------------------- ConfigWritable ------------------- */
|
||||
impl ConfigWritable for DummyItem {
|
||||
fn save_path(&self) -> String {
|
||||
self.save.clone()
|
||||
}
|
||||
|
||||
fn save_folder(&self) -> String {
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_prettiable_as_table() {
|
||||
let item = DummyItem::new(42);
|
||||
let table = DummyItem::as_table(vec![&item]).expect("Dummytable");
|
||||
assert_snapshot!(format!("{}", table));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_prettiable_show_as_log() {
|
||||
DummyItem::new(7).show_as_log();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_writable_as_json() {
|
||||
let item = DummyItem::new(123);
|
||||
let json = item.as_json("test action".into()).unwrap();
|
||||
assert_snapshot!(json);
|
||||
}
|
||||
|
||||
// fn dummy_with_tempfile(value: i32) -> (DummyItem, std::path::PathBuf) {
|
||||
// let dummy = DummyItem::new(value);
|
||||
// (dummy.clone(), dummy.save.clone().into())
|
||||
// }
|
||||
// #[test]
|
||||
// fn test_config_writable_save_with_context_erase() {
|
||||
// let (item, path) = dummy_with_tempfile(10);
|
||||
// // Erase: we expect a file to be created (or overwritten)
|
||||
// item.save_with_context("Erase test", &SaveMode::DoNotErase)
|
||||
// .unwrap();
|
||||
|
||||
// // Verify file contents
|
||||
// let mut file = File::open(&path).unwrap();
|
||||
// let mut contents = String::new();
|
||||
// file.read_to_string(&mut contents).unwrap();
|
||||
|
||||
// assert_snapshot!(contents);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn test_config_writable_save_with_context_donot_erase() {
|
||||
// let (item, _path) = dummy_with_tempfile(20);
|
||||
|
||||
// // First write – must succeed
|
||||
// item.save_with_context("DoNotErase test 1", &SaveMode::DoNotErase)
|
||||
// .unwrap();
|
||||
|
||||
// // Second write – should fail with `ConfigCreation` because the file
|
||||
// // already exists.
|
||||
// let err = item
|
||||
// .save_with_context("DoNotErase test 2", &SaveMode::DoNotErase)
|
||||
// .unwrap_err();
|
||||
|
||||
// assert_snapshot!(format!("{}", err));
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn test_diffable_delta() {
|
||||
let a = DummyItem::new(30);
|
||||
let b = DummyItem::new(15);
|
||||
let diff = a.delta(&b);
|
||||
assert_eq!(diff.value, 15);
|
||||
assert_eq!(diff.id, a.id); // same id
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
source: lib_efo/src/epoch/epoch_traits.rs
|
||||
expression: json
|
||||
---
|
||||
{
|
||||
"id": "00000000-0000-0000-0000-000000000000",
|
||||
"value": 123
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
source: lib_efo/src/epoch/epoch_traits.rs
|
||||
expression: "format!(\"{}\", table)"
|
||||
---
|
||||
[0m[1m┌[0m[0m[1m──────────────────────────────────────[0m[0m[1m┬[0m[0m[1m───────[0m[0m[1m┐[0m
|
||||
[0m[0m[1m│[0m[0m[1m [0m[0m[0m[1mid [0m[1m [0m[0m[1m│[0m[0m[1m [0m[0m[0m[1mvalue[0m[1m [0m[0m[1m│[0m
|
||||
[0m[0m[1m├[0m[0m[1m──────────────────────────────────────[0m[0m[1m┼[0m[0m[1m───────[0m[0m[1m┤[0m
|
||||
[0m[0m[1m│[0m[0m[1m [0m[0m[0m00000000-0000-0000-0000-000000000000[0m[1m [0m[0m[1m│[0m[0m[1m [0m[0m[0m42 [0m[1m [0m[0m[1m│[0m
|
||||
[0m[0m[1m└[0m[0m[1m──────────────────────────────────────[0m[0m[1m┴[0m[0m[1m───────[0m[0m[1m┘[0m
|
||||
[0m
|
||||
|
|
@ -18,33 +18,33 @@ pub mod naming;
|
|||
pub mod stats;
|
||||
pub mod utils;
|
||||
|
||||
use std::io::Result as IoResult;
|
||||
// use std::io::Result as IoResult;
|
||||
|
||||
/// Marker trait for anything that can be persisted via the `Store<T>` abstraction.
|
||||
///
|
||||
/// It is a thin wrapper around `serde::Serialize` and `serde::de::DeserializeOwned` so that the
|
||||
/// compiler can infer that the type is both serialisable *and* deserialisable.
|
||||
pub trait Persistable: serde::Serialize + serde::de::DeserializeOwned {}
|
||||
impl<T> Persistable for T where T: serde::Serialize + serde::de::DeserializeOwned + 'static {}
|
||||
// /// Marker trait for anything that can be persisted via the `Store<T>` abstraction.
|
||||
// ///
|
||||
// /// It is a thin wrapper around `serde::Serialize` and `serde::de::DeserializeOwned` so that the
|
||||
// /// compiler can infer that the type is both serialisable *and* deserialisable.
|
||||
// pub trait Persistable: serde::Serialize + serde::de::DeserializeOwned {}
|
||||
// impl<T> Persistable for T where T: serde::Serialize + serde::de::DeserializeOwned + 'static {}
|
||||
|
||||
/// The generic store. All domain entities that need persistence should use this trait.
|
||||
/// It can be backed by a file, a DB, an HTTP API, … as long as it knows how to read/write the type `T`.
|
||||
pub trait Store<T>
|
||||
where
|
||||
T: Persistable,
|
||||
{
|
||||
/// Read a value identified by `id`.
|
||||
fn read(&self, id: &str) -> IoResult<Option<T>>;
|
||||
// /// The generic store. All domain entities that need persistence should use this trait.
|
||||
// /// It can be backed by a file, a DB, an HTTP API, … as long as it knows how to read/write the type `T`.
|
||||
// pub trait Store<T>
|
||||
// where
|
||||
// T: Persistable,
|
||||
// {
|
||||
// /// Read a value identified by `id`.
|
||||
// fn read(&self, id: &str) -> IoResult<Option<T>>;
|
||||
|
||||
/// Write (or overwrite) a value identified by `id`.
|
||||
fn write(&self, id: &str, value: &T) -> IoResult<()>;
|
||||
// /// Write (or overwrite) a value identified by `id`.
|
||||
// fn write(&self, id: &str, value: &T) -> IoResult<()>;
|
||||
|
||||
/// Delete the value identified by `id`.
|
||||
fn delete(&self, id: &str) -> IoResult<()>;
|
||||
// /// Delete the value identified by `id`.
|
||||
// fn delete(&self, id: &str) -> IoResult<()>;
|
||||
|
||||
/// List all ids known to this store.
|
||||
fn list_ids(&self) -> IoResult<Vec<String>>;
|
||||
}
|
||||
// /// List all ids known to this store.
|
||||
// fn list_ids(&self) -> IoResult<Vec<String>>;
|
||||
// }
|
||||
/// A trait for types that can be displayed in a command-line tool.
|
||||
///
|
||||
/// Implementors should provide three levels of representation:
|
||||
|
|
@ -103,3 +103,40 @@ impl<T: Displayable> Displayable for Vec<T> {
|
|||
.join(",")
|
||||
}
|
||||
}
|
||||
|
||||
// tests/api.rs --------------------------------------------------------------
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use insta::assert_snapshot;
|
||||
struct Dummy {
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl Displayable for Dummy {
|
||||
fn oneline_display(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_displayable_vec() {
|
||||
let vec = vec![
|
||||
Dummy {
|
||||
name: "Alice".to_string(),
|
||||
},
|
||||
Dummy {
|
||||
name: "Bob".to_string(),
|
||||
},
|
||||
Dummy {
|
||||
name: "Eve".to_string(),
|
||||
},
|
||||
];
|
||||
|
||||
assert_eq!(vec.oneline_display(), "Alice,Bob,Eve".to_string());
|
||||
vec.print_full();
|
||||
vec.print_short();
|
||||
vec.print_oneline();
|
||||
assert_snapshot!("dummy_vec_oneline", vec.oneline_display());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
source: lib_efo/src/epoch.rs
|
||||
expression: json
|
||||
---
|
||||
"{\n \"id\": 13\n}"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
source: lib_efo/src/epoch.rs
|
||||
expression: json
|
||||
---
|
||||
"\"alpha\""
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
source: lib_efo/src/lib.rs
|
||||
expression: vec.oneline_display()
|
||||
---
|
||||
Alice,Bob,Eve
|
||||
|
|
@ -58,7 +58,57 @@ fn test_stat_pair_parsing() {
|
|||
assert_pair_ok("Pop -5", ListStats::Population, -5);
|
||||
assert_pair_ok("grow 0", ListStats::Growthrate, 0);
|
||||
}
|
||||
#[test]
|
||||
fn stat_pair_ill_formed_errors_are_detected() {
|
||||
// 1️⃣ No separator at all
|
||||
let err = StatPair::from_str("justastring").unwrap_err();
|
||||
assert!(matches!(err, EpochError::IllFormedStatQualifier(_)));
|
||||
|
||||
// 2️⃣ Separator but missing the value
|
||||
let err = StatPair::from_str("cri:").unwrap_err();
|
||||
assert!(matches!(err, EpochError::IllFormedStatQualifier(_)));
|
||||
|
||||
// 3️⃣ Separator but missing the stat name
|
||||
let err = StatPair::from_str(":10").unwrap_err();
|
||||
assert!(matches!(err, EpochError::UnknownStatAcronim(_)));
|
||||
|
||||
// 4️⃣ Two spaces (splits into > 2 parts)
|
||||
let err = StatPair::from_str("cri 10 20").unwrap_err();
|
||||
assert!(matches!(err, EpochError::IllFormedStatQualifier(_)));
|
||||
|
||||
// 5️⃣ Value part is not an integer
|
||||
let err = StatPair::from_str("cri:abc").unwrap_err();
|
||||
assert!(matches!(err, EpochError::IllFormedStatQualifier(_)));
|
||||
|
||||
// 6️⃣ Unknown stat – this is caught by the `ListStats::parse()` call,
|
||||
// which already returns `IllFormedStatQualifier`
|
||||
let err = StatPair::from_str("unknown:5").unwrap_err();
|
||||
assert!(matches!(err, EpochError::UnknownStatAcronim(_)));
|
||||
}
|
||||
#[test]
|
||||
fn position_invalid_format_errors_are_detected() {
|
||||
// 1️⃣ No comma
|
||||
let err = Position::from_str("123 456").unwrap_err();
|
||||
assert!(matches!(err, EpochError::InvalidPositionFormat(_)));
|
||||
|
||||
// 2️⃣ Too many commas
|
||||
let err = Position::from_str("1,2,3").unwrap_err();
|
||||
assert!(matches!(err, EpochError::InvalidPositionFormat(_)));
|
||||
|
||||
// 3️⃣ Leading/trailing comma
|
||||
let err = Position::from_str(",5").unwrap_err();
|
||||
assert!(matches!(err, EpochError::ParsePositionError(_)));
|
||||
|
||||
let err = Position::from_str("5,").unwrap_err();
|
||||
assert!(matches!(err, EpochError::ParsePositionError(_)));
|
||||
|
||||
// 4️⃣ Non‑integer components
|
||||
let err = Position::from_str("x,5").unwrap_err();
|
||||
assert!(matches!(err, EpochError::ParsePositionError(_)));
|
||||
|
||||
let err = Position::from_str("5,y").unwrap_err();
|
||||
assert!(matches!(err, EpochError::ParsePositionError(_)));
|
||||
}
|
||||
#[test]
|
||||
fn test_liststats_aliases() {
|
||||
// Each tuple: (alias, expected enum variant)
|
||||
|
|
|
|||
Loading…
Reference in New Issue