-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsavior_test.go
More file actions
70 lines (56 loc) · 2.18 KB
/
savior_test.go
File metadata and controls
70 lines (56 loc) · 2.18 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
64
65
66
67
68
69
70
package figtree
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFigTree_ReadFile(t *testing.T) {
figs := With(Options{Germinate: true})
assert.NoError(t, figs.ReadFrom(filepath.Join(".", "test.config.yaml")))
assert.Equal(t, "yahuah", *figs.String("name"))
}
func TestFigTree_SaveTo(t *testing.T) {
testFile := filepath.Join(".", "test.saved.config.yaml")
figs := With(Options{Germinate: true})
figs.NewString("name", "unknown", "name")
figs.NewInt("age", 0, "age")
figs.NewString("sex", "unknown", "sex")
figs.StoreString("name", t.Name())
figs.StoreInt("age", 33)
figs.StoreString("sex", "male")
assert.NoError(t, figs.SaveTo(testFile))
fig2 := With(Options{Germinate: true})
assert.NoError(t, fig2.ReadFrom(testFile))
nameFig := fig2.FigFlesh("name")
assert.NotNil(t, nameFig)
name := fig2.String("name")
assert.Equal(t, t.Name(), *name)
assert.NoError(t, os.RemoveAll(testFile))
}
func TestFigTree_SaveTo_MapRoundTrip(t *testing.T) {
path := filepath.Join(t.TempDir(), "out.yaml")
figs := With(Options{Germinate: true})
figs.NewMap("cfg", map[string]string{"key": "value", "foo": "bar"}, "usage")
// intentionally do NOT call StoreMap — use the raw MapFlag state from NewMap
assert.NoError(t, figs.SaveTo(path))
figs2 := With(Options{Germinate: true})
figs2.NewMap("cfg", map[string]string{}, "usage")
assert.NoError(t, figs2.ReadFrom(path))
result := *figs2.Map("cfg")
assert.Equal(t, map[string]string{"key": "value", "foo": "bar"}, result,
"MapFlag should be unwrapped before serialization — got %v", result)
}
func TestFigTree_SaveTo_ListRoundTrip(t *testing.T) {
path := filepath.Join(t.TempDir(), "out.yaml")
figs := With(Options{Germinate: true})
figs.NewList("items", []string{"one", "two", "three"}, "usage")
// intentionally do NOT call StoreList — use the raw ListFlag state from NewList
assert.NoError(t, figs.SaveTo(path))
figs2 := With(Options{Germinate: true})
figs2.NewList("items", []string{}, "usage")
assert.NoError(t, figs2.ReadFrom(path))
result := *figs2.List("items")
assert.Equal(t, []string{"one", "three", "two"}, result,
"ListFlag should be unwrapped before serialization — got %v", result)
}