Repository navigation
Expand file tree
/
Copy pathtypes_test.go
More file actions
63 lines (51 loc) · 1.5 KB
/
Copy pathtypes_test.go
File metadata and controls
63 lines (51 loc) · 1.5 KB
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
59
60
61
62
63
package tinyconf
import (
"context"
"errors"
"fmt"
"testing"
"github.com/insei/fmap/v3"
"github.com/stretchr/testify/assert"
)
type customParseError struct {
field string
}
func (e *customParseError) Error() string { return "custom error of " + e.field }
func Test_parseErrors(t *testing.T) {
var err error = parseErrors{
fmt.Errorf("first: %w", ErrValueNotFound),
fmt.Errorf("second: %w", &customParseError{field: "Name"}),
}
assert.Equal(t, "first: value was not found; second: custom error of Name", err.Error())
assert.ErrorIs(t, err, ErrValueNotFound)
assert.False(t, errors.Is(err, ErrRequired))
var custom *customParseError
if assert.True(t, errors.As(err, &custom)) {
assert.Equal(t, "Name", custom.field)
}
var other *testAsError
assert.False(t, errors.As(err, &other))
}
type testAsError struct{}
func (e *testAsError) Error() string { return "" }
func TestManager_StorageErrors(t *testing.T) {
type Sub struct {
Name string
}
type Config struct {
Sub Sub
}
manager, err := New(WithDriver(&staticMockDriver{}))
assert.NoError(t, err)
assert.NoError(t, manager.Register(&Config{}))
storageErr := errors.New("storage error")
original := getStorage
getStorage = func(any) (fmap.Storage, error) { return nil, storageErr }
defer func() { getStorage = original }()
type Other struct {
Name string
}
assert.ErrorIs(t, manager.Register(&Other{}), storageErr)
// fields of the sub config are not found
assert.ErrorIs(t, manager.Parse(context.Background(), &Sub{}), storageErr)
}