use super::*;
use crate::NormCoord;
fn code(payload: &str, symbology: Symbology) -> BarcodeDetection<NormCoord> {
BarcodeDetection::new(
payload,
symbology,
Score::new(0.98).expect("the fixture's confidence is in range"),
BoundingBox::try_unit(0.05, 0.60, 0.90, 0.20).expect("the fixture's rectangle is in frame"),
)
}
#[test]
fn a_decoded_code_states_its_columns_and_which_of_them_hold_the_value() {
type Code = BarcodeDetection<NormCoord>;
assert_eq!(crate::width(<Code as ColumnKind>::SEGMENTS), 8);
assert_eq!(crate::value_width(<Code as ColumnKind>::SEGMENTS), 7);
let names: Vec<_> = <Code as ColumnKind>::SEGMENTS
.iter()
.map(|segment| segment.field().expect("named").declared().as_str())
.collect();
assert_eq!(names, ["payload", "symbology", "confidence", "bbox"]);
}
#[test]
fn a_codes_order_names_its_seven_value_leaves_and_never_the_boxs_index() {
let levels: Vec<_> = <BarcodeDetection<NormCoord> as ColumnKind>::ORDER
.iter()
.map(|level| level.leaf())
.collect();
assert_eq!(levels, [0, 1, 2, 3, 4, 5, 6]);
let ticket = code("M1DOE/JOHN", Symbology::Pdf417);
assert_eq!(
crate::OrderLeaves::leaf(&ticket, 1),
Some(BarcodeDetectionLeaf::Symbology(Symbology::Pdf417))
);
assert!(matches!(
crate::OrderLeaves::leaf(&ticket, 5),
Some(BarcodeDetectionLeaf::Bbox(BoundingBoxLeaf::Width(_)))
));
assert_eq!(crate::OrderLeaves::leaf(&ticket, 7), None);
}
#[test]
fn a_code_round_trips_through_a_cursor_and_refuses_a_forged_one() {
use crate::CursorValue as _;
for value in [
code("https://example.invalid/a", Symbology::Qr),
code("", Symbology::new("han xin")),
] {
let mut bytes = Vec::new();
value.write_cursor(&mut bytes);
assert_eq!(
<BarcodeDetection<NormCoord> as crate::CursorValue>::read_cursor(&bytes),
Some(value)
);
}
let mut bytes = Vec::new();
code("x", Symbology::Qr).write_cursor(&mut bytes);
assert_eq!(
<BarcodeDetection<NormCoord> as crate::CursorValue>::read_cursor(&bytes[..bytes.len() - 1]),
None
);
}
#[test]
fn a_filter_is_empty_until_one_of_its_four_facets_is_set() {
type Face = BarcodeDetectionFilter<crate::ScalarFilter<NormCoord>>;
assert!(crate::GraphqlFilter::is_empty(&Face::new()));
assert!(crate::GraphqlFilter::is_empty(
&Face::new().with_symbology(EnumFilter::new())
));
assert!(!crate::GraphqlFilter::is_empty(
&Face::new().with_symbology(EnumFilter::new().with_eq(Symbology::Qr))
));
}