-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck_test.go
80 lines (64 loc) · 1.89 KB
/
deck_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
package main
import (
"os"
"testing"
)
// test newDeck
func TestNewDeck(t *testing.T) {
d := newDeck()
// should init with 52 cards
if len(d) != 52 {
t.Errorf("Expected deck length of 52, instead received actual %v", len(d))
}
// first card should be ace of spades
if d[0] != "Ace of Spades" {
t.Errorf("Expected first card in new deck to be Ace of Spades, instead received actual %v", d[0])
}
// last card should be king of clubs
if d[len(d)-1] != "King of Clubs" {
t.Errorf("Expected last card in new deck to be King Of Clubs, instead received %v", d[len(d)-1])
}
}
// test saveToFile and loadDeckFromFile
func TestSavingAndLoadingDecks(t *testing.T) {
// init testing environment from scratch
os.Remove("_decktesting")
d := newDeck()
d.saveToFile("_decktesting")
// should save the deck
_, err := os.Open("_decktesting")
if err != nil {
t.Errorf("Expected deck to be saved but instead received error %v ", err)
os.Exit(1)
}
// should load the saved deck
loadedDeck := loadDeckFromFile("_decktesting")
if len(loadedDeck) != 52 {
t.Errorf("Expected 52 cards in loaded deck, instead received actual %v ", len(loadedDeck))
}
// reset testing enviroment
os.Remove("_decktesting")
}
// test Deal
func TestDeal(t *testing.T) {
d := newDeck()
hand, remainingDeck := deal(d, 5)
if len(hand) != 5 {
t.Errorf("Expected dealt hand to have size 5, instead received actual %v ", len(hand))
}
if len(remainingDeck) != 47 {
t.Errorf("Expected dealt hand to have size 47, instead received actual %v ", len(remainingDeck))
}
}
// test shuffle
func TestShuffle(t *testing.T) {
d := newDeck()
firstCard := d[0]
d.shuffle()
firstCardAfterShuffle := d[0]
// this is probably really bad logic to determine a shuffle
if firstCard == firstCardAfterShuffle {
t.Fail()
}
// not sure if I should be saving a reference or not, but it's like 1am and I really don't care about this too much
}