Advertisement
Guest User

Untitled

a guest
Nov 9th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. describe 'Fourth Task' do
  2. describe Card do
  3. it "Returns the card's rank" do
  4. expect(Card.new(2, :spades).rank).to eq 2
  5. end
  6.  
  7. it "Returns the cards suit" do
  8. expect(Card.new(:queen, :diamonds).suit).to eq :diamonds
  9. end
  10.  
  11. it "Converts card to String when card rank is a digit" do
  12. expect(Card.new(10, :hearts).to_s).to eq "10 of Hearts"
  13. end
  14.  
  15. it "Converts card to String when card rank is a symbol" do
  16. expect(Card.new(:ace, :spades).to_s).to eq "Ace of Spades"
  17. end
  18.  
  19. it "Checks that two cards of the same rank and suit are equal" do
  20. expect(Card.new(:queen, :spades) == Card.new(:queen, :spades)).to be true
  21. end
  22.  
  23. it "Checks that two cards of the same rank but different suit are not equal" do
  24. expect(Card.new(10, :hearts) == Card.new(10, :spades)).to be false
  25. end
  26.  
  27. it "Checks that two cards of the same suit but different ranks are not equal" do
  28. expect(Card.new(:ace, :hearts) == Card.new(10, :hearts)).to be false
  29. end
  30. end
  31.  
  32. describe Deck do
  33. DECK_SIZE = 5
  34. def standard_deck
  35. suits = [:clubs, :diamonds, :hearts, :spades]
  36. ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
  37.  
  38. ranks.product(suits).collect {|rank, suit| Card.new(rank, suit)}
  39. end
  40.  
  41. before :each do
  42. @deck = Deck.new(standard_deck.sample DECK_SIZE)
  43. end
  44.  
  45. it "Checks that a standard deck with 52 cards is created implicitly if no arguments are passed to the constructor" do
  46. expect(Deck.new.size).to eq 52
  47. expect(Deck.new.to_a).to eq standard_deck
  48. end
  49.  
  50. it "Checks that deck size decreases when the top card is drawn" do
  51. expect(@deck.size).to eq DECK_SIZE
  52. @deck.draw_top_card
  53. expect(@deck.size).to eq DECK_SIZE - 1
  54. end
  55.  
  56. it "Checks that draw_top_card returns the correct card" do
  57. top_card = @deck.top_card
  58. expect(@deck.draw_top_card).to eq top_card
  59. end
  60.  
  61. it "Checks that top_card does not change the deck size" do
  62. top_card = @deck.top_card
  63. expect(@deck.draw_top_card).to eq top_card
  64. end
  65.  
  66. it "Checks that deck size decreases when the bottom card is drawn" do
  67. @deck.draw_bottom_card
  68. expect(@deck.size).to eq DECK_SIZE - 1
  69. end
  70.  
  71. it "Checks that draw_bottom_card returns the correct card" do
  72. bottom_card = @deck.bottom_card
  73. expect(@deck.draw_bottom_card).to eq bottom_card
  74. end
  75.  
  76. it "Checks that bottom_card does not change the deck size" do
  77. @deck.bottom_card
  78. expect(@deck.size).to eq DECK_SIZE
  79. end
  80.  
  81. it "Checks that deck can be shuffled" do
  82. start_deck = standard_deck.sample(DECK_SIZE)
  83. deck = Deck.new(start_deck)
  84.  
  85. deck.shuffle
  86. expect(deck.to_a).not_to be == start_deck
  87. end
  88.  
  89. it "Checks that deck is sorted correctly" do
  90. sorted_deck = [Card.new(:ace, :spades), Card.new(:king, :hearts),
  91. Card.new(:jack, :hearts), Card.new(10, :diamonds),
  92. Card.new(2, :clubs)
  93. ]
  94. deck = Deck.new(sorted_deck.shuffle)
  95. deck.sort
  96. expect(deck.to_a).to be == sorted_deck
  97. end
  98. end
  99.  
  100. describe WarDeck do
  101. def standard_deck
  102. suits = [:clubs, :diamonds, :hearts, :spades]
  103. ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
  104.  
  105. ranks.product(suits).collect {|rank, suit| Card.new(rank, suit)}
  106. end
  107.  
  108. it "Checks that a war deck has all necessairy cards" do
  109. expect(WarDeck.new.to_a).to match_array standard_deck
  110. end
  111.  
  112. it "Checks that a ward deck has 52 cards" do
  113. expect(WarDeck.new.size).to be 52
  114. end
  115.  
  116. it "Checks that a player is delt 26 cards" do
  117. expect(WarDeck.new.deal.size).to be 26
  118. end
  119.  
  120. it "Checks that a war deck is empty after two hands are delt" do
  121. deck = WarDeck.new
  122. 2.times { deck.deal }
  123. expect(deck.size).to be 0
  124. end
  125.  
  126. it "Checks that a WarDeck can be sorted" do
  127. sorted_deck = [Card.new(:ace, :spades), Card.new(:king, :hearts),
  128. Card.new(:jack, :hearts), Card.new(10, :diamonds),
  129. Card.new(2, :clubs)
  130. ]
  131. deck = WarDeck.new(sorted_deck.shuffle)
  132. deck.sort
  133. expect(deck.to_a).to be == sorted_deck
  134. end
  135. end
  136.  
  137. describe "Object returned by WarDeck#deal" do
  138. def standard_deck
  139. suits = [:clubs, :diamonds, :hearts, :spades]
  140. ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
  141.  
  142. ranks.product(suits).collect {|rank, suit| Card.new(rank, suit)}
  143. end
  144.  
  145. it "Checks that play card returns a card from the player's hand" do
  146. player_deck = WarDeck.new.deal
  147. player_hand_size = player_deck.size
  148.  
  149. expect(standard_deck).to include(player_deck.play_card)
  150. expect(player_deck.size).to be player_hand_size - 1
  151. end
  152.  
  153. it "Checks that player deck can face up when there are 3 or less cards in the player's hand" do
  154. cards = [Card.new(:ace, :spades), Card.new(7, :diamonds),
  155. Card.new(:king, :hearts)]
  156. expect(PlayerWarDeck.new(cards).allow_face_up?).to be true
  157. end
  158.  
  159. it "Chacks that player deck cannot face up when there are more than 3 cards in the player's card" do
  160. cards = [Card.new(:ace, :spades), Card.new(7, :diamonds),
  161. Card.new(:king, :hearts), Card.new(:king, :diamonds)
  162. ]
  163. expect(PlayerWarDeck.new(cards).allow_face_up?).to be false
  164. end
  165. end
  166.  
  167. describe BeloteDeck do
  168. def belote_deck
  169. ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
  170. suits = [:clubs, :diamonds, :hearts, :spades]
  171. ranks.product(suits).map {|rank, suit| Card.new(rank, suit)}
  172. end
  173.  
  174. it "Checks that a belote deck has the correct amout of cards" do
  175. expect(BeloteDeck.new.to_a.size).to be 32
  176. end
  177.  
  178. it "Checks that a belote deck only has the cards needed to play belote" do
  179. expect(BeloteDeck.new.to_a).to match_array belote_deck
  180. end
  181.  
  182. it "Checks that a belote deck can be sorted" do
  183. sorted_deck = [Card.new(:ace, :spades), Card.new(9, :spades),
  184. Card.new(9, :hearts), Card.new(:ace, :diamonds),
  185. Card.new(10, :diamonds), Card.new(:king, :diamonds),
  186. Card.new(8, :clubs)
  187. ]
  188. deck = BeloteDeck.new(sorted_deck.shuffle)
  189. deck.sort
  190. expect(deck.to_a).to be == sorted_deck
  191. end
  192. end
  193.  
  194. describe "Object returned by BeloteDeck#deal" do
  195. it "Checks that highest_of_suit returns the correct card" do
  196. deck = [Card.new(:ace, :spades), Card.new(:king, :spades),
  197. Card.new(9, :spades), Card.new(9, :hearts),
  198. Card.new(:ace, :diamonds), Card.new(10, :diamonds),
  199. Card.new(:king, :diamonds), Card.new(8, :clubs)
  200. ]
  201. player_deck = BeloteDeck.new(deck).deal
  202.  
  203. expect(player_deck.highest_of_suit(:spades)).to be == Card.new(:ace, :spades)
  204. expect(player_deck.highest_of_suit(:hearts)).to be == Card.new(9, :hearts)
  205. expect(player_deck.highest_of_suit(:clubs)).to be == Card.new(8, :clubs)
  206. expect(player_deck.highest_of_suit(:diamonds)).to be == Card.new(:ace, :diamonds)
  207. end
  208.  
  209. it "Checks the hand for belote when it's present" do
  210. deck = [Card.new(:ace, :spades), Card.new(:king, :spades),
  211. Card.new(9, :spades), Card.new(9, :hearts),
  212. Card.new(:ace, :diamonds), Card.new(10, :diamonds),
  213. Card.new(:king, :diamonds), Card.new(:queen, :spades)
  214. ]
  215.  
  216. player_deck = BeloteDeck.new(deck).deal
  217.  
  218. expect(player_deck.belote?).to be true
  219. end
  220.  
  221. it "Checks the hand for belote when it's not present" do
  222. deck = [Card.new(:ace, :spades), Card.new(:king, :spades),
  223. Card.new(9, :spades), Card.new(9, :hearts),
  224. Card.new(:ace, :diamonds), Card.new(10, :diamonds),
  225. Card.new(:king, :diamonds), Card.new(:jack, :spades)
  226. ]
  227.  
  228. player_deck = BeloteDeck.new(deck).deal
  229.  
  230. expect(player_deck.belote?).to be false
  231. end
  232.  
  233. it "Checks the hand for tierce when it's present" do
  234. deck = [Card.new(:ace, :spades), Card.new(10, :spades),
  235. Card.new(:king, :spades), Card.new(8, :diamonds),
  236. Card.new(10, :hearts), Card.new(7, :clubs),
  237. Card.new(:jack, :spades), Card.new(:jack, :diamonds)
  238. ]
  239. player_deck = BeloteDeck.new(deck).deal
  240. expect(player_deck.tierce?).to be true
  241. end
  242.  
  243. it "Checks the hand for tierce when it's present" do
  244. deck = [Card.new(7, :hearts), Card.new(7, :diamonds),
  245. Card.new(7, :spades), Card.new(7, :clubs),
  246. Card.new(8, :hearts), Card.new(8, :diamonds),
  247. Card.new(8, :spades), Card.new(:jack, :diamonds)
  248. ]
  249. player_deck = BeloteDeck.new(deck).deal
  250. expect(player_deck.tierce?).to be false
  251. end
  252.  
  253. it "Checks the hand for carre_of_jacks when they are present" do
  254. deck = [Card.new(:jack, :hearts), Card.new(:jack, :diamonds),
  255. Card.new(:jack, :spades), Card.new(7, :clubs),
  256. Card.new(8, :hearts), Card.new(8, :diamonds),
  257. Card.new(8, :spades), Card.new(:jack, :clubs)
  258. ]
  259. player_deck = BeloteDeck.new(deck).deal
  260. expect(player_deck.carre_of_jacks?).to be true
  261. end
  262.  
  263. it "Checks the hand for carre_of_jacks when they are not present" do
  264. deck = [Card.new(:jack, :hearts), Card.new(:jack, :diamonds),
  265. Card.new(:jack, :spades), Card.new(7, :clubs),
  266. Card.new(8, :hearts), Card.new(8, :diamonds),
  267. Card.new(8, :spades), Card.new(8, :clubs)
  268. ]
  269. player_deck = BeloteDeck.new(deck).deal
  270. expect(player_deck.carre_of_jacks?).to be false
  271. end
  272. end
  273. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement