1// Copyright 2009 The Go Authors. All rights reserved. 2// Use of this source code ds governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package pflag 6 7import ( 8 "fmt" 9 "strings" 10 "testing" 11 "time" 12) 13 14func setUpDSFlagSet(dsp *[]time.Duration) *FlagSet { 15 f := NewFlagSet("test", ContinueOnError) 16 f.DurationSliceVar(dsp, "ds", []time.Duration{}, "Command separated list!") 17 return f 18} 19 20func setUpDSFlagSetWithDefault(dsp *[]time.Duration) *FlagSet { 21 f := NewFlagSet("test", ContinueOnError) 22 f.DurationSliceVar(dsp, "ds", []time.Duration{0, 1}, "Command separated list!") 23 return f 24} 25 26func TestEmptyDS(t *testing.T) { 27 var ds []time.Duration 28 f := setUpDSFlagSet(&ds) 29 err := f.Parse([]string{}) 30 if err != nil { 31 t.Fatal("expected no error; got", err) 32 } 33 34 getDS, err := f.GetDurationSlice("ds") 35 if err != nil { 36 t.Fatal("got an error from GetDurationSlice():", err) 37 } 38 if len(getDS) != 0 { 39 t.Fatalf("got ds %v with len=%d but expected length=0", getDS, len(getDS)) 40 } 41} 42 43func TestDS(t *testing.T) { 44 var ds []time.Duration 45 f := setUpDSFlagSet(&ds) 46 47 vals := []string{"1ns", "2ms", "3m", "4h"} 48 arg := fmt.Sprintf("--ds=%s", strings.Join(vals, ",")) 49 err := f.Parse([]string{arg}) 50 if err != nil { 51 t.Fatal("expected no error; got", err) 52 } 53 for i, v := range ds { 54 d, err := time.ParseDuration(vals[i]) 55 if err != nil { 56 t.Fatalf("got error: %v", err) 57 } 58 if d != v { 59 t.Fatalf("expected ds[%d] to be %s but got: %d", i, vals[i], v) 60 } 61 } 62 getDS, err := f.GetDurationSlice("ds") 63 if err != nil { 64 t.Fatalf("got error: %v", err) 65 } 66 for i, v := range getDS { 67 d, err := time.ParseDuration(vals[i]) 68 if err != nil { 69 t.Fatalf("got error: %v", err) 70 } 71 if d != v { 72 t.Fatalf("expected ds[%d] to be %s but got: %d from GetDurationSlice", i, vals[i], v) 73 } 74 } 75} 76 77func TestDSDefault(t *testing.T) { 78 var ds []time.Duration 79 f := setUpDSFlagSetWithDefault(&ds) 80 81 vals := []string{"0s", "1ns"} 82 83 err := f.Parse([]string{}) 84 if err != nil { 85 t.Fatal("expected no error; got", err) 86 } 87 for i, v := range ds { 88 d, err := time.ParseDuration(vals[i]) 89 if err != nil { 90 t.Fatalf("got error: %v", err) 91 } 92 if d != v { 93 t.Fatalf("expected ds[%d] to be %d but got: %d", i, d, v) 94 } 95 } 96 97 getDS, err := f.GetDurationSlice("ds") 98 if err != nil { 99 t.Fatal("got an error from GetDurationSlice():", err) 100 } 101 for i, v := range getDS { 102 d, err := time.ParseDuration(vals[i]) 103 if err != nil { 104 t.Fatal("got an error from GetDurationSlice():", err) 105 } 106 if d != v { 107 t.Fatalf("expected ds[%d] to be %d from GetDurationSlice but got: %d", i, d, v) 108 } 109 } 110} 111 112func TestDSWithDefault(t *testing.T) { 113 var ds []time.Duration 114 f := setUpDSFlagSetWithDefault(&ds) 115 116 vals := []string{"1ns", "2ns"} 117 arg := fmt.Sprintf("--ds=%s", strings.Join(vals, ",")) 118 err := f.Parse([]string{arg}) 119 if err != nil { 120 t.Fatal("expected no error; got", err) 121 } 122 for i, v := range ds { 123 d, err := time.ParseDuration(vals[i]) 124 if err != nil { 125 t.Fatalf("got error: %v", err) 126 } 127 if d != v { 128 t.Fatalf("expected ds[%d] to be %d but got: %d", i, d, v) 129 } 130 } 131 132 getDS, err := f.GetDurationSlice("ds") 133 if err != nil { 134 t.Fatal("got an error from GetDurationSlice():", err) 135 } 136 for i, v := range getDS { 137 d, err := time.ParseDuration(vals[i]) 138 if err != nil { 139 t.Fatalf("got error: %v", err) 140 } 141 if d != v { 142 t.Fatalf("expected ds[%d] to be %d from GetDurationSlice but got: %d", i, d, v) 143 } 144 } 145} 146 147func TestDSAsSliceValue(t *testing.T) { 148 var ds []time.Duration 149 f := setUpDSFlagSet(&ds) 150 151 in := []string{"1ns", "2ns"} 152 argfmt := "--ds=%s" 153 arg1 := fmt.Sprintf(argfmt, in[0]) 154 arg2 := fmt.Sprintf(argfmt, in[1]) 155 err := f.Parse([]string{arg1, arg2}) 156 if err != nil { 157 t.Fatal("expected no error; got", err) 158 } 159 160 f.VisitAll(func(f *Flag) { 161 if val, ok := f.Value.(SliceValue); ok { 162 _ = val.Replace([]string{"3ns"}) 163 } 164 }) 165 if len(ds) != 1 || ds[0] != time.Duration(3) { 166 t.Fatalf("Expected ss to be overwritten with '3ns', but got: %v", ds) 167 } 168} 169 170func TestDSCalledTwice(t *testing.T) { 171 var ds []time.Duration 172 f := setUpDSFlagSet(&ds) 173 174 in := []string{"1ns,2ns", "3ns"} 175 expected := []time.Duration{1, 2, 3} 176 argfmt := "--ds=%s" 177 arg1 := fmt.Sprintf(argfmt, in[0]) 178 arg2 := fmt.Sprintf(argfmt, in[1]) 179 err := f.Parse([]string{arg1, arg2}) 180 if err != nil { 181 t.Fatal("expected no error; got", err) 182 } 183 for i, v := range ds { 184 if expected[i] != v { 185 t.Fatalf("expected ds[%d] to be %d but got: %d", i, expected[i], v) 186 } 187 } 188} 189