lex-core 0.8.2

Parser library for the lex format
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Table assertions

use crate::lex::ast::elements::verbatim::VerbatimBlockMode;
use crate::lex::ast::{Table, TableCellAlignment};

pub struct TableAssertion<'a> {
    pub(crate) table: &'a Table,
    pub(crate) context: String,
}

impl<'a> TableAssertion<'a> {
    pub fn subject(self, expected: &str) -> Self {
        let actual = self.table.subject.as_string();
        assert_eq!(
            actual, expected,
            "{}: Expected table subject '{}', got '{}'",
            self.context, expected, actual
        );
        self
    }

    pub fn mode(self, expected: VerbatimBlockMode) -> Self {
        assert_eq!(
            self.table.mode, expected,
            "{}: Expected table mode {:?}, got {:?}",
            self.context, expected, self.table.mode
        );
        self
    }

    /// Assert that the table has an annotation with the given label
    pub fn has_annotation_with_label(self, expected: &str) -> Self {
        let found = self
            .table
            .annotations
            .iter()
            .any(|a| a.data.label.value == expected);
        assert!(
            found,
            "{}: Expected annotation with label '{}'",
            self.context, expected
        );
        self
    }

    pub fn header_row_count(self, expected: usize) -> Self {
        let actual = self.table.header_rows.len();
        assert_eq!(
            actual, expected,
            "{}: Expected {} header rows, got {}",
            self.context, expected, actual
        );
        self
    }

    pub fn body_row_count(self, expected: usize) -> Self {
        let actual = self.table.body_rows.len();
        assert_eq!(
            actual, expected,
            "{}: Expected {} body rows, got {}",
            self.context, expected, actual
        );
        self
    }

    pub fn row_count(self, expected: usize) -> Self {
        let actual = self.table.row_count();
        assert_eq!(
            actual, expected,
            "{}: Expected {} total rows, got {}",
            self.context, expected, actual
        );
        self
    }

    pub fn column_count(self, expected: usize) -> Self {
        let actual = self.table.column_count();
        assert_eq!(
            actual, expected,
            "{}: Expected {} columns, got {}",
            self.context, expected, actual
        );
        self
    }

