Skip to content

Commit

Permalink
player test GoDoc update
Browse files Browse the repository at this point in the history
  • Loading branch information
teelau committed May 21, 2018
1 parent adf0de1 commit 08186cb
Showing 1 changed file with 132 additions and 42 deletions.
174 changes: 132 additions & 42 deletions server/models/player_test.go
Original file line number Diff line number Diff line change
@@ -1,80 +1,170 @@
package models

import (
"math"
"testing"

"github.com/gorilla/websocket"
)

const (
TestHeight = 400
TestWidth = 800
TestName = "testy"
TestColor = "blue"
)

var (
centerPos = Position{TestWidth / 2, TestHeight / 2}
)

func TestAddPoints(t *testing.T) {
p := new(Player)

p.AddPoints(10)
if p.Points != 10 {
t.Error("Error adding points")
}
}

func TestCreatePlayer(t *testing.T) {
testPosition := Position{5, 5}
testName := "testy"
testColor := "blue"
ws := new(*websocket.Conn)

//Test initialization of player
p := CreatePlayer(testName, testPosition, testColor, *ws)
p := CreatePlayer(TestName, centerPos, TestColor, *ws)

//Test name assignment of player
if p.Name != testName {
if p.Name != TestName {
t.Error("Error assigning name")
}
}

func TestUpdatePosition(t *testing.T) {
//Mock player and info
testHeight1 := 10.0
testWidth1 := 20.0
testAngle1 := 0.0
testPosition1 := Position{5, 5}
p := new(Player)
p.Position = testPosition1
p.Angle = testAngle1
p.Controls.Left = false
p.Controls.Right = false
p.Controls.Up = false

//Test left control
p.Controls.Left = true
p.UpdatePosition(testHeight1, testWidth1)
if p.Angle != (testAngle1 + 0.1) {
t.Error("Error in left key player control")
p.Angle = 0
p.Position = centerPos
rangeAngle := 2
testCases := []struct {
description string
playerVelocity Velocity
playerPosition Position
playerAngle float64
}{
{"Not moving", Velocity{0, 0}, centerPos, 0},
{"Max velocity", Velocity{math.Sqrt(MaxVelocity), math.Sqrt(MaxVelocity)}, centerPos, 0},
{"Moving N", Velocity{5, 0}, centerPos, 0},
{"Moving E", Velocity{0, 5}, centerPos, 0},
{"Moving S", Velocity{-5, 0}, centerPos, 0},
{"Moving W", Velocity{0, -5}, centerPos, 0},
}
p.Controls.Left = false
p.Controls.Right = true
p.UpdatePosition(testHeight1, testWidth1)
if p.Angle != (testAngle1) {
t.Error("Error in right key player control or symmetry")
}
p.Controls.Right = false
p.Controls.Up = true
p.UpdatePosition(testHeight1, testWidth1)
if p.Angle != (testAngle1) {
t.Error("Error in up key player control")
}
p.UpdatePosition(testHeight1, testWidth1)
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
p := new(Player)
p.Velocity = tc.playerVelocity
p.Position = tc.playerPosition
p.Angle = tc.playerAngle

p.UpdatePosition(TestHeight, TestWidth)
//Test max velocity
if p.Velocity.magnitude() > MaxVelocity {
t.Error("Error calculating max velocity")
}
//Test directional controls
//Left
p.Controls.Left = true
for i := 0; i < rangeAngle; i++ {
prevAngle := p.Angle
p.UpdatePosition(TestHeight, TestWidth)
if (p.Angle - prevAngle) != 0.1 {
t.Error("Error calculating left control")
}
}
p.Controls.Left = false
p.Controls.Right = true
//Right
for i := 0; i < rangeAngle; i++ {
prevAngle := p.Angle
p.UpdatePosition(TestHeight, TestWidth)
if (p.Angle - prevAngle) != -0.1 {
t.Error("Error calculating right control")
}
}
p.Controls.Left = true
//Both
for i := 0; i < rangeAngle; i++ {
prevAngle := p.Angle
p.UpdatePosition(TestHeight, TestWidth)
if p.Angle != prevAngle {
t.Error("Error calculating both controls")
}
}
p.Controls.Left = false
//Up
//Friction
prevMagnitude := p.Velocity.magnitude()
p.UpdatePosition(TestHeight, TestWidth)
if p.Velocity.magnitude() > prevMagnitude {
t.Error("Error calculating friction")
}
})

//Test friction and accelerate
t.Run(tc.description, func(t *testing.T) {
p := new(Player)
p.Velocity = tc.playerVelocity
p.Position = tc.playerPosition
p.Angle = tc.playerAngle
//Test Friction
prevMagnitude := p.Velocity.magnitude()
p.UpdatePosition(TestHeight, TestWidth)
if p.Velocity.magnitude() > prevMagnitude {
t.Error("Error calculating friction")
}

// p.Controls.Up = true
// //Test Accelerate
// prevMagnitude = p.Velocity.magnitude()
// p.UpdatePosition(TestHeight, TestWidth)
// if p.Velocity.magnitude() < prevMagnitude {
// t.Error("Error calculating acceleration")
// }
})
}
}

func TestHitjunk(t *testing.T) {
p := new(Player)
testVelocity := Velocity{4, 4}
p.Velocity = testVelocity
p.hitJunk()
if p.Velocity.Dx != testVelocity.Dx*JunkBounceFactor {
t.Error("Error calculating player Dx hitting junk")
testCases := []struct {
description string
playerVelocity Velocity
}{
{"Moving NW", Velocity{5, -5}},
{"Moving NE", Velocity{5, 5}},
{"Moving SW", Velocity{-5, -5}},
{"Moving SE", Velocity{-5, 5}},
{"Stationary", Velocity{0, 0}},
{"Moving N", Velocity{5, 0}},
{"Moving E", Velocity{0, 5}},
{"Moving S", Velocity{-5, 0}},
{"Moving W", Velocity{0, -5}},
}
if p.Velocity.Dy != testVelocity.Dy*JunkBounceFactor {
t.Error("Error calculating player Dy hitting junk")
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
p := new(Player)
p.Velocity = tc.playerVelocity
p.hitJunk()
if p.Velocity.Dx != tc.playerVelocity.Dx*JunkBounceFactor {
t.Error("Error calculating player Dx hitting junk")
}
if p.Velocity.Dy != tc.playerVelocity.Dy*JunkBounceFactor {
t.Error("Error calculating player Dy hitting junk")
}
if p.Velocity.Dx < tc.playerVelocity.Dx*JunkBounceFactor {
t.Error("Error x axis bounce factor greater than 1")
}
if p.Velocity.Dy < tc.playerVelocity.Dy*JunkBounceFactor {
t.Error("Error y axis bounce factor greater than 1")
}
})
}
}

Expand Down

0 comments on commit 08186cb

Please sign in to comment.