1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package flags
16
17 import (
18 "flag"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 )
23
24 func TestUint32Value(t *testing.T) {
25 cases := []struct {
26 name string
27 s string
28 expectedVal uint32
29 expectError bool
30 }{
31 {
32 name: "normal uint32 value",
33 s: "200",
34 expectedVal: 200,
35 },
36 {
37 name: "zero value",
38 s: "0",
39 expectedVal: 0,
40 },
41 {
42 name: "negative int value",
43 s: "-200",
44 expectError: true,
45 },
46 {
47 name: "invalid integer value",
48 s: "invalid",
49 expectError: true,
50 },
51 }
52 for _, tc := range cases {
53 t.Run(tc.name, func(t *testing.T) {
54 var val uint32Value
55 err := val.Set(tc.s)
56
57 if tc.expectError {
58 if err == nil {
59 t.Errorf("Expected failure on parsing uint32 value from %s", tc.s)
60 }
61 } else {
62 if err != nil {
63 t.Errorf("Unexpected error when parsing %s: %v", tc.s, err)
64 }
65 assert.Equal(t, uint32(val), tc.expectedVal)
66 }
67 })
68 }
69 }
70
71 func TestUint32FromFlag(t *testing.T) {
72 const flagName = "max-concurrent-streams"
73
74 cases := []struct {
75 name string
76 defaultVal uint32
77 arguments []string
78 expectedVal uint32
79 }{
80 {
81 name: "only default value",
82 defaultVal: 15,
83 arguments: []string{},
84 expectedVal: 15,
85 },
86 {
87 name: "argument has different value from the default one",
88 defaultVal: 16,
89 arguments: []string{"--max-concurrent-streams", "200"},
90 expectedVal: 200,
91 },
92 {
93 name: "argument has the same value from the default one",
94 defaultVal: 105,
95 arguments: []string{"--max-concurrent-streams", "105"},
96 expectedVal: 105,
97 },
98 }
99
100 for _, tc := range cases {
101 t.Run(tc.name, func(t *testing.T) {
102 fs := flag.NewFlagSet("etcd", flag.ContinueOnError)
103 fs.Var(NewUint32Value(tc.defaultVal), flagName, "Maximum concurrent streams that each client can open at a time.")
104 if err := fs.Parse(tc.arguments); err != nil {
105 t.Fatalf("Unexpected error: %v\n", err)
106 }
107 actualMaxStream := Uint32FromFlag(fs, flagName)
108 assert.Equal(t, actualMaxStream, tc.expectedVal)
109 })
110 }
111 }
112
View as plain text