openethereum/util/rlp_derive/tests/rlp.rs
David 28c731881f
Rlp decode returns Result (#8527)
rlp::decode returns Result

Make a best effort to handle decoding errors gracefully throughout the code, using `expect` where the value is guaranteed to be valid (and in other places where it makes sense).
2018-05-08 11:22:12 +02:00

45 lines
839 B
Rust

extern crate rlp;
#[macro_use]
extern crate rlp_derive;
use rlp::{encode, decode};
#[derive(Debug, PartialEq, RlpEncodable, RlpDecodable)]
struct Foo {
a: String,
}
#[derive(Debug, PartialEq, RlpEncodableWrapper, RlpDecodableWrapper)]
struct FooWrapper {
a: String,
}
#[test]
fn test_encode_foo() {
let foo = Foo {
a: "cat".into(),
};
let expected = vec![0xc4, 0x83, b'c', b'a', b't'];
let out = encode(&foo).into_vec();
assert_eq!(out, expected);
let decoded = decode(&expected).expect("decode failure");
assert_eq!(foo, decoded);
}
#[test]
fn test_encode_foo_wrapper() {
let foo = FooWrapper {
a: "cat".into(),
};
let expected = vec![0x83, b'c', b'a', b't'];
let out = encode(&foo).into_vec();
assert_eq!(out, expected);
let decoded = decode(&expected).expect("decode failure");
assert_eq!(foo, decoded);
}