Skip to content

Commit

Permalink
Implemented Macros
Browse files Browse the repository at this point in the history
  • Loading branch information
davassi committed Sep 16, 2023
1 parent b1be043 commit 53dce72
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl Hand {
&self.hand
}

/// An handy constructor for tests
/// An handy constructor for tests and macros
///
pub fn new(hand: [Card; 5]) -> Hand {
Hand { hand }
Expand Down Expand Up @@ -270,10 +270,11 @@ macro_rules! newcard {
};
}

macro_rules! newhand {
#[macro_export]
macro_rules! hand {

($c:expr,$c1:expr,$c2:expr,$c3:expr,$c4:expr) => {
[newcard![$c],newcard![$c1],newcard![$c2],newcard![$c3],newcard![$c4]]
Hand::new([newcard![$c],newcard![$c1],newcard![$c2],newcard![$c3],newcard![$c4]])
};
}

Expand All @@ -299,7 +300,7 @@ mod tests {
#[test]
fn test_try_from_valid_hand() {

let hand = newhand!["Ad","Kd","Qd","Jd","10d"];
assert_eq!(hand[0], Card::new(14, Suit::Diamonds));
let hand = hand!["Ad","Kd","Qd","Jd","10d"];
assert_eq!(hand.hand[0], Card::new(14, Suit::Diamonds));
}
}
17 changes: 7 additions & 10 deletions src/match_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,21 @@ impl MatchHandEvaluator {
}
}


macro_rules! assert_rank {
($hand:expr, $rank:expr) => {
assert_eq!(MatchHandEvaluator::slow_eval(&mut $hand), $rank);
};
}

#[cfg(test)]
mod test {
use super::MatchHandEvaluator;
use crate::card::{Card, Hand, Rank, Suit, self};
use crate::newcard;
use crate::hand;

#[test]
fn rank_royal_flush() {
let cards = [
newcard!["Ah"],
newcard!["Kh"],
newcard!["Jh"],
newcard!["Qh"],
newcard!["10h"],
];
let mut hand = Hand::new(cards);
assert_eq!(MatchHandEvaluator::slow_eval(&mut hand), Rank::RoyalFlush);
assert_rank!(hand!["Ad","Kd","Qd","Jd","10d"], Rank::RoyalFlush);
}
}

0 comments on commit 53dce72

Please sign in to comment.