|
| 1 | +//! Test using wrapper types as we expect the lib to be used. |
| 2 | +
|
| 3 | +#![cfg(feature = "std")] |
| 4 | + |
| 5 | +use consensus_encoding as encoding; |
| 6 | +use encoding::{ArrayEncoder, BytesEncoder, Encodable, Encoder2}; |
| 7 | + |
| 8 | +encoding::encoder_newtype! { |
| 9 | + /// An encoder that uses an inner `ArrayEncoder`. |
| 10 | + pub struct TestArrayEncoder(ArrayEncoder<4>); |
| 11 | +} |
| 12 | + |
| 13 | +encoding::encoder_newtype! { |
| 14 | + /// An encoder that uses an inner `BytesEncoder`. |
| 15 | + pub struct TestBytesEncoder<'e>(BytesEncoder<'e>); |
| 16 | +} |
| 17 | + |
| 18 | +#[test] |
| 19 | +fn array_encoder() { |
| 20 | + #[derive(Debug, Default, Clone)] |
| 21 | + pub struct Test(u32); |
| 22 | + |
| 23 | + impl Encodable for Test { |
| 24 | + type Encoder<'e> = TestArrayEncoder; |
| 25 | + fn encoder(&self) -> Self::Encoder<'_> { |
| 26 | + TestArrayEncoder(ArrayEncoder::without_length_prefix(self.0.to_le_bytes())) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + let t = Test(0xcafe_babe); // Encodes using an array. |
| 31 | + |
| 32 | + let want = [0xbe, 0xba, 0xfe, 0xca]; |
| 33 | + let got = encoding::encode_to_vec(&t); |
| 34 | + |
| 35 | + assert_eq!(got, want); |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn bytes_encoder_without_length_prefix() { |
| 40 | + #[derive(Debug, Default, Clone)] |
| 41 | + pub struct Test(Vec<u8>); |
| 42 | + |
| 43 | + impl Encodable for Test { |
| 44 | + type Encoder<'e> |
| 45 | + = TestBytesEncoder<'e> |
| 46 | + where |
| 47 | + Self: 'e; |
| 48 | + |
| 49 | + fn encoder(&self) -> Self::Encoder<'_> { |
| 50 | + TestBytesEncoder(BytesEncoder::without_length_prefix(self.0.as_ref())) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + let t = Test(vec![0xca, 0xfe]); |
| 55 | + |
| 56 | + let want = [0xca, 0xfe]; |
| 57 | + let got = encoding::encode_to_vec(&t); |
| 58 | + |
| 59 | + assert_eq!(got, want); |
| 60 | +} |
| 61 | + |
| 62 | +#[test] |
| 63 | +fn bytes_encoder_with_length_prefix() { |
| 64 | + #[derive(Debug, Default, Clone)] |
| 65 | + pub struct Test(Vec<u8>); |
| 66 | + |
| 67 | + impl Encodable for Test { |
| 68 | + type Encoder<'e> |
| 69 | + = TestBytesEncoder<'e> |
| 70 | + where |
| 71 | + Self: 'e; |
| 72 | + |
| 73 | + fn encoder(&self) -> Self::Encoder<'_> { |
| 74 | + TestBytesEncoder(BytesEncoder::with_length_prefix(self.0.as_ref())) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + let t = Test(vec![0xca, 0xfe]); |
| 79 | + |
| 80 | + let want = [0x02, 0xca, 0xfe]; |
| 81 | + let got = encoding::encode_to_vec(&t); |
| 82 | + |
| 83 | + assert_eq!(got, want); |
| 84 | +} |
| 85 | + |
| 86 | +#[test] |
| 87 | +fn two_encoder() { |
| 88 | + #[derive(Debug, Default, Clone)] |
| 89 | + pub struct Test { |
| 90 | + a: Vec<u8>, // Encode without prefix. |
| 91 | + b: Vec<u8>, // Encode with prefix. |
| 92 | + } |
| 93 | + |
| 94 | + impl Encodable for Test { |
| 95 | + type Encoder<'e> = Encoder2<TestBytesEncoder<'e>, TestBytesEncoder<'e>>; |
| 96 | + |
| 97 | + fn encoder(&self) -> Self::Encoder<'_> { |
| 98 | + let a = TestBytesEncoder(BytesEncoder::without_length_prefix(self.a.as_ref())); |
| 99 | + let b = TestBytesEncoder(BytesEncoder::with_length_prefix(self.b.as_ref())); |
| 100 | + |
| 101 | + Encoder2::new(a, b) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + let t = Test { a: vec![0xca, 0xfe], b: (vec![0xba, 0xbe]) }; |
| 106 | + |
| 107 | + let want = [0xca, 0xfe, 0x02, 0xba, 0xbe]; |
| 108 | + let got = encoding::encode_to_vec(&t); |
| 109 | + |
| 110 | + assert_eq!(got, want); |
| 111 | +} |
0 commit comments