aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2023-05-21 20:25:25 +0200
committerGitHub <noreply@github.com>2023-05-21 20:25:25 +0200
commitb2859dc565afe12ae20a858874f8fa8654ce16f8 (patch)
treebb55a5585def6a9aed32d01a0e9c383bd865a0dd
parentffa6674637a5bf906d78ae6675f9a4680a78ab7b (diff)
parent11fc65639149bdb1297f93fe3dce105fbedbded5 (diff)
Merge pull request #125 from marcellmartini/master
Refactory of multiValueFlag.String()
-rw-r--r--cmd/echoip/main.go10
-rw-r--r--cmd/echoip/main_test.go42
2 files changed, 44 insertions, 8 deletions
diff --git a/cmd/echoip/main.go b/cmd/echoip/main.go
index 737a4f4..f3d4c94 100644
--- a/cmd/echoip/main.go
+++ b/cmd/echoip/main.go
@@ -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 {
diff --git a/cmd/echoip/main_test.go b/cmd/echoip/main_test.go
new file mode 100644
index 0000000..045a82a
--- /dev/null
+++ b/cmd/echoip/main_test.go
@@ -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)
+ }
+ }
+}