-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource_test.go
107 lines (90 loc) · 2.73 KB
/
resource_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
package tmx_test
import (
"errors"
"image"
"io"
"os"
. "github.com/manyminds/tmx"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type nilResourceLoader struct {
}
func (m nilResourceLoader) LocateResource(filepath string) (image.Image, error) {
return nil, nil
}
type moduloErrorResourceLoader struct {
callCount int
}
var errAlreadyLoaded = errors.New("Already loaded")
func (m *moduloErrorResourceLoader) LocateResource(filepath string) (image.Image, error) {
m.callCount++
if m.callCount%2 == 0 {
return nil, errAlreadyLoaded
}
return nil, nil
}
var _ = Describe("Test public api", func() {
Context("check types for interface interface", func() {
It("can be implemented", func() {
var loader interface{}
loader = nilResourceLoader{}
_, ok := loader.(ResourceLocator)
Expect(ok).To(Equal(true))
})
It("is implemented by default", func() {
var loader interface{}
loader = FilesystemLocator{}
_, ok := loader.(ResourceLocator)
Expect(ok).To(Equal(true))
})
It("will error on invalid resource", func() {
loader := FilesystemLocator{}
_, err := loader.LocateResource("/null")
Expect(err).To(HaveOccurred())
})
It("will find a testfile", func() {
loader := FilesystemLocator{}
_, err := loader.LocateResource("testfiles/chipset.png")
Expect(err).ToNot(HaveOccurred())
})
})
Context("test lazy resource locator", func() {
BeforeEach(func() {
c, err := os.Open("testfiles/chipset.png")
Expect(err).ToNot(HaveOccurred())
t, err := os.Create("testfiles/chipset_copy.png")
Expect(err).ToNot(HaveOccurred())
_, err = io.Copy(t, c)
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
os.Remove("testfiles/chipset_copy.png")
})
It("caches files even after remove", func() {
loader := NewLazyResourceLocator(FilesystemLocator{})
img, err := loader.LocateResource("testfiles/chipset_copy.png")
Expect(err).ToNot(HaveOccurred())
Expect(img).ToNot(BeNil())
err = os.Remove("testfiles/chipset_copy.png")
Expect(err).ToNot(HaveOccurred())
img, err = loader.LocateResource("testfiles/chipset_copy.png")
Expect(err).ToNot(HaveOccurred())
Expect(img).ToNot(BeNil())
manager, ok := loader.(ResourceManager)
Expect(ok).To(BeTrue())
manager.UnsetResource("testfiles/chipset_copy.png")
img, err = loader.LocateResource("testfiles/chipset_copy.png")
Expect(err).To(HaveOccurred())
Expect(img).To(BeNil())
})
It("will only load a specific resource once", func() {
loader := &moduloErrorResourceLoader{}
lazyLoader := NewLazyResourceLocator(loader)
_, err := lazyLoader.LocateResource("lol")
Expect(err).ToNot(HaveOccurred())
_, err = lazyLoader.LocateResource("lol")
Expect(err).ToNot(HaveOccurred())
})
})
})