-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_test.go
81 lines (71 loc) · 1.48 KB
/
card_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package deck
import (
"fmt"
"testing"
)
func ExampleCard() {
fmt.Println(Card{Rank: Ace, Suit: Heart})
fmt.Println(Card{Rank: Jack, Suit: Club})
fmt.Println(Card{Rank: Two, Suit: Diamond})
fmt.Println(Card{Suit: Joker})
// Output:
// Ace of Hearts
// Jack of Clubs
// Two of Diamonds
// Joker
}
func TestNew(t *testing.T) {
cards := New()
// 13 ranks * 4 suits
if len(cards) != 13*4 {
t.Error("wrong number of cards in a new deck")
}
}
func TestDefaultSort(t *testing.T) {
cards := New(DefaultSort)
exp := Card{Rank: Ace, Suit: Spade}
if cards[0] != exp {
t.Log(cards)
t.Error("expected ace of spades received")
}
}
func TestSort(t *testing.T) {
cards := New(Sort(Less))
exp := Card{Rank: Ace, Suit: Spade}
if cards[0] != exp {
t.Log(cards)
t.Error("expected ace of spades received")
}
}
func TestJokers(t *testing.T) {
cards := New(Jokers(2))
count := 0
for _, c := range cards {
{
if c.Suit == Joker {
count++
}
}
}
if count != 3 {
t.Errorf("execpted 3 jokers got:%d", count)
}
}
func TestFilter(t *testing.T) {
filter := func(card Card) bool {
return card.Rank == Two || card.Rank == Three
}
cards := New(Filter(filter))
for _, c := range cards {
if c.Rank == Two || c.Rank == Three {
t.Error("expected all twos and threes to be filtered out")
}
}
}
func TestDeck(t *testing.T) {
cards := New(Deck(3))
// 13 ranks * 4 suits * 3 decks
if len(cards) != 13*4*3 {
t.Errorf("exected %d cards, received %d cards", 13*4*3, len(cards))
}
}