use preserves2 as preserves; use preserves::BinarySource; use preserves::BytesBinarySource; use preserves::IOBinarySource; use preserves::IOValue; use preserves::IOValues; use preserves::PackedWriter; use preserves::Reader; use preserves::TextWriter; use preserves::ValueReader; use std::io; use std::iter::Iterator; mod samples; use samples::*; fn parse_all(text: &str) -> io::Result> { IOValues::new(BytesBinarySource::new(text.as_bytes()).into_text()).collect() } fn decode_all(bytes: &'_ [u8]) -> io::Result> { IOValues::new(BytesBinarySource::new(bytes).into_packed()).collect() } #[test] fn compare_text_with_packed() -> io::Result<()> { use io::prelude::*; let from_text = { let mut fh = std::fs::File::open("./tests/samples.pr").unwrap(); let mut contents = String::new(); fh.read_to_string(&mut contents)?; IOValue::iovalue_from_str(&contents)? }; let from_packed = { let mut fh = std::fs::File::open("./tests/samples.bin").unwrap(); IOValue::read_iovalue(&mut IOBinarySource::new(&mut fh).into_packed(), true)? }; assert_eq!(from_text, from_packed); Ok(()) } #[test] fn compare_deserialize_text_with_packed() -> io::Result<()> { use io::prelude::*; let from_text = { let mut fh = std::fs::File::open("./tests/samples.pr").unwrap(); let mut contents = String::new(); fh.read_to_string(&mut contents)?; let tests: TestCases = preserves::serde::from_text(&contents)?; tests }; let from_packed = { let mut fh = std::fs::File::open("./tests/samples.bin").unwrap(); let tests: TestCases = preserves::serde::from_read(&mut fh)?; tests }; assert_eq!(from_text, from_packed); Ok(()) } #[test] fn read_write_read_text() -> io::Result<()> { use io::prelude::*; let from_text = { let mut fh = std::fs::File::open("./tests/samples.pr").unwrap(); let mut contents = String::new(); fh.read_to_string(&mut contents)?; IOValue::iovalue_from_str(&contents)? }; let roundtripped = { let mut bs = Vec::new(); let mut w = TextWriter::new(&mut bs); preserves::serde::to_writer(&mut w, &from_text)?; let s = String::from_utf8(bs).unwrap(); IOValue::iovalue_from_str(&s)? }; let roundtripped_indented = { let mut bs = Vec::new(); let mut w = TextWriter::new(&mut bs); w.indentation = 4; preserves::serde::to_writer(&mut w, &from_text)?; let s = String::from_utf8(bs).unwrap(); IOValue::iovalue_from_str(&s)? }; assert_eq!(from_text, roundtripped); assert_eq!(from_text, roundtripped_indented); Ok(()) } #[test] fn deserialize_serialize_deserialize_text() -> io::Result<()> { use io::prelude::*; let from_text = { let mut fh = std::fs::File::open("./tests/samples.pr").unwrap(); let mut contents = String::new(); fh.read_to_string(&mut contents)?; let tests: TestCases = preserves::serde::from_text(&contents)?; tests }; let roundtripped = { let mut bs = Vec::new(); let mut w = TextWriter::new(&mut bs); preserves::serde::to_writer(&mut w, &from_text)?; let s = String::from_utf8(bs).unwrap(); preserves::serde::from_text(&s)? }; assert_eq!(from_text, roundtripped); Ok(()) } #[test] fn run() -> io::Result<()> { let mut fh = std::fs::File::open("./tests/samples.bin").unwrap(); let mut d = IOValues::new(IOBinarySource::new(&mut fh).into_packed()).read_annotations(true); let tests: TestCases = preserves::serde::from_value(&d.next().unwrap().unwrap()).unwrap(); for (ref name, ref case) in tests.tests { println!("{:?} ==> {:?}", name, case); match case { TestCase::Test(ref bin, ref val) => { assert_eq!(&decode_all(&PackedWriter::encode(val)?[..])?, &[val.clone()]); assert_eq!(&decode_all(&bin[..])?, &[val.clone()]); assert_eq!(&PackedWriter::encode(val)?, bin); } TestCase::NondeterministicTest(ref bin, ref val) => { // The test cases in samples.pr are carefully written // so that while strictly "nondeterministic", the // order of keys in encoded dictionaries follows // Preserves canonical order. assert_eq!(&PackedWriter::encode(val)?, bin); assert_eq!(&decode_all(&PackedWriter::encode(val)?[..])?, &[val.clone()]); assert_eq!(&decode_all(&bin[..])?, &[val.clone()]); } TestCase::DecodeTest(ref bin, ref val) => { assert_eq!(&decode_all(&PackedWriter::encode(val)?[..])?, &[val.clone()]); assert_eq!(&decode_all(&bin[..])?, &[val.clone()]); } TestCase::ParseError(text) => { match parse_all(&text) { Ok(vs) => panic!("Unexpected success: {:?}", vs), Err(e) => if e.kind() == io::ErrorKind::InvalidData { // all is OK } else { panic!("Unexpected error {:?}", e) } } } TestCase::ParseShort(text) => { let mut r = BytesBinarySource::new(text.as_bytes()).into_text(); assert!(match IOValue::read_iovalue(&mut r, true) { Err(preserves::error::ReadError::Io(e)) => e.kind() == io::ErrorKind::UnexpectedEof, _ => false, }) } TestCase::ParseEOF(text) => { assert!(BytesBinarySource::new(text.as_bytes()).into_text().peek_class()?.is_none()); } TestCase::DecodeError(ref bin) => { match decode_all(&bin[..]) { Ok(vs) => panic!("Unexpected success: {:?}", vs), Err(e) => if e.kind() == io::ErrorKind::InvalidData { // all is OK } else { panic!("Unexpected error {:?}", e) } } } TestCase::DecodeShort(ref bin) => { let mut r = BytesBinarySource::new(bin).into_packed(); assert!(r.peek_class()?.is_some()); assert!(match IOValue::read_iovalue(&mut r, true) { Err(preserves::error::ReadError::Io(e)) => e.kind() == io::ErrorKind::UnexpectedEof, _ => false, }) } TestCase::DecodeEOF(ref bin) => { assert!(BytesBinarySource::new(bin).into_packed().peek_class()?.is_none()); } } } Ok(()) }