Skip to content

Commit

Permalink
More generated Rust code tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Jan 26, 2019
1 parent d93c004 commit 17ac10f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 3 deletions.
94 changes: 94 additions & 0 deletions tests/rust_source/src/lib.rs
Expand Up @@ -626,6 +626,100 @@ pub mod rust_source {
}
_ => {
decoder.abort(Error::BadChoice);

return;
}
}
}
}
}

/// Type E.
pub mod e {
use super::super::{Error, Encoder, Decoder};

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum EAB {
C(bool)
}

impl Default for EAB {
fn default() -> Self {
EAB::C(bool::default())
}
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum EA {
B(EAB)
}

impl Default for EA {
fn default() -> Self {
EA::B(EAB::default())
}
}

#[derive(Debug, Default, PartialEq, Copy, Clone)]
pub struct E {
pub a: EA
}

impl E {
pub fn encode(&mut self, mut dst: &mut [u8]) -> Result<usize, Error> {
let mut encoder = Encoder::new(&mut dst);

self.encode_inner(&mut encoder);

encoder.get_result()
}

pub fn decode(&mut self, src: &[u8]) -> Result<usize, Error> {
let mut decoder = Decoder::new(&src);

self.decode_inner(&mut decoder);

decoder.get_result()
}

pub fn encode_inner(&mut self, encoder: &mut Encoder) {
match self.a {
EA::B(value) => {
encoder.append_non_negative_binary_integer(0, 0);

match value {
EAB::C(value_2) => {
encoder.append_non_negative_binary_integer(0, 0);
encoder.append_bool(value_2);
}
}
}
}
}

pub fn decode_inner(&mut self, decoder: &mut Decoder) {
let choice = decoder.read_non_negative_binary_integer(0);

match choice {
0 => {
let EA::B(ref mut b) = self.a;
let choice_2 = decoder.read_non_negative_binary_integer(0);

match choice_2 {
0 => {
*b = EAB::C(decoder.read_bool());
},
_ => {
decoder.abort(Error::BadChoice);

return;
}
}
},
_ => {
decoder.abort(Error::BadChoice);

return;
}
}
}
Expand Down
28 changes: 25 additions & 3 deletions tests/rust_source/tests/integration_tests.rs
@@ -1,5 +1,5 @@
use rust_source::Error;
use rust_source::rust_source::{a, b, d};
use rust_source::rust_source::{a, b, d, e};

#[test]
fn test_uper_c_source_a() {
Expand Down Expand Up @@ -138,11 +138,9 @@ fn test_uper_c_source_b_choice_b() {
assert_eq!(b.decode(&encoded).unwrap(), encoded.len());

match b {

b::B::B(value) => {
assert_eq!(value, a);
},

_ => panic!("")
}
}
Expand Down Expand Up @@ -253,3 +251,27 @@ fn test_uper_c_source_d_decode_error_bad_enum() {

assert_eq!(d.decode(&encoded), Err(Error::BadEnum));
}

#[test]
fn test_uper_c_source_e() {
let mut encoded = [0; 1];
let mut e: e::E = Default::default();

// Encode.
e.a = e::EA::B(e::EAB::C(true));

assert_eq!(e.encode(&mut encoded).unwrap(), encoded.len());
assert_eq!(encoded[..],
[
0x80
][..]);

// Decode.
e = Default::default();

assert_eq!(e.decode(&encoded).unwrap(), encoded.len());

let e::EA::B(e::EAB::C(value)) = e.a;

assert_eq!(value, true);
}

0 comments on commit 17ac10f

Please sign in to comment.