use super::*;
fn n(count: u64) -> NonZeroU64 {
NonZeroU64::new(count).expect("a count from 1")
}
fn task(counts: &[u64]) -> TaskPath {
TaskPath::new(counts.iter().copied().map(n).collect())
}
#[test]
fn an_id_is_exactly_32_lowercase_hexadecimal_digits() {
let id = RunId::parse("7f3a91c04be2d5e6f708192a3b4c5d6e").unwrap();
assert_eq!(id.as_str(), "7f3a91c04be2d5e6f708192a3b4c5d6e");
assert_eq!(id.to_string(), id.as_str());
assert_eq!(id.shown(), "7f3a91c04be2");
assert_eq!(format!("{id:?}"), "RunId(7f3a91c04be2d5e6f708192a3b4c5d6e)");
assert_eq!("7f3a91c04be2d5e6f708192a3b4c5d6e".parse::<RunId>(), Ok(id));
for refused in [
"",
"run-1",
"7f3a91c04be2",
"7F3A91C04BE2D5E6F708192A3B4C5D6E",
"7f3a91c04be2d5e6f708192a3b4c5d6e0",
"7f3a91c04be2d5e6f708192a3b4c5d6g",
"+7f3a91c04be2d5e6f708192a3b4c5d6",
"../7f3a91c04be2d5e6f708192a3b4c5d",
] {
let error = RunId::parse(refused).unwrap_err();
assert_eq!((error.kind(), error.text()), (PlaceKind::Run, refused));
}
}
#[test]
fn an_example_and_bits_are_written_as_32_digits_and_order_as_they_read() {
assert_eq!(
RunId::example(1).to_string(),
"00000000000000000000000000000001"
);
assert_eq!(
RunId::example(0xab).to_string(),
"000000000000000000000000000000ab"
);
let mut bits = [0_u8; 16];
bits[15] = 0x0f;
bits[0] = 0xa0;
assert_eq!(
RunId::from_bits(bits).to_string(),
"a000000000000000000000000000000f"
);
assert!(RunId::parse(&RunId::example(u128::MAX).to_string()).is_ok());
let (low, high) = (RunId::example(0x0f), RunId::example(0xa0));
assert!(low < high && low.to_string() < high.to_string());
assert!(
RunId::parse("run-1")
.unwrap_err()
.to_string()
.contains("32 lowercase hexadecimal digits")
);
}
#[test]
fn a_call_id_is_a_task_id_and_a_counter_with_an_optional_segment() {
for (text, segment, counts, index) in [
("root#1", 0, &[][..], 1),
("root/2#3", 0, &[2][..], 3),
("root/1/12#40", 0, &[1, 12][..], 40),
("p1:root#6", 1, &[][..], 6),
("p23:root/4#5", 23, &[4][..], 5),
("root#18446744073709551615", 0, &[][..], u64::MAX),
] {
let id = CallRef::parse(text).unwrap();
assert_eq!(id, CallRef::new(segment, task(counts), n(index)), "{text}");
assert_eq!(
(id.segment(), id.task(), id.index()),
(segment, &task(counts), n(index)),
"{text}"
);
assert_eq!(id.to_string(), text, "one spelling is read and written");
assert_eq!(format!("{id:?}"), format!("CallRef({text})"));
assert_eq!(text.parse::<CallRef>(), Ok(id.clone()));
assert_eq!(id.position(), CallRef::first(task(counts), n(index)));
}
}
#[test]
fn any_other_text_is_not_a_call_id() {
for refused in [
"",
"root",
"root#",
"root#0",
"root#01",
"root#-1",
"root#+1",
"root#18446744073709551616",
"root/#1",
"root/0#1",
"root/01#1",
"root/1/#1",
"root//1#1",
"root/2/0#3",
"task#1",
"Root#1",
"#1",
"root#x",
"root#1#2",
"root#1+0",
"p0:root#1",
"p:root#1",
"p01:root#1",
"p1root#1",
"p1:p2:root#1",
"q1:root#1",
"protocol:1",
" root#1",
"root#1 ",
] {
let error = CallRef::parse(refused).unwrap_err();
assert_eq!((error.kind(), error.text()), (PlaceKind::Call, refused));
}
assert!(
CallRef::parse("one")
.unwrap_err()
.to_string()
.contains("`one` is not a call id")
);
}
#[test]
fn a_task_id_is_root_and_its_spawn_counts() {
assert_eq!(TaskPath::parse("root"), Ok(TaskPath::root()));
assert_eq!(TaskPath::parse("root/2/1"), Ok(task(&[2, 1])));
assert_eq!(TaskPath::root().child(n(2)).child(n(1)), task(&[2, 1]));
assert_eq!(task(&[2, 1]).to_string(), "root/2/1");
assert_eq!(task(&[2, 1]).counts(), [n(2), n(1)]);
for refused in [
"",
"rot",
"root/",
"root/0",
"root/01",
"root/-1",
"root/+1",
"root1",
"rootx",
"root//1",
"toor/1",
"root#1",
"p1:root",
"root/18446744073709551616",
] {
let error = TaskPath::parse(refused).unwrap_err();
assert_eq!((error.kind(), error.text()), (PlaceKind::Task, refused));
}
}
#[test]
fn a_task_path_is_written_from_the_root() {
assert_eq!(TaskPath::root().to_string(), "root");
let child = TaskPath::root().child(n(1)).child(n(2));
assert_eq!(child.to_string(), "root/1/2");
assert_eq!(child.counts(), [n(1), n(2)]);
assert_eq!(TaskPath::root().counts(), []);
}
#[test]
fn a_moment_is_read_as_code_parse_moment_reads_it_and_printed_in_full() {
for (text, segment, counts, call, steps) in [
("root/2#4+17", 0, &[2][..], 4, 17),
("p1:root#4+5", 1, &[][..], 4, 5),
("root#0+0", 0, &[][..], 0, 0),
] {
let moment = Moment {
segment,
task: task(counts),
call,
steps,
};
assert_eq!(Moment::parse(text), Ok(moment.clone()), "{text}");
assert_eq!(moment.to_string(), text);
}
for refused in [
"p0:root#4+5",
"root/0#1+1",
"root#1",
"root",
"root/2#04+1",
"root#1+01",
"task#1+1",
"px:root#1+1",
"root#1+x",
"root#1+",
"root#+1",
] {
let error = Moment::parse(refused).unwrap_err();
assert_eq!((error.kind(), error.text()), (PlaceKind::Moment, refused));
}
}
#[test]
fn every_place_is_written_as_the_text_it_reads_in_serde() {
let call = CallRef::new(2, task(&[1]), n(3));
let written = serde_json::to_string(&call).unwrap();
assert_eq!(written, "\"p2:root/1#3\"");
assert_eq!(serde_json::from_str::<CallRef>(&written).unwrap(), call);
assert!(serde_json::from_str::<CallRef>("\"root#0\"").is_err());
let run = RunId::example(7);
assert_eq!(
serde_json::from_str::<RunId>(&serde_json::to_string(&run).unwrap()).unwrap(),
run
);
}