Skip to content

Commit 8a0b507

Browse files
committed
test(render/list_ranking): cover fit_label's zero-width, pad, and truncate-with-ellipsis branches, pad_left prepending spaces only when shorter than the target width, and align_rect offsetting for center/right while passing the area through unchanged for no hint, zero content width, or content wider than the area
1 parent 0d78814 commit 8a0b507

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎src/render/list_ranking.rs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,4 +497,39 @@ mod tests {
497497
assert_eq!(display_width("🥇"), 2);
498498
assert_eq!(display_width("10."), 3);
499499
}
500+
501+
#[test]
502+
fn fit_label_covers_pad_truncate_and_zero_width_branches() {
503+
// Zero width collapses to empty regardless of the label.
504+
assert_eq!(fit_label("anything", 0), "");
505+
// A label that fits is right-padded with spaces to the column width.
506+
assert_eq!(fit_label("ab", 5), "ab ");
507+
// Width 1 has no room for content beside the ellipsis, so it is the ellipsis alone.
508+
assert_eq!(fit_label("hello", 1), "…");
509+
// A longer label keeps `width - 1` chars and appends a single ellipsis.
510+
assert_eq!(fit_label("hello", 3), "he…");
511+
}
512+
513+
#[test]
514+
fn pad_left_prepends_spaces_only_when_shorter_than_width() {
515+
assert_eq!(pad_left("9", 4), " 9");
516+
// Already at or past the width is returned unchanged.
517+
assert_eq!(pad_left("142", 3), "142");
518+
assert_eq!(pad_left("longer", 2), "longer");
519+
}
520+
521+
#[test]
522+
fn align_rect_offsets_for_center_and_right_and_passes_through_otherwise() {
523+
let area = Rect::new(2, 1, 20, 3);
524+
// Center places the block in the middle of the spare width.
525+
assert_eq!(align_rect(area, 10, Some("center")).x, 2 + 5);
526+
// Right pushes it flush against the trailing edge.
527+
let right = align_rect(area, 10, Some("right"));
528+
assert_eq!(right.x, 2 + 10);
529+
assert_eq!(right.width, 10);
530+
// No align hint, zero content, or content wider than the area all return the area as-is.
531+
assert_eq!(align_rect(area, 10, None), area);
532+
assert_eq!(align_rect(area, 0, Some("center")), area);
533+
assert_eq!(align_rect(area, 99, Some("right")), area);
534+
}
500535
}

0 commit comments

Comments
 (0)