Skip to content

Commit

Permalink
Replace ext_tests workaround with proper integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FSMaxB committed Nov 11, 2021
1 parent f990d3f commit d578716
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 130 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,3 @@ features = ["derive"]
lax_deserialize = ["serde"]
default = ["std"]
std = []

[workspace]
members = ["ext_tests"]
16 changes: 0 additions & 16 deletions ext_tests/Cargo.toml

This file was deleted.

6 changes: 0 additions & 6 deletions ext_tests/README.md

This file was deleted.

105 changes: 0 additions & 105 deletions ext_tests/src/lib.rs

This file was deleted.

59 changes: 59 additions & 0 deletions tests/int.rs
Original file line number Diff line number Diff line change
@@ -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, serde::de::value::Error> {
Int::deserialize(value.into_deserializer())
}
10 changes: 10 additions & 0 deletions ext_tests/src/test_serializer.rs → tests/test_serializer.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -178,4 +181,11 @@ impl Serializer for TestSerializer {
) -> Result<Self::SerializeStructVariant, Self::Error> {
unimplemented!()
}

fn collect_str<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error>
where
T: Display,
{
unimplemented!()
}
}
51 changes: 51 additions & 0 deletions tests/uint.rs
Original file line number Diff line number Diff line change
@@ -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, serde::de::value::Error> {
UInt::deserialize(value.into_deserializer())
}

0 comments on commit d578716

Please sign in to comment.