Merge pull request #125 from marcellmartini/master

Refactory of multiValueFlag.String()
This commit is contained in:
Martin Polden 2023-05-21 20:25:25 +02:00 committed by GitHub
commit b2859dc565
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"flag"
"log"
"strings"
"os"
@ -14,14 +15,7 @@ import (
type multiValueFlag []string
func (f *multiValueFlag) String() string {
vs := ""
for i, v := range *f {
vs += v
if i < len(*f)-1 {
vs += ", "
}
}
return vs
return strings.Join([]string(*f), ", ")
}
func (f *multiValueFlag) Set(v string) error {

42
cmd/echoip/main_test.go Normal file
View File

@ -0,0 +1,42 @@
package main
import "testing"
func TestMultiValueFlagString(t *testing.T) {
var xmvf = []struct {
values multiValueFlag
expect string
}{
{
values: multiValueFlag{
"test",
"with multiples",
"flags",
},
expect: `test, with multiples, flags`,
},
{
values: multiValueFlag{
"test",
},
expect: `test`,
},
{
values: multiValueFlag{
"",
},
expect: ``,
},
{
values: nil,
expect: ``,
},
}
for _, mvf := range xmvf {
got := mvf.values.String()
if got != mvf.expect {
t.Errorf("\nFor: %#v\nExpected: %v\nGot: %v", mvf.values, mvf.expect, got)
}
}
}