StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | use float_cmp::assert_approx_eq; |
| 2 | |
| 3 | use crate::scene::ZIndex; |
| 4 | use crate::App; |
| 5 | |
| 6 | use super::*; |
| 7 | |
| 8 | #[test] |
| 9 | fn test_laid_out_text_height() { |
| 10 | App::test((), |mut app| async move { |
| 11 | app.update(|_ctx| { |
| 12 | let text_frame = TextFrame::mock("foo\nbar\nbaz"); |
| 13 | let line_count = text_frame.lines().len(); |
| 14 | let laid_out_text = LaidOutText::Frame(Arc::new(text_frame)); |
| 15 | let height = laid_out_text.height(); |
| 16 | let expected = 13. * 1.2 * line_count as f32; |
| 17 | assert_approx_eq!(f32, height, expected); |
| 18 | }); |
| 19 | }); |
| 20 | } |
| 21 | |
| 22 | /// We calculate height of a line by multiplying the line's font size by line |
| 23 | /// height ratio. This test ensures that the height of a laid out line respects |
| 24 | /// this calculation. |
| 25 | #[test] |
| 26 | fn test_laid_out_line_height() { |
| 27 | App::test((), |mut app| async move { |
| 28 | app.update(|_ctx| { |
| 29 | let line = Line::mock_from_str("foo"); |
| 30 | let laid_out_line = LaidOutText::Line(Arc::new(line)); |
| 31 | let height = laid_out_line.height(); |
| 32 | |
| 33 | // 13 and 1.2 are the default font size and line height ratios, respectively. |
| 34 | let expected = 13. * 1.2; |
| 35 | assert_approx_eq!(f32, height, expected); |
| 36 | }); |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn test_single_line_char_hit_testing_respects_y_bounds() { |
| 42 | App::test((), |mut app| async move { |
| 43 | app.update(|_ctx| { |
| 44 | let mut line = Line::mock_from_str("foo"); |
| 45 | line.width = 30.; |
| 46 | line.runs[0].width = 30.; |
| 47 | let line = Arc::new(line); |
| 48 | let line_height = line.height(); |
| 49 | let mut text = Text::new_inline("foo", crate::fonts::FamilyId(0), 13.); |
| 50 | text.laid_out_text = LaidOutText::Line(Arc::clone(&line)); |
| 51 | text.origin = Some(Point::from_vec2f(vec2f(10., 20.), ZIndex::new(0))); |
| 52 | |
| 53 | assert!(text.get_char_index(&vec2f(10., 19.9)).is_none()); |
| 54 | assert!(text |
| 55 | .get_char_index(&vec2f(10., 20. + line_height + 0.1)) |
| 56 | .is_none()); |
| 57 | assert!(text |
| 58 | .get_char_index(&vec2f(10., 20. + line_height / 2.)) |
| 59 | .is_some()); |
| 60 | }); |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn test_merge_non_overlapping_ranges() { |
| 66 | let highlight = Highlight::new(); |
| 67 | |
| 68 | let range1 = HighlightedRange { |
| 69 | highlight, |
| 70 | highlight_indices: vec![1, 2, 3], |
| 71 | }; |
| 72 | let range2 = HighlightedRange { |
| 73 | highlight, |
| 74 | highlight_indices: vec![5, 6, 7], |
| 75 | }; |
| 76 | |
| 77 | let result = HighlightedRange::merge_overlapping_ranges(vec![range1.clone(), range2.clone()]); |
| 78 | |
| 79 | assert_eq!(result, vec![range1, range2]); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn test_merge_contiguous_ranges() { |
| 84 | let highlight = Highlight::new(); |
| 85 | |
| 86 | let range1 = HighlightedRange { |
| 87 | highlight, |
| 88 | highlight_indices: vec![1, 2, 3], |
| 89 | }; |
| 90 | let range2 = HighlightedRange { |
| 91 | highlight, |
| 92 | highlight_indices: vec![4, 5, 6], |
| 93 | }; |
| 94 | |
| 95 | let result = HighlightedRange::merge_overlapping_ranges(vec![range1.clone(), range2.clone()]); |
| 96 | |
| 97 | assert_eq!( |
| 98 | result, |
| 99 | vec![HighlightedRange { |
| 100 | highlight, |
| 101 | highlight_indices: vec![1, 2, 3, 4, 5, 6], |
| 102 | }] |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn test_merge_overlapping_ranges() { |
| 108 | let highlight = Highlight::new(); |
| 109 | |
| 110 | let range1 = HighlightedRange { |
| 111 | highlight, |
| 112 | highlight_indices: vec![1, 2, 3], |
| 113 | }; |
| 114 | let range2 = HighlightedRange { |
| 115 | highlight, |
| 116 | highlight_indices: vec![3, 4, 5], |
| 117 | }; |
| 118 | |
| 119 | let result = HighlightedRange::merge_overlapping_ranges(vec![range1.clone(), range2.clone()]); |
| 120 | |
| 121 | assert_eq!( |
| 122 | result, |
| 123 | vec![HighlightedRange { |
| 124 | highlight, |
| 125 | highlight_indices: vec![1, 2, 3, 4, 5], |
| 126 | }] |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn test_merge_single_range() { |
| 132 | let highlight = Highlight::new(); |
| 133 | |
| 134 | let range = HighlightedRange { |
| 135 | highlight, |
| 136 | highlight_indices: vec![1, 2, 3], |
| 137 | }; |
| 138 | |
| 139 | let result = HighlightedRange::merge_overlapping_ranges(vec![range.clone()]); |
| 140 | |
| 141 | assert_eq!(result, vec![range]); |
| 142 | } |
| 143 | |
| 144 | #[test] |
| 145 | fn test_merge_empty_ranges() { |
| 146 | let result = HighlightedRange::merge_overlapping_ranges(vec![]); |
| 147 | assert!(result.is_empty()); |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn test_merge_adjacent_non_contiguous_ranges() { |
| 152 | let highlight1 = Highlight::new(); |
| 153 | let highlight2 = Highlight::new(); |
| 154 | |
| 155 | let range1 = HighlightedRange { |
| 156 | highlight: highlight1, |
| 157 | highlight_indices: vec![1, 2], |
| 158 | }; |
| 159 | let range2 = HighlightedRange { |
| 160 | highlight: highlight2, |
| 161 | highlight_indices: vec![4, 5], |
| 162 | }; |
| 163 | |
| 164 | let result = HighlightedRange::merge_overlapping_ranges(vec![range1.clone(), range2.clone()]); |
| 165 | |
| 166 | assert_eq!(result, vec![range1, range2]); |
| 167 | } |
| 168 |