MCPcopy
hub / github.com/spf13/pflag / TestAnnotation

Function TestAnnotation

flag_test.go:142–176  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

140}
141
142func TestAnnotation(t *testing.T) {
143 f := NewFlagSet("shorthand", ContinueOnError)
144
145 err := f.SetAnnotation("missing-flag", "key", nil)
146 expectedErr := "no such flag -missing-flag"
147 if err == nil {
148 t.Errorf("Expected error setting annotation on non-existent flag")
149 }
150 if err.Error() != expectedErr {
151 t.Errorf("expected error %q, got %q", expectedErr, err.Error())
152 }
153
154 f.StringP("stringa", "a", "", "string value")
155 if err := f.SetAnnotation("stringa", "key", nil); err != nil {
156 t.Errorf("Unexpected error setting new nil annotation: %v", err)
157 }
158 if annotation := f.Lookup("stringa").Annotations["key"]; annotation != nil {
159 t.Errorf("Unexpected annotation: %v", annotation)
160 }
161
162 f.StringP("stringb", "b", "", "string2 value")
163 if err := f.SetAnnotation("stringb", "key", []string{"value1"}); err != nil {
164 t.Errorf("Unexpected error setting new annotation: %v", err)
165 }
166 if annotation := f.Lookup("stringb").Annotations["key"]; !reflect.DeepEqual(annotation, []string{"value1"}) {
167 t.Errorf("Unexpected annotation: %v", annotation)
168 }
169
170 if err := f.SetAnnotation("stringb", "key", []string{"value2"}); err != nil {
171 t.Errorf("Unexpected error updating annotation: %v", err)
172 }
173 if annotation := f.Lookup("stringb").Annotations["key"]; !reflect.DeepEqual(annotation, []string{"value2"}) {
174 t.Errorf("Unexpected annotation: %v", annotation)
175 }
176}
177
178func TestName(t *testing.T) {
179 flagSetName := "bob"

Callers

nothing calls this directly

Calls 5

SetAnnotationMethod · 0.95
StringPMethod · 0.95
LookupMethod · 0.95
NewFlagSetFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected