Skip to content

Commit 6a40ad1

Browse files
committed
more functora-tagged tests
1 parent afc7743 commit 6a40ad1

File tree

6 files changed

+777
-143
lines changed

6 files changed

+777
-143
lines changed

rust/functora-tagged/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
22
tarpaulin-report.html
3+
lcov.info

rust/functora-tagged/tests/infallible.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,37 @@ fn test_infallible_result_construction() {
3131
assert!(res.is_ok());
3232
assert_eq!(res.infallible(), "hello");
3333
}
34+
35+
#[test]
36+
fn test_infallible_on_ok_result() {
37+
let ok_result: Result<String, Infallible> =
38+
Ok("test".to_string());
39+
assert_eq!(ok_result.infallible(), "test");
40+
}
41+
42+
#[test]
43+
fn test_infallible_direct_call_on_ok() {
44+
let result: Result<i64, Infallible> = Ok(12345);
45+
46+
assert_eq!(result.infallible(), 12345);
47+
}
48+
49+
#[test]
50+
fn test_infallible_trait_implementation() {
51+
let value: i32 = 50;
52+
let result: Result<i32, Infallible> = Ok(value);
53+
54+
assert_eq!(result.infallible(), value);
55+
}
56+
57+
#[test]
58+
fn test_infallible_trait_definition_usage() {
59+
fn requires_infallible_into<T: InfallibleInto<i32>>(
60+
val: T,
61+
) -> i32 {
62+
val.infallible()
63+
}
64+
65+
let ok_result: Result<i32, Infallible> = Ok(200);
66+
assert_eq!(requires_infallible_into(ok_result), 200);
67+
}

rust/functora-tagged/tests/integration.rs

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use derive_more::Display;
22
use functora_tagged::{
33
InfallibleInto, ParseError, Refine, Tagged,
44
};
5-
#[cfg(feature = "serde")]
6-
use serde::{Deserialize, Serialize};
75
use std::collections::hash_map::DefaultHasher;
86
use std::convert::Infallible;
97
use std::error::Error;
@@ -225,65 +223,74 @@ fn test_tagged_deref() {
225223
}
226224

227225
#[cfg(feature = "serde")]
228-
#[test]
229-
fn test_serde_user_id_roundtrip() {
230-
#[derive(Serialize, Deserialize, PartialEq, Debug)]
231-
struct Wrapper {
232-
user_id: UserId,
226+
mod serde_tests {
227+
use super::*;
228+
use serde::{Deserialize, Serialize};
229+
230+
#[test]
231+
fn test_serde_user_id_roundtrip() {
232+
#[derive(
233+
Serialize, Deserialize, PartialEq, Debug,
234+
)]
235+
struct Wrapper {
236+
user_id: UserId,
237+
}
238+
let original = Wrapper {
239+
user_id: "user_456".parse().unwrap(),
240+
};
241+
let toml = toml::to_string(&original).unwrap();
242+
let deserialized: Wrapper =
243+
toml::from_str(&toml).unwrap();
244+
assert_eq!(original, deserialized);
245+
assert_eq!(
246+
deserialized.user_id.rep().rep(),
247+
"user_456"
248+
);
233249
}
234-
let original = Wrapper {
235-
user_id: "user_456".parse().unwrap(),
236-
};
237-
let toml = toml::to_string(&original).unwrap();
238-
let deserialized: Wrapper =
239-
toml::from_str(&toml).unwrap();
240-
assert_eq!(original, deserialized);
241-
assert_eq!(
242-
deserialized.user_id.rep().rep(),
243-
"user_456"
244-
);
245-
}
246250

247-
#[cfg(feature = "serde")]
248-
#[test]
249-
fn test_serde_user_id_invalid_refine() {
250-
#[derive(Deserialize, Debug)]
251-
struct Wrapper {
252-
user_id: UserId,
251+
#[test]
252+
fn test_serde_user_id_invalid_refine() {
253+
#[derive(Deserialize, Debug)]
254+
struct Wrapper {
255+
user_id: UserId,
256+
}
257+
let toml = r#"user_id = "bad""#;
258+
let err =
259+
toml::from_str::<Wrapper>(toml).unwrap_err();
260+
assert!(
261+
err.to_string().contains("UserIdError"),
262+
"Unexpected failure: {err}"
263+
);
264+
let toml = r#"user_id = "user_123""#;
265+
let wrapper: Wrapper =
266+
toml::from_str(toml).unwrap();
267+
assert_eq!(wrapper.user_id.rep().rep(), "user_123");
253268
}
254-
let toml = r#"user_id = "bad""#;
255-
let err = toml::from_str::<Wrapper>(toml).unwrap_err();
256-
assert!(
257-
err.to_string().contains("UserIdError"),
258-
"Unexpected failure: {err}"
259-
);
260-
let toml = r#"user_id = "user_123""#;
261-
let wrapper: Wrapper = toml::from_str(toml).unwrap();
262-
assert_eq!(wrapper.user_id.rep().rep(), "user_123");
263-
}
264269

265-
#[cfg(feature = "serde")]
266-
#[test]
267-
fn test_serde_email_roundtrip() {
268-
#[derive(Serialize, Deserialize, PartialEq, Debug)]
269-
struct Wrapper {
270-
email: Email,
270+
#[test]
271+
fn test_serde_email_roundtrip() {
272+
#[derive(
273+
Serialize, Deserialize, PartialEq, Debug,
274+
)]
275+
struct Wrapper {
276+
email: Email,
277+
}
278+
let original = Wrapper {
279+
email: "[email protected]".parse().unwrap(),
280+
};
281+
let toml = toml::to_string(&original).unwrap();
282+
let deserialized: Wrapper =
283+
toml::from_str(&toml).unwrap();
284+
assert_eq!(original, deserialized);
285+
assert_eq!(
286+
deserialized.email.rep().rep(),
287+
288+
);
271289
}
272-
let original = Wrapper {
273-
email: "[email protected]".parse().unwrap(),
274-
};
275-
let toml = toml::to_string(&original).unwrap();
276-
let deserialized: Wrapper =
277-
toml::from_str(&toml).unwrap();
278-
assert_eq!(original, deserialized);
279-
assert_eq!(
280-
deserialized.email.rep().rep(),
281-
282-
);
283290
}
284291

285292
#[cfg(feature = "diesel")]
286-
mod diesel_tests {
293+
mod diesel_integration_tests {
287294
use super::*;
288295
use diesel::insert_into;
289296
use diesel::prelude::*;

0 commit comments

Comments
 (0)