diff --git a/Cargo.toml b/Cargo.toml index aa2feff..60c5487 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,3 @@ features = ["derive"] lax_deserialize = ["serde"] default = ["std"] std = [] - -[workspace] -members = ["ext_tests"] diff --git a/ext_tests/Cargo.toml b/ext_tests/Cargo.toml deleted file mode 100644 index e7a9302..0000000 --- a/ext_tests/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "js_int_ext_tests" -version = "0.1.0" -authors = ["Jonas Platte "] -edition = "2018" -publish = false - -[dependencies] -serde = "1.0" - -[dependencies.js_int] -path = "../" -features = ["serde"] - -[features] -lax_deserialize = ["js_int/lax_deserialize"] diff --git a/ext_tests/README.md b/ext_tests/README.md deleted file mode 100644 index 780cdfb..0000000 --- a/ext_tests/README.md +++ /dev/null @@ -1,6 +0,0 @@ -Due to [cargo issue #1796](https://github.com/rust-lang/cargo/issues/1796), we can not depend on -`serde_json` as a `dev-dependency` in `js_int` without breaking `no_std` compatibility, because -`serde_json` isn't `no_std`-compatible (because of that same cargo bug – see -[serde_json PR #516](https://github.com/serde-rs/json/pull/516)). - -Thus, the `serde` tests live in this separate crate. diff --git a/ext_tests/src/lib.rs b/ext_tests/src/lib.rs deleted file mode 100644 index ce1b113..0000000 --- a/ext_tests/src/lib.rs +++ /dev/null @@ -1,105 +0,0 @@ -mod test_serializer; - -#[cfg(test)] -mod tests { - use js_int::{int, uint, Int, UInt}; - use serde::{de::IntoDeserializer, Deserialize, Serialize}; - - use crate::test_serializer::{Serialized, TestSerializer}; - - #[test] - fn serialize_int() { - assert_eq!(int!(100).serialize(TestSerializer).unwrap(), Serialized::Signed(100)); - assert_eq!(int!(0).serialize(TestSerializer).unwrap(), Serialized::Signed(0)); - assert_eq!(int!(-100).serialize(TestSerializer).unwrap(), Serialized::Signed(-100)); - } - - #[test] - fn deserialize_int() { - assert_eq!(deserialize_int_from(100).unwrap(), int!(100)); - assert_eq!(deserialize_int_from(0).unwrap(), int!(0)); - assert_eq!(deserialize_int_from(-100).unwrap(), int!(-100)); - assert_eq!(deserialize_int_from(-9007199254740991i64).unwrap(), Int::MIN); - assert_eq!(deserialize_int_from(9007199254740991i64).unwrap(), Int::MAX); - assert!(deserialize_int_from(9007199254740992i64).is_err()); - assert!(deserialize_int_from(-9007199254740992i64).is_err()); - } - - #[test] - fn serialize_uint() { - assert_eq!(uint!(100).serialize(TestSerializer).unwrap(), Serialized::Unsigned(100)); - assert_eq!(uint!(0).serialize(TestSerializer).unwrap(), Serialized::Unsigned(0)); - } - - #[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_int() { - assert!(deserialize_int_from(-10.0).is_err()); - assert!(deserialize_int_from(-0.0).is_err()); - assert!(deserialize_int_from(0.5).is_err()); - assert!(deserialize_int_from(1.0).is_err()); - assert!(deserialize_int_from(9007199254740991.0).is_err()); - assert!(deserialize_int_from(9007199254740991.49).is_err()); - assert!(deserialize_int_from(9007199254740992.0).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_int() { - assert_eq!(deserialize_int_from(-10.0).unwrap(), int!(-10)); - assert_eq!(deserialize_int_from(-0.0).unwrap(), int!(0)); - assert_eq!(deserialize_int_from(0.5).unwrap(), int!(0)); - assert_eq!(deserialize_int_from(1.0).unwrap(), int!(1)); - assert_eq!(deserialize_int_from(9007199254740991.0).unwrap(), Int::MAX); - assert_eq!(deserialize_int_from(9007199254740991.49).unwrap(), Int::MAX); - assert!(deserialize_int_from(9007199254740992.0).is_err()); - - assert!(deserialize_int_from(f64::NAN).is_err()); - assert!(deserialize_int_from(f64::INFINITY).is_err()); - assert!(deserialize_int_from(f64::NEG_INFINITY).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_int_from<'de, Value: IntoDeserializer<'de>>( - value: Value, - ) -> Result { - Int::deserialize(value.into_deserializer()) - } - - fn deserialize_uint_from<'de, Value: IntoDeserializer<'de>>( - value: Value, - ) -> Result { - UInt::deserialize(value.into_deserializer()) - } -} diff --git a/tests/int.rs b/tests/int.rs new file mode 100644 index 0000000..cc6f3cd --- /dev/null +++ b/tests/int.rs @@ -0,0 +1,59 @@ +#![cfg(feature = "serde")] +mod test_serializer; + +use js_int::{int, Int}; +use serde::{de::IntoDeserializer, Deserialize, Serialize}; + +use crate::test_serializer::{Serialized, TestSerializer}; + +#[test] +fn serialize_int() { + assert_eq!(int!(100).serialize(TestSerializer).unwrap(), Serialized::Signed(100)); + assert_eq!(int!(0).serialize(TestSerializer).unwrap(), Serialized::Signed(0)); + assert_eq!(int!(-100).serialize(TestSerializer).unwrap(), Serialized::Signed(-100)); +} + +#[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::deserialize(value.into_deserializer()) +} diff --git a/ext_tests/src/test_serializer.rs b/tests/test_serializer.rs similarity index 93% rename from ext_tests/src/test_serializer.rs rename to tests/test_serializer.rs index 46f2653..ab00f5d 100644 --- a/ext_tests/src/test_serializer.rs +++ b/tests/test_serializer.rs @@ -1,5 +1,8 @@ +#![cfg(feature = "serde")] use serde::{ser::Impossible, Serialize, Serializer}; +use std::fmt::Display; +/// serde Serializer for testing that the `Serialize` implementation of `Int` and `UInt` work correctly. pub struct TestSerializer; #[derive(Debug, PartialEq)] @@ -178,4 +181,11 @@ impl Serializer for TestSerializer { ) -> Result { unimplemented!() } + + fn collect_str(self, _: &T) -> Result + where + T: Display, + { + unimplemented!() + } } diff --git a/tests/uint.rs b/tests/uint.rs new file mode 100644 index 0000000..5e4f677 --- /dev/null +++ b/tests/uint.rs @@ -0,0 +1,51 @@ +#![cfg(feature = "serde")] +mod test_serializer; + +use js_int::{uint, UInt}; +use serde::{de::IntoDeserializer, Deserialize, Serialize}; + +use crate::test_serializer::{Serialized, TestSerializer}; + +#[test] +fn serialize_uint() { + assert_eq!(uint!(100).serialize(TestSerializer).unwrap(), Serialized::Unsigned(100)); + assert_eq!(uint!(0).serialize(TestSerializer).unwrap(), Serialized::Unsigned(0)); +} + +#[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::deserialize(value.into_deserializer()) +}