Skip to main content

max / shop

1.6 KB · 59 lines History Blame Raw
1 //! Helpers the inline test modules share.
2 //!
3 //! Compiled only for tests, and deliberately without a test module of its own:
4 //! the mutation selector picks a file by the test attribute in it, and test
5 //! helpers are not something to mutate. The modules that use these carry the
6 //! attribute instead.
7
8 use crate::{Grid, Identity};
9 use shop_vt::Parser;
10
11 /// Feed `bytes` through a fresh Parser into `grid`.
12 pub(crate) fn feed(grid: &mut Grid, bytes: &[u8]) {
13 let mut p = Parser::new();
14 p.advance(grid, bytes);
15 }
16
17 pub(crate) fn row_str(grid: &Grid, r: u16) -> String {
18 grid.row(r)
19 .iter()
20 .map(crate::Cell::c)
21 .collect::<String>()
22 .trim_end()
23 .to_string()
24 }
25
26 pub(crate) fn assert_cursor(grid: &Grid, row: u16, col: u16) {
27 let c = grid.cursor();
28 assert_eq!(
29 (c.row, c.col),
30 (row, col),
31 "cursor mismatch (wrap_next={})",
32 c.wrap_next
33 );
34 }
35
36 pub(crate) fn identified() -> Grid {
37 let mut g = Grid::new(80, 24);
38 g.set_identity(Identity {
39 name: "shop".into(),
40 version: "1.2.3".into(),
41 cell_px: (9, 20),
42 fg: [0xe6, 0xde, 0xd3],
43 bg: [0x25, 0x23, 0x1f],
44 });
45 g
46 }
47
48 pub(crate) fn reply_to(g: &mut Grid, bytes: &[u8]) -> String {
49 feed(g, bytes);
50 String::from_utf8(g.take_pending_replies()).unwrap()
51 }
52
53 /// Everything in scrollback, as text. The round-trip property is stated
54 /// over this rather than over rows, because the rows are exactly what a
55 /// rewrap is allowed to change.
56 pub(crate) fn history_text(g: &Grid) -> String {
57 g.text_range(0, g.history_len())
58 }
59