snapshot tests

This commit is contained in:
Robin Appelman 2023-03-16 22:40:48 +01:00
commit fbe3eee614
8 changed files with 1603 additions and 5 deletions

View file

@ -28,6 +28,8 @@ once_cell = "1.17.1"
criterion = "0.4"
iai = "0.1"
miette = { version = "5.5.0", features = ["fancy"] }
insta = { version = "1.28.0", features = ["json"] }
test-case = "3.0.0"
[[bench]]
name = "bench"

View file

@ -31,6 +31,7 @@
hyperfine
cargo-expand
rustup
cargo-insta
];
};
});

View file

@ -190,6 +190,12 @@ impl<T: PartialEq> PartialEq for ClassMap<T> {
}
}
impl<T> From<ClassMap<T>> for [T; 10] {
fn from(value: ClassMap<T>) -> Self {
value.0
}
}
/// Optimized subject id
#[derive(Copy, Clone, Eq, PartialEq, Debug, Ord, PartialOrd, Hash)]
pub enum SubjectId {

View file

@ -8,10 +8,10 @@ use std::collections::BTreeMap;
#[derive(Debug, Serialize, Default, PartialEq)]
pub struct ClassStats {
kills: ClassMap<u8>,
deaths: ClassMap<u8>,
assists: ClassMap<u8>,
damage: ClassMap<u16>,
pub kills: ClassMap<u8>,
pub deaths: ClassMap<u8>,
pub assists: ClassMap<u8>,
pub damage: ClassMap<u16>,
}
#[derive(Default)]

View file

@ -32,3 +32,12 @@ impl PlayerSpecificData for HealSpread {
self
}
}
impl IntoIterator for HealSpread {
type Item = (SteamId3, u32);
type IntoIter = <BTreeMap<SteamId3, u32> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

View file

@ -170,7 +170,7 @@ macro_rules! handler {
}
pub struct [<$name PerSubjectOutput>] {
pub $($child: <$ty as $crate::EventHandler>::PerSubjectOutput),*
$(pub $child: <$ty as $crate::EventHandler>::PerSubjectOutput),*
}
impl serde::Serialize for [<$name PerSubjectOutput>] {

66
tests/snapshot.rs Normal file
View file

@ -0,0 +1,66 @@
use serde::Serialize;
use std::collections::BTreeMap;
use std::fs::read_to_string;
use test_case::test_case;
use tf_log_parser::module::{ClassStats, MedicStats};
use tf_log_parser::{parse, EventHandler, LogHandler, LogHandlerPerSubjectOutput};
#[derive(Serialize)]
struct LogResult {
global: <LogHandler as EventHandler>::GlobalOutput,
per_player: BTreeMap<String, LogPlayerData>,
}
#[derive(Serialize)]
struct LogPlayerData {
stats: ClassStatsRaw,
heals: BTreeMap<String, u32>,
medic: MedicStats,
}
impl From<LogHandlerPerSubjectOutput> for LogPlayerData {
fn from(value: LogHandlerPerSubjectOutput) -> Self {
LogPlayerData {
stats: value.class_stats.into(),
medic: value.medic_stats,
heals: value
.heal_spread
.into_iter()
.map(|(user, heals)| (user.0.steam3(), heals))
.collect(),
}
}
}
#[derive(Serialize)]
struct ClassStatsRaw {
kills: [u8; 10],
deaths: [u8; 10],
assists: [u8; 10],
damage: [u16; 10],
}
impl From<ClassStats> for ClassStatsRaw {
fn from(value: ClassStats) -> Self {
ClassStatsRaw {
kills: value.kills.into(),
deaths: value.deaths.into(),
assists: value.assists.into(),
damage: value.damage.into(),
}
}
}
#[test_case("log_2892242.log")]
fn test_parse(input: &str) {
let content = read_to_string(&format!("test_data/{}", input)).unwrap();
let (global, per_player) = parse(&content).unwrap();
let log = LogResult {
global,
per_player: per_player
.into_iter()
.map(|(key, value)| (key.0.steam3(), value.into()))
.collect(),
};
insta::assert_json_snapshot!(log);
}

File diff suppressed because it is too large Load diff