2020-10-10 22:49:17 -04:00
|
|
|
package d2systems
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-12 17:35:11 -04:00
|
|
|
"github.com/gravestench/akara"
|
2020-10-10 22:49:17 -04:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_FileHandleResolver_Process(t *testing.T) {
|
2020-11-22 16:18:50 -05:00
|
|
|
const testDataPath = "testdata"
|
2020-11-22 04:37:55 -05:00
|
|
|
|
2020-10-12 17:35:11 -04:00
|
|
|
cfg := akara.NewWorldConfig()
|
2020-10-10 22:49:17 -04:00
|
|
|
|
2020-11-28 00:45:58 -05:00
|
|
|
typeSys := &FileTypeResolver{}
|
|
|
|
handleSys := &FileHandleResolver{}
|
|
|
|
sourceSys := &FileSourceResolver{}
|
2020-10-10 22:49:17 -04:00
|
|
|
|
2020-11-28 00:45:58 -05:00
|
|
|
cfg.With(typeSys).
|
|
|
|
With(sourceSys).
|
|
|
|
With(handleSys)
|
2020-10-10 22:49:17 -04:00
|
|
|
|
2020-10-12 17:35:11 -04:00
|
|
|
world := akara.NewWorld(cfg)
|
2020-10-10 22:49:17 -04:00
|
|
|
|
2020-12-08 16:38:46 -05:00
|
|
|
fileHandles := handleSys.Components.FileHandle
|
2020-10-10 22:49:17 -04:00
|
|
|
|
|
|
|
sourceEntity := world.NewEntity()
|
2020-12-08 16:38:46 -05:00
|
|
|
source := typeSys.Components.File.Add(sourceEntity)
|
2020-11-28 00:45:58 -05:00
|
|
|
source.Path = testDataPath
|
2020-10-10 22:49:17 -04:00
|
|
|
|
|
|
|
fileEntity := world.NewEntity()
|
2020-12-08 16:38:46 -05:00
|
|
|
file := typeSys.Components.File.Add(fileEntity)
|
2020-11-28 00:45:58 -05:00
|
|
|
file.Path = "testfile_a.txt"
|
2020-10-10 22:49:17 -04:00
|
|
|
|
|
|
|
_ = world.Update(0)
|
|
|
|
|
2020-12-08 16:38:46 -05:00
|
|
|
ft, found := typeSys.Components.FileType.Get(sourceEntity)
|
2020-10-10 22:49:17 -04:00
|
|
|
if !found {
|
|
|
|
t.Error("file source type not created for entity")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ft.Type != d2enum.FileTypeDirectory {
|
|
|
|
t.Error("expected file system source type for entity")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:38:46 -05:00
|
|
|
handle, found := fileHandles.Get(fileEntity)
|
2020-10-10 22:49:17 -04:00
|
|
|
if !found {
|
|
|
|
t.Error("file handle for entity was not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, buf := make([]byte, 0), make([]byte, 16)
|
|
|
|
|
|
|
|
for {
|
|
|
|
numRead, err := handle.Data.Read(buf)
|
|
|
|
|
|
|
|
data = append(data, buf[:numRead]...)
|
|
|
|
|
|
|
|
if err != nil || numRead == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := strings.Trim(string(data), "\r\n")
|
|
|
|
|
|
|
|
if result != "test a" {
|
|
|
|
t.Error("unexpected data read from `./testdata/testfile_a.txt`")
|
|
|
|
}
|
|
|
|
}
|