@@ -2,13 +2,14 @@ package pflag
22
33import (
44 "fmt"
5+ "strings"
56 "testing"
67 "time"
78)
89
910func setUpTimeVar (t * time.Time , formats []string ) * FlagSet {
1011 f := NewFlagSet ("test" , ContinueOnError )
11- f .TimeVar (t , "time" , time. Time {} , formats , "Time" )
12+ f .TimeVar (t , "time" , * t , formats , "Time" )
1213 return f
1314}
1415
@@ -60,3 +61,28 @@ func TestTime(t *testing.T) {
6061 }
6162 }
6263}
64+
65+ func usageForTimeFlagSet (t * testing.T , timeVar time.Time ) string {
66+ t .Helper ()
67+ formats := []string {time .RFC3339Nano , time .RFC1123Z }
68+ f := setUpTimeVar (& timeVar , formats )
69+ if err := f .Parse ([]string {}); err != nil {
70+ t .Fatalf ("expected success, got %q" , err )
71+ }
72+ return f .FlagUsages ()
73+ }
74+
75+ func TestTimeDefaultZero (t * testing.T ) {
76+ usage := usageForTimeFlagSet (t , time.Time {})
77+ if strings .Contains (usage , "default" ) {
78+ t .Errorf ("expected no default value in usage, got %q" , usage )
79+ }
80+ }
81+
82+ func TestTimeDefaultNonZero (t * testing.T ) {
83+ timeVar := time .Date (2025 , 1 , 1 , 1 , 1 , 1 , 0 , time .UTC )
84+ usage := usageForTimeFlagSet (t , timeVar )
85+ if ! strings .Contains (usage , "default" ) || ! strings .Contains (usage , "2025" ) {
86+ t .Errorf ("expected default value in usage, got %q" , usage )
87+ }
88+ }
0 commit comments