-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgofigure_test.go
106 lines (84 loc) · 1.98 KB
/
gofigure_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package gofigure
import (
"os"
"testing"
)
const PROJECT_NAME = "my-gofigure-test-project"
// Define the structure of your config
type myGlobalConfigType struct {
User struct {
Name string `yaml:"name"`
Email string `yaml:"email"`
} `yaml:"user"`
VerbosityLevel uint8 `yaml:"verbosity_level"`
}
// Create a config entity, set default values (if desired)
var myGlobalConfig = &myGlobalConfigType{VerbosityLevel: 1}
// Define the explicit interface to your configuration
func SetUsername(unm string) {
CheckErr(GlobalConfig)
myGlobalConfig.User.Name = unm
}
func Username() string {
CheckErr(GlobalConfig)
return myGlobalConfig.User.Name
}
func VerbosityLevel() uint8 {
CheckErr(GlobalConfig)
return myGlobalConfig.VerbosityLevel
}
func setup(m *testing.M) error {
var (
err error
)
Setup(PROJECT_NAME)
Register(GlobalConfig, myGlobalConfig)
err = RemoveAll()
if err != nil {
return err
}
code := m.Run()
err = RemoveAll()
if err != nil {
return err
}
os.Exit(code)
return nil
}
func TestMain(m *testing.M) {
err := setup(m)
if err != nil {
panic(err)
}
}
// Create & update your config
func TestGlobalConfigCreation(t *testing.T) {
Setup(PROJECT_NAME)
Register(GlobalConfig, myGlobalConfig)
if usr := Username(); usr != "" {
t.Error("username was set, i.e. accidentially persisted")
}
if vl := VerbosityLevel(); vl != 1 {
t.Error("verbosity was not set as default")
}
SetUsername("nanvenomous")
if usr := Username(); usr != "nanvenomous" {
t.Error("failed to set username")
}
// write the config to disk
WriteAll()
}
// Retrieve config from disk
func TestGlobalConfigRetrieval(t *testing.T) {
Setup(PROJECT_NAME)
Register(GlobalConfig, myGlobalConfig)
// empty the configuration entity
myGlobalConfig = &myGlobalConfigType{}
Register(GlobalConfig, myGlobalConfig)
if usr := Username(); usr != "nanvenomous" {
t.Error("the username was not persisted")
}
if vl := VerbosityLevel(); vl != 1 {
t.Error("the verbosity was not persisted")
}
}