generated from sv-tools/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
58 lines (44 loc) · 1.3 KB
/
parser_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
package confyaml_test
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/sv-tools/conf"
confyaml "github.com/sv-tools/conf-parser-yaml"
)
const data = `
foo: 42
bar: test
`
const wrongData = `
foo: 42
- bar: test
`
func TestParser(t *testing.T) {
c := conf.New().WithReaders(conf.NewStreamParser(strings.NewReader(data)).WithParser(confyaml.Parser))
require.NoError(t, c.Load(context.Background()))
require.Equal(t, 42, c.GetInt("foo"))
require.Equal(t, "test", c.Get("bar"))
}
var errFake = errors.New("fake error")
type testReader struct{}
func (t *testReader) Read(_ []byte) (int, error) {
return 0, errFake
}
func TestParserErrors(t *testing.T) {
c := conf.New().WithReaders(conf.NewStreamParser(&testReader{}).WithParser(confyaml.Parser))
require.ErrorIs(t, c.Load(context.Background()), errFake)
c = conf.New().WithReaders(conf.NewStreamParser(strings.NewReader(wrongData)).WithParser(confyaml.Parser))
require.EqualError(t, c.Load(context.Background()), "yaml: line 1: did not find expected key")
}
func ExampleParser() {
c := conf.New().WithReaders(conf.NewStreamParser(strings.NewReader(data)).WithParser(confyaml.Parser))
if err := c.Load(context.Background()); err != nil {
panic(err)
}
fmt.Println(c.GetInt("foo"))
// Output: 42
}