use constr::constr;
constr! {
type Hyphen = "-";
type K1 = "greeting";
type K2 = "entity";
}
mod basic {
mod before {
#[test]
fn empty() {
let basic = crate::act::<String, crate::tests::Hyphen, false>();
assert_eq!(basic.finish(), Err(crate::IncompleteError::Empty));
}
#[test]
fn first() {
let mut basic = crate::act::<String, crate::tests::Hyphen, false>();
basic = basic.first("hello").unwrap();
assert_eq!(
basic.finish().unwrap_err(),
crate::IncompleteError::First("hello")
);
}
#[test]
fn second() {
let mut basic = crate::act::<String, crate::tests::Hyphen, false>();
basic = basic.second("world").unwrap();
assert_eq!(
basic.finish().unwrap_err(),
crate::IncompleteError::Second("world")
);
}
#[test]
fn forward() {
let mut basic = crate::act::<String, crate::tests::Hyphen, false>();
basic = basic.first("hello").unwrap();
basic = basic.second("world").unwrap();
assert_eq!(
basic.finish().unwrap(),
<crate::Spair<String, _, _>>::new("hello", "world").unwrap()
);
}
#[test]
fn backward() {
let mut basic = crate::act::<String, crate::tests::Hyphen, false>();
basic = basic.second("world").unwrap();
basic = basic.first("hello").unwrap();
assert_eq!(
basic.finish().unwrap(),
<crate::Spair<String, _, _>>::new("hello", "world").unwrap()
);
}
#[test]
fn ff() {
let mut basic = crate::act::<String, crate::tests::Hyphen, false>();
basic = basic.first("hello").unwrap();
assert_eq!(
basic.first("hallo").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("hello", "hallo").unwrap(),
which: crate::Duplicated::First
})
);
}
#[test]
fn fsf() {
let mut basic = crate::act::<String, crate::tests::Hyphen, false>();
basic = basic.first("hello").unwrap();
basic = basic.second("world").unwrap();
assert_eq!(
basic.first("hallo").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("hello", "hallo").unwrap(),
which: crate::Duplicated::First
})
);
}
#[test]
fn sff() {
let mut basic = crate::act::<String, crate::tests::Hyphen, false>();
basic = basic.second("world").unwrap();
basic = basic.first("hello").unwrap();
assert_eq!(
basic.first("hallo").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("hello", "hallo").unwrap(),
which: crate::Duplicated::First
})
);
}
#[test]
fn ss() {
let mut basic = crate::act::<String, crate::tests::Hyphen, false>();
basic = basic.second("world").unwrap();
assert_eq!(
basic.second("whirl").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("world", "whirl").unwrap(),
which: crate::Duplicated::Second
})
);
}
#[test]
fn sfs() {
let mut basic = crate::act::<String, crate::tests::Hyphen, false>();
basic = basic.second("world").unwrap();
basic = basic.first("hello").unwrap();
assert_eq!(
basic.second("whirl").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("world", "whirl").unwrap(),
which: crate::Duplicated::Second
})
);
}
#[test]
fn fss() {
let mut basic = crate::act::<String, crate::tests::Hyphen, false>();
basic = basic.first("hello").unwrap();
basic = basic.second("world").unwrap();
assert_eq!(
basic.second("whirl").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("world", "whirl").unwrap(),
which: crate::Duplicated::Second
})
);
}
}
mod after {
#[test]
fn empty() {
let basic = crate::act::<String, crate::tests::Hyphen, true>();
assert_eq!(basic.finish(), Err(crate::IncompleteError::Empty));
}
#[test]
fn first() {
let mut basic = crate::act::<String, crate::tests::Hyphen, true>();
basic = basic.first("hello").unwrap();
assert_eq!(
basic.finish().unwrap_err(),
crate::IncompleteError::First("hello")
);
}
#[test]
fn second() {
let mut basic = crate::act::<String, crate::tests::Hyphen, true>();
basic = basic.second("world").unwrap();
assert_eq!(
basic.finish().unwrap_err(),
crate::IncompleteError::Second("world")
);
}
#[test]
fn forward() {
let mut basic = crate::act::<String, crate::tests::Hyphen, true>();
basic = basic.first("hello").unwrap();
basic = basic.second("world").unwrap();
assert_eq!(
basic.finish().unwrap(),
<crate::Spair<String, _, _>>::new("hello", "world").unwrap()
);
}
#[test]
fn backward() {
let mut basic = crate::act::<String, crate::tests::Hyphen, true>();
basic = basic.second("world").unwrap();
basic = basic.first("hello").unwrap();
assert_eq!(
basic.finish().unwrap(),
<crate::Spair<String, _, _>>::new("hello", "world").unwrap()
);
}
#[test]
fn ff() {
let mut basic = crate::act::<String, crate::tests::Hyphen, true>();
basic = basic.first("hello").unwrap();
assert_eq!(
basic.first("hallo").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("hello", "hallo").unwrap(),
which: crate::Duplicated::First
})
);
}
#[test]
fn fsf() {
let mut basic = crate::act::<String, crate::tests::Hyphen, true>();
basic = basic.first("hello").unwrap();
basic = basic.second("world").unwrap();
assert_eq!(
basic.first("hallo").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("hello", "hallo").unwrap(),
which: crate::Duplicated::First
})
);
}
#[test]
fn sff() {
let mut basic = crate::act::<String, crate::tests::Hyphen, true>();
basic = basic.second("world").unwrap();
basic = basic.first("hello").unwrap();
assert_eq!(
basic.first("hallo").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("hello", "hallo").unwrap(),
which: crate::Duplicated::First
})
);
}
#[test]
fn ss() {
let mut basic = crate::act::<String, crate::tests::Hyphen, true>();
basic = basic.second("world").unwrap();
assert_eq!(
basic.second("whirl").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("world", "whirl").unwrap(),
which: crate::Duplicated::Second
})
);
}
#[test]
fn sfs() {
let mut basic = crate::act::<String, crate::tests::Hyphen, true>();
basic = basic.second("world").unwrap();
basic = basic.first("hello").unwrap();
assert_eq!(
basic.second("whirl").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("world", "whirl").unwrap(),
which: crate::Duplicated::Second
})
);
}
#[test]
fn fss() {
let mut basic = crate::act::<String, crate::tests::Hyphen, true>();
basic = basic.first("hello").unwrap();
basic = basic.second("world").unwrap();
assert_eq!(
basic.second("whirl").unwrap_err(),
crate::BuildError::Duplicate(crate::DuplicateError {
pair: <crate::Spair<String, _, _>>::new("world", "whirl").unwrap(),
which: crate::Duplicated::Second
})
);
}
}
}
mod incomplete {
#[test]
fn empty() {
assert_eq!(<crate::IncompleteError<&str>>::Empty.pair(), (None, None));
assert_eq!(
<crate::IncompleteError<&str>>::Empty.to_string(),
r"unfinished pair: (_, _)",
);
assert_eq!(
format!("{:?}", <crate::IncompleteError<&str>>::Empty),
r"IncompleteError(_, _)"
);
assert_eq!(
<crate::IncompleteError<&str>>::Empty
.custom::<crate::tests::K1, crate::tests::K2>()
.to_string(),
r"missing greeting and entity",
);
assert_eq!(
format!(
"{:?}",
<crate::IncompleteError<&str>>::Empty
.custom::<crate::tests::K1, crate::tests::K2>()
),
r"IncompleteError { greeting: _, entity: _ }"
);
}
#[test]
fn first() {
assert_eq!(
crate::IncompleteError::First("hello").pair(),
(Some("hello"), None)
);
assert_eq!(
crate::IncompleteError::First("hello").to_string(),
r#"unfinished pair: ("hello", _)"#
);
assert_eq!(
format!("{:?}", crate::IncompleteError::First("hello")),
r#"IncompleteError("hello", _)"#
);
assert_eq!(
crate::IncompleteError::First("hello")
.custom::<crate::tests::K1, crate::tests::K2>()
.to_string(),
r#"got greeting "hello", but missing entity"#
);
assert_eq!(
format!(
"{:?}",
crate::IncompleteError::First("hello")
.custom::<crate::tests::K1, crate::tests::K2>()
),
r#"IncompleteError { greeting: "hello", entity: _ }"#
);
}
#[test]
fn second() {
assert_eq!(
crate::IncompleteError::Second("world").pair(),
(None, Some("world"))
);
assert_eq!(
crate::IncompleteError::Second("world").to_string(),
r#"unfinished pair: (_, "world")"#
);
assert_eq!(
format!("{:?}", crate::IncompleteError::Second("world")),
r#"IncompleteError(_, "world")"#
);
assert_eq!(
crate::IncompleteError::Second("world")
.custom::<crate::tests::K1, crate::tests::K2>()
.to_string(),
r#"got entity "world", but missing greeting"#
);
assert_eq!(
format!(
"{:?}",
crate::IncompleteError::Second("world")
.custom::<crate::tests::K1, crate::tests::K2>()
),
r#"IncompleteError { greeting: _, entity: "world" }"#
);
}
}
mod duplicate {
#[test]
fn first() {
assert_eq!(
crate::DuplicateError {
pair: <crate::Spair<String>>::new("hello", "hallo").unwrap(),
which: crate::Duplicated::First,
}
.to_string(),
r#"got duplicate first key: "hello" and "hallo""#
);
assert_eq!(
format!(
"{:?}",
crate::DuplicateError {
pair: <crate::Spair<String>>::new("hello", "hallo").unwrap(),
which: crate::Duplicated::First,
}
),
r#"DuplicateError { pair: ("hello", "hallo"), which: First }"#
);
assert_eq!(
crate::DuplicateError {
pair: <crate::Spair<String>>::new("hello", "hallo").unwrap(),
which: crate::Duplicated::First,
}
.custom::<crate::tests::K1, crate::tests::K2>()
.to_string(),
r#"got duplicate greeting: "hello" and "hallo""#
);
assert_eq!(
format!(
"{:?}",
crate::DuplicateError {
pair: <crate::Spair<String>>::new("hello", "hallo").unwrap(),
which: crate::Duplicated::First,
}
.custom::<crate::tests::K1, crate::tests::K2>()
),
r#"DuplicateError { pair: ("hello", "hallo"), which: greeting }"#
);
}
#[test]
fn second() {
assert_eq!(
crate::DuplicateError {
pair: <crate::Spair<String>>::new("world", "whirl").unwrap(),
which: crate::Duplicated::Second,
}
.to_string(),
r#"got duplicate second key: "world" and "whirl""#
);
assert_eq!(
format!(
"{:?}",
crate::DuplicateError {
pair: <crate::Spair<String>>::new("world", "whirl").unwrap(),
which: crate::Duplicated::Second,
}
),
r#"DuplicateError { pair: ("world", "whirl"), which: Second }"#
);
assert_eq!(
crate::DuplicateError {
pair: <crate::Spair<String>>::new("world", "whirl").unwrap(),
which: crate::Duplicated::Second,
}
.custom::<crate::tests::K1, crate::tests::K2>()
.to_string(),
r#"got duplicate entity: "world" and "whirl""#
);
assert_eq!(
format!(
"{:?}",
crate::DuplicateError {
pair: <crate::Spair<String>>::new("world", "whirl").unwrap(),
which: crate::Duplicated::Second,
}
.custom::<crate::tests::K1, crate::tests::K2>()
),
r#"DuplicateError { pair: ("world", "whirl"), which: entity }"#
);
}
}
mod act {
mod before {
#[test]
fn empty() {
assert_eq!(
format!("{:?}", crate::act::<String, crate::tests::Hyphen, false>()),
r"Act(_, _)"
);
}
#[test]
fn first() {
assert_eq!(
format!(
"{:?}",
crate::act::<String, crate::tests::Hyphen, false>()
.first("hello")
.unwrap()
),
r#"Act("hello", _)"#
);
}
#[test]
fn second() {
assert_eq!(
format!(
"{:?}",
crate::act::<String, crate::tests::Hyphen, false>()
.second("world")
.unwrap()
),
r#"Act(_, "world")"#
);
}
#[test]
fn both() {
assert_eq!(
format!(
"{:?}",
crate::act::<String, crate::tests::Hyphen, false>()
.first("hello")
.unwrap()
.second("world")
.unwrap()
),
r#"Act("hello", "world")"#
);
}
}
mod after {
#[test]
fn empty() {
assert_eq!(
format!("{:?}", crate::act::<String, crate::tests::Hyphen, true>()),
r"Act(_, _)"
);
}
#[test]
fn first() {
assert_eq!(
format!(
"{:?}",
crate::act::<String, crate::tests::Hyphen, true>()
.first("hello")
.unwrap()
),
r#"Act("hello", _)"#
);
}
#[test]
fn second() {
assert_eq!(
format!(
"{:?}",
crate::act::<String, crate::tests::Hyphen, true>()
.second("world")
.unwrap()
),
r#"Act(_, "world")"#
);
}
#[test]
fn both() {
assert_eq!(
format!(
"{:?}",
crate::act::<String, crate::tests::Hyphen, true>()
.first("hello")
.unwrap()
.second("world")
.unwrap()
),
r#"Act("hello", "world")"#
);
}
}
}
mod spair {
#[test]
fn before() {
assert_eq!(
<crate::Spair<String, crate::tests::Hyphen, false>>::new("hello", "world")
.unwrap()
.to_string(),
r"hello-world"
);
assert_eq!(
format!(
"{:?}",
<crate::Spair<String, crate::tests::Hyphen, false>>::new("hello", "world").unwrap(),
),
r#"Spair("hello", "world")"#
);
}
#[test]
fn after() {
assert_eq!(
<crate::Spair<String, crate::tests::Hyphen, true>>::new("hello", "world")
.unwrap()
.to_string(),
r"hello-world"
);
assert_eq!(
format!(
"{:?}",
<crate::Spair<String, crate::tests::Hyphen, true>>::new("hello", "world").unwrap(),
),
r#"Spair("hello", "world")"#
);
}
#[test]
fn split() {
assert!(<crate::Spair<_, crate::tests::Hyphen, false>>::split("hello").is_none(),);
assert!(<crate::Spair<_, crate::tests::Hyphen, true>>::split("hello").is_none(),);
assert_eq!(
<crate::Spair<_, crate::tests::Hyphen, false>>::split("hello-the-world")
.unwrap()
.pair(),
("hello", "the-world"),
);
assert_eq!(
<crate::Spair<_, crate::tests::Hyphen, true>>::split("hello-the-world")
.unwrap()
.pair(),
("hello", "the-world"),
);
}
#[test]
fn rsplit() {
assert!(<crate::Spair<_, crate::tests::Hyphen, false>>::rsplit("hello").is_none(),);
assert!(<crate::Spair<_, crate::tests::Hyphen, true>>::rsplit("hello").is_none(),);
assert_eq!(
<crate::Spair<_, crate::tests::Hyphen, false>>::rsplit("hello-the-world")
.unwrap()
.pair(),
("hello-the", "world"),
);
assert_eq!(
<crate::Spair<_, crate::tests::Hyphen, true>>::rsplit("hello-the-world")
.unwrap()
.pair(),
("hello-the", "world"),
);
}
}
#[allow(clippy::undocumented_unsafe_blocks)]
mod split_unchecked {
#[test]
fn before_valid() {
unsafe {
assert_eq!(
<crate::Spair<_, crate::tests::Hyphen, false>>::split_unchecked("hello-world", 5)
.pair(),
("hello", "world"),
);
}
}
#[test]
fn after_valid() {
unsafe {
assert_eq!(
<crate::Spair<_, crate::tests::Hyphen, true>>::split_unchecked("hello-world", 6)
.pair(),
("hello", "world"),
);
}
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn before_is_after() {
unsafe {
core::hint::black_box(
<crate::Spair<_, crate::tests::Hyphen, false>>::split_unchecked("hello-world", 6),
);
}
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn after_is_before() {
unsafe {
core::hint::black_box(
<crate::Spair<_, crate::tests::Hyphen, true>>::split_unchecked("hello-world", 5),
);
}
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn start_before() {
unsafe {
core::hint::black_box(
<crate::Spair<_, crate::tests::Hyphen, false>>::split_unchecked("hello-world", 0),
);
}
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn start_after() {
unsafe {
core::hint::black_box(
<crate::Spair<_, crate::tests::Hyphen, true>>::split_unchecked("hello-world", 0),
);
}
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn end_before() {
unsafe {
core::hint::black_box(
<crate::Spair<_, crate::tests::Hyphen, false>>::split_unchecked("hello-world", 11),
);
}
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn end_after() {
unsafe {
core::hint::black_box(
<crate::Spair<_, crate::tests::Hyphen, true>>::split_unchecked("hello-world", 11),
);
}
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn oob_before() {
unsafe {
core::hint::black_box(
<crate::Spair<_, crate::tests::Hyphen, false>>::split_unchecked("hello-world", 100),
);
}
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn oob_after() {
unsafe {
core::hint::black_box(
<crate::Spair<_, crate::tests::Hyphen, true>>::split_unchecked("hello-world", 100),
);
}
}
}