File: string_array_flag_test.go

package info (click to toggle)
golang-github-grpc-ecosystem-grpc-gateway.v2 2.11.3-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,148 kB
  • sloc: javascript: 352; makefile: 136; sh: 26
file content (50 lines) | stat: -rw-r--r-- 1,052 bytes parent folder | download | duplicates (2)
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
package utilities_test

import (
	"flag"
	"reflect"
	"testing"

	"github.com/grpc-ecosystem/grpc-gateway/v2/utilities"
)

func TestStringArrayFlag(t *testing.T) {
	tests := []struct {
		name  string
		flags []string
		want  string
	}{
		{
			name:  "No Value",
			flags: []string{},
			want:  "",
		},
		{
			name:  "Single Value",
			flags: []string{"--my_flag=1"},
			want:  "1",
		},
		{
			name:  "Repeated Value",
			flags: []string{"--my_flag=1", "--my_flag=2"},
			want:  "1,2",
		},
		{
			name:  "Repeated Same Value",
			flags: []string{"--my_flag=1", "--my_flag=1"},
			want:  "1,1",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			flagSet := flag.NewFlagSet("test", flag.PanicOnError)
			result := utilities.StringArrayFlag(flagSet, "my_flag", "repeated flag")
			if err := flagSet.Parse(tt.flags); err != nil {
				t.Errorf("flagSet.Parse() failed with %v", err)
			}
			if !reflect.DeepEqual(result.String(), tt.want) {
				t.Errorf("StringArrayFlag() = %v, want %v", result.String(), tt.want)
			}
		})
	}
}