Add tests for source_plate component

This commit is contained in:
Emilia Allison 2023-05-11 20:39:43 -04:00
parent b9cc4a6477
commit 94bb6b249d
Signed by: emilia
GPG Key ID: 7A3F8997BFE894E0
1 changed files with 58 additions and 2 deletions

View File

@ -44,7 +44,7 @@ pub fn SourcePlate(cx: Scope<SourcePlateProps>) -> Element {
#[inline_props]
fn SourcePlateCell(cx: Scope<PlateCellProps>, i: u8, j: u8, color: Option<String>) -> Element {
let selection_state = use_shared_state::<SelectionState>(cx).unwrap();
let selected = in_square(
let selected = in_rect(
selection_state.read().m_start,
selection_state.read().m_end,
(*i, *j),
@ -80,7 +80,7 @@ fn SourcePlateCell(cx: Scope<PlateCellProps>, i: u8, j: u8, color: Option<String
})
}
fn in_square(corner1: Option<(u8, u8)>, corner2: Option<(u8, u8)>, pt: (u8, u8)) -> bool {
fn in_rect(corner1: Option<(u8, u8)>, corner2: Option<(u8, u8)>, pt: (u8, u8)) -> bool {
if let (Some(c1), Some(c2)) = (corner1, corner2) {
return pt.0 <= u8::max(c1.0, c2.0)
&& pt.0 >= u8::min(c1.0, c2.0)
@ -117,3 +117,59 @@ fn SourcePlateSelectionController(cx: Scope) -> Element {
}
})
}
#[cfg(test)]
mod tests {
use super::in_rect;
// in_rect tests
#[test]
fn test_in_rect1() {
// Test in center of rect
let c1 = (1,1);
let c2 = (10,10);
let pt = (5,5);
assert!(in_rect(Some(c1), Some(c2), pt));
// Order of the corners should not matter:
assert!(in_rect(Some(c2), Some(c1), pt));
}
#[test]
fn test_in_rect2() {
// Test on top/bottom edges of rect
let c1 = (1,1);
let c2 = (10,10);
let pt1 = (1,5);
let pt2 = (10,5);
assert!(in_rect(Some(c1), Some(c2), pt1));
assert!(in_rect(Some(c1), Some(c2), pt2));
// Order of the corners should not matter:
assert!(in_rect(Some(c2), Some(c1), pt1));
assert!(in_rect(Some(c2), Some(c1), pt2));
}
#[test]
fn test_in_rect3() {
// Test on left/right edges of rect
let c1 = (1,1);
let c2 = (10,10);
let pt1 = (5,1);
let pt2 = (5,10);
assert!(in_rect(Some(c1), Some(c2), pt1));
assert!(in_rect(Some(c1), Some(c2), pt2));
// Order of the corners should not matter:
assert!(in_rect(Some(c2), Some(c1), pt1));
assert!(in_rect(Some(c2), Some(c1), pt2));
}
#[test]
fn test_in_rect4() {
// Test cases that should fail
let c1 = (1,1);
let c2 = (10,10);
let pt1 = (0,0);
let pt2 = (15,15);
assert_eq!(false, in_rect(Some(c1), Some(c2), pt1));
assert_eq!(false, in_rect(Some(c1), Some(c2), pt2));
}
}