Add string serialization and separate tests

This commit is contained in:
Hamcha 2019-05-31 14:07:49 +02:00
parent 38d36b6306
commit 1a3d72a006
Signed by: hamcha
GPG Key ID: A40413D21021EAEE
2 changed files with 44 additions and 20 deletions

14
pack.go
View File

@ -1,6 +1,9 @@
package draft // import "git.fromouter.space/mcg/draft" package draft // import "git.fromouter.space/mcg/draft"
import "math/rand" import (
"math/rand"
"strings"
)
// Pack is a collection of cards from a booster pack // Pack is a collection of cards from a booster pack
type Pack []Card type Pack []Card
@ -66,3 +69,12 @@ func MakePackWithSchema(schema PackSchema) Pack {
} }
return pack return pack
} }
// String encodes a pack to a list of space-separated IDs
func (p Pack) String() (str string) {
for _, card := range p {
str += " " + card.ID
}
str = strings.TrimSpace(str)
return
}

View File

@ -1,21 +1,23 @@
package draft package draft_test
import ( import (
"fmt" "fmt"
"math/rand" "math/rand"
"testing" "testing"
"git.fromouter.space/mcg/draft"
) )
// TestSetRepeatable makes sure that a set can generate more cards than it contains // TestSetRepeatable makes sure that a set can generate more cards than it contains
func TestSetRepeatable(t *testing.T) { func TestSetRepeatable(t *testing.T) {
const PACKSIZE = 5 const PACKSIZE = 5
s := &GenericSet{ s := &draft.GenericSet{
Cards: []Card{{ID: "a"}, {ID: "b"}, {ID: "c"}}, Cards: []draft.Card{{ID: "a"}, {ID: "b"}, {ID: "c"}},
PackSize: PACKSIZE, PackSize: PACKSIZE,
} }
// Create a pack // Create a pack
pack := MakePack(s) pack := draft.MakePack(s)
if len(pack) < PACKSIZE { if len(pack) < PACKSIZE {
t.Errorf("Pack expected to contain %d cards, contains %d", PACKSIZE, len(pack)) t.Errorf("Pack expected to contain %d cards, contains %d", PACKSIZE, len(pack))
@ -34,17 +36,17 @@ func TestSetRepeatable(t *testing.T) {
// TestAlternateProviders tests alternate providers // TestAlternateProviders tests alternate providers
func TestAlternateProviders(t *testing.T) { func TestAlternateProviders(t *testing.T) {
customProvider := func(n int) []Card { customProvider := func(n int) []draft.Card {
out := make([]Card, n) out := make([]draft.Card, n)
for n := range out { for n := range out {
out[n] = Card{ID: "x"} out[n] = draft.Card{ID: "x"}
} }
return out return out
} }
s := &GenericSet{ s := &draft.GenericSet{
Cards: []Card{{ID: "a"}, {ID: "b"}, {ID: "c"}}, Cards: []draft.Card{{ID: "a"}, {ID: "b"}, {ID: "c"}},
PackSize: 1, PackSize: 1,
Alternates: []AlternateProvider{ Alternates: []draft.AlternateProvider{
{ {
Probability: 0.3, Probability: 0.3,
Provider: customProvider, Provider: customProvider,
@ -56,7 +58,7 @@ func TestAlternateProviders(t *testing.T) {
nonalternates, alternates := 0, 0 nonalternates, alternates := 0, 0
for i := 0; i < 500000; i++ { for i := 0; i < 500000; i++ {
pack := MakePack(s) pack := draft.MakePack(s)
if pack[0].ID == "x" { if pack[0].ID == "x" {
alternates++ alternates++
} else { } else {
@ -76,16 +78,16 @@ func TestAlternateProviders(t *testing.T) {
// TestCubeOverflow makes sure cubes stop providing cards as they are exhausted instead of going out of bound // TestCubeOverflow makes sure cubes stop providing cards as they are exhausted instead of going out of bound
func TestCubeOverflow(t *testing.T) { func TestCubeOverflow(t *testing.T) {
const PACKSIZE = 5 const PACKSIZE = 5
c := &GenericCube{ c := &draft.GenericCube{
Cards: []Card{ Cards: []draft.Card{
{ID: "a"}, {ID: "b"}, {ID: "c"}, {ID: "d"}, {ID: "e"}, {ID: "a"}, {ID: "b"}, {ID: "c"}, {ID: "d"}, {ID: "e"},
{ID: "f"}, {ID: "g"}, {ID: "h"}, {ID: "i"}, {ID: "f"}, {ID: "g"}, {ID: "h"}, {ID: "i"},
}, },
PackSize: PACKSIZE, PackSize: PACKSIZE,
} }
pack1 := MakePack(c) pack1 := draft.MakePack(c)
pack2 := MakePack(c) pack2 := draft.MakePack(c)
// Pack 2 can only contain 4 cards, as there are not enough cards to fill it // Pack 2 can only contain 4 cards, as there are not enough cards to fill it
if len(pack2) >= PACKSIZE { if len(pack2) >= PACKSIZE {
@ -100,11 +102,21 @@ func TestCubeOverflow(t *testing.T) {
} }
} }
// TestPackString makes sure packs are serialized correctly
func TestPackString(t *testing.T) {
p := draft.Pack{{ID: "a"}, {ID: "b"}, {ID: "c"}}
expected := "a b c"
if p.String() != expected {
t.Errorf("Expected \"%s\" but got \"%s\"", expected, p)
}
}
// ExampleGenericSet is an example usage of the Set APIs to make packs // ExampleGenericSet is an example usage of the Set APIs to make packs
func ExampleGenericSet() { func ExampleGenericSet() {
// Create a set with some items // Create a set with some items
s := &GenericSet{ s := &draft.GenericSet{
Cards: []Card{ Cards: []draft.Card{
{ID: "a"}, {ID: "a"},
{ID: "b"}, {ID: "b"},
{ID: "c"}, {ID: "c"},
@ -113,7 +125,7 @@ func ExampleGenericSet() {
} }
// Create a pack // Create a pack
pack := MakePack(s) pack := draft.MakePack(s)
// Print cards in pack // Print cards in pack
for i, card := range pack { for i, card := range pack {
@ -122,7 +134,7 @@ func ExampleGenericSet() {
} }
// https://gist.github.com/alioygur/16c66b4249cb42715091fe010eec7e33 // https://gist.github.com/alioygur/16c66b4249cb42715091fe010eec7e33
func sliceUniq(s []Card) []Card { func sliceUniq(s []draft.Card) []draft.Card {
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
for i2 := i + 1; i2 < len(s); i2++ { for i2 := i + 1; i2 < len(s); i2++ {
if s[i].ID == s[i2].ID { if s[i].ID == s[i2].ID {