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

cleanup tests

This commit is contained in:
Robin Appelman 2020-03-21 16:04:53 +01:00
commit acc1584524
5 changed files with 70 additions and 123 deletions

19
Cargo.lock generated
View file

@ -577,6 +577,17 @@ dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "test-case"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "tf-demo-parser" name = "tf-demo-parser"
version = "0.1.0" version = "0.1.0"
@ -597,6 +608,7 @@ dependencies = [
"serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_repr 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde_repr 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "snap 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"test-case 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -630,6 +642,11 @@ name = "unicode-xid"
version = "0.2.0" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.8" version = "0.3.8"
@ -720,11 +737,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum syn_util 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "54d358a6d0e025630dc486f8a76caf9b7c9ffefb117ca426c3f9b3911f20eb89" "checksum syn_util 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "54d358a6d0e025630dc486f8a76caf9b7c9ffefb117ca426c3f9b3911f20eb89"
"checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" "checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545"
"checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625"
"checksum test-case 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "199464148b42bcf3da8b2a56f6ee87ca68f47402496d1268849291ec9fb463c8"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "01d1404644c8b12b16bfcffa4322403a91a451584daaaa7c28d3152e6cbc98cf" "checksum toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "01d1404644c8b12b16bfcffa4322403a91a451584daaaa7c28d3152e6cbc98cf"
"checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" "checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
"checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce"
"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View file

@ -31,6 +31,7 @@ fnv = "1.0.6"
[dev-dependencies] [dev-dependencies]
pretty_assertions = "0.6" pretty_assertions = "0.6"
test-case = "1.0.0"
[profile.release] [profile.release]
lto = true lto = true

View file

@ -1,24 +1,16 @@
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_variables)]
use pretty_assertions::assert_eq;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr}; use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fs::{self, File}; use std::fs::{self, File};
use test_case::test_case;
use std::collections::{HashMap, HashSet}; use std::collections::HashMap;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
use std::rc::Rc;
use tf_demo_parser::demo::message::packetentities::{EntityId, PacketEntity, PVS}; use tf_demo_parser::demo::message::packetentities::{EntityId, PacketEntity, PVS};
use tf_demo_parser::demo::message::Message; use tf_demo_parser::demo::message::Message;
use tf_demo_parser::demo::packet::datatable::{ use tf_demo_parser::demo::packet::datatable::{ServerClass, ServerClassName};
ParseSendTable, SendTableName, ServerClass, ServerClassName,
};
use tf_demo_parser::demo::packet::stringtable::StringTableEntry;
use tf_demo_parser::demo::parser::MessageHandler; use tf_demo_parser::demo::parser::MessageHandler;
use tf_demo_parser::demo::sendprop::{SendPropDefinition, SendPropName, SendPropValue}; use tf_demo_parser::demo::sendprop::SendPropValue;
use tf_demo_parser::{Demo, DemoParser, MatchState, MessageType, MessageTypeAnalyser, ParserState}; use tf_demo_parser::{Demo, DemoParser, MessageType, ParserState};
/// Compatible serialization with the js parser entity dumps /// Compatible serialization with the js parser entity dumps
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize_repr, Deserialize_repr)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
@ -53,7 +45,6 @@ struct EntityDump {
impl EntityDump { impl EntityDump {
pub fn from_entity(entity: PacketEntity, tick: u32, classes: &[ServerClass]) -> Self { pub fn from_entity(entity: PacketEntity, tick: u32, classes: &[ServerClass]) -> Self {
let id = entity.entity_index;
EntityDump { EntityDump {
tick, tick,
server_class: classes[usize::from(entity.server_class)].name.clone(), server_class: classes[usize::from(entity.server_class)].name.clone(),
@ -115,6 +106,7 @@ impl MessageHandler for EntityDumper {
} }
} }
#[test_case("data/small.dem", "data/small_entities.json"; "small.dem")]
fn entity_test(input_file: &str, snapshot_file: &str) { fn entity_test(input_file: &str, snapshot_file: &str) {
let file = fs::read(input_file).expect("Unable to read file"); let file = fs::read(input_file).expect("Unable to read file");
let demo = Demo::new(file); let demo = Demo::new(file);
@ -135,31 +127,35 @@ fn entity_test(input_file: &str, snapshot_file: &str) {
buffer.clear(); buffer.clear();
} }
assert_eq!(expected.len(), entities.len()); pretty_assertions::assert_eq!(expected.len(), entities.len());
let entity_ids: Vec<_> = entities.iter().map(|entity| entity.id).collect(); let entity_ids: Vec<_> = entities.iter().map(|entity| entity.id).collect();
let expected_ids: Vec<_> = expected.iter().map(|entity| entity.id).collect(); let expected_ids: Vec<_> = expected.iter().map(|entity| entity.id).collect();
assert_eq!(expected_ids, entity_ids); pretty_assertions::assert_eq!(expected_ids, entity_ids);
for (expected_entity, entity) in expected.into_iter().zip(entities.into_iter()) { for (expected_entity, entity) in expected.into_iter().zip(entities.into_iter()) {
assert_eq!( pretty_assertions::assert_eq!(
expected_entity.tick, entity.tick, expected_entity.tick,
entity.tick,
"Failed comparing entity {}", "Failed comparing entity {}",
entity.id entity.id
); );
assert_eq!( pretty_assertions::assert_eq!(
expected_entity.id, entity.id, expected_entity.id,
entity.id,
"Failed comparing entity {}", "Failed comparing entity {}",
entity.id entity.id
); );
assert_eq!( pretty_assertions::assert_eq!(
expected_entity.server_class, entity.server_class, expected_entity.server_class,
entity.server_class,
"Failed comparing entity {}", "Failed comparing entity {}",
entity.id entity.id
); );
assert_eq!( pretty_assertions::assert_eq!(
expected_entity.pvs, entity.pvs, expected_entity.pvs,
entity.pvs,
"Failed comparing entity {}", "Failed comparing entity {}",
entity.id entity.id
); );
@ -168,14 +164,15 @@ fn entity_test(input_file: &str, snapshot_file: &str) {
prop_names.sort(); prop_names.sort();
expected_prop_names.sort(); expected_prop_names.sort();
assert_eq!( pretty_assertions::assert_eq!(
expected_prop_names, prop_names, expected_prop_names,
prop_names,
"Failed comparing entity {}", "Failed comparing entity {}",
entity.id entity.id
); );
for prop_name in expected_prop_names { for prop_name in expected_prop_names {
assert_eq!( pretty_assertions::assert_eq!(
expected_entity.props.get(prop_name), expected_entity.props.get(prop_name),
entity.props.get(prop_name), entity.props.get(prop_name),
"Failed comparing entity {} prop {}", "Failed comparing entity {} prop {}",
@ -184,15 +181,11 @@ fn entity_test(input_file: &str, snapshot_file: &str) {
); );
} }
assert_eq!( pretty_assertions::assert_eq!(
expected_entity, entity, expected_entity,
entity,
"Failed comparing entity {}", "Failed comparing entity {}",
entity.id entity.id
); );
} }
} }
#[test]
fn entity_test_short() {
entity_test("data/small.dem", "data/small_entities.json");
}

View file

@ -1,19 +1,10 @@
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_variables)]
use pretty_assertions::assert_eq;
use std::fs; use std::fs;
use test_case::test_case;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use tf_demo_parser::demo::message::Message; use tf_demo_parser::demo::packet::datatable::{ParseSendTable, SendTableName, ServerClass};
use tf_demo_parser::demo::packet::datatable::{
ParseSendTable, SendTable, SendTableName, ServerClass,
};
use tf_demo_parser::demo::packet::stringtable::StringTableEntry;
use tf_demo_parser::demo::parser::MessageHandler; use tf_demo_parser::demo::parser::MessageHandler;
use tf_demo_parser::demo::sendprop::SendPropDefinition; use tf_demo_parser::{Demo, DemoParser, MessageType, ParserState};
use tf_demo_parser::{Demo, DemoParser, MatchState, MessageType, MessageTypeAnalyser, ParserState};
pub struct SendPropAnalyser { pub struct SendPropAnalyser {
tables: Vec<ParseSendTable>, tables: Vec<ParseSendTable>,
@ -28,19 +19,20 @@ impl SendPropAnalyser {
impl MessageHandler for SendPropAnalyser { impl MessageHandler for SendPropAnalyser {
type Output = Vec<ParseSendTable>; type Output = Vec<ParseSendTable>;
fn does_handle(message_type: MessageType) -> bool { fn does_handle(_message_type: MessageType) -> bool {
false false
} }
fn handle_data_tables(&mut self, tables: &[ParseSendTable], server_classes: &[ServerClass]) { fn handle_data_tables(&mut self, tables: &[ParseSendTable], _server_classes: &[ServerClass]) {
self.tables = tables.to_vec() self.tables = tables.to_vec()
} }
fn into_output(self, state: &ParserState) -> Self::Output { fn into_output(self, _state: &ParserState) -> Self::Output {
self.tables self.tables
} }
} }
#[test_case("data/gully.dem", "data/gully_props.json"; "gully.dem")]
fn flatten_test(input_file: &str, snapshot_file: &str) { fn flatten_test(input_file: &str, snapshot_file: &str) {
let file = fs::read(input_file).expect("Unable to read file"); let file = fs::read(input_file).expect("Unable to read file");
let demo = Demo::new(file); let demo = Demo::new(file);
@ -72,13 +64,8 @@ fn flatten_test(input_file: &str, snapshot_file: &str) {
let expected_tables: HashSet<_> = expected.keys().collect(); let expected_tables: HashSet<_> = expected.keys().collect();
let actual_tables: HashSet<_> = flat_props.keys().collect(); let actual_tables: HashSet<_> = flat_props.keys().collect();
assert_eq!(expected_tables, actual_tables); pretty_assertions::assert_eq!(expected_tables, actual_tables);
for table in expected_tables { for table in expected_tables {
assert_eq!(expected[table], flat_props[table]); pretty_assertions::assert_eq!(expected[table], flat_props[table]);
} }
} }
#[test]
fn sendprop_test_gully() {
flatten_test("data/gully.dem", "data/gully_props.json");
}

View file

@ -1,9 +1,16 @@
use pretty_assertions::assert_eq;
use std::fs; use std::fs;
use test_case::test_case;
use tf_demo_parser::demo::parser::gamestateanalyser::{GameState, GameStateAnalyser}; use tf_demo_parser::demo::parser::gamestateanalyser::{GameState, GameStateAnalyser};
use tf_demo_parser::{Demo, DemoParser, MatchState, MessageType, MessageTypeAnalyser}; use tf_demo_parser::{Demo, DemoParser, MatchState};
#[test_case("data/small.dem", "data/small.json"; "small.dem")]
#[test_case("data/gully.dem", "data/gully.json"; "gully.dem")]
#[test_case("data/comp.dem", "data/comp.json"; "comp.dem")]
#[test_case("data/malformed_cvar.dem", "data/malformed_cvar.json"; "malformed_cvar.dem")]
#[test_case("data/unicode-saytext.dem", "data/unicode-saytext.json"; "unicode-saytext.dem")]
#[test_case("data/nousers.dem", "data/nousers.json"; "nousers.dem")]
#[test_case("data/decal.dem", "data/decal.json"; "decal.dem")]
fn snapshot_test(input_file: &str, snapshot_file: &str) { fn snapshot_test(input_file: &str, snapshot_file: &str) {
let file = fs::read(input_file).expect("Unable to read file"); let file = fs::read(input_file).expect("Unable to read file");
let demo = Demo::new(file); let demo = Demo::new(file);
@ -15,26 +22,11 @@ fn snapshot_test(input_file: &str, snapshot_file: &str) {
.as_slice(), .as_slice(),
) )
.unwrap(); .unwrap();
assert_eq!(expected, state); pretty_assertions::assert_eq!(expected, state);
}
fn test_message_types(input_file: &str, snapshot_file: &str) {
let file = fs::read(input_file).expect("Unable to read file");
let demo = Demo::new(file);
let (_, message_types) =
DemoParser::new_with_analyser(demo.get_stream(), MessageTypeAnalyser::default())
.parse()
.unwrap();
let expected: Vec<MessageType> = serde_json::from_slice(
fs::read(snapshot_file)
.expect("Unable to read file")
.as_slice(),
)
.unwrap();
assert_eq!(expected, message_types);
} }
#[test_case("data/small.dem", "data/small_game_state.json"; "small.dem")]
#[test_case("data/gully.dem", "data/gully_game_state.json"; "gully.dem")]
fn game_state_test(input_file: &str, snapshot_file: &str) { fn game_state_test(input_file: &str, snapshot_file: &str) {
let file = fs::read(input_file).expect("Unable to read file"); let file = fs::read(input_file).expect("Unable to read file");
let demo = Demo::new(file); let demo = Demo::new(file);
@ -48,50 +40,5 @@ fn game_state_test(input_file: &str, snapshot_file: &str) {
.as_slice(), .as_slice(),
) )
.unwrap(); .unwrap();
assert_eq!(expected, state); pretty_assertions::assert_eq!(expected, state);
}
#[test]
fn snapshot_test_small() {
snapshot_test("data/small.dem", "data/small.json");
}
#[test]
fn snapshot_test_gully() {
snapshot_test("data/gully.dem", "data/gully.json");
}
#[test]
fn snapshot_test_comp() {
snapshot_test("data/comp.dem", "data/comp.json");
}
#[test]
fn snapshot_test_malformed_cvar() {
snapshot_test("data/malformed_cvar.dem", "data/malformed_cvar.json");
}
#[test]
fn snapshot_test_unicode_chat() {
snapshot_test("data/unicode-saytext.dem", "data/unicode-saytext.json");
}
#[test]
fn snapshot_test_player_in_update() {
snapshot_test("data/nousers.dem", "data/nousers.json");
}
#[test]
fn snapshot_test_decal() {
snapshot_test("data/decal.dem", "data/decal.json");
}
#[test]
fn game_state_test_small() {
game_state_test("data/small.dem", "data/small_game_state.json");
}
#[test]
fn game_state_test_gully() {
game_state_test("data/gully.dem", "data/gully_game_state.json");
} }