mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-11-19 02:46:35 -05:00
783993470e
* fixed lint error in d2app/app.go * go fmt entire project * adding doc.go for d2records * fixed lint issues in d2core/d2map * fixed lint error in d2interface/palette.go * fixed lint error in d2core/d2hero/hero_state_factory.go * adding dov.go to d2common/d2geom * fixing lint errors in d2common/d2loader * adding doc.go to d2common/d2cache * adding doc files for d2datautils, d2util, d2path * adding package doc strings for mapgen, in-geam help screen, and tcp client connection * removed all cuddling lint errors * changed stamina equality check to '<=' instead of '<'
178 lines
3.8 KiB
Go
178 lines
3.8 KiB
Go
package d2loader
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"testing"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/asset"
|
|
)
|
|
|
|
const (
|
|
sourcePathA = "testdata/A"
|
|
sourcePathB = "testdata/B"
|
|
sourcePathC = "testdata/C"
|
|
sourcePathD = "testdata/D.mpq"
|
|
commonFile = "common.txt"
|
|
exclusiveA = "exclusive_a.txt"
|
|
exclusiveB = "exclusive_b.txt"
|
|
exclusiveC = "exclusive_c.txt"
|
|
exclusiveD = "exclusive_d.txt"
|
|
subdirCommonD = "dir\\common.txt"
|
|
badSourcePath = "/x/y/z.mpq"
|
|
badFilePath = "a/bad/file/path.txt"
|
|
)
|
|
|
|
func TestLoader_NewLoader(t *testing.T) {
|
|
loader := NewLoader(nil)
|
|
|
|
if loader.Cache == nil {
|
|
t.Error("loader should not be nil")
|
|
}
|
|
}
|
|
|
|
func TestLoader_AddSource(t *testing.T) {
|
|
loader := NewLoader(nil)
|
|
|
|
sourceA, errA := loader.AddSource(sourcePathA)
|
|
sourceB, errB := loader.AddSource(sourcePathB)
|
|
sourceC, errC := loader.AddSource(sourcePathC)
|
|
sourceD, errD := loader.AddSource(sourcePathD)
|
|
sourceE, errE := loader.AddSource(badSourcePath)
|
|
|
|
if errA != nil {
|
|
t.Error(errA)
|
|
}
|
|
|
|
if errB != nil {
|
|
t.Error(errB)
|
|
}
|
|
|
|
if errC != nil {
|
|
t.Error(errC)
|
|
}
|
|
|
|
if errD != nil {
|
|
t.Error(errD)
|
|
}
|
|
|
|
if errE == nil {
|
|
t.Error("expecting error on bad file path")
|
|
}
|
|
|
|
if sourceA.String() != sourcePathA {
|
|
t.Error("source path not the same as what we added")
|
|
}
|
|
|
|
if sourceB.String() != sourcePathB {
|
|
t.Error("source path not the same as what we added")
|
|
}
|
|
|
|
if sourceC.String() != sourcePathC {
|
|
t.Error("source path not the same as what we added")
|
|
}
|
|
|
|
if sourceD.String() != sourcePathD {
|
|
t.Error("source path not the same as what we added")
|
|
}
|
|
|
|
if sourceE != nil {
|
|
t.Error("source for bad path should be nil")
|
|
}
|
|
}
|
|
|
|
// nolint:gocyclo // this is just a test, not a big deal if we ignore linter here
|
|
func TestLoader_Load(t *testing.T) {
|
|
loader := NewLoader(nil)
|
|
|
|
_, err := loader.AddSource(sourcePathB) // we expect files common to any source to come from here
|
|
if err != nil {
|
|
t.Fail()
|
|
log.Print(err)
|
|
}
|
|
|
|
_, err = loader.AddSource(sourcePathD)
|
|
if err != nil {
|
|
t.Fail()
|
|
log.Print(err)
|
|
}
|
|
|
|
_, err = loader.AddSource(sourcePathA)
|
|
if err != nil {
|
|
t.Fail()
|
|
log.Print(err)
|
|
}
|
|
|
|
_, err = loader.AddSource(sourcePathC)
|
|
if err != nil {
|
|
t.Fail()
|
|
log.Print(err)
|
|
}
|
|
|
|
entryCommon, errCommon := loader.Load(commonFile) // common file exists in all three Sources
|
|
|
|
entryA, errA := loader.Load(exclusiveA) // each source has a file exclusive to itself
|
|
entryB, errB := loader.Load(exclusiveB)
|
|
entryC, errC := loader.Load(exclusiveC)
|
|
entryD, errD := loader.Load(exclusiveD)
|
|
entryDsubdir, errDsubdir := loader.Load(subdirCommonD)
|
|
|
|
_, expectedError := loader.Load(badFilePath) // we expect an Error for this bad file path
|
|
|
|
if entryCommon == nil || errCommon != nil {
|
|
t.Error("common entry should exist")
|
|
} else if entryCommon.Source() != loader.Sources[0] {
|
|
t.Error("common entry should come from the first loader source")
|
|
}
|
|
|
|
if errA != nil || errB != nil || errC != nil || errD != nil {
|
|
t.Error("files exclusive to each source don't exist")
|
|
}
|
|
|
|
if errDsubdir != nil {
|
|
t.Error("mpq subdir entry not found")
|
|
}
|
|
|
|
if expectedError == nil {
|
|
t.Error("expected Error for nonexistant file path")
|
|
}
|
|
|
|
var result []byte
|
|
|
|
buffer := make([]byte, 1)
|
|
|
|
tests := []struct {
|
|
entry asset.Asset
|
|
data string
|
|
}{
|
|
{entryCommon, "b"}, // sourcePathB is loaded first, we expect a "b"
|
|
{entryA, "a"},
|
|
{entryB, "b"},
|
|
{entryC, "c"},
|
|
{entryD, "d"},
|
|
{entryDsubdir, "d"},
|
|
}
|
|
|
|
for idx := range tests {
|
|
entry, expected := tests[idx].entry, tests[idx].data
|
|
|
|
result = make([]byte, 0)
|
|
|
|
for {
|
|
if bytesRead, err := entry.Read(buffer); err != nil || bytesRead == 0 {
|
|
break
|
|
}
|
|
|
|
result = append(result, buffer...)
|
|
}
|
|
|
|
got := string(result[0])
|
|
|
|
if got != expected {
|
|
fmtStr := "unexpected data in file %s, loaded from source `%s`: expected `%s`, got `%s`"
|
|
msg := fmt.Sprintf(fmtStr, entry.Path(), entry.Source(), expected, got)
|
|
t.Error(msg)
|
|
}
|
|
}
|
|
}
|