From 45fdda75d0154c741bd96cd14550b956b2f56ede Mon Sep 17 00:00:00 2001 From: Benjamin Winger Date: Wed, 29 Apr 2020 11:38:35 -0400 Subject: [PATCH] Added tests for parsing plugin contents --- src/plugin.rs | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/record.rs | 22 +++++++++ 2 files changed, 142 insertions(+) diff --git a/src/plugin.rs b/src/plugin.rs index 38c50e4..815c61e 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -702,6 +702,21 @@ mod tests { } } + #[test] + fn parse_file_should_succeed_mode_all() { + let mut plugin = Plugin::new( + GameId::Morrowind, + Path::new("testing-plugins/Morrowind/Data Files/Blank.esm"), + ); + + assert!(plugin.parse_file(ParseMode::All).is_ok()); + + match plugin.data.record_ids { + RecordIds::NamespacedIds(ids) => assert_eq!(10, ids.len()), + _ => panic!("Expected namespaced record IDs"), + } + } + #[test] fn plugin_parse_should_read_a_unique_id_for_each_record() { let mut plugin = Plugin::new( @@ -720,6 +735,45 @@ mod tests { } } + #[test] + fn plugin_parse_should_read_a_unique_id_for_each_record_mode_all() { + let mut plugin = Plugin::new( + GameId::Morrowind, + Path::new("testing-plugins/Morrowind/Data Files/Blank.esm"), + ); + + assert!(plugin.parse_file(ParseMode::All).is_ok()); + + match plugin.data.record_ids { + RecordIds::NamespacedIds(ids) => { + let set: HashSet = HashSet::from_iter(ids.iter().cloned()); + assert_eq!(set.len(), ids.len()); + } + _ => panic!("Expected namespaced record IDs"), + } + } + + #[test] + fn plugin_parse_should_read_all_records_mode_all() { + let mut plugin = Plugin::new( + GameId::Morrowind, + Path::new("testing-plugins/Morrowind/Data Files/Blank.esm"), + ); + + assert!(plugin.parse_file(ParseMode::All).is_ok()); + assert_eq!(10, plugin.get_entries().len()); + + for entry in plugin.get_entries() { + match entry { + PluginEntry::Record(record) => { + assert_eq!(record.header_type(), *b"GMST"); + assert_eq!(record.subrecords().len(), 2); + } + PluginEntry::Group(_) => panic!("Unexpected group in morrowind record"), + } + } + } + #[test] fn parse_file_header_only_should_not_store_record_ids() { let mut plugin = Plugin::new( @@ -847,6 +901,23 @@ mod tests { } } + #[test] + fn record_and_group_count_should_match_record_ids_and_entries_length_mode_all() { + let mut plugin = Plugin::new( + GameId::Morrowind, + Path::new("testing-plugins/Morrowind/Data Files/Blank.esm"), + ); + + assert!(plugin.record_and_group_count().is_none()); + assert!(plugin.parse_file(ParseMode::All).is_ok()); + assert_eq!(10, plugin.record_and_group_count().unwrap()); + assert_eq!(10, plugin.get_entries().len()); + match plugin.data.record_ids { + RecordIds::NamespacedIds(ids) => assert_eq!(10, ids.len()), + _ => panic!("Expected namespaced record IDs"), + } + } + #[test] fn count_override_records_should_return_0_even_when_override_records_are_present() { let mut plugin = Plugin::new( @@ -858,6 +929,18 @@ mod tests { assert_eq!(0, plugin.count_override_records()); } + #[test] + fn count_override_records_should_return_0_even_when_override_records_are_present_mode_all() + { + let mut plugin = Plugin::new( + GameId::Morrowind, + Path::new("testing-plugins/Morrowind/Data Files/Blank - Master Dependent.esm"), + ); + + assert!(plugin.parse_file(ParseMode::All).is_ok()); + assert_eq!(0, plugin.count_override_records()); + } + #[test] fn overlaps_with_should_detect_when_two_plugins_have_a_record_with_the_same_id() { let mut plugin1 = Plugin::new( @@ -876,6 +959,43 @@ mod tests { assert!(!plugin1.overlaps_with(&plugin2)); } + #[test] + fn overlaps_with_should_detect_when_two_plugins_have_a_record_with_the_same_id_mode_all() { + let mut plugin1 = Plugin::new( + GameId::Morrowind, + Path::new("testing-plugins/Morrowind/Data Files/Blank.esm"), + ); + let mut plugin2 = Plugin::new( + GameId::Morrowind, + Path::new("testing-plugins/Morrowind/Data Files/Blank - Different.esm"), + ); + + assert!(plugin1.parse_file(ParseMode::All).is_ok()); + assert!(plugin2.parse_file(ParseMode::All).is_ok()); + + assert!(plugin1.overlaps_with(&plugin1)); + assert!(!plugin1.overlaps_with(&plugin2)); + } + + #[test] + fn overlaps_with_should_detect_when_two_plugins_have_a_record_with_the_same_id_mode_mixed() + { + let mut plugin1 = Plugin::new( + GameId::Morrowind, + Path::new("testing-plugins/Morrowind/Data Files/Blank.esm"), + ); + let mut plugin2 = Plugin::new( + GameId::Morrowind, + Path::new("testing-plugins/Morrowind/Data Files/Blank - Different.esm"), + ); + + assert!(plugin1.parse_file(ParseMode::RecordIds).is_ok()); + assert!(plugin2.parse_file(ParseMode::All).is_ok()); + + assert!(plugin1.overlaps_with(&plugin1)); + assert!(!plugin1.overlaps_with(&plugin2)); + } + #[test] fn overlap_size_should_only_count_each_record_once() { let mut plugin1 = Plugin::new( diff --git a/src/record.rs b/src/record.rs index 52d3ce7..fca5f9b 100644 --- a/src/record.rs +++ b/src/record.rs @@ -483,6 +483,28 @@ mod tests { assert!(record.header.form_id.is_none()); } + #[test] + fn parse_record_id_from_self_should_return_none_for_tes3_header() { + let data = + &include_bytes!("../testing-plugins/Morrowind/Data Files/Blank.esm")[..0x144]; + + let record = Record::parse(data, GameId::Morrowind, false).unwrap().1; + let form_id = record.parse_record_id_from_self(GameId::Morrowind); + + assert!(form_id.is_none()); + } + + #[test] + fn parse_record_id_from_self_should_return_some_namespaced_id_for_gmst() { + let data = + &include_bytes!("../testing-plugins/Morrowind/Data Files/Blank.esm")[0x144..0x16F]; + + let record = Record::parse(data, GameId::Morrowind, false).unwrap().1; + let form_id = record.parse_record_id_from_self(GameId::Morrowind); + + assert!(form_id.unwrap().namespaced_id().is_some()); + } + #[test] fn parse_record_id_should_return_none_for_tes3_header() { let data =