Debian Codegraph @

tests/integration/std_types.rs

raw symbols SCIP
1 use crate::prelude::*;
2 use std::ffi::{CStr, CString, OsStr, OsString};
3 use std::marker::PhantomData;
4 use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
5 use std::num::{NonZeroI64, NonZeroU64};
6 use std::ops::{Bound, Range, RangeInclusive};
7 use std::path::{Path, PathBuf};
8 use std::time::{Duration, SystemTime};
9
10 #[test]
11 fn option() {
12 test!(Option<bool>)
13 .assert_allows_ser_roundtrip([Some(true), None])
14 .assert_matches_de_roundtrip(arbitrary_values());
15 }
16
17 #[test]
18 fn result() {
19 test!(Result<bool, String>)
20 .assert_allows_ser_roundtrip([Ok(true), Err("oh no!".to_owned())])
21 .assert_matches_de_roundtrip(arbitrary_values());
22 }
23
24 #[test]
25 fn nonzero() {
26 test!(NonZeroI64)
27 .assert_allows_ser_roundtrip([NonZeroI64::MIN, NonZeroI64::MAX])
28 .assert_rejects_de([Value::from(0)])
29 .assert_matches_de_roundtrip(arbitrary_values_except(
30 |v| v.as_u64().is_some_and(|u| u > i64::MAX as u64),
31 "FIXME schema allows out-of-range positive integers",
32 ));
33
34 test!(NonZeroU64)
35 .assert_allows_ser_roundtrip([NonZeroU64::MIN, NonZeroU64::MAX])
36 .assert_rejects_de([Value::from(0)])
37 .assert_matches_de_roundtrip(arbitrary_values());
38 }
39
40 const IPV4_SAMPLES: [Ipv4Addr; 3] = [
41 // Commented-out until https://github.com/Stranger6667/jsonschema-rs/issues/512 is fixed
42 // Ipv4Addr::UNSPECIFIED,
43 Ipv4Addr::LOCALHOST,
44 Ipv4Addr::BROADCAST,
45 Ipv4Addr::new(1, 2, 3, 4),
46 ];
47 const IPV6_SAMPLES: [Ipv6Addr; 4] = [
48 Ipv6Addr::UNSPECIFIED,
49 Ipv6Addr::LOCALHOST,
50 Ipv4Addr::LOCALHOST.to_ipv6_mapped(),
51 Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8),
52 ];
53
54 #[test]
55 fn ip_addr() {
56 test!(Ipv4Addr)
57 .assert_allows_ser_roundtrip(IPV4_SAMPLES)
58 .assert_matches_de_roundtrip(arbitrary_values());
59
60 test!(Ipv6Addr)
61 .assert_allows_ser_roundtrip(IPV6_SAMPLES)
62 .assert_matches_de_roundtrip(arbitrary_values());
63
64 test!(IpAddr)
65 .assert_allows_ser_roundtrip(IPV4_SAMPLES.map(Into::into))
66 .assert_allows_ser_roundtrip(IPV6_SAMPLES.map(Into::into))
67 .assert_matches_de_roundtrip(arbitrary_values_except(
68 Value::is_string,
69 "Custom format 'ip', so arbitrary strings technically allowed by schema",
70 ));
71 }
72
73 #[test]
74 fn socket_addr() {
75 let port = 12345;
76
77 test!(SocketAddrV4)
78 .assert_allows_ser_roundtrip(IPV4_SAMPLES.map(|ip| SocketAddrV4::new(ip, port)))
79 .assert_matches_de_roundtrip(arbitrary_values_except(
80 Value::is_string,
81 "Arbitrary strings allowed by schema",
82 ));
83
84 test!(SocketAddrV6)
85 .assert_allows_ser_roundtrip(IPV6_SAMPLES.map(|ip| SocketAddrV6::new(ip, port, 0, 0)))
86 .assert_matches_de_roundtrip(arbitrary_values_except(
87 Value::is_string,
88 "Arbitrary strings allowed by schema",
89 ));
90
91 test!(SocketAddr)
92 .assert_allows_ser_roundtrip(IPV4_SAMPLES.map(|ip| (ip, port).into()))
93 .assert_allows_ser_roundtrip(IPV6_SAMPLES.map(|ip| (ip, port).into()))
94 .assert_matches_de_roundtrip(arbitrary_values_except(
95 Value::is_string,
96 "Arbitrary strings allowed by schema",
97 ));
98 }
99
100 #[test]
101 fn time() {
102 test!(SystemTime)
103 .assert_allows_ser_roundtrip([SystemTime::UNIX_EPOCH, SystemTime::now()])
104 .assert_matches_de_roundtrip(arbitrary_values());
105
106 test!(Duration)
107 .assert_allows_ser_roundtrip([Duration::from_secs(0), Duration::from_secs_f64(123.456)])
108 .assert_matches_de_roundtrip(arbitrary_values());
109 }
110
111 #[test]
112 fn c_strings() {
113 let strings = [
114 CString::default(),
115 CString::new("test").unwrap(),
116 CString::new([255]).unwrap(),
117 ];
118
119 test!(CString)
120 .assert_allows_ser_roundtrip(strings.clone())
121 .assert_matches_de_roundtrip(arbitrary_values_except(
122 |v| v.as_str().is_some_and(|s| s.contains('\0')),
123 "CString cannot contain null bytes, but schema does not enforce this",
124 ));
125
126 test!(Box<CStr>)
127 .assert_identical::<CString>()
128 .assert_allows_ser_roundtrip(strings.into_iter().map(Into::into))
129 .assert_matches_de_roundtrip(arbitrary_values_except(
130 |v| v.as_str().is_some_and(|s| s.contains('\0')),
131 "CString cannot contain null bytes, but schema does not enforce this",
132 ));
133 }
134
135 #[test]
136 fn os_strings() {
137 let strings = [OsString::new(), OsString::from("test")];
138
139 test!(OsString)
140 .assert_allows_ser_roundtrip(strings.clone())
141 .assert_matches_de_roundtrip(arbitrary_values());
142
143 test!(Box<OsStr>)
144 .assert_identical::<OsString>()
145 .assert_allows_ser_roundtrip(strings.into_iter().map(Into::into))
146 .assert_matches_de_roundtrip(arbitrary_values());
147 }
148
149 #[test]
150 fn paths() {
151 let strings = [PathBuf::new(), PathBuf::from("test")];
152
153 test!(PathBuf)
154 .assert_identical::<String>()
155 .assert_allows_ser_roundtrip(strings.clone())
156 .assert_matches_de_roundtrip(arbitrary_values());
157
158 test!(Box<Path>)
159 .assert_identical::<String>()
160 .assert_identical::<PathBuf>()
161 .assert_allows_ser_roundtrip(strings.into_iter().map(Into::into))
162 .assert_matches_de_roundtrip(arbitrary_values());
163 }
164
165 #[test]
166 fn bound() {
167 test!(Bound<i32>).assert_allows_ser_roundtrip([
168 Bound::Included(123),
169 Bound::Excluded(456),
170 Bound::Unbounded,
171 ]);
172 }
173
174 #[test]
175 fn ranges() {
176 test!(Range<i32>)
177 .assert_allows_ser_roundtrip([0..0, 123..456])
178 .assert_matches_de_roundtrip(arbitrary_values());
179
180 test!(RangeInclusive<i32>)
181 .assert_allows_ser_roundtrip([0..=0, 123..=456])
182 .assert_matches_de_roundtrip(arbitrary_values());
183 }
184
185 #[test]
186 fn phantom_data() {
187 struct DoesNotImplementJsonSchema;
188
189 test!(PhantomData<DoesNotImplementJsonSchema>)
190 .assert_allows_ser_roundtrip_default()
191 .assert_matches_de_roundtrip(arbitrary_values());
192 }