use fours::bot::Difficulty;
use fours::cli;
use fours::cli::CliError;
use fours::cli::Command;
use fours::cli::Options;
use fours::game::BoardDimensions;
use fours::game::GameError;
fn parse(arguments: &[&str]) -> Result<Command, CliError> {
cli::parse(arguments.iter().map(|argument| (*argument).to_owned()))
}
#[track_caller]
fn check_options(arguments: &[&str], expected: Options) {
assert_eq!(parse(arguments), Ok(Command::Play(expected)));
}
#[test]
fn an_empty_command_line_plays_a_standard_game() {
check_options(&[], Options::default());
assert_eq!(Options::default().dimensions, BoardDimensions::STANDARD);
assert_eq!(Options::default().difficulty, Difficulty::default());
assert_eq!(Options::default().seed, None);
}
#[test]
fn a_level_can_be_picked_on_the_command_line() {
for (text, expected) in [
("easy", Difficulty::Easy),
("Medium", Difficulty::Medium),
("HARD", Difficulty::Hard),
] {
check_options(
&["--difficulty", text],
Options {
difficulty: expected,
..Options::default()
},
);
}
}
#[test]
fn a_level_the_game_does_not_have_is_reported() {
let error = parse(&["--difficulty", "impossible"]).expect_err("the level does not exist");
assert!(matches!(error, CliError::InvalidDifficulty { .. }));
assert!(error.to_string().contains("impossible"));
}
#[test]
fn the_board_can_be_resized() {
let dimensions = BoardDimensions::new(10, 8).expect("the size is playable");
check_options(
&["--columns", "10", "--rows", "8"],
Options {
dimensions,
..Options::default()
},
);
}
#[test]
fn the_opponent_can_be_seeded() {
check_options(
&["--seed", "12345"],
Options {
seed: Some(12345),
..Options::default()
},
);
}
#[test]
fn help_and_version_are_asked_for_in_the_usual_ways() {
for argument in ["-h", "--help"] {
assert_eq!(parse(&[argument]), Ok(Command::Help));
}
for argument in ["-V", "--version"] {
assert_eq!(parse(&[argument]), Ok(Command::Version));
}
assert!(cli::HELP.contains("Usage"));
assert!(!cli::NAME.is_empty());
assert!(!cli::VERSION.is_empty());
}
#[test]
fn an_unknown_argument_is_reported() {
assert_eq!(
parse(&["--sideways"]),
Err(CliError::UnknownArgument {
argument: "--sideways".to_owned()
})
);
}
#[test]
fn an_option_without_a_value_is_reported() {
assert_eq!(
parse(&["--columns"]),
Err(CliError::MissingValue {
option: "--columns".to_owned()
})
);
}
#[test]
fn a_value_that_is_not_a_number_is_reported() {
assert_eq!(
parse(&["--rows", "many"]),
Err(CliError::InvalidNumber {
option: "--rows".to_owned(),
value: "many".to_owned()
})
);
}
#[test]
fn a_board_that_cannot_be_played_on_is_reported() {
assert_eq!(
parse(&["--columns", "0"]),
Err(CliError::InvalidBoard(GameError::InvalidDimensions {
column_count: 0,
row_count: 6,
}))
);
}