Skip to content

Commit

Permalink
Pass all existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirReza committed Oct 31, 2023
1 parent 0c3ff7e commit dd9ac33
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 40 deletions.
30 changes: 30 additions & 0 deletions server/models/game_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package models

import (
"github.com/AmirRezaM75/skull-king/pkg/syncx"
"testing"
)

func TestGame_GetAvailableAvatar(t *testing.T) {
var players syncx.Map[string, *Player]

players.Store("Hermione", &Player{
Avatar: "1.jpg",
})
players.Store("Harry", &Player{
Avatar: "3.jpg",
})
players.Store("Dobby", &Player{
Avatar: "4.jpg",
})

game := Game{
Players: players,
}

image := game.GetAvailableAvatar()

if image != "2.jpg" {
t.Errorf("Expected 2.jpg as avatar, got %s", image)
}
}
38 changes: 28 additions & 10 deletions server/models/hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,45 @@ package models

import (
"github.com/AmirRezaM75/skull-king/constants"
"github.com/AmirRezaM75/skull-king/pkg/syncx"
"testing"
"time"
)

func TestHub_Cleanup(t *testing.T) {
games := syncx.Map[string, *Game]{}
games.Store("786e4150-77ca-11ee-b962-0242ac120002", &Game{
Id: "786e4150-77ca-11ee-b962-0242ac120002",
CreatedAt: time.Now().Add(-15 * time.Minute).Unix(),
State: constants.StatePending,
})
games.Store("786e481c-77ca-11ee-b962-0242ac120002", &Game{
Id: "786e481c-77ca-11ee-b962-0242ac120002",
CreatedAt: time.Now().Add(-40 * time.Minute).Unix(),
State: constants.StatePending,
})
games.Store("786e495c-77ca-11ee-b962-0242ac120002", &Game{
Id: "786e495c-77ca-11ee-b962-0242ac120002",
CreatedAt: time.Now().Add(-40 * time.Minute).Unix(),
State: constants.StatePicking,
})
games.Store("786e4ba0-77ca-11ee-b962-0242ac120002", &Game{
Id: "786e4ba0-77ca-11ee-b962-0242ac120002",
CreatedAt: time.Now().Unix(),
State: constants.StatePending,
})

hub := Hub{
Games: map[string]*Game{
"Legolas": {Id: "Legolas", CreatedAt: time.Now().Add(-15 * time.Minute).Unix(), State: constants.StatePending},
"Galadriel": {Id: "Galadriel", CreatedAt: time.Now().Add(-40 * time.Minute).Unix(), State: constants.StatePending},
"Sauron": {Id: "Sauron", CreatedAt: time.Now().Add(-40 * time.Minute).Unix(), State: constants.StatePicking},
"Elrond": {Id: "Elrond", CreatedAt: time.Now().Unix(), State: constants.StatePending},
},
Games: games,
}

hub.Cleanup()

if len(hub.Games) != 3 {
t.Errorf("Expected 3 games but got %d", len(hub.Games))
if hub.Games.Len() != 3 {
t.Errorf("Expected 3 games but got %d", hub.Games.Len())
}

if _, ok := hub.Games["Galadriel"]; ok {
t.Errorf("Expected to remove Galadriel game.")
if _, ok := hub.Games.Load("786e481c-77ca-11ee-b962-0242ac120002"); ok {
t.Errorf("Expected to remove 786e481c-77ca-11ee-b962-0242ac120002 game.")
}
}
69 changes: 39 additions & 30 deletions server/models/round_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"github.com/AmirRezaM75/skull-king/pkg/syncx"
"testing"
)

Expand All @@ -11,11 +12,12 @@ func TestCalculateScore(t *testing.T) {
StarterPlayerIndex: 0,
}

bids := syncx.Map[string, int]{}
bids.Store("Bilbo", 1)
bids.Store("Arwen", 0)

var round = Round{
Bids: map[string]int{
"Bilbo": 1,
"Arwen": 0,
},
Bids: bids,
Number: 1,
Tricks: []*Trick{trick},
Scores: make(map[string]int, 2),
Expand Down Expand Up @@ -45,11 +47,12 @@ func TestCalculateScore2(t *testing.T) {
StarterPlayerIndex: 0,
}

bids := syncx.Map[string, int]{}
bids.Store("Bilbo", 2)
bids.Store("Arwen", 0)

var round = Round{
Bids: map[string]int{
"Bilbo": 2,
"Arwen": 0,
},
Bids: bids,
Number: 2,
Tricks: []*Trick{trick1, trick2},
Scores: make(map[string]int, 2),
Expand All @@ -67,11 +70,12 @@ func TestCalculateScore2(t *testing.T) {
}

func TestCalculateScore3(t *testing.T) {
bids := syncx.Map[string, int]{}
bids.Store("Bilbo", 0)
bids.Store("Arwen", 1)

var round = Round{
Bids: map[string]int{
"Bilbo": 0,
"Arwen": 1,
},
Bids: bids,
Number: 2,
Tricks: []*Trick{
{
Expand All @@ -95,10 +99,11 @@ func TestCalculateScore3(t *testing.T) {
}

func TestCalculateScore4(t *testing.T) {
bids := syncx.Map[string, int]{}
bids.Store("Bilbo", 0)

var round = Round{
Bids: map[string]int{
"Bilbo": 0,
},
Bids: bids,
Number: 9,
Tricks: []*Trick{
{
Expand All @@ -118,10 +123,11 @@ func TestCalculateScore4(t *testing.T) {
}

func TestCalculateScore5(t *testing.T) {
bids := syncx.Map[string, int]{}
bids.Store("Bilbo", 0)

var round = Round{
Bids: map[string]int{
"Bilbo": 0,
},
Bids: bids,
Number: 7,
Tricks: []*Trick{},
Scores: make(map[string]int, 1),
Expand All @@ -135,11 +141,12 @@ func TestCalculateScore5(t *testing.T) {
}

func TestCalculateScoreWith14CardsBonusPoint(t *testing.T) {
bids := syncx.Map[string, int]{}
bids.Store("Frodo", 3)
bids.Store("Gandalf", 1)

var round = Round{
Bids: map[string]int{
"Frodo": 3,
"Gandalf": 1,
},
Bids: bids,
Number: 3,
Tricks: []*Trick{
{
Expand Down Expand Up @@ -175,11 +182,12 @@ func TestCalculateScoreWith14CardsBonusPoint(t *testing.T) {
}

func TestCalculateScoreWithSkullKingBonusPoint(t *testing.T) {
bids := syncx.Map[string, int]{}
bids.Store("Frodo", 3)
bids.Store("Gandalf", 1)

var round = Round{
Bids: map[string]int{
"Frodo": 3,
"Gandalf": 1,
},
Bids: bids,
Number: 3,
Tricks: []*Trick{
{
Expand Down Expand Up @@ -216,11 +224,12 @@ func TestCalculateScoreWithSkullKingBonusPoint(t *testing.T) {
}

func TestNoBonusPointWhenYouBidWrong(t *testing.T) {
bids := syncx.Map[string, int]{}
bids.Store("Frodo", 3)
bids.Store("Gandalf", 1)

var round = Round{
Bids: map[string]int{
"Frodo": 3,
"Gandalf": 1,
},
Bids: bids,
Number: 3,
Tricks: []*Trick{
{
Expand Down

0 comments on commit dd9ac33

Please sign in to comment.