generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader_test.go
59 lines (53 loc) · 1.51 KB
/
loader_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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package jetpackr
import (
"bytes"
"io/ioutil"
"reflect"
"testing"
"github.com/gobuffalo/packr/v2"
)
func TestNew(t *testing.T) {
box := packr.New("test", "test")
l := New(box)
if !reflect.DeepEqual(box, l.box) {
t.Errorf("expected box %v, got %v", box, l.box)
}
}
func TestLoaderExists(t *testing.T) {
box := packr.New("views", "./testdata/views")
l := New(box)
views := []string{"index.tmpl", "non-existence.tmpl"}
for _, name := range views {
actualName, actualExists := l.Exists(name)
expectedExists := box.Has(name)
if expectedExists != actualExists {
t.Errorf("expected exists %t, got %t", expectedExists, actualExists)
}
if actualName != name {
t.Errorf("expected name %s, got %s", name, actualName)
}
}
}
func TestLoaderOpen(t *testing.T) {
box := packr.New("views", "./testdata/views")
l := New(box)
views := []string{"index.tmpl", "non-existence.tmpl"}
for _, name := range views {
file, err := l.Open(name)
expectedFile, expectedErr := box.Open(name)
if expectedErr != nil {
if err == nil || err.Error() != expectedErr.Error() {
t.Errorf("expected error %s, got %s", expectedErr, err)
}
continue
}
content, _ := ioutil.ReadAll(file)
expectedContent, _ := ioutil.ReadAll(expectedFile)
if !bytes.Equal(content, expectedContent) {
t.Errorf("expected file content %s, got %s", content, expectedContent)
}
}
}