use std::io::Cursor;
use std::path::Path;
use nbted::unstable::data::NBTFile;
fn complete_loop_from_nbt(nbt: &[u8]) {
let mut original = Vec::new();
original.extend_from_slice(nbt);
let nbtfile1 = nbted::unstable::read::read_file(&mut Cursor::new(original.clone())).unwrap();
let mut tmp = Vec::new();
nbted::unstable::string_write::write_file(&mut tmp, &nbtfile1).unwrap();
let string: String = String::from_utf8(tmp).unwrap();
let mut cursor = Cursor::new(string.into_bytes());
let nbtfile2 = nbted::unstable::string_read::read_file(&mut cursor).unwrap();
assert_eq!(&nbtfile1, &nbtfile2);
let mut tmp = Vec::new();
nbted::unstable::write::write_file(&mut tmp, &nbtfile2).unwrap();
assert_eq!(original, tmp);
}
fn complete_loop_from_enum(original: &NBTFile) {
let mut tmp = Vec::new();
nbted::unstable::string_write::write_file(&mut tmp, original).unwrap();
let string: String = String::from_utf8(tmp).unwrap();
let mut cursor = Cursor::new(string.into_bytes());
let nbtfile = nbted::unstable::string_read::read_file(&mut cursor).unwrap();
assert_eq!(original, &nbtfile);
let mut tmp = Vec::new();
nbted::unstable::write::write_file(&mut tmp, &nbtfile).unwrap();
let mut cursor = Cursor::new(tmp);
let nbtfile = nbted::unstable::read::read_file(&mut cursor).unwrap();
assert_eq!(original, &nbtfile);
}
fn uncompressed_nbt_file_byte_for_byte(path: &Path) -> datatest_stable::Result<()> {
let file = std::fs::read(path).unwrap();
complete_loop_from_nbt(&file);
Ok(())
}
fn nbt_file_decode_encode(path: &Path) -> datatest_stable::Result<()> {
let file = std::fs::read(path).unwrap();
let mut cursor = Cursor::new(file);
let nbtfile = nbted::unstable::read::read_file(&mut cursor).unwrap();
complete_loop_from_enum(&nbtfile);
Ok(())
}
fn txt_file_decode_encode(path: &Path) -> datatest_stable::Result<()> {
let file = std::fs::read(path).unwrap();
let nbtfile = nbted::unstable::string_read::read_file(&mut Cursor::new(&file)).unwrap();
let mut encoded = Vec::with_capacity(file.len());
nbted::unstable::string_write::write_file(&mut encoded, &nbtfile).unwrap();
assert_eq!(file, encoded);
Ok(())
}
fn compare_nbt_txt(path: &Path) -> datatest_stable::Result<()> {
let nbt = {
let file = std::fs::read(path).unwrap();
nbted::unstable::read::read_file(&mut Cursor::new(&file)).unwrap()
};
let txt = {
let mut txt_path = path.parent().unwrap().parent().unwrap().to_path_buf();
txt_path.push("txtfiles");
let filename_prefix = path
.file_name()
.unwrap()
.to_str()
.unwrap()
.trim_end_matches(".uncompressed.nbt");
txt_path.push(format!("{}.txt", filename_prefix));
let file = std::fs::read(&txt_path).unwrap();
nbted::unstable::string_read::read_file(&mut Cursor::new(&file)).unwrap()
};
assert_eq!(nbt, txt);
Ok(())
}
datatest_stable::harness! {
{ test = uncompressed_nbt_file_byte_for_byte, root = "tests/nbtfiles", pattern = "^.*\\.uncompressed\\.nbt$" },
{ test = nbt_file_decode_encode, root = "tests/nbtfiles" },
{ test = txt_file_decode_encode, root = "tests/txtfiles" },
{ test = compare_nbt_txt, root = "tests/nbtfiles", pattern = "^.*\\.uncompressed\\.nbt$" },
}