    /// Assert a specific header row's cell contents
    pub fn header_row(self, row_index: usize, assertion: impl FnOnce(RowAssertion<'a>)) -> Self {
        assert!(
            row_index < self.table.header_rows.len(),
            "{}: Header row index {} out of bounds ({} header rows)",
            self.context,
            row_index,
            self.table.header_rows.len()
        );
        assertion(RowAssertion {
            row: &self.table.header_rows[row_index],
            context: format!("{}::header[{}]", self.context, row_index),
        });
        self
    }

    /// Assert a specific body row's cell contents
    pub fn body_row(self, row_index: usize, assertion: impl FnOnce(RowAssertion<'a>)) -> Self {
        assert!(
            row_index < self.table.body_rows.len(),
            "{}: Body row index {} out of bounds ({} body rows)",
            self.context,
            row_index,
            self.table.body_rows.len()
        );
        assertion(RowAssertion {
            row: &self.table.body_rows[row_index],
            context: format!("{}::body[{}]", self.context, row_index),
        });
        self
    }

    /// Assert all header cell texts for a header row
    pub fn header_cells(self, row_index: usize, expected: &[&str]) -> Self {
        assert!(
            row_index < self.table.header_rows.len(),
            "{}: Header row index {} out of bounds",
            self.context,
            row_index
        );
        let row = &self.table.header_rows[row_index];
        let actual: Vec<&str> = row.cells.iter().map(|c| c.content.as_string()).collect();
        assert_eq!(
            actual, expected,
            "{}: Header row {} cells mismatch",
            self.context, row_index
        );
        self
    }

    /// Assert all body cell texts for a body row
    pub fn body_cells(self, row_index: usize, expected: &[&str]) -> Self {
        assert!(
            row_index < self.table.body_rows.len(),
            "{}: Body row index {} out of bounds",
            self.context,
            row_index
        );
        let row = &self.table.body_rows[row_index];
        let actual: Vec<&str> = row.cells.iter().map(|c| c.content.as_string()).collect();
        assert_eq!(
            actual, expected,
            "{}: Body row {} cells mismatch",
            self.context, row_index
        );
        self
    }

    /// Assert that one of the table's annotations has a parameter with the given key/value
    pub fn has_annotation_parameter_with_value(self, key: &str, value: &str) -> Self {
        let found = self.table.annotations.iter().any(|a| {
            a.data
                .parameters
                .iter()
                .any(|p| p.key == key && p.value == value)
        });
        assert!(
            found,
            "{}: Expected annotation parameter '{}={}'",
            self.context, key, value
        );
        self
    }

    pub fn has_footnotes(self) -> Self {
        assert!(
            self.table.footnotes.is_some(),
            "{}: Expected table to have footnotes",
            self.context
        );
        self
    }

    pub fn no_footnotes(self) -> Self {
        assert!(
            self.table.footnotes.is_none(),
            "{}: Expected table to have no footnotes",
            self.context
        );
        self
    }

    pub fn footnote_count(self, expected: usize) -> Self {
        let list = self
            .table
            .footnotes
            .as_ref()
            .unwrap_or_else(|| panic!("{}: Expected table to have footnotes", self.context));
        let items: Vec<_> = list.items.iter().collect();
        assert_eq!(
            items.len(),
            expected,
            "{}: Expected {} footnotes, got {}",
            self.context,
            expected,
            items.len()
        );
        self
    }

    pub fn footnote_text(self, index: usize, expected: &str) -> Self {
        let list = self
            .table
            .footnotes
            .as_ref()
            .unwrap_or_else(|| panic!("{}: Expected table to have footnotes", self.context));
        let items: Vec<_> = list.items.iter().collect();
        assert!(
            index < items.len(),
            "{}: Footnote index {} out of bounds ({} footnotes)",
            self.context,
            index,
            items.len()
        );
        let item = items[index]
            .as_list_item()
            .expect("Footnote should be a ListItem");
        let actual: String = item
            .text
            .iter()
            .map(|t| t.as_string())
            .collect::<Vec<_>>()
            .join(" ");
        assert_eq!(
            actual, expected,
            "{}: Footnote {} text mismatch",
            self.context, index
        );
        self
    }

    pub fn annotation_count(self, expected: usize) -> Self {
        let actual = self.table.annotations.len();
        assert_eq!(
            actual, expected,
            "{}: Expected {} annotations, got {}",
            self.context, expected, actual
        );
        self
    }

    /// Assert that a cell has (or doesn't have) block content
    pub fn cell_has_block_content(
        self,
        row_type: &str,
        row_idx: usize,
        col: usize,
        expected: bool,
    ) -> Self {
        let rows = match row_type {
            "header" => &self.table.header_rows,
            "body" => &self.table.body_rows,
            _ => panic!("row_type must be 'header' or 'body'"),
        };
        assert!(
            row_idx < rows.len(),
            "{}: {} row {} out of bounds",
            self.context,
            row_type,
            row_idx
        );
        assert!(
            col < rows[row_idx].cells.len(),
            "{}: Cell {} out of bounds in {} row {}",
            self.context,
            col,
            row_type,
            row_idx
        );
        let actual = rows[row_idx].cells[col].has_block_content();
        assert_eq!(
            actual, expected,
            "{}: Expected {}[{}] cell {} has_block_content={}, got {}",
            self.context, row_type, row_idx, col, expected, actual
        );
        self
    }

    /// Assert specific child element within a cell using a callback
    pub fn cell_child(
        self,
        row_type: &str,
        row_idx: usize,
        col: usize,
        child_idx: usize,
        assertion: impl FnOnce(&crate::lex::ast::ContentItem),
    ) -> Self {
        let rows = match row_type {
            "header" => &self.table.header_rows,
            "body" => &self.table.body_rows,
            _ => panic!("row_type must be 'header' or 'body'"),
        };
        let cell = &rows[row_idx].cells[col];
        let children: Vec<&crate::lex::ast::ContentItem> = cell.children.iter().collect();
        assert!(
            child_idx < children.len(),
            "{}: Child index {} out of bounds ({} children in {}[{}] cell {})",
            self.context,
            child_idx,
            children.len(),
            row_type,
            row_idx,
            col
        );
        assertion(children[child_idx]);
        self
    }

    /// Count block children in a cell
    pub fn cell_child_count(
        self,
        row_type: &str,
        row_idx: usize,
        col: usize,
        expected: usize,
    ) -> Self {
        let rows = match row_type {
            "header" => &self.table.header_rows,
            "body" => &self.table.body_rows,
            _ => panic!("row_type must be 'header' or 'body'"),
        };
        let cell = &rows[row_idx].cells[col];
        let actual = cell.children.len();
        assert_eq!(
            actual, expected,
            "{}: Expected {} children in {}[{}] cell {}, got {}",
            self.context, expected, row_type, row_idx, col, actual
        );
        self
    }
}

pub struct RowAssertion<'a> {
    row: &'a crate::lex::ast::TableRow,
    context: String,
}

impl<'a> RowAssertion<'a> {
    pub fn cell_count(self, expected: usize) -> Self {
        let actual = self.row.cells.len();
        assert_eq!(
            actual, expected,
            "{}: Expected {} cells, got {}",
            self.context, expected, actual
        );
        self
    }

