Add rstest crate and add more tests

This commit is contained in:
stefiosif
2025-02-03 20:42:30 +02:00
parent ea6800183d
commit abfa6d3c1f
3 changed files with 280 additions and 78 deletions

View File

@@ -251,98 +251,70 @@ impl Move {
#[cfg(test)]
mod tests {
use super::Move;
use crate::board::fen::from_fen;
use crate::board::square::Square;
use crate::movegen::r#move::MoveType;
use super::Move;
use rstest::rstest;
const FEN: &'static str = "1r2k2r/2P1pq1p/2npb3/1p3ppP/p3P3/P2B1Q2/1P1PNPP1/R3K2R w KQk g6 0 1";
#[test]
fn test_parse_from_str_quiet() -> Result<(), String> {
let game = from_fen(FEN)?;
let mv_str = "b2b3";
let actual = Move::parse_from_str(&game, &mv_str)?;
let expected = Move::new(Square::B2, Square::B3);
#[rstest]
#[case::quiet("b2b3")]
#[case::double_push("b2b4")]
#[case::king_castle("e1g1")]
#[case::queen_castle("e1c1")]
#[case::capture("d3b5")]
#[case::en_passant("h5g6")]
#[case::promotion_knight("c7c8n")]
#[case::promotion_bishop("c7c8b")]
#[case::promotion_rook("c7c8r")]
#[case::promotion_queen("c7c8q")]
#[case::promotion_capture_knight("c7b8n")]
#[case::promotion_capture_bishop("c7b8b")]
#[case::promotion_capture_rook("c7b8r")]
#[case::promotion_capture_queen("c7b8q")]
fn test_parse_from_str(#[case] mv_str: &str) {
use crate::movegen::r#move::Move;
let game = from_fen(FEN).unwrap();
let actual = Move::parse_from_str(&game, mv_str).unwrap();
let expected = match mv_str {
"b2b3" => Move::new_with_type(Square::B2, Square::B3, MoveType::Quiet),
"b2b4" => Move::new_with_type(Square::B2, Square::B4, MoveType::DoublePush),
"e1g1" => Move::new_with_type(Square::E1, Square::G1, MoveType::KingCastle),
"e1c1" => Move::new_with_type(Square::E1, Square::C1, MoveType::QueenCastle),
"d3b5" => Move::new_with_type(Square::D3, Square::B5, MoveType::Capture),
"h5g6" => Move::new_with_type(Square::H5, Square::G6, MoveType::EnPassant),
"c7c8n" => Move::new_with_type(Square::C7, Square::C8, MoveType::PromotionKnight),
"c7c8b" => Move::new_with_type(Square::C7, Square::C8, MoveType::PromotionBishop),
"c7c8r" => Move::new_with_type(Square::C7, Square::C8, MoveType::PromotionRook),
"c7c8q" => Move::new_with_type(Square::C7, Square::C8, MoveType::PromotionQueen),
"c7b8n" => Move::new_with_type(Square::C7, Square::B8, MoveType::PromotionCaptureKnight),
"c7b8b" => Move::new_with_type(Square::C7, Square::B8, MoveType::PromotionCaptureBishop),
"c7b8r" => Move::new_with_type(Square::C7, Square::B8, MoveType::PromotionCaptureRook),
"c7b8q" => Move::new_with_type(Square::C7, Square::B8, MoveType::PromotionCaptureQueen),
_ => unreachable!(),
};
assert_eq!(expected, actual);
Ok(())
}
#[test]
fn test_parse_from_str_capture() -> Result<(), String> {
let game = from_fen(FEN)?;
let mv_str = "d3b5";
let actual = Move::parse_from_str(&game, &mv_str)?;
let expected = Move::new_with_type(Square::D3, Square::B5, MoveType::Capture);
assert_eq!(expected, actual);
Ok(())
}
#[test]
fn test_parse_from_str_en_passant() -> Result<(), String> {
let game = from_fen(FEN)?;
let mv_str = "h5g6";
let actual = Move::parse_from_str(&game, &mv_str)?;
let expected = Move::new_with_type(Square::H5, Square::G6, MoveType::EnPassant);
assert_eq!(expected, actual);
Ok(())
}
#[test]
fn test_parse_from_str_double_push() -> Result<(), String> {
let game = from_fen(FEN)?;
let mv_str = "b2b4";
let actual = Move::parse_from_str(&game, &mv_str)?;
let expected = Move::new_with_type(Square::B2, Square::B4, MoveType::DoublePush);
assert_eq!(expected, actual);
Ok(())
}
#[test]
fn test_parse_from_str_promotion() -> Result<(), String> {
let game = from_fen(FEN)?;
let mv_str = "c7c8q";
let actual = Move::parse_from_str(&game, &mv_str)?;
let expected = Move::new_with_type(Square::C7, Square::C8, MoveType::PromotionQueen);
assert_eq!(expected, actual);
Ok(())
}
#[test]
fn test_parse_from_str_promotion_capture() -> Result<(), String> {
let game = from_fen(FEN)?;
let mv_str = "c7b8q";
let actual = Move::parse_from_str(&game, &mv_str)?;
let expected = Move::new_with_type(Square::C7, Square::B8, MoveType::PromotionCaptureQueen);
assert_eq!(expected, actual);
Ok(())
}
#[test]
#[rstest]
#[case("c7c8qq")]
#[should_panic(expected = "Invalid move characters length")]
fn test_parse_from_str_panic_1() {
fn test_parse_from_str_panic_invalid_char_length(#[case] mv_str: &str) {
let game = from_fen(FEN);
let mv_str = "c7c8qq";
Move::parse_from_str(&game.unwrap(), &mv_str).unwrap();
}
#[test]
#[rstest]
#[case("c7c8w")]
#[should_panic(expected = "Unrecongisable character in promotion piece index")]
fn test_parse_from_str_panic_2() -> () {
let game = from_fen(FEN);
let mv_str = "c7c8w";
fn test_parse_from_str_panic_invalid_promo_char(#[case] mv_str: &str) {
let game: Result<crate::board::game::Game, String> = from_fen(FEN);
Move::parse_from_str(&game.unwrap(), &mv_str).unwrap();
}
}