Skip to content

Commit

Permalink
add test detailing unexpected behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
knickish committed Dec 16, 2023
1 parent aeb86ae commit 480fe58
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,3 +842,30 @@ fn array_leak_test() {

assert!(TOGGLED_ON_DROP.load(std::sync::atomic::Ordering::SeqCst))
}

// https://github.com/not-fl3/nanoserde/issues/89
#[test]
fn test() {
use nanoserde::DeJson;

#[derive(DeJson, Clone, PartialEq, Debug)]
pub struct EnumConstant {
value: i32,
}
let max_json = r#"{"value": 2147483647 }"#;
let wrap_json = r#"{"value": 2147483648 }"#;
assert_eq!(
<EnumConstant as DeJson>::deserialize_json(max_json).unwrap(),
EnumConstant { value: i32::MAX }
);
assert_eq!(
<EnumConstant as DeJson>::deserialize_json(wrap_json)
.unwrap_err()
.msg,
format!(
"Value {} too large for container of type {}",
2147483648_i64.to_string(),
std::any::type_name::<i32>()
)
);
}
28 changes: 28 additions & 0 deletions tests/ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,31 @@ fn array_leak_test() {

assert!(TOGGLED_ON_DROP.load(std::sync::atomic::Ordering::SeqCst))
}

// https://github.com/not-fl3/nanoserde/issues/89
#[test]
fn test() {
use nanoserde::DeRon;

#[derive(SerRon, DeRon, Clone, PartialEq, Debug)]
pub struct EnumConstant {
value: i32,
}
dbg!((&EnumConstant { value: 2147483647 }).serialize_ron());
let max_ron = r#"(value:2147483647)"#;
let wrap_ron = r#"(value:2147483648)"#;
assert_eq!(
<EnumConstant as DeRon>::deserialize_ron(max_ron).unwrap(),
EnumConstant { value: i32::MAX }
);
assert_eq!(
<EnumConstant as DeRon>::deserialize_ron(wrap_ron)
.unwrap_err()
.msg,
format!(
"Value {} too large for container of type {}",
2147483648_i64.to_string(),
std::any::type_name::<i32>()
)
);
}

0 comments on commit 480fe58

Please sign in to comment.