Docker container that powers the high scores page at nethack.jerryaldrichiii.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

127 lines
3.2 KiB

package logparser
import (
"reflect"
"strings"
"testing"
)
func TestParseDungeon(t *testing.T) {
t.Parallel()
t.Run("InvalidDungeon", func(t *testing.T) {
t.Parallel()
badDungeons := []int{8, -1}
for _, d := range badDungeons {
result, err := ParseDungeon(d, 1)
if result != "" {
t.Errorf("Expected empty result, got: %v", result)
}
expectedText := "Invalid dungeon"
expectedError := strings.Contains(err.Error(), expectedText)
if !expectedError {
t.Errorf("Error must contain '%v', got: %v", expectedText, err)
}
}
})
t.Run("InvalidDungeonPlaneCombo", func(t *testing.T) {
t.Parallel()
result, err := ParseDungeon(3, -1)
if result != "" {
t.Errorf("Expected empty result, got: %v", result)
}
expectedText := "Must be > 0 if dungeon number != 7"
expectedError := strings.Contains(err.Error(), expectedText)
if !expectedError {
t.Errorf("Error must contain '%v', got: %v", expectedText, err)
}
})
t.Run("InvalidPlane", func(t *testing.T) {
t.Parallel()
badPlanes := []int{1, -6}
for _, p := range badPlanes {
result, err := ParseDungeon(7, p)
if result != "" {
t.Errorf("Expected empty result, got: %v", result)
}
expectedText := "Must be > -1 or < -5 if dungeon number is 7"
expectedError := strings.Contains(err.Error(), expectedText)
if !expectedError {
t.Errorf("Error must contain '%v', got: %v", expectedText, err)
}
}
})
tests := []struct {
dNum int
dLvl int
expectedResult string
}{
{0, 1, "The Dungeons of Doom (Level 1)"},
{0, 2, "The Dungeons of Doom (Level 2)"},
{1, 3, "Gehennom (Level 3)"},
{2, 4, "The Gnomish Mines (Level 4)"},
{3, 5, "The Quest (Level 5)"},
{4, 6, "Sokoban (Level 6)"},
{5, 7, "Fort Ludios (Level 7)"},
{6, 55, "Vlad's Tower (Level 55)"},
{7, -1, "Plane of Earth"},
{7, -2, "Plane of Air"},
{7, -3, "Plane of Fire"},
{7, -4, "Plane of Water"},
{7, -5, "Astral Plane"},
}
for _, test := range tests {
t.Run(test.expectedResult, func(t *testing.T) {
result, err := ParseDungeon(test.dNum, test.dLvl)
if err != nil {
t.Error(err)
}
if result != test.expectedResult {
t.Errorf("Expected '%v', but got '%v'", test.expectedResult, result)
}
})
}
}
func TestTopX(t *testing.T) {
t.Parallel()
records := []Record{
{Points: 50, Name: "Test50"},
{Points: 20, Name: "Test20"},
{Points: 30, Name: "Test30"},
{Points: 40, Name: "Test40"},
{Points: 10, Name: "Test10"},
}
sortedTop3 := TopX(records, 3)
expectedTop3 := []Record{
{Points: 50, Name: "Test50"},
{Points: 40, Name: "Test40"},
{Points: 30, Name: "Test30"},
}
if !reflect.DeepEqual(sortedTop3, expectedTop3) {
t.Errorf(
"Expected the following two []Record to be equal\n%v\n%v",
sortedTop3, expectedTop3,
)
}
sortedTooMany := TopX(records, 100)
expectedTooMany := []Record{
{Points: 50, Name: "Test50"},
{Points: 40, Name: "Test40"},
{Points: 30, Name: "Test30"},
{Points: 20, Name: "Test20"},
{Points: 10, Name: "Test10"},
}
if !reflect.DeepEqual(sortedTooMany, expectedTooMany) {
t.Errorf(
"Expected the following two []Record to be equal\n%v\n%v",
sortedTooMany, expectedTooMany,
)
}
}