func_test.go

 1package document
 2
 3import (
 4	"encoding/json"
 5	"os"
 6	"testing"
 7
 8	"github.com/stretchr/testify/assert"
 9	"github.com/stretchr/testify/require"
10)
11
12func TestFilter(t *testing.T) {
13	content, err := os.ReadFile("testdata/test_list.json")
14	require.NoError(t, err)
15
16	var doc DocumentContent
17	assert.NoError(t, json.Unmarshal(content, &doc))
18	nodes := Filter(doc, func(n Node) bool { return n.Type == Waypoint })
19	assert.Equal(t, 3, len(nodes))
20	var names []string
21	for _, node := range nodes {
22		var attrs WaypointAttrs
23		assert.NoError(t, json.Unmarshal(node.Attrs, &attrs))
24		names = append(names, attrs.Label)
25	}
26	assert.EqualValues(t, []string{"Kathmandu", "Pokhara", "New Delhi"}, names)
27}