Skip to content

Commit

Permalink
Add integration tests for more std types
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Sep 14, 2024
1 parent c442a7a commit 94d3459
Showing 1 changed file with 85 additions and 2 deletions.
87 changes: 85 additions & 2 deletions schemars/tests/integration/std_types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::prelude::*;
use std::ffi::{CStr, CString, OsStr, OsString};
use std::marker::PhantomData;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::num::{NonZeroI64, NonZeroU64};
use std::ops::{Bound, Range, RangeInclusive};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};

#[test]
Expand All @@ -23,11 +25,76 @@ fn result() {
fn nonzero() {
test!(NonZeroI64)
.assert_allows_ser_roundtrip([NonZeroI64::MIN, NonZeroI64::MAX])
.assert_rejects_de([Value::from(0)]);
.assert_rejects_de([Value::from(0)])
.assert_matches_de_roundtrip(arbitrary_values_except(
|v| v.as_u64().is_some_and(|u| u > i64::MAX as u64),
"FIXME schema allows out-of-range positive integers",
));

test!(NonZeroU64)
.assert_allows_ser_roundtrip([NonZeroU64::MIN, NonZeroU64::MAX])
.assert_rejects_de([Value::from(0)]);
.assert_rejects_de([Value::from(0)])
.assert_matches_de_roundtrip(arbitrary_values());
}

const IPV4_SAMPLES: [Ipv4Addr; 3] = [
// Commented-out until https://github.com/Stranger6667/jsonschema-rs/issues/512 is fixed
// Ipv4Addr::UNSPECIFIED,
Ipv4Addr::LOCALHOST,
Ipv4Addr::BROADCAST,
Ipv4Addr::new(1, 2, 3, 4),
];
const IPV6_SAMPLES: [Ipv6Addr; 4] = [
Ipv6Addr::UNSPECIFIED,
Ipv6Addr::LOCALHOST,
Ipv4Addr::LOCALHOST.to_ipv6_mapped(),
Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8),
];

#[test]
fn ip_addr() {
test!(Ipv4Addr)
.assert_allows_ser_roundtrip(IPV4_SAMPLES)
.assert_matches_de_roundtrip(arbitrary_values());

test!(Ipv6Addr)
.assert_allows_ser_roundtrip(IPV6_SAMPLES)
.assert_matches_de_roundtrip(arbitrary_values());

test!(IpAddr)
.assert_allows_ser_roundtrip(IPV4_SAMPLES.map(Into::into))
.assert_allows_ser_roundtrip(IPV6_SAMPLES.map(Into::into))
.assert_matches_de_roundtrip(arbitrary_values_except(
Value::is_string,
"Custom format 'ip', so arbitrary strings technically allowed by schema",
));
}

#[test]
fn socket_addr() {
let port = 12345;

test!(SocketAddrV4)
.assert_allows_ser_roundtrip(IPV4_SAMPLES.map(|ip| SocketAddrV4::new(ip, port)))
.assert_matches_de_roundtrip(arbitrary_values_except(
Value::is_string,
"Arbitrary strings allowed by schema",
));

test!(SocketAddrV6)
.assert_allows_ser_roundtrip(IPV6_SAMPLES.map(|ip| SocketAddrV6::new(ip, port, 0, 0)))
.assert_matches_de_roundtrip(arbitrary_values_except(
Value::is_string,
"Arbitrary strings allowed by schema",
));

test!(SocketAddr)
.assert_allows_ser_roundtrip(IPV4_SAMPLES.map(|ip| (ip, port).into()))
.assert_allows_ser_roundtrip(IPV6_SAMPLES.map(|ip| (ip, port).into()))
.assert_matches_de_roundtrip(arbitrary_values_except(
Value::is_string,
"Arbitrary strings allowed by schema",
));
}

#[test]
Expand Down Expand Up @@ -79,6 +146,22 @@ fn os_strings() {
.assert_matches_de_roundtrip(arbitrary_values());
}

#[test]
fn paths() {
let strings = [PathBuf::new(), PathBuf::from("test")];

test!(PathBuf)
.assert_identical::<String>()
.assert_allows_ser_roundtrip(strings.clone())
.assert_matches_de_roundtrip(arbitrary_values());

test!(Box<Path>)
.assert_identical::<String>()
.assert_identical::<PathBuf>()
.assert_allows_ser_roundtrip(strings.into_iter().map(Into::into))
.assert_matches_de_roundtrip(arbitrary_values());
}

#[test]
fn bound() {
test!(Bound<i32>).assert_allows_ser_roundtrip([
Expand Down

0 comments on commit 94d3459

Please sign in to comment.