-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsize_var_test.go
57 lines (51 loc) · 1.23 KB
/
size_var_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
package goflags
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSizeVar(t *testing.T) {
t.Run("valid-size", func(t *testing.T) {
var fileSize Size
flagSet := NewFlagSet()
flagSet.CreateGroup("Config", "Config",
flagSet.SizeVarP(&fileSize, "max-size", "ms", "", "max size of the file"),
)
os.Args = []string{
os.Args[0],
"-max-size", "2kb",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, Size(2048), fileSize)
tearDown(t.Name())
})
t.Run("default-value", func(t *testing.T) {
var fileSize Size
flagSet := NewFlagSet()
flagSet.CreateGroup("Config", "Config",
flagSet.SizeVarP(&fileSize, "max-size", "ms", "2kb", "max size of the file"),
)
os.Args = []string{
os.Args[0],
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, Size(2048), fileSize)
tearDown(t.Name())
})
t.Run("without-unit", func(t *testing.T) {
var fileSize Size
err := fileSize.Set("2")
assert.Nil(t, err)
assert.Equal(t, Size(2097152), fileSize)
tearDown(t.Name())
})
t.Run("invalid-size-unit", func(t *testing.T) {
var fileSize Size
err := fileSize.Set("2kilobytes")
assert.NotNil(t, err)
assert.ErrorContains(t, err, "parse error")
tearDown(t.Name())
})
}