-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bugfix: lax_deserialize currently accepts deserializing from NaN #25
Changes from all commits
b369adc
fdee54b
502c172
f990d3f
d578716
e508b6d
382410e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#![cfg(feature = "serde")] | ||
|
||
use js_int::{int, Int}; | ||
use serde::{de::IntoDeserializer, Deserialize}; | ||
use serde_test::{assert_ser_tokens, Token}; | ||
|
||
#[test] | ||
fn serialize_int() { | ||
assert_serialize(100); | ||
assert_serialize(0); | ||
assert_serialize(-100); | ||
} | ||
|
||
fn assert_serialize(number: i32) { | ||
assert_ser_tokens( | ||
&Int::from(number), | ||
&[Token::NewtypeStruct { name: "Int" }, Token::I64(number as _)], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Writing out the This relates to #16 because a manual There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point. Idk which serializers out there actually serialize sth. for |
||
) | ||
} | ||
|
||
#[test] | ||
fn deserialize_int() { | ||
assert_eq!(deserialize_from(100).unwrap(), int!(100)); | ||
assert_eq!(deserialize_from(0).unwrap(), int!(0)); | ||
assert_eq!(deserialize_from(-100).unwrap(), int!(-100)); | ||
assert_eq!(deserialize_from(-9007199254740991i64).unwrap(), Int::MIN); | ||
assert_eq!(deserialize_from(9007199254740991i64).unwrap(), Int::MAX); | ||
assert!(deserialize_from(9007199254740992i64).is_err()); | ||
assert!(deserialize_from(-9007199254740992i64).is_err()); | ||
} | ||
|
||
#[test] | ||
#[cfg_attr(feature = "lax_deserialize", ignore)] | ||
fn strict_deserialize_int() { | ||
assert!(deserialize_from(-10.0).is_err()); | ||
assert!(deserialize_from(-0.0).is_err()); | ||
assert!(deserialize_from(0.5).is_err()); | ||
assert!(deserialize_from(1.0).is_err()); | ||
assert!(deserialize_from(9007199254740991.0).is_err()); | ||
assert!(deserialize_from(9007199254740991.49).is_err()); | ||
assert!(deserialize_from(9007199254740992.0).is_err()); | ||
} | ||
|
||
#[test] | ||
#[cfg_attr(not(feature = "lax_deserialize"), ignore)] | ||
fn lax_deserialize_int() { | ||
assert_eq!(deserialize_from(-10.0).unwrap(), int!(-10)); | ||
assert_eq!(deserialize_from(-0.0).unwrap(), int!(0)); | ||
assert_eq!(deserialize_from(0.5).unwrap(), int!(0)); | ||
assert_eq!(deserialize_from(1.0).unwrap(), int!(1)); | ||
assert_eq!(deserialize_from(9007199254740991.0).unwrap(), Int::MAX); | ||
assert_eq!(deserialize_from(9007199254740991.49).unwrap(), Int::MAX); | ||
assert!(deserialize_from(9007199254740992.0).is_err()); | ||
|
||
assert!(deserialize_from(f64::NAN).is_err()); | ||
assert!(deserialize_from(f64::INFINITY).is_err()); | ||
assert!(deserialize_from(f64::NEG_INFINITY).is_err()); | ||
} | ||
|
||
fn deserialize_from<'de, Value: IntoDeserializer<'de>>( | ||
value: Value, | ||
) -> Result<Int, serde::de::value::Error> { | ||
Int::deserialize(value.into_deserializer()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#![cfg(feature = "serde")] | ||
|
||
use js_int::{uint, UInt}; | ||
use serde::{de::IntoDeserializer, Deserialize}; | ||
use serde_test::{assert_ser_tokens, Token}; | ||
|
||
#[test] | ||
fn serialize_uint() { | ||
assert_serialize(100); | ||
assert_serialize(0); | ||
} | ||
|
||
fn assert_serialize(number: u32) { | ||
assert_ser_tokens( | ||
&UInt::from(number), | ||
&[Token::NewtypeStruct { name: "UInt" }, Token::U64(number as _)], | ||
) | ||
} | ||
|
||
#[test] | ||
fn deserialize_uint() { | ||
assert_eq!(deserialize_uint_from(100).unwrap(), uint!(100)); | ||
assert_eq!(deserialize_uint_from(0).unwrap(), uint!(0)); | ||
assert_eq!(deserialize_uint_from(9007199254740991i64).unwrap(), UInt::MAX); | ||
assert!(deserialize_uint_from(9007199254740992i64).is_err()); | ||
} | ||
|
||
#[test] | ||
#[cfg_attr(feature = "lax_deserialize", ignore)] | ||
fn strict_deserialize_uint() { | ||
assert!(deserialize_uint_from(0.5).is_err()); | ||
assert!(deserialize_uint_from(1.0).is_err()); | ||
assert!(deserialize_uint_from(9007199254740991.0).is_err()); | ||
assert!(deserialize_uint_from(9007199254740991.49).is_err()); | ||
assert!(deserialize_uint_from(9007199254740992.0).is_err()); | ||
} | ||
|
||
#[test] | ||
#[cfg_attr(not(feature = "lax_deserialize"), ignore)] | ||
fn lax_deserialize_uint() { | ||
assert_eq!(deserialize_uint_from(0.5).unwrap(), uint!(0)); | ||
assert_eq!(deserialize_uint_from(1.0).unwrap(), uint!(1)); | ||
assert_eq!(deserialize_uint_from(9007199254740991.0).unwrap(), UInt::MAX); | ||
assert_eq!(deserialize_uint_from(9007199254740991.49).unwrap(), UInt::MAX); | ||
assert!(deserialize_uint_from(9007199254740992.0).is_err()); | ||
|
||
assert!(deserialize_uint_from(f64::NAN).is_err()); | ||
assert!(deserialize_uint_from(f64::INFINITY).is_err()); | ||
assert!(deserialize_uint_from(f64::NEG_INFINITY).is_err()); | ||
} | ||
|
||
fn deserialize_uint_from<'de, Value: IntoDeserializer<'de>>( | ||
value: Value, | ||
) -> Result<UInt, serde::de::value::Error> { | ||
UInt::deserialize(value.into_deserializer()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was tempted to use
#[track_caller]
here but then I remembered that the MSRV is 1.35.Using a macro would also be possible but I don't think it's really worth it.