Browse Source

Update testing a lot

main
Amelia A 7 months ago
parent
commit
eec9dd85ea
Signed by: amy GPG Key ID: 98E879BC3B6E1BF8
6 changed files with 47 additions and 31 deletions
  1. +2
    -3
      Dockerfile
  2. +3
    -3
      cmd/download/download.go
  3. +5
    -0
      docker-compose.override.yml
  4. +2
    -2
      docker-compose.yml
  5. +9
    -4
      lib/cards.go
  6. +26
    -19
      lib/cards_test.go

+ 2
- 3
Dockerfile View File

@ -9,12 +9,11 @@ ARG CGO_ENABLED=0
RUN go install ./cmd/silicon-dawn
# Final Stage
FROM scratch
FROM scratch AS production
EXPOSE 3200/tcp
COPY --from=builder /go/bin/silicon-dawn /
COPY templates /templates
COPY data /data
ENTRYPOINT ["./silicon-dawn"]
CMD ["./silicon-dawn"]

+ 3
- 3
cmd/download/download.go View File

@ -20,19 +20,19 @@ func main() {
fmt.Printf("Creating card directory %s\n", cardsDirectory)
err := os.MkdirAll(cardsDirectory, 0700)
if err != nil {
fmt.Printf("Making Directory: %w\n", err)
fmt.Printf("os.MkDirAll err = %v\n", err)
}
fmt.Print("Getting Zip File\n")
z, err := retrieveZip(cardsURL)
if err != nil {
fmt.Printf("Downloading file: %w\n", err)
fmt.Printf("retrieveZip(%s) err = %v\n", cardsURL, err)
}
fmt.Print("Unzipping files\n")
err = unzipFiles(z, cardsDirectory)
if err != nil {
fmt.Printf("Unzipping file: %w\n", err)
fmt.Printf("unzipZiles(zipfile, %q) err = %v\n", cardsDirectory, err)
}
fmt.Print("Finished!\n")


+ 5
- 0
docker-compose.override.yml View File

@ -0,0 +1,5 @@
version: "3.8"
services:
silicon-dawn:
build: .

+ 2
- 2
docker-compose.yml View File

@ -1,8 +1,8 @@
version: "3"
version: "3.8"
services:
silicon-dawn:
image: skwrl/silicon-dawn:latest
restart: always
ports:
- "3200:3200"
- "3200:3200"

+ 9
- 4
lib/cards.go View File

@ -1,13 +1,18 @@
package lib
import (
"fmt"
"errors"
"math/rand"
"path/filepath"
"strings"
"time"
)
var (
BadCardError = errors.New("bad card")
NoCardsAvailible = errors.New("no cards in deck")
)
type Card struct {
name string
}
@ -18,7 +23,7 @@ func NewCard(file string) (Card, error) {
cardName := baseName[0 : len(baseName)-4]
return Card{name: cardName}, nil
}
return Card{}, fmt.Errorf("Bad Card: Discarding %s", file)
return Card{}, BadCardError
}
func (c *Card) Front() string {
@ -45,7 +50,7 @@ func NewCardDeck(dir string) (*CardDeck, error) {
return &CardDeck{}, err
}
if deck.empty() {
return &CardDeck{}, fmt.Errorf("NoCardsAvailable")
return &CardDeck{}, NoCardsAvailible
}
return deck, nil
}
@ -56,7 +61,7 @@ func (d *CardDeck) Count() int {
func (d *CardDeck) Draw() (Card, error) {
if d.empty() {
return Card{}, fmt.Errorf("NoCardsAvailable")
return Card{}, NoCardsAvailible
}
drawNum := rand.Intn(len(d.cards))
c := d.cards[drawNum]


+ 26
- 19
lib/cards_test.go View File

@ -1,18 +1,25 @@
package lib
import "testing"
import (
"errors"
"testing"
)
func TestNewCard(t *testing.T) {
// todo: replace with table tests
func TestNewCard_old(t *testing.T) {
// Happy Path
got, err := NewCard("what/a/good/card.jpg")
arg := "what/a/good/card.jpg"
got, err := NewCard(arg)
if err != nil {
t.Errorf("Good Card Expected: nil err, got: %w", err)
t.Errorf("NewCard(%q) err = %w", arg, err)
}
if f := got.Front(); f != "card.jpg" {
t.Errorf("Expected Front: card.jpg, Got: %s", f)
wantFront := "card.jpg"
if gotFront := got.Front(); gotFront != wantFront {
t.Errorf("NewCard(%q).Front() = %q; want %q", arg, gotFront, wantFront)
}
if b := got.Back(); b != "card-text.png" {
t.Errorf("Expected Back: card-text.png, Got: %s", b)
wantBack := "card-text.png"
if gotBack := got.Back(); gotBack != wantBack {
t.Errorf("Expected Back: card-text.png, Got: %s", gotBack)
}
got, err = NewCard("just-card.jpg")
@ -27,11 +34,11 @@ func TestNewCard(t *testing.T) {
}
}
func TestNewCardDiscard(t *testing.T) {
// Bad Card
_, err := NewCard("bad-text.png")
if err == nil {
t.Error("Bad Card: Expected err Got: nil")
func TestNewCard_discard(t *testing.T) {
arg := "invalid-card.png"
_, err := NewCard(arg)
if !errors.Is(err, BadCardError) {
t.Errorf("NewCard(%q) err = %v; want %v", arg, err, BadCardError)
}
}
@ -47,16 +54,16 @@ func TestNewCardDeck(t *testing.T) {
}
func TestCardDeck_Draw(t *testing.T) {
card, _ := NewCard("c.jpg")
want, _ := NewCard("c.jpg")
d := CardDeck{
Directory: "test_draw",
cards: []Card{card},
Directory: "",
cards: []Card{want},
}
got, err := d.Draw()
if err != nil {
t.Errorf("Expected: nil err, Got: %w", err)
t.Errorf("CardDeck.Draw() err = %v", err)
}
if got != card {
t.Errorf("Expected: Text Card, Got: %s", got)
if got != want {
t.Errorf("CardDeck.Draw() = %v; want %v", got, want)
}
}

Loading…
Cancel
Save