-
Notifications
You must be signed in to change notification settings - Fork 136
/
assets_test.go
195 lines (154 loc) · 5.71 KB
/
assets_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package engo
import (
"bytes"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
type assetTestScene struct{}
func (*assetTestScene) Preload() {}
func (*assetTestScene) Setup(Updater) {}
func (*assetTestScene) Type() string { return "testScene" }
// TestSetRoot makes sure set root sets Files.root to the proper value.
func TestFilesSetRoot(t *testing.T) {
// Start an instance of engo
Run(RunOptions{
NoRun: true,
HeadlessMode: true,
}, &assetTestScene{})
Files.SetRoot("testing")
if Files.root != "testing" {
t.Errorf("Root was not set to %v, it was %v instead", "testing", Files.root)
}
if Files.GetRoot() != "testing" {
t.Errorf("Root was set, but GetRoot() did not return proper value. Wanted: %v, got: %v", "testing", Files.GetRoot())
}
}
type testLoader struct{}
func (l *testLoader) Load(url string, data io.Reader) error {
return nil
}
func (l *testLoader) Unload(url string) error {
return nil
}
func (l *testLoader) Resource(url string) (Resource, error) {
return testResource{url: url}, nil
}
type testResource struct {
url string
}
func (r testResource) URL() string {
return r.url
}
func TestFilesRegister(t *testing.T) {
Files.Register(".test", &testLoader{})
_, ok := Files.formats[".test"]
if !ok {
t.Error("Files.Register failed to register .test")
}
}
func TestFilesLoad(t *testing.T) {
Files.Register(".test", &testLoader{})
content := []byte("testing")
dir, err := os.MkdirTemp(".", "testing")
if err != nil {
t.Errorf("failed to create temp directory for testing, error: %v", err)
}
defer os.RemoveAll(dir)
Files.SetRoot(dir)
tmpfn := filepath.Join(dir, "test1.test")
if err = os.WriteFile(tmpfn, content, 0666); err != nil {
t.Errorf("failed to create temp file for testing, file: %v, error: %v", tmpfn, err)
}
if err = Files.Load("test1.test"); err != nil {
t.Errorf("could not load test file %v, error: %v", "test1.test", err)
}
}
func TestFilesMultipleLoad(t *testing.T) {
Files.Register(".test", &testLoader{})
content := []byte("testing")
dir, err := os.MkdirTemp(".", "testing")
if err != nil {
t.Errorf("failed to create temp directory for testing, error: %v", err)
}
defer os.RemoveAll(dir)
Files.SetRoot(dir)
tmpfn := filepath.Join(dir, "test1.test")
if err = os.WriteFile(tmpfn, content, 0666); err != nil {
t.Errorf("failed to create temp file for testing, file: %v, error: %v", tmpfn, err)
}
tmpfn = filepath.Join(dir, "test2.test")
if err = os.WriteFile(tmpfn, content, 0666); err != nil {
t.Errorf("failed to create temp file for testing, file: %v, error: %v", tmpfn, err)
}
tmpfn = filepath.Join(dir, "test3.test")
if err = os.WriteFile(tmpfn, content, 0666); err != nil {
t.Errorf("failed to create temp file for testing, file: %v, error: %v", tmpfn, err)
}
if err = Files.Load("test1.test", "test2.test", "test3.test"); err != nil {
t.Errorf("could not load test file %v, error: %v", "test1.test", err)
}
}
func TestFilesLoadNotExist(t *testing.T) {
Files.Register(".test", &testLoader{})
expected := "unable to open resource:"
if err := Files.Load("notExist.test"); err == nil {
t.Error("did not report loading non-existant file as an error")
} else if !strings.HasPrefix(err.Error(), expected) {
t.Errorf("wrong error returned loading non-existant file. want: %v, got: %v", expected, err.Error())
}
}
func TestFilesLoadNoFileLoader(t *testing.T) {
expected := "no `FileLoader` associated with this extension:"
if err := Files.Load("test.wrongExtension"); err == nil {
t.Error("did not report loading file without an associated file loader")
} else if !strings.HasPrefix(err.Error(), expected) {
t.Errorf("wrong error returned loading file without an associated file loader. want: %v, got %v", expected, err.Error())
}
}
func TestFilesLoadReaderData(t *testing.T) {
Files.Register(".test", &testLoader{})
f := bytes.NewReader([]byte("testing"))
if err := Files.LoadReaderData("readerTest.test", f); err != nil {
t.Errorf("unable to load from an io.Reader. error: %v", err)
}
}
func TestFilesLoadReaderDataNoFileLoader(t *testing.T) {
f := bytes.NewReader([]byte("testing"))
expected := "no `FileLoader` associated with this extension:"
if err := Files.LoadReaderData("test.wrongExtension", f); err == nil {
t.Error("did not report loading reader without an associated file loader")
} else if !strings.HasPrefix(err.Error(), expected) {
t.Errorf("wrong error returned loading a reader without an associated file loader. want: %v, got: %v", expected, err.Error())
}
}
func TestFilesUnload(t *testing.T) {
Files.Register(".test", &testLoader{})
if err := Files.Unload("test.test"); err != nil {
t.Errorf("unable to unload a file. error: %v", err)
}
}
func TestFilesUnloadNoFileLoader(t *testing.T) {
expected := "no `FileLoader` associated with this extension:"
if err := Files.Unload("test.wrongExtension"); err == nil {
t.Error("did not report error unloading without an associated file loader")
} else if !strings.HasPrefix(err.Error(), expected) {
t.Errorf("wrong error returned unloading without an associated file loader. want: %v, got: %v", expected, err.Error())
}
}
func TestFilesResource(t *testing.T) {
Files.Register(".test", &testLoader{})
if _, err := Files.Resource("test.test"); err != nil {
t.Errorf("unable to fetch a resource. error: %v", err)
}
}
func TestFilesResourceNoFileLoader(t *testing.T) {
expected := "no `FileLoader` associated with this extension:"
if _, err := Files.Resource("test.wrongExtension"); err == nil {
t.Error("did not report error retrieving a resource without an associated file loader")
} else if !strings.HasPrefix(err.Error(), expected) {
t.Errorf("wrong error returned retrieving a resource without an associated file loader. want: %v, got: %v", expected, err.Error())
}
}