mirror of https://github.com/mpolden/echoip
Merge pull request #125 from marcellmartini/master
Refactory of multiValueFlag.String()
This commit is contained in:
commit
b2859dc565
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue