mod support;
use fours::app::Action;
use fours::app::App;
use fours::app::Theme;
use fours::app::layout;
use fours::app::layout::Scale;
use fours::bot::Difficulty;
use fours::game::BoardDimensions;
use fours::game::Cell;
use fours::game::Game;
use fours::game::Player;
use ratatui::layout::Rect;
use ratatui::style::Modifier;
use support::ScriptedBot;
use support::app_with_script;
use support::check_centered;
use support::disc_style;
use support::drawn_bounds;
use support::find_symbol;
use support::geometry_of;
use support::render;
use support::render_fitting;
use support::text_of;
const SELECTION_MARK: &str = "▼";
const ROOMY_TERMINALS: [(u16, u16); 5] = [(80, 24), (120, 40), (58, 21), (100, 31), (200, 60)];
#[test]
fn the_interface_sits_in_the_middle_of_the_terminal() {
for (width, height) in ROOMY_TERMINALS {
let app = app_with_script(&[]);
let buffer = render(&app, width, height);
check_centered(Rect::new(0, 0, width, height), drawn_bounds(&buffer));
}
}
#[test]
fn the_board_sits_in_the_middle_of_the_interface() {
for columns in 1..=10 {
for rows in 1..=10 {
let dimensions = BoardDimensions::new(columns, rows).expect("the size is playable");
let app =
support::playing_against(Game::with_dimensions(dimensions), ScriptedBot::default());
let buffer = render_fitting(&app);
let geometry = geometry_of(&app, &buffer);
check_centered(geometry.content, geometry.board);
check_centered(buffer.area, geometry.content);
}
}
}
#[test]
fn every_part_of_the_interface_shares_one_centre() {
let app = app_with_script(&[]);
let buffer = render_fitting(&app);
let geometry = geometry_of(&app, &buffer);
for part in [
geometry.title,
geometry.indicator,
geometry.notice,
geometry.status,
geometry.help,
] {
assert_eq!(part.x, geometry.content.x);
assert_eq!(part.width, geometry.content.width);
assert_eq!(part.height, 1);
}
}
#[test]
fn the_parts_of_the_interface_do_not_overlap() {
let app = app_with_script(&[]);
let geometry = geometry_of(&app, &render_fitting(&app));
let parts = [
geometry.title,
geometry.indicator,
geometry.board,
geometry.notice,
geometry.status,
geometry.help,
];
for pair in parts.windows(2) {
assert!(
pair[0].bottom() <= pair[1].y,
"{:?} overlaps {:?}",
pair[0],
pair[1]
);
}
assert_eq!(
parts.last().map(|part| part.bottom()),
Some(geometry.content.bottom())
);
}
#[test]
fn the_slots_of_the_board_form_a_grid() {
let app = app_with_script(&[]);
let geometry = geometry_of(&app, &render_fitting(&app));
let state = app.state();
for row in 0..state.row_count() {
for column in 0..state.column_count() {
let cell = geometry
.cell(Cell::new(column, row))
.expect("the cell is on the board");
let first_of_row = geometry
.cell(Cell::new(0, row))
.expect("the cell is on the board");
let first_of_column = geometry
.cell(Cell::new(column, 0))
.expect("the cell is on the board");
assert_eq!(cell.y, first_of_row.y, "rows are not aligned");
assert_eq!(cell.x, first_of_column.x, "columns are not aligned");
assert_eq!(cell.width, geometry.scale().cell_width());
assert_eq!(cell.height, geometry.scale().cell_height());
assert!(
geometry.board.contains(ratatui::layout::Position::new(
cell.right() - 1,
cell.bottom() - 1
)),
"the cell reaches outside the board"
);
}
}
}
#[test]
fn rows_are_stacked_from_the_bottom_of_the_board() {
let app = app_with_script(&[]);
let geometry = geometry_of(&app, &render_fitting(&app));
let bottom = geometry.cell(Cell::new(0, 0)).expect("the board has a row");
let above = geometry.cell(Cell::new(0, 1)).expect("the board has a row");
assert_eq!(above.y + geometry.scale().cell_height(), bottom.y);
assert_eq!(bottom.bottom() + 1, geometry.board.bottom());
}
#[test]
fn discs_are_drawn_where_they_were_played() {
let theme = Theme::default();
let mut app = app_with_script(&[0, 0, 0]);
app.handle(Action::Choose(3));
app.handle(Action::Confirm);
app.handle(Action::Confirm);
let buffer = render_fitting(&app);
let geometry = geometry_of(&app, &buffer);
for cell in board_cells(&app) {
let (colour, _) = disc_style(&buffer, &geometry, cell);
let expected = app.state().piece_at(cell).map(|player| theme.disc(player));
if let Some(expected) = expected {
assert_eq!(colour, expected, "wrong colour at {cell}");
} else if app.state().landing_cell(app.selected_column()) != Some(cell) {
assert_ne!(colour, theme.disc(Player::Red), "a disc appeared at {cell}");
assert_ne!(
colour,
theme.disc(Player::Yellow),
"a disc appeared at {cell}"
);
}
}
}
#[test]
fn the_selected_column_is_marked_above_the_board() {
let mut app = app_with_script(&[]);
for column in 0..app.state().column_count() {
app.handle(Action::Choose(column));
let buffer = render_fitting(&app);
let geometry = geometry_of(&app, &buffer);
let mark = find_symbol(&buffer, geometry.indicator, SELECTION_MARK);
assert_eq!(
mark.map(|(x, _)| x),
geometry.column_middle(column),
"the mark is not above column {column}"
);
}
}
#[test]
fn the_selected_column_shows_where_a_disc_would_land() {
let theme = Theme::default();
let mut app = app_with_script(&[3, 3, 3]);
app.handle(Action::Choose(3));
for _ in 0..2 {
let buffer = render_fitting(&app);
let geometry = geometry_of(&app, &buffer);
let landing = app
.state()
.landing_cell(3)
.expect("the column has room left");
let (colour, modifier) = disc_style(&buffer, &geometry, landing);
assert_eq!(colour, theme.disc(app.human_player()));
assert!(modifier.contains(Modifier::DIM), "the preview is not faint");
assert_eq!(app.state().piece_at(landing), None);
app.handle(Action::Confirm);
}
}
#[test]
fn a_finished_game_shows_no_selection() {
let mut app = app_with_script(&[6, 6, 6]);
for _ in 0..4 {
app.handle(Action::Choose(0));
app.handle(Action::Confirm);
}
let buffer = render_fitting(&app);
let geometry = geometry_of(&app, &buffer);
assert_eq!(app.state().winner(), Some(Player::Red));
assert_eq!(
find_symbol(&buffer, geometry.indicator, SELECTION_MARK),
None
);
}
#[test]
fn the_winning_line_stands_out() {
let mut app = app_with_script(&[6, 6, 6]);
for _ in 0..4 {
app.handle(Action::Choose(0));
app.handle(Action::Confirm);
}
let buffer = render_fitting(&app);
let geometry = geometry_of(&app, &buffer);
let winning: Vec<Cell> = (0..4).map(|row| Cell::new(0, row)).collect();
for cell in board_cells(&app) {
let (_, modifier) = disc_style(&buffer, &geometry, cell);
let is_winning = winning.contains(&cell);
let holds_a_disc = app.state().piece_at(cell).is_some();
if holds_a_disc && !is_winning {
assert!(modifier.contains(Modifier::DIM), "{cell} should step back");
}
if is_winning {
assert!(!modifier.contains(Modifier::DIM), "{cell} should stand out");
}
}
}
#[test]
fn the_status_line_follows_the_game() {
let mut app = app_with_script(&[6, 6, 6]);
let read_status = |app: &App| {
let buffer = render_fitting(app);
let geometry = geometry_of(app, &buffer);
text_of(&buffer, geometry.status)
};
assert_eq!(read_status(&app), "Your turn");
for _ in 0..4 {
app.handle(Action::Choose(0));
app.handle(Action::Confirm);
}
assert_eq!(read_status(&app), "You win!");
}
#[test]
fn a_remark_appears_when_a_move_is_refused() {
let full_column = support::play(&[0, 0, 0, 0, 0, 0]);
let mut app = support::playing_against(full_column, ScriptedBot::new(&[1]));
let read_notice = |app: &App| {
let buffer = render_fitting(app);
let geometry = geometry_of(app, &buffer);
text_of(&buffer, geometry.notice)
};
assert_eq!(read_notice(&app), "");
app.handle(Action::Choose(0));
app.handle(Action::Confirm);
assert_eq!(read_notice(&app), "That column is full, pick another one.");
app.handle(Action::Previous);
assert_eq!(read_notice(&app), "");
}
#[test]
fn the_title_and_the_key_bindings_are_always_there() {
let app = app_with_script(&[]);
let buffer = render_fitting(&app);
let geometry = geometry_of(&app, &buffer);
assert_eq!(text_of(&buffer, geometry.title), layout::TITLE);
assert_eq!(text_of(&buffer, geometry.help), layout::HELP);
}
#[test]
fn a_terminal_that_is_too_small_says_so() {
let app = app_with_script(&[]);
let (width, height) = layout::required_size(app.state().dimensions());
for (narrow, short) in [(width - 1, height), (width, height - 1), (30, 4), (20, 3)] {
let buffer = render(&app, narrow, short);
let text: String = support::lines(&buffer).join(" ");
assert!(
text.contains("too small"),
"a {narrow} by {short} terminal should ask for more room, it showed {text:?}"
);
}
for (narrow, short) in [(1, 1), (2, 1), (1, 20), (12, 2)] {
render(&app, narrow, short);
}
}
#[test]
fn a_terminal_that_is_just_big_enough_shows_the_board() {
let app = app_with_script(&[]);
let (width, height) = layout::required_size(app.state().dimensions());
let buffer = render(&app, width, height);
let geometry = geometry_of(&app, &buffer);
assert_eq!(geometry.content, Rect::new(0, 0, width, height));
assert_eq!(text_of(&buffer, geometry.title), layout::TITLE);
}
#[test]
fn every_board_size_can_be_drawn() {
for columns in [1, 2, 4, 7, 12, 20] {
for rows in [1, 2, 4, 6, 11, 20] {
let dimensions = BoardDimensions::new(columns, rows).expect("the size is playable");
let mut app =
support::playing_against(Game::with_dimensions(dimensions), ScriptedBot::default());
for _ in 0..3 {
app.handle(Action::Confirm);
app.handle(Action::Next);
}
let buffer = render_fitting(&app);
let geometry = geometry_of(&app, &buffer);
check_centered(geometry.content, geometry.board);
assert_eq!(text_of(&buffer, geometry.help), layout::HELP);
}
}
}
fn board_cells(app: &App) -> Vec<Cell> {
let state = app.state();
(0..state.row_count())
.flat_map(|row| (0..state.column_count()).map(move |column| Cell::new(column, row)))
.collect()
}
#[test]
fn a_game_says_which_opponent_it_is_against() {
let mut app = App::seeded(BoardDimensions::STANDARD, 2);
for level in Difficulty::ALL {
app.set_difficulty(level);
let buffer = render_fitting(&app);
let geometry = geometry_of(&app, &buffer);
let subtitle = text_of(&buffer, geometry.subtitle);
assert!(
subtitle.contains(level.name()),
"{subtitle:?} misses {level}"
);
assert_eq!(subtitle, layout::opponent_text(level));
}
}
#[test]
fn the_board_grows_when_the_terminal_has_room() {
let app = app_with_script(&[]);
let dimensions = app.state().dimensions();
let (narrow, short) = layout::required_size(dimensions);
let small = geometry_of(&app, &render(&app, narrow, short));
let (wide, tall) = layout::required_size_at(dimensions, Scale::Large);
let large = geometry_of(&app, &render(&app, wide, tall));
assert_eq!(small.scale(), Scale::Normal);
assert_eq!(large.scale(), Scale::Large);
assert!(
large.board.width > small.board.width,
"the board did not widen"
);
assert!(
large.board.height > small.board.height,
"the board did not grow"
);
}
#[test]
fn the_board_only_grows_when_the_bigger_one_fits() {
let wide_board = BoardDimensions::new(12, 6).expect("the size is playable");
let app = support::playing_against(Game::with_dimensions(wide_board), ScriptedBot::default());
let (wide, tall) = layout::required_size_at(wide_board, Scale::Large);
for (width, height) in [(wide - 1, tall), (wide, tall - 1)] {
let geometry = geometry_of(&app, &render(&app, width, height));
assert_eq!(
geometry.scale(),
Scale::Normal,
"a {width} by {height} terminal should keep the smaller board"
);
}
}
#[test]
fn the_interface_stays_centred_at_every_size() {
let app = app_with_script(&[]);
let (narrowest, shortest) = layout::required_size(app.state().dimensions());
for spare_width in [0, 1, 5, 22, 43, 82] {
for spare_height in [0, 1, 5, 6, 10, 21] {
let buffer = render(&app, narrowest + spare_width, shortest + spare_height);
let geometry = geometry_of(&app, &buffer);
check_centered(buffer.area, drawn_bounds(&buffer));
check_centered(geometry.content, geometry.board);
}
}
}
#[test]
fn discs_are_drawn_where_they_were_played_at_every_scale() {
let theme = Theme::default();
let dimensions = BoardDimensions::STANDARD;
for scale in Scale::ALL {
let mut app = app_with_script(&[0, 0, 0]);
app.handle(Action::Choose(3));
app.handle(Action::Confirm);
app.handle(Action::Confirm);
let (width, height) = layout::required_size_at(dimensions, scale);
let buffer = render(&app, width, height);
let geometry = geometry_of(&app, &buffer);
assert_eq!(geometry.scale(), scale);
for cell in board_cells(&app) {
let (colour, _) = disc_style(&buffer, &geometry, cell);
if let Some(player) = app.state().piece_at(cell) {
assert_eq!(
colour,
theme.disc(player),
"wrong colour at {cell} at {scale:?}"
);
}
}
}
}