    pub fn cell_text(self, col: usize, expected: &str) -> Self {
        assert!(
            col < self.row.cells.len(),
            "{}: Cell index {} out of bounds ({} cells)",
            self.context,
            col,
            self.row.cells.len()
        );
        let actual = self.row.cells[col].content.as_string();
        assert_eq!(
            actual, expected,
            "{}: Cell {} text mismatch",
            self.context, col
        );
        self
    }

    pub fn cell_colspan(self, col: usize, expected: usize) -> Self {
        assert!(col < self.row.cells.len());
        assert_eq!(
            self.row.cells[col].colspan, expected,
            "{}: Cell {} colspan mismatch",
            self.context, col
        );
        self
    }

    pub fn cell_rowspan(self, col: usize, expected: usize) -> Self {
        assert!(col < self.row.cells.len());
        assert_eq!(
            self.row.cells[col].rowspan, expected,
            "{}: Cell {} rowspan mismatch",
            self.context, col
        );
        self
    }

    pub fn cell_align(self, col: usize, expected: TableCellAlignment) -> Self {
        assert!(col < self.row.cells.len());
        assert_eq!(
            self.row.cells[col].align, expected,
            "{}: Cell {} alignment mismatch",
            self.context, col
        );
        self
    }

    pub fn cell_is_header(self, col: usize, expected: bool) -> Self {
        assert!(col < self.row.cells.len());
        assert_eq!(
            self.row.cells[col].header, expected,
            "{}: Cell {} header flag mismatch",
            self.context, col
        );
        self
    }

    pub fn cell_has_block_content(self, col: usize, expected: bool) -> Self {
        assert!(col < self.row.cells.len());
        let actual = self.row.cells[col].has_block_content();
        assert_eq!(
            actual, expected,
            "{}: Cell {} has_block_content mismatch",
            self.context, col
        );
        self
    }

    pub fn cell_child_count(self, col: usize, expected: usize) -> Self {
        assert!(col < self.row.cells.len());
        let actual = self.row.cells[col].children.len();
        assert_eq!(
            actual, expected,
            "{}: Cell {} child count mismatch",
            self.context, col
        );
        self
    }
}