1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 10:14:06 +02:00

switch to cargo-insta for snapshot testing

This commit is contained in:
Robin Appelman 2023-12-21 23:02:41 +01:00
commit 07039da23d
23 changed files with 42991 additions and 22877 deletions

View file

@ -1,11 +1,10 @@
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fs::{self, File};
use std::fs;
use test_case::test_case;
use fnv::FnvHashMap;
use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use tf_demo_parser::demo::data::DemoTick;
use tf_demo_parser::demo::message::packetentities::{EntityId, PacketEntity, UpdateType};
use tf_demo_parser::demo::message::Message;
@ -134,86 +133,15 @@ impl MessageHandler for EntityDumper {
}
}
#[test_case("test_data/small.dem", "test_data/small_entities.json"; "small.dem")]
fn entity_test(input_file: &str, snapshot_file: &str) {
#[test_case("test_data/small.dem")]
fn entity_test(input_file: &str) {
let file = fs::read(input_file).expect("Unable to read file");
let demo = Demo::new(&file);
let (_, entities) = DemoParser::new_with_analyser(demo.get_stream(), EntityDumper::new())
.parse()
.unwrap();
let json_file = File::open(snapshot_file).expect("Unable to read file");
let mut reader = BufReader::new(json_file);
let mut buffer = String::new();
let mut expected = Vec::with_capacity(128);
while reader.read_line(&mut buffer).expect("failed to read line") > 0 {
let entity: EntityDump =
serde_json::from_str(buffer.trim_end()).expect("failed to parse json");
expected.push(entity);
buffer.clear();
}
pretty_assertions::assert_eq!(expected.len(), entities.len());
let entity_ids: Vec<_> = entities.iter().map(|entity| entity.id).collect();
let expected_ids: Vec<_> = expected.iter().map(|entity| entity.id).collect();
pretty_assertions::assert_eq!(expected_ids, entity_ids);
for (expected_entity, entity) in expected.into_iter().zip(entities.into_iter()) {
pretty_assertions::assert_eq!(
expected_entity.tick,
entity.tick,
"Failed comparing entity {}",
entity.id
);
pretty_assertions::assert_eq!(
expected_entity.id,
entity.id,
"Failed comparing entity {}",
entity.id
);
pretty_assertions::assert_eq!(
expected_entity.server_class,
entity.server_class,
"Failed comparing entity {}",
entity.id
);
pretty_assertions::assert_eq!(
expected_entity.pvs,
entity.pvs,
"Failed comparing entity {}",
entity.id
);
let mut prop_names: Vec<_> = entity.props.keys().collect();
let mut expected_prop_names: Vec<_> = expected_entity.props.keys().collect();
prop_names.sort();
expected_prop_names.sort();
pretty_assertions::assert_eq!(
expected_prop_names,
prop_names,
"Failed comparing entity {}",
entity.id
);
for prop_name in expected_prop_names {
pretty_assertions::assert_eq!(
expected_entity.props.get(prop_name),
entity.props.get(prop_name),
"Failed comparing entity {} prop {}",
entity.id,
prop_name
);
}
pretty_assertions::assert_eq!(
expected_entity,
entity,
"Failed comparing entity {}",
entity.id
);
}
insta::with_settings!({sort_maps =>true}, {
insta::assert_json_snapshot!(entities);
});
}

View file

@ -2,7 +2,7 @@ use std::fs;
use test_case::test_case;
use fnv::FnvHashMap;
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use tf_demo_parser::demo::packet::datatable::{ParseSendTable, SendTableName, ServerClass};
use tf_demo_parser::demo::parser::MessageHandler;
use tf_demo_parser::demo::sendprop::{SendPropIdentifier, SendPropName};
@ -53,8 +53,8 @@ impl MessageHandler for SendPropAnalyser {
}
}
#[test_case("test_data/gully.dem", "test_data/gully_props.json"; "gully.dem")]
fn flatten_test(input_file: &str, snapshot_file: &str) {
#[test_case("test_data/gully.dem")]
fn flatten_test(input_file: &str) {
let file = fs::read(input_file).expect("Unable to read file");
let demo = Demo::new(&file);
let (_, (send_tables, prop_names)) =
@ -79,18 +79,7 @@ fn flatten_test(input_file: &str, snapshot_file: &str) {
})
.collect();
let expected: HashMap<SendTableName, Vec<String>> = serde_json::from_slice(
fs::read(snapshot_file)
.expect("Unable to read file")
.as_slice(),
)
.unwrap();
let expected_tables: HashSet<_> = expected.keys().collect();
let actual_tables: HashSet<_> = flat_props.keys().collect();
pretty_assertions::assert_eq!(expected_tables, actual_tables);
for table in expected_tables {
pretty_assertions::assert_eq!(expected[table], flat_props[table]);
}
insta::with_settings!({sort_maps =>true}, {
insta::assert_json_snapshot!(flat_props);
});
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,49 @@
---
source: tests/tests.rs
expression: state
---
{
"players": [
{
"entity": 1,
"position": {
"x": -3599.1775,
"y": 421.331,
"z": 298.0
},
"health": 125,
"max_health": 125,
"class": "scout",
"team": "red",
"view_angle": 0.0,
"pitch_angle": 0.35294342,
"state": "Alive",
"info": {
"classes": {},
"name": "Icewind | demos.tf",
"userId": 2,
"steamId": "[U:1:64229260]",
"team": "other"
},
"charge": 0,
"simtime": 78,
"ping": 5,
"in_pvs": true
}
],
"buildings": {},
"world": {
"boundary_min": {
"x": -3882.0,
"y": -4570.0,
"z": -560.0
},
"boundary_max": {
"x": 5240.0,
"y": 3936.0,
"z": 1641.0
}
},
"kills": [],
"tick": 115
}

View file

@ -0,0 +1,21 @@
---
source: tests/tests.rs
expression: state
---
{
"chat": [],
"users": {
"2": {
"classes": {},
"name": "Icewind | demos.tf",
"userId": 2,
"steamId": "[U:1:64229260]",
"team": "other"
}
},
"deaths": [],
"rounds": [],
"startTick": 68,
"intervalPerTick": 0.015,
"pauses": []
}

File diff suppressed because it is too large Load diff

View file

@ -1,63 +1,38 @@
use std::fs;
use test_case::test_case;
use tf_demo_parser::demo::parser::gamestateanalyser::{GameState, GameStateAnalyser};
use tf_demo_parser::{Demo, DemoParser, MatchState};
use tf_demo_parser::demo::parser::gamestateanalyser::GameStateAnalyser;
use tf_demo_parser::{Demo, DemoParser};
#[test_case("small.dem", "small.json"; "small.dem")]
#[test_case("gully.dem", "gully.json"; "gully.dem")]
#[test_case("comp.dem", "comp.json"; "comp.dem")]
#[test_case("malformed_cvar.dem", "malformed_cvar.json"; "malformed_cvar.dem")]
#[test_case("unicode-saytext.dem", "unicode-saytext.json"; "unicode-saytext.dem")]
#[test_case("nousers.dem", "nousers.json"; "nousers.dem")]
#[test_case("decal.dem", "decal.json"; "decal.dem")]
#[test_case("saytext2.dem", "saytext2.json"; "saytext2.dem")]
#[test_case("emptysaytext.dem", "emptysaytext.json"; "emptysaytext.dem")]
#[test_case("protocol23.dem", "protocol23.json"; "protocol23.dem")]
fn snapshot_test(input_file: &str, snapshot_file: &str) {
#[test_case("small.dem")]
#[test_case("gully.dem")]
#[test_case("comp.dem")]
#[test_case("malformed_cvar.dem")]
#[test_case("unicode-saytext.dem")]
#[test_case("nousers.dem")]
#[test_case("decal.dem")]
#[test_case("saytext2.dem")]
#[test_case("emptysaytext.dem")]
#[test_case("protocol23.dem")]
fn snapshot_test(input_file: &str) {
let file = fs::read(format!("test_data/{}", input_file)).expect("Unable to read file");
let demo = Demo::new(&file);
let (_, state) = DemoParser::new(demo.get_stream()).parse().unwrap();
//
// fs::write(
// format!("test_data/{}", snapshot_file),
// serde_json::to_string_pretty(&state).unwrap(),
// )
// .unwrap();
let expected: MatchState = serde_json::from_slice(
fs::read(format!("test_data/{}", snapshot_file))
.expect("Unable to read file")
.as_slice(),
)
.unwrap();
pretty_assertions::assert_eq!(expected, state);
insta::assert_json_snapshot!(format!("{input_file}_minimal"), state);
let (_, state) = DemoParser::new_all(demo.get_stream()).parse().unwrap();
pretty_assertions::assert_eq!(expected, state);
insta::assert_json_snapshot!(format!("{input_file}_minimal"), state);
}
#[test_case("small.dem", "small_game_state.json"; "small.dem")]
#[test_case("gully.dem", "gully_game_state.json"; "gully.dem")]
fn game_state_test(input_file: &str, snapshot_file: &str) {
#[test_case("small.dem")]
#[test_case("gully.dem")]
fn game_state_test(input_file: &str) {
let file = fs::read(format!("test_data/{}", input_file)).expect("Unable to read file");
let demo = Demo::new(&file);
let (_, state) = DemoParser::new_with_analyser(demo.get_stream(), GameStateAnalyser::new())
.parse()
.unwrap();
// fs::write(
// format!("test_data/{}", snapshot_file),
// serde_json::to_string_pretty(&state).unwrap(),
// )
// .unwrap();
let expected: GameState = serde_json::from_slice(
fs::read(format!("test_data/{}", snapshot_file))
.expect("Unable to read file")
.as_slice(),
)
.unwrap();
pretty_assertions::assert_eq!(expected.players, state.players);
pretty_assertions::assert_eq!(expected, state);
insta::assert_json_snapshot!(input_file, state);
}