-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathitems_test.go
More file actions
41 lines (35 loc) · 993 Bytes
/
items_test.go
File metadata and controls
41 lines (35 loc) · 993 Bytes
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
package main
import (
"embed"
"encoding/json"
"strings"
"testing"
"github.com/creativeprojects/gopenhab/api"
"github.com/stretchr/testify/require"
)
//go:embed examples
var exampleFiles embed.FS
// load any file named item*.json in folder examples/
// and try to decode the content as []api.Item
func TestCanLoadExampleItems(t *testing.T) {
t.Parallel()
files, err := exampleFiles.ReadDir("examples")
if err != nil || len(files) == 0 {
t.Skip("no example file")
}
for _, itemsFile := range files {
if strings.HasPrefix(itemsFile.Name(), "items") && strings.HasSuffix(itemsFile.Name(), ".json") {
t.Run(itemsFile.Name(), func(t *testing.T) {
t.Parallel()
file, err := exampleFiles.Open("examples/" + itemsFile.Name())
require.NoError(t, err)
decoder := json.NewDecoder(file)
decoder.DisallowUnknownFields()
var items []api.Item
err = decoder.Decode(&items)
require.NoError(t, err)
t.Logf("loaded %d items", len(items))
})
}
}
}