You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
2.8 KiB
Go

2 years ago
package deck
import (
"testing"
"fmt"
)
func showDeck(d []Card) {
fmt.Println("Showing whole deck")
for _, card := range d {
fmt.Println(card)
}
}
func TestPrintCard(t *testing.T) {
tests := []struct{
input Card
want string
}{
{Card{Hearts, Queen}, "Queen of Hearts"},
{Card{Suit: Joker}, "Joker"},
}
for _, test := range tests {
if test.input.String() != test.want {
t.Errorf("Got %s; wanted %s", test.input.String(), test.want)
}
}
}
func TestNew(t *testing.T) {
d := New()
c := Card{Diamonds, Six}
if d[18] != c {
t.Errorf("Deck's 5th card should be %s, is %s", c, d[18])
showDeck(d)
}
}
func TestSortByRank(t *testing.T) {
d := New(SortByRank)
c := Card{Clubs, Five}
if d[18] != c {
t.Errorf("Deck's 18th card should be %s, is %s", c, d[18])
showDeck(d)
}
}
func TestSortBySuit(t *testing.T) {
d := New()
d = SortByRank(d)
d = SortBySuit(d)
c := Card{Diamonds, Six}
if d[18] != c {
t.Errorf("Deck's 5th card should be %s, is %s", c, d[18])
showDeck(d)
}
}
func TestAddJokers(t *testing.T) {
d := New()
initLength := len(d)
d = AddNJokers(17)(d)
if initLength + 17 != len(d) {
t.Errorf("Wrong number of cards in deck after adding Jokers")
showDeck(d)
}
}
func TestRemoveCard(t *testing.T) {
d := New(RemoveCards(Card{Spades, Ace}))
for _, card := range d {
if card == (Card{Spades, Ace}) {
t.Error("The ace of spades is still in the deck after calling RemoveCard(Card{Spades, Ace})")
}
}
before := len(d)
d = RemoveCards(Card{Clubs, Jack})(d)
if len(d) != (before - 1) {
t.Error("Remove() didn't reduce card count by 1")
}
}
func TestRemoveRank(t *testing.T) {
d := New(RemoveRank(Seven))
for _, card := range d {
if card.Rank == Seven {
t.Error("There are still Sevens in the deck after calling RemoveRank(7)")
}
}
}
func TestRemoveSuit(t *testing.T) {
d := New(RemoveSuit(Hearts))
for _, card := range d {
if card.Suit == Hearts {
t.Error("There are still Hearts in the deck after calling RemoveSuit(Hearts)")
}
}
}
func TestAddDeck(t *testing.T) {
d := New()
before := len(d)
d = AddDeck(d)
if len(d) != before*2 {
t.Error("AddDeck didn't double default deck size")
}
}
func TestDraw(t *testing.T) {
d := New()
lenBefore := len(d)
firstTwo := d[0:2]
hand := make([]Card, 0, 5)
hand, d = Draw(hand, d)
hand, d = Draw(hand, d)
if firstTwo[0] != hand[0] {
t.Errorf("The first drawn card (got %s) should be the first in the deck (was %s)\n",
hand[0], firstTwo[0])
}
if firstTwo[1] != hand[1] {
t.Errorf("The second drawn card (got %s) should be the second in the deck (was %s)\n",
hand[0], firstTwo[0])
}
if len(d) != lenBefore - 2 {
t.Errorf(`Expected length of deck to be reduced by two when calling Draw() twice,
length before is %d, and length after is %d`, lenBefore, len(d))
}
}