mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-10-15 22:54:35 -04:00
50d40fb5d3
* adding logger implementation to d2common * Adding file loader implementation The file loader works in terms of `Sources` and `Assets`. A `Source` is something like a filesystem that has a cache. An `Asset` is something that implements `io.ReadSeeker` and has a few methods of its own. There are currently `Source` implementations for MPQ archives and for the host filesystem, meaning that one can specify a directory on the host fs to load files from. `Sources` are added to a loader with `loader.AddSource(path)`, where `path` resolves somewhere on disk. In the case that the path points to an MPQ, then an MPQ `Source` is created and added to the loader. If `path` resolves to a directory, then a filesystem source is added. Files are loaded with `loader.Load("data/global/excel/monstats.txt")`, and the sources are searched in the order that they were added. * adding tests for d2common/logger_test.go * adding tests and testdata for d2loader * logger lint fixes, fixed missing test case * minor edits, lint fixes, changes some comments, embedded Logger into Loader * moved d2loader into d2common (I dont think it belonged in d2core) * removed my simple cache implementation in favor of our existing cache in d2common
125 lines
2.9 KiB
Go
125 lines
2.9 KiB
Go
package d2loader
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/asset"
|
|
)
|
|
|
|
const (
|
|
sourceA = "testdata/A"
|
|
sourceB = "testdata/B"
|
|
sourceC = "testdata/C"
|
|
sourceD = "testdata/D.mpq"
|
|
commonFile = "common.txt"
|
|
exclusiveA = "exclusive_a.txt"
|
|
exclusiveB = "exclusive_b.txt"
|
|
exclusiveC = "exclusive_c.txt"
|
|
exclusiveD = "exclusive_d.txt"
|
|
badFilePath = "a/bad/file/path.txt"
|
|
)
|
|
|
|
func TestLoader_NewLoader(t *testing.T) {
|
|
loader := NewLoader()
|
|
|
|
if loader.Cache == nil {
|
|
t.Error("loader should not be nil")
|
|
}
|
|
}
|
|
|
|
func TestLoader_AddSource(t *testing.T) {
|
|
loader := NewLoader()
|
|
|
|
loader.AddSource(sourceA)
|
|
loader.AddSource(sourceB)
|
|
loader.AddSource(sourceC)
|
|
loader.AddSource(sourceD)
|
|
loader.AddSource("bad/path")
|
|
|
|
if loader.sources[0].String() != sourceA {
|
|
t.Error("source path not the same as what we added")
|
|
}
|
|
|
|
if loader.sources[1].String() != sourceB {
|
|
t.Error("source path not the same as what we added")
|
|
}
|
|
|
|
if loader.sources[2].String() != sourceC {
|
|
t.Error("source path not the same as what we added")
|
|
}
|
|
|
|
if loader.sources[3].String() != sourceD {
|
|
t.Error("source path not the same as what we added")
|
|
}
|
|
}
|
|
|
|
func TestLoader_Load(t *testing.T) {
|
|
loader := NewLoader()
|
|
|
|
loader.AddSource(sourceB) // we expect files common to any source to come from here
|
|
loader.AddSource(sourceD)
|
|
loader.AddSource(sourceA)
|
|
loader.AddSource(sourceC)
|
|
|
|
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)
|
|
|
|
_, 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 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"}, // sourceB is loaded first, we expect a "b"
|
|
{entryA, "a"},
|
|
{entryB, "b"},
|
|
{entryC, "c"},
|
|
{entryD, "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)
|
|
}
|
|
}
|
|
}
|