use super::*;
use crate::NormCoord;
fn reading(text: &str) -> TextDetection<NormCoord> {
TextDetection::new(
text,
Score::new(0.91).expect("the fixture's confidence is in range"),
BoundingBox::try_unit(0.10, 0.42, 0.30, 0.06).expect("the fixture's rectangle is in frame"),
)
}
#[test]
fn a_reading_of_no_characters_is_an_observation_rather_than_a_refusal() {
let nothing = reading("");
assert_eq!(nothing.text(), "");
assert_eq!(
nothing.bbox().width(),
crate::NormCoord::new(0.30).expect("in range"),
"…and the region it was read from survives, which is the whole reason not to refuse"
);
assert!(super::super::Label::new("").is_err(), "the CONTRAST");
}
#[test]
fn a_reading_states_its_columns_and_which_of_them_hold_the_value() {
type Read = TextDetection<NormCoord>;
assert_eq!(crate::width(<Read as ColumnKind>::SEGMENTS), 7);
assert_eq!(crate::value_width(<Read as ColumnKind>::SEGMENTS), 6);
let names: Vec<_> = <Read as ColumnKind>::SEGMENTS
.iter()
.map(|segment| segment.field().expect("named").declared().as_str())
.collect();
assert_eq!(names, ["text", "confidence", "bbox"]);
}
#[test]
fn a_readings_order_names_its_six_value_leaves_and_never_the_boxs_index() {
let levels: Vec<_> = <TextDetection<NormCoord> as ColumnKind>::ORDER
.iter()
.map(|level| level.leaf())
.collect();
assert_eq!(levels, [0, 1, 2, 3, 4, 5]);
let line = reading("Gate B14");
assert!(matches!(
crate::OrderLeaves::leaf(&line, 0),
Some(TextDetectionLeaf::Text(_))
));
assert!(matches!(
crate::OrderLeaves::leaf(&line, 2),
Some(TextDetectionLeaf::Bbox(BoundingBoxLeaf::X(_)))
));
assert_eq!(crate::OrderLeaves::leaf(&line, 6), None);
}
#[test]
fn a_reading_round_trips_through_a_cursor_and_refuses_a_forged_one() {
use crate::CursorValue as _;
let line = reading("Gate B14 · 19:40");
let mut bytes = Vec::new();
line.write_cursor(&mut bytes);
assert_eq!(
<TextDetection<NormCoord> as crate::CursorValue>::read_cursor(&bytes),
Some(line)
);
assert_eq!(
<TextDetection<NormCoord> as crate::CursorValue>::read_cursor(&bytes[..bytes.len() - 1]),
None,
"a frame this type did not write is refused rather than read as a shorter one"
);
let mut trailing = bytes.clone();
trailing.push(0);
assert_eq!(
<TextDetection<NormCoord> as crate::CursorValue>::read_cursor(&trailing),
None,
"…and so are bytes past the last member"
);
}
#[test]
fn a_filter_is_empty_until_one_of_its_three_facets_is_set() {
type Face = TextDetectionFilter<crate::ScalarFilter<NormCoord>>;
assert!(crate::GraphqlFilter::is_empty(&Face::new()));
assert!(
crate::GraphqlFilter::is_empty(&Face::new().with_bbox(BoundingBoxFilter::new())),
"a facet holding an empty filter asks nothing"
);
assert!(!crate::GraphqlFilter::is_empty(&Face::new().with_bbox(
BoundingBoxFilter::new().with_area(
crate::ScalarFilter::new().with_gte(crate::NormCoord::new(0.05).expect("in range"))
)
)));
}