generated from sv-tools/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
51 lines (39 loc) · 1.32 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
package confjson_test
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/sv-tools/conf"
confjson "github.com/sv-tools/conf-parser-json"
)
func TestParser(t *testing.T) {
data := strings.NewReader(`{"foo": 42, "bar": "test"}`)
c := conf.New().WithReaders(conf.NewStreamParser(data).WithParser(confjson.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(confjson.Parser))
require.ErrorIs(t, c.Load(context.Background()), errFake)
data := strings.NewReader(`{"foo": 42, "bar": "test"`)
c = conf.New().WithReaders(conf.NewStreamParser(data).WithParser(confjson.Parser))
require.EqualError(t, c.Load(context.Background()), "unexpected end of JSON input")
}
func ExampleParser() {
data := strings.NewReader(`{"foo": 42, "bar": "test"}`)
c := conf.New().WithReaders(conf.NewStreamParser(data).WithParser(confjson.Parser))
if err := c.Load(context.Background()); err != nil {
panic(err)
}
fmt.Println(c.GetInt("foo"))
// Output: 42
}