-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrules_test.go
More file actions
73 lines (61 loc) · 1.62 KB
/
rules_test.go
File metadata and controls
73 lines (61 loc) · 1.62 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
71
72
73
package figtree
import (
"os"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRules(t *testing.T) {
kName := "name"
os.Args = []string{os.Args[0]}
figs := With(Options{Germinate: true, Pollinate: true})
assert.NotNil(t, figs)
figs = figs.NewString(kName, "", "usage of name").WithValidator(kName, AssureStringNotEmpty)
assert.Error(t, figs.Parse())
figs = figs.WithRule(kName, RuleNoValidations)
assert.NoError(t, figs.Parse())
figs = figs.WithCallback(kName, CallbackBeforeVerify, func(_ interface{}) error {
panic("this shouldn't be called")
return nil
})
assert.Panics(t, func() { _ = figs.Parse() })
figs = figs.WithRule(kName, RuleNoCallbacks)
assert.NoError(t, figs.Parse())
changeEnv := func(n, m string) {
nu := strings.ToUpper(n)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
timer := time.NewTimer(time.Second * 1)
checker := time.NewTicker(100 * time.Millisecond)
for {
select {
case <-timer.C:
return
case <-checker.C:
assert.NoError(t, os.Setenv(nu, m))
}
}
}()
defer assert.NoError(t, os.Unsetenv(nu))
wg.Wait()
}
// verify empty string
assert.Equal(t, "", *figs.String(kName))
// assign value
figs.StoreString(kName, t.Name())
// verify value set
assert.Equal(t, t.Name(), *figs.String(kName))
// assign env value
changeEnv(kName, "Yeshua")
// verify value was assigned
assert.Equal(t, "Yeshua", *figs.String(kName))
// turn off env
figs.WithTreeRule(RuleNoEnv)
changeEnv(kName, "Andrei")
// value should stay Yeshua - message over messenger
assert.Equal(t, "Yeshua", *figs.String(kName))
}