use super::*;
use crate::primitives::geometry::tests::{point, quad};
#[test]
fn the_corners_are_in_perimeter_winding_order_and_never_raster_order() {
let page = quad();
assert_eq!(page.top_left(), point(0.10, 0.11));
assert_eq!(page.top_right(), point(0.90, 0.12));
assert_eq!(page.bottom_right(), point(0.91, 0.88));
assert_eq!(page.bottom_left(), point(0.13, 0.89));
assert_eq!(
page.corners(),
[
point(0.10, 0.11),
point(0.90, 0.12),
point(0.91, 0.88),
point(0.13, 0.89),
],
"the array walks the perimeter, so the third entry is diagonally opposite the first"
);
let third = page.corners()[2];
assert_eq!(
third,
page.bottom_right(),
"reading the third corner as `bottom_left` is the bow-tie this assertion exists to catch"
);
}
#[test]
fn the_constructor_takes_its_corners_in_the_order_the_accessors_return_them() {
let corners = [
point(0.1, 0.1),
point(0.9, 0.1),
point(0.9, 0.9),
point(0.1, 0.9),
];
let page = Quad::new(corners[0], corners[1], corners[2], corners[3]);
assert_eq!(page.corners(), corners);
}
#[test]
fn a_unit_quad_refuses_exactly_what_its_component_does() {
assert!(Quad::try_unit((0.1, 0.1), (0.9, 0.1), (0.9, 0.9), (0.1, 0.9)).is_ok());
assert!(Quad::try_unit((0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)).is_ok());
assert!(Quad::try_unit((1.9, 0.1), (0.9, 0.1), (0.9, 0.9), (0.1, 0.9)).is_err());
assert!(Quad::try_unit((0.1, 0.1), (0.9, 0.1), (0.9, 0.9), (0.1, f32::NAN)).is_err());
}
#[test]
fn a_rotated_quad_whose_corners_defy_their_names_is_still_admitted() {
let tilted = Quad::try_unit((0.10, 0.40), (0.60, 0.10), (0.90, 0.60), (0.40, 0.90))
.expect("a rotated document is a document");
assert!(
tilted.top_left().y() > tilted.top_right().y(),
"the first corner sits BELOW the second, and nothing refuses it"
);
}
#[test]
fn a_quad_nests_four_named_corners_of_two_columns_each() {
let segments = <Quad<NormCoord> as ColumnKind>::SEGMENTS;
assert_eq!(segments.len(), 4);
let names: Vec<_> = segments
.iter()
.map(|segment| {
segment
.field()
.expect("every corner is named")
.declared()
.as_str()
})
.collect();
assert_eq!(
names,
["top_left", "top_right", "bottom_right", "bottom_left"],
"the segment order is the WINDING order, so the columns are written in it too"
);
for segment in segments {
assert_eq!(crate::width(core::slice::from_ref(segment)), 2);
}
}
#[test]
fn a_quad_round_trips_through_a_cursor_and_refuses_a_forged_one() {
let mut bytes = Vec::new();
let written = quad();
crate::CursorValue::write_cursor(&written, &mut bytes);
assert_eq!(
<Quad<NormCoord> as crate::CursorValue>::read_cursor(&bytes),
Some(written)
);
assert_eq!(
<Quad<NormCoord> as crate::CursorValue>::read_cursor(&bytes[..bytes.len() - 1]),
None
);
assert_eq!(
<Quad<NormCoord> as crate::CursorValue>::read_cursor(&[]),
None
);
}
#[test]
fn a_filter_is_empty_until_a_component_under_some_corner_is_set() {
let bare = QuadFilter::<crate::ScalarFilter<NormCoord>>::new();
assert!(crate::GraphqlFilter::is_empty(&bare));
let emptily =
QuadFilter::<crate::ScalarFilter<NormCoord>>::new().with_top_left(Point2Filter::new());
assert!(
crate::GraphqlFilter::is_empty(&emptily),
"a corner holding an empty position filter asks nothing"
);
let asked =
QuadFilter::<crate::ScalarFilter<NormCoord>>::new()
.with_bottom_right(Point2Filter::new().with_x(
crate::ScalarFilter::new().with_gt(crate::primitives::geometry::tests::coord(0.5)),
));
assert!(!crate::GraphqlFilter::is_empty(&asked));
}
#[test]
fn the_bounds_are_the_tightest_box_the_four_corners_span() {
let page = Quad::new(
Point2::new(0.10_f32, 0.11),
Point2::new(0.90, 0.12),
Point2::new(0.91, 0.88),
Point2::new(0.13, 0.89),
);
let around = page.bounds();
assert_eq!(around.x(), 0.10, "the leftmost corner, which is the FIRST");
assert_eq!(
around.y(),
0.11,
"the topmost corner, which is also the first"
);
assert_eq!(around.width(), 0.91 - 0.10, "…out to the THIRD corner's x");
assert_eq!(
around.height(),
0.89 - 0.11,
"…down to the FOURTH corner's y"
);
}
#[test]
fn a_rectangular_quad_bounds_to_the_rectangle_it_already_is() {
let square = Quad::new(
Point2::new(0.2_f32, 0.3),
Point2::new(0.8, 0.3),
Point2::new(0.8, 0.7),
Point2::new(0.2, 0.7),
);
let around = square.bounds();
assert_eq!((around.x(), around.y()), (0.2, 0.3));
assert!((around.width() - 0.6).abs() < 1e-6);
assert!((around.height() - 0.4).abs() < 1e-6);
}
#[test]
fn the_bounds_are_taken_in_whatever_the_component_measures_in() {
let pixels = Quad::new(
Point2::new(12_u32, 40),
Point2::new(212, 44),
Point2::new(208, 190),
Point2::new(10, 186),
);
let around = pixels.bounds();
assert_eq!((around.x(), around.y()), (10, 40));
assert_eq!((around.width(), around.height()), (202, 150));
}