use super::super::Symbol;
fn cells(line: &str) -> Vec<char> {
line.trim_start_matches("\u{1b}[30;47m")
.trim_end_matches("\u{1b}[0m")
.chars()
.collect()
}
fn as_modules(block: &str) -> Vec<Vec<bool>> {
block
.lines()
.flat_map(|line| {
let row = cells(line);
let half = |want: [char; 2]| {
row.iter()
.map(|glyph| want.contains(glyph))
.collect::<Vec<bool>>()
};
[
half(['\u{2588}', '\u{2580}']),
half(['\u{2588}', '\u{2584}']),
]
})
.collect()
}
#[test]
fn the_block_reads_back_as_the_symbol_inside_its_quiet_zone() {
let symbol = Symbol::encode(b"a block of bytes").expect("it fits");
let read = as_modules(&symbol.block());
let quiet = 4;
let span = symbol.side() + 2 * quiet;
assert!(
read.len() >= span,
"the block is at least the symbol's span"
);
for (y, row) in read.iter().enumerate() {
assert_eq!(row.len(), span, "row {y} is the full span");
for (x, &module) in row.iter().enumerate() {
let inside = (quiet..quiet + symbol.side()).contains(&x)
&& (quiet..quiet + symbol.side()).contains(&y);
let want = inside && symbol.dark(x - quiet, y - quiet);
assert_eq!(module, want, "at ({x}, {y})");
}
}
}
#[test]
fn the_span_s_odd_last_row_is_drawn_as_paper() {
let symbol = Symbol::encode(b"odd").expect("it fits");
let span = symbol.side() + 8;
assert_eq!(span % 2, 1, "the span is odd at every version");
let read = as_modules(&symbol.block());
assert_eq!(read.len(), span + 1, "the half cell is a whole row");
assert!(
read.last().is_some_and(|row| row.iter().all(|&m| !m)),
"and it is light"
);
}
#[test]
fn every_line_sets_and_clears_its_own_colours() {
let block = Symbol::encode(b"paper").expect("it fits").block();
assert!(
block
.lines()
.all(|line| line.starts_with("\u{1b}[30;47m") && line.ends_with("\u{1b}[0m")),
"a line was drawn without paper"
);
assert!(block.ends_with('\n'), "and the last line is terminated");
}
#[test]
fn all_four_cell_glyphs_are_drawn() {
let block = Symbol::encode(b"glyphs, all of them")
.expect("it fits")
.block();
for glyph in ['\u{2588}', '\u{2580}', '\u{2584}', ' '] {
assert!(block.contains(glyph), "no {glyph:?} anywhere in the block");
}
}