-
Notifications
You must be signed in to change notification settings - Fork 0
/
nildptr_test.go
65 lines (60 loc) · 1.69 KB
/
nildptr_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2020 Laurent Demailly. All Rights Reserved.
// See LICENSE for licensing terms.
// Tests the fix for #375
// panic: runtime error: invalid memory address or nil pointer dereference
// through flag.isZeroValue() -> dyn .Get()
package dflag
import (
"bytes"
"flag"
"fmt"
"strings"
"testing"
"time"
)
type foo struct{}
// Started as the -h not crashing test but now also checks value name etc.
func TestDynFlagPrintDefaultsNotCrashing(t *testing.T) {
fs := flag.NewFlagSet("test", flag.PanicOnError)
DynBool(fs, "a_boolean", false, "b")
DynDuration(fs, "d", 0*time.Second, "d")
DynFloat64(fs, "f", 0, "f")
DynInt64(fs, "int_with_named_type", 0, "`some` value")
DynJSON(fs, "j", &foo{}, "j")
DynString(fs, "s", "", "s")
DynStringSet(fs, "sset", []string{}, "sset")
DynStringSlice(fs, "sslice", []string{}, "sslice")
var b bytes.Buffer
fs.SetOutput(&b)
fs.PrintDefaults() // no crash/panic == test passes
s := b.String()
fmt.Printf("got output %q\n", s)
good := 0
for _, line := range strings.Split(s, "\n") {
fmt.Printf("line is %q\n", line)
if strings.Contains(line, "-a_boolean") {
good++
if strings.Contains(line, "value") {
t.Errorf("line %q should not have value (boolean flag)", line)
}
continue
}
if strings.Contains(line, "-int_with_named_type") {
good++
if !strings.Contains(line, "some") {
t.Errorf("line %q should use the `` named param", line)
}
continue
}
if strings.Contains(line, "-f ") {
good++
if !strings.Contains(line, "value") {
t.Errorf("line %q should have \"value\" to show flag value is needed", line)
}
continue
}
}
if good != 3 {
t.Errorf("missing expected 3 flags tested in output, got %v", good)
}
}