#[cfg(test)]
mod tests {
use crate::decode::decode;
use crate::error::DecodeErrorKind;
use crate::value::Value;
#[test]
fn decode_null() {
let bytes = vec![0x00];
assert_eq!(decode(&bytes).unwrap(), Value::Null);
}
#[test]
fn decode_bool() {
assert_eq!(decode(&[0x01]).unwrap(), Value::Bool(false));
assert_eq!(decode(&[0x02]).unwrap(), Value::Bool(true));
}
#[test]
fn decode_int() {
assert_eq!(decode(&[0x10, 0x01]), Ok(Value::Int(1)));
}
#[test]
fn decode_string() {
let bytes = vec![0x20, 0x02, b'h', b'i'];
assert_eq!(decode(&bytes), Ok(Value::String("hi".into())));
}
#[test]
fn decode_bytes() {
let bytes = vec![0x21, 0x02, 0xaa, 0xbb];
assert_eq!(decode(&bytes), Ok(Value::Bytes(vec![0xaa, 0xbb])));
}
#[test]
fn decode_list() {
let bytes = vec![0x30, 0x02, 0x10, 0x01, 0x10, 0x02];
assert_eq!(
decode(&bytes),
Ok(Value::List(vec![Value::Int(1), Value::Int(2)]))
);
}
#[test]
fn decode_map() {
let bytes = vec![
0x40, 0x01, 0x20, 0x01, b'a', 0x10, 0x01, ];
let mut map = std::collections::BTreeMap::new();
map.insert("a".into(), Value::Int(1));
assert_eq!(decode(&bytes), Ok(Value::Map(map)));
}
#[test]
fn decode_nested() {
let bytes = vec![
0x40, 0x01, 0x20, 0x01, b'x', 0x30, 0x02, 0x02, 0x00, ];
let mut map = std::collections::BTreeMap::new();
map.insert(
"x".into(),
Value::List(vec![Value::Bool(true), Value::Null]),
);
assert_eq!(decode(&bytes), Ok(Value::Map(map)));
}
#[test]
fn decode_truncated_string() {
let bytes = vec![0x20, 0x05, b'h'];
let err = decode(&bytes).unwrap_err();
assert_eq!(err.kind, DecodeErrorKind::UnexpectedEOF);
assert_eq!(err.offset, 2);
}
#[test]
fn decode_invalid_tag() {
let bytes = vec![0xFF];
let err = decode(&bytes).unwrap_err();
assert_eq!(err.kind, DecodeErrorKind::InvalidTag(0xFF));
assert_eq!(err.offset, 1);
}
#[test]
fn decode_trailing_bytes() {
let bytes = vec![0x00, 0x00];
let err = decode(&bytes).unwrap_err();
assert_eq!(err.kind, DecodeErrorKind::TrailingBytes);
assert_eq!(err.offset, 1);
}
}