-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
assert
and assert_eq
attributes
- Loading branch information
1 parent
17255f5
commit c506699
Showing
8 changed files
with
243 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod test_assert; | ||
mod test_assert_eq; | ||
mod test_cond; | ||
mod test_ctx; | ||
mod test_limits; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use deku::prelude::*; | ||
use hexlit::hex; | ||
use rstest::rstest; | ||
use std::convert::{TryFrom, TryInto}; | ||
|
||
#[derive(Default, PartialEq, Debug, DekuRead, DekuWrite)] | ||
struct TestStruct { | ||
field_a: u8, | ||
#[deku(assert = "*field_a + *field_b >= 3")] | ||
field_b: u8, | ||
} | ||
|
||
#[rstest(input, expected, | ||
case(&hex!("0102"), TestStruct { | ||
field_a: 0x01, | ||
field_b: 0x02, | ||
}), | ||
#[should_panic(expected = r#"Assertion("field \'field_b\' failed assertion: * field_a + * field_b >= 3")"#)] | ||
case(&hex!("0101"), TestStruct::default()) | ||
)] | ||
fn test_assert_read(input: &[u8], expected: TestStruct) { | ||
let ret_read = TestStruct::try_from(input).unwrap(); | ||
assert_eq!(expected, ret_read); | ||
} | ||
|
||
#[rstest(input, expected, | ||
case(TestStruct { | ||
field_a: 0x01, | ||
field_b: 0x02, | ||
}, hex!("0102").to_vec()), | ||
#[should_panic(expected = r#"Assertion("field \'field_b\' failed assertion: * field_a + * field_b >= 3")"#)] | ||
case(TestStruct { | ||
field_a: 0x01, | ||
field_b: 0x01, | ||
}, hex!("").to_vec()), | ||
)] | ||
fn test_assert_write(input: TestStruct, expected: Vec<u8>) { | ||
let ret_write: Vec<u8> = input.try_into().unwrap(); | ||
assert_eq!(expected, ret_write); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use deku::prelude::*; | ||
use hexlit::hex; | ||
use rstest::rstest; | ||
use std::convert::{TryFrom, TryInto}; | ||
|
||
#[derive(Default, PartialEq, Debug, DekuRead, DekuWrite)] | ||
struct TestStruct { | ||
field_a: u8, | ||
#[deku(assert_eq = "*field_a")] | ||
field_b: u8, | ||
} | ||
|
||
#[rstest(input, expected, | ||
case(&hex!("0101"), TestStruct { | ||
field_a: 0x01, | ||
field_b: 0x01, | ||
}), | ||
#[should_panic(expected = r#"Assertion("field \'field_b\' failed assertion: field_b == * field_a")"#)] | ||
case(&hex!("0102"), TestStruct::default()) | ||
)] | ||
fn test_assert_eq_read(input: &[u8], expected: TestStruct) { | ||
let ret_read = TestStruct::try_from(input).unwrap(); | ||
assert_eq!(expected, ret_read); | ||
} | ||
|
||
#[rstest(input, expected, | ||
case(TestStruct { | ||
field_a: 0x01, | ||
field_b: 0x01, | ||
}, hex!("0101").to_vec()), | ||
#[should_panic(expected = r#"Assertion("field \'field_b\' failed assertion: field_b == * field_a")"#)] | ||
case(TestStruct { | ||
field_a: 0x01, | ||
field_b: 0x02, | ||
}, hex!("").to_vec()), | ||
)] | ||
fn test_assert_eq_write(input: TestStruct, expected: Vec<u8>) { | ||
let ret_write: Vec<u8> = input.try_into().unwrap(); | ||
assert_eq!(expected, ret_write); | ||
} |