Skip to main content

laser_pdf/test_utils/
old.rs

1use super::*;
2
3pub fn test_measure_draw_compatibility<E: Element>(
4    element: &E,
5    width: WidthConstraint,
6    first_height: f32,
7    full_height: Option<f32>,
8    pos: (f32, f32),
9    page_size: (f32, f32),
10) -> ElementMeasureDrawCompatibilityOutput {
11    let measure = measure_element(element, width, first_height, full_height);
12    let draw = draw_element(
13        element,
14        width,
15        first_height,
16        None,
17        pos,
18        page_size,
19        full_height.map(|f| BreakableDrawConfig {
20            pos,
21            full_height: f,
22            preferred_height_break_count: 0,
23        }),
24    );
25    let restricted_draw = draw_element(
26        element,
27        width,
28        first_height,
29        measure.size.height,
30        pos,
31        page_size,
32        full_height.map(|f| BreakableDrawConfig {
33            pos,
34            full_height: f,
35            preferred_height_break_count: measure.break_count,
36        }),
37    );
38
39    assert_eq!(measure.break_count, draw.break_count);
40    assert_eq!(measure.break_count, restricted_draw.break_count);
41
42    assert_eq!(measure.size, draw.size);
43    assert_eq!(measure.size, restricted_draw.size);
44
45    ElementMeasureDrawCompatibilityOutput {
46        width,
47        first_height,
48        pos,
49        page_size,
50        size: measure.size,
51        breakable: full_height.map(|f| ElementMeasureDrawCompatibilityOutputBreakable {
52            first_location_usage: FirstLocationUsage::NoneHeight, // TODO
53            full_height: f,
54            break_count: measure.break_count,
55            extra_location_min_height: measure.extra_location_min_height,
56        }),
57    }
58}
59
60pub struct ElementTestParams {
61    /// Will be tested with None and this.
62    pub width: f32,
63
64    pub first_height: f32,
65    pub full_height: f32,
66
67    pub pos: (f32, f32),
68    pub page_size: (f32, f32),
69}
70
71impl Default for ElementTestParams {
72    fn default() -> Self {
73        Self {
74            width: 186.,
75            first_height: 136.5,
76            full_height: 273.,
77
78            pos: (12., 297. - 12.),
79            page_size: (210., 297.),
80        }
81    }
82}
83
84pub struct TestConfiguration<'a> {
85    pub use_first_height: bool,
86    pub breakable: bool,
87    pub expand_width: bool,
88    pub params: &'a ElementTestParams,
89}
90
91impl<'a> TestConfiguration<'a> {
92    pub fn run(&self, element: &impl Element) -> ElementMeasureDrawCompatibilityOutput {
93        let width = WidthConstraint {
94            max: self.params.width,
95            expand: self.expand_width,
96        };
97
98        let first_height = if self.use_first_height {
99            self.params.first_height
100        } else {
101            self.params.full_height
102        };
103
104        let full_height = if self.breakable {
105            Some(self.params.full_height)
106        } else {
107            None
108        };
109
110        test_measure_draw_compatibility(
111            element,
112            width,
113            first_height,
114            full_height,
115            self.params.pos,
116            self.params.page_size,
117        )
118    }
119}
120
121impl ElementTestParams {
122    pub fn configurations(&self) -> impl Iterator<Item = TestConfiguration<'_>> {
123        [
124            (false, false, false),
125            (false, false, true),
126            (false, true, false),
127            (false, true, true),
128            (true, false, false),
129            (true, false, true),
130            (true, true, false),
131            (true, true, true),
132        ]
133        .into_iter()
134        .map(
135            move |(use_first_height, breakable, expand_width)| TestConfiguration {
136                use_first_height,
137                breakable,
138                expand_width,
139                params: self,
140            },
141        )
142    }
143
144    pub fn run<'a, E: Element>(
145        &'a self,
146        element: &'a E,
147    ) -> impl Iterator<Item = ElementMeasureDrawCompatibilityOutput> + 'a {
148        self.configurations().map(|c| c.run(element))
149    }
150}
151
152pub struct ElementMeasureDrawCompatibilityOutputBreakable {
153    pub full_height: f32,
154
155    pub break_count: u32,
156    pub extra_location_min_height: Option<f32>,
157
158    pub first_location_usage: FirstLocationUsage,
159}
160
161impl ElementMeasureDrawCompatibilityOutputBreakable {
162    pub fn assert_break_count(&self, break_count: u32) -> &Self {
163        assert_eq!(self.break_count, break_count);
164        self
165    }
166
167    pub fn assert_extra_location_min_height(
168        &self,
169        extra_location_min_height: Option<f32>,
170    ) -> &Self {
171        assert_eq!(self.extra_location_min_height, extra_location_min_height);
172        self
173    }
174
175    pub fn assert_first_location_usage(&self, expected: FirstLocationUsage) -> &Self {
176        assert_eq!(self.first_location_usage, expected);
177        self
178    }
179}
180
181pub struct ElementMeasureDrawCompatibilityOutput {
182    pub width: WidthConstraint,
183    pub first_height: f32,
184
185    pub pos: (f32, f32),
186    pub page_size: (f32, f32),
187
188    pub size: ElementSize,
189
190    pub breakable: Option<ElementMeasureDrawCompatibilityOutputBreakable>,
191}
192
193impl ElementMeasureDrawCompatibilityOutput {
194    pub fn assert_no_breaks(&self) -> &Self {
195        if let Some(b) = &self.breakable {
196            b.assert_break_count(0);
197        }
198
199        self
200    }
201
202    pub fn assert_size(&self, size: ElementSize) -> &Self {
203        assert_eq!(self.size, size);
204        self
205    }
206}