Skip to content

Commit

Permalink
Added tests for parsing plugin contents
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminwinger committed Apr 29, 2020
1 parent 73e375a commit 80c40aa
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,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(
Expand All @@ -719,6 +734,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<NamespacedId> = 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_str(), Ok("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(
Expand Down Expand Up @@ -846,6 +900,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(
Expand All @@ -857,6 +928,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(
Expand All @@ -875,6 +958,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(
Expand Down
22 changes: 22 additions & 0 deletions src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,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 =
Expand Down

0 comments on commit 80c40aa

Please sign in to comment.