@@ -2,6 +2,7 @@ package flagsfiller_test
22
33import (
44 "flag"
5+ "log/slog"
56 "net"
67 "net/netip"
78 "testing"
@@ -117,3 +118,59 @@ func TestTextUnmarshalerType(t *testing.T) {
117118
118119 assert .Equal (t , netip .AddrFrom4 ([4 ]byte {1 , 2 , 3 , 4 }), config .Addr )
119120}
121+
122+ func TestSlogLevels (t * testing.T ) {
123+ tests := []struct {
124+ name string
125+ value string
126+ expected slog.Level
127+ }{
128+ {
129+ name : "info" ,
130+ value : "info" ,
131+ expected : slog .LevelInfo ,
132+ },
133+ {
134+ name : "error" ,
135+ value : "error" ,
136+ expected : slog .LevelError ,
137+ },
138+ {
139+ name : "numeric offset" ,
140+ // Borrowed from https://pkg.go.dev/log/slog#Level.UnmarshalText
141+ value : "Error-8" ,
142+ expected : slog .LevelInfo ,
143+ },
144+ }
145+ for _ , test := range tests {
146+ t .Run (test .name , func (t * testing.T ) {
147+ var args struct {
148+ Level slog.Level
149+ }
150+
151+ var flagset flag.FlagSet
152+ err := flagsfiller .New ().Fill (& flagset , & args )
153+ require .NoError (t , err )
154+
155+ err = flagset .Parse ([]string {"--level" , test .value })
156+ require .NoError (t , err )
157+
158+ assert .Equal (t , test .expected , args .Level )
159+ })
160+ }
161+ }
162+
163+ func TestSlogLevelWithDefault (t * testing.T ) {
164+ var args struct {
165+ Level slog.Level `default:"info"`
166+ }
167+
168+ var flagset flag.FlagSet
169+ err := flagsfiller .New ().Fill (& flagset , & args )
170+ require .NoError (t , err )
171+
172+ err = flagset .Parse ([]string {})
173+ require .NoError (t , err )
174+
175+ assert .Equal (t , slog .LevelInfo , args .Level )
176+ }
0 commit comments