summaryrefslogtreecommitdiff
path: root/srv/src/cfg/cfg_test.go
blob: 8266c897ab3966c6bd30d411e2ba81547d7fa87c (plain)
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
package cfg

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestStringVar(t *testing.T) {

	cfg := New(Params{
		Args: []string{"--foo=CLI"},
		Env:  map[string]string{"FOO": "ENV", "BAR": "ENV"},
	})

	var foo, bar, baz string

	cfg.StringVar(&foo, "foo", "DEF", "")
	cfg.StringVar(&bar, "bar", "DEF", "")
	cfg.StringVar(&baz, "baz", "DEF", "")

	assert.NoError(t, cfg.Init(context.Background()))
	assert.Equal(t, "CLI", foo)
	assert.Equal(t, "ENV", bar)
	assert.Equal(t, "DEF", baz)
}

func TestIntVar(t *testing.T) {

	cfg := New(Params{
		Args: []string{"--foo=111"},
		Env:  map[string]string{"FOO": "222", "BAR": "222"},
	})

	var foo, bar, baz int

	cfg.IntVar(&foo, "foo", 333, "")
	cfg.IntVar(&bar, "bar", 333, "")
	cfg.IntVar(&baz, "baz", 333, "")

	assert.NoError(t, cfg.Init(context.Background()))
	assert.Equal(t, 111, foo)
	assert.Equal(t, 222, bar)
	assert.Equal(t, 333, baz)
}

func TestBoolVar(t *testing.T) {

	cfg := New(Params{
		Args: []string{"--foo=1"},
		Env:  map[string]string{"FOO": "0", "BAR": "anything", "BIZ": "0"},
	})

	var foo, bar, baz, biz bool

	cfg.BoolVar(&foo, "foo", false, "")
	cfg.BoolVar(&bar, "bar", false, "")
	cfg.BoolVar(&baz, "baz", false, "")
	cfg.BoolVar(&biz, "biz", true, "")

	assert.NoError(t, cfg.Init(context.Background()))
	assert.Equal(t, true, foo)
	assert.Equal(t, true, bar)
	assert.Equal(t, false, baz)
	assert.Equal(t, false, biz)
}