Add _test for the net.IP flag type
diff --git a/ip.go b/ip.go
index 746eefd..baa442b 100644
--- a/ip.go
+++ b/ip.go
@@ -3,8 +3,11 @@
import (
"fmt"
"net"
+ "strings"
)
+var _ = strings.TrimSpace
+
// -- net.IP value
type ipValue net.IP
@@ -15,7 +18,7 @@
func (i *ipValue) String() string { return net.IP(*i).String() }
func (i *ipValue) Set(s string) error {
- ip := net.ParseIP(s)
+ ip := net.ParseIP(strings.TrimSpace(s))
if ip == nil {
return fmt.Errorf("failed to parse IP: %q", s)
}
diff --git a/ip_test.go b/ip_test.go
new file mode 100644
index 0000000..1fec50e
--- /dev/null
+++ b/ip_test.go
@@ -0,0 +1,63 @@
+package pflag
+
+import (
+ "fmt"
+ "net"
+ "os"
+ "testing"
+)
+
+func setUpIP(ip *net.IP) *FlagSet {
+ f := NewFlagSet("test", ContinueOnError)
+ f.IPVar(ip, "address", net.ParseIP("0.0.0.0"), "IP Address")
+ return f
+}
+
+func TestIP(t *testing.T) {
+ testCases := []struct {
+ input string
+ success bool
+ expected string
+ }{
+ {"0.0.0.0", true, "0.0.0.0"},
+ {" 0.0.0.0 ", true, "0.0.0.0"},
+ {"1.2.3.4", true, "1.2.3.4"},
+ {"127.0.0.1", true, "127.0.0.1"},
+ {"255.255.255.255", true, "255.255.255.255"},
+ {"", false, ""},
+ {"0", false, ""},
+ {"localhost", false, ""},
+ {"0.0.0", false, ""},
+ {"0.0.0.", false, ""},
+ {"0.0.0.0.", false, ""},
+ {"0.0.0.256", false, ""},
+ {"0 . 0 . 0 . 0", false, ""},
+ }
+
+ devnull, _ := os.Open(os.DevNull)
+ os.Stderr = devnull
+ for i := range testCases {
+ var addr net.IP
+ f := setUpIP(&addr)
+
+ tc := &testCases[i]
+
+ arg := fmt.Sprintf("--address=%s", tc.input)
+ err := f.Parse([]string{arg})
+ if err != nil && tc.success == true {
+ t.Errorf("expected success, got %q", err)
+ continue
+ } else if err == nil && tc.success == false {
+ t.Errorf("expected failure")
+ continue
+ } else if tc.success {
+ ip, err := f.GetIP("address")
+ if err != nil {
+ t.Errorf("Got error trying to fetch the IP flag: %v", err)
+ }
+ if ip.String() != tc.expected {
+ t.Errorf("expected %q, got %q", tc.expected, ip.String())
+ }
+ }
+ }
+}