Select Git revision
assets_test.go
assets_test.go 1.49 KiB
package perxis
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type State int
const (
State1 State = iota
State2
)
type testEntry struct {
ID string
Enum State
Data map[string]interface{}
Struct *nested
}
type nested struct {
Option *bool
}
func TestFromFS(t *testing.T) {
tr := true
i1 := &testEntry{
ID: "item1",
Enum: State2,
Data: map[string]interface{}{
"obj": map[string]interface{}{"str": "value"},
"arr": []interface{}{"str1", "str2"},
},
Struct: &nested{
Option: &tr,
},
}
i2 := *i1
i2.ID = "item2"
i3 := *i1
i3.ID = "item3"
assets := NewAssets[*testEntry]()
r, err := assets.FromFS(os.DirFS("assets/tests/assets"))
require.NoError(t, err)
require.Len(t, r, 3)
assert.ElementsMatch(t, []*testEntry{i1, &i2, &i3}, r)
}
func TestFrom(t *testing.T) {
tr := true
i1 := &testEntry{
ID: "item1",
Enum: State2,
Data: map[string]interface{}{
"obj": map[string]interface{}{"str": "value"},
"arr": []interface{}{"str1", "str2"},
},
Struct: &nested{
Option: &tr,
},
}
i2 := *i1
i2.ID = "item2"
i3 := *i1
i3.ID = "item3"
assets := NewAssets[*testEntry]()
r, err := assets.From(os.DirFS("assets"), "tests/assets")
require.NoError(t, err)
require.Len(t, r, 3)
assert.ElementsMatch(t, []*testEntry{i1, &i2, &i3}, r)
r, err = assets.From(os.DirFS("assets"), "tests/assets/items.yaml")
require.NoError(t, err)
require.Len(t, r, 2)
assert.Equal(t, []*testEntry{i1, &i2}, r)
}