| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package event_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"golang.org/x/exp/event" |
| 12 |
) |
| 13 |
|
| 14 |
func TestOfAs(t *testing.T) { |
| 15 |
const i = 3 |
| 16 |
var v event.Label |
| 17 |
v = event.Int64("key", i) |
| 18 |
if got := v.Int64(); got != i { |
| 19 |
t.Errorf("got %v, want %v", got, i) |
| 20 |
} |
| 21 |
v = event.Uint64("key", i) |
| 22 |
if got := v.Uint64(); got != i { |
| 23 |
t.Errorf("got %v, want %v", got, i) |
| 24 |
} |
| 25 |
v = event.Float64("key", i) |
| 26 |
if got := v.Float64(); got != i { |
| 27 |
t.Errorf("got %v, want %v", got, i) |
| 28 |
} |
| 29 |
v = event.Bool("key", true) |
| 30 |
if got := v.Bool(); got != true { |
| 31 |
t.Errorf("got %v, want %v", got, true) |
| 32 |
} |
| 33 |
const s = "foo" |
| 34 |
v = event.String("key", s) |
| 35 |
if got := v.String(); got != s { |
| 36 |
t.Errorf("got %v, want %v", got, s) |
| 37 |
} |
| 38 |
v = event.Bytes("key", []byte(s)) |
| 39 |
if got := v.Bytes(); string(got) != s { |
| 40 |
t.Errorf("got %v, want %v", got, s) |
| 41 |
} |
| 42 |
tm := time.Now() |
| 43 |
v = event.Value("key", tm) |
| 44 |
if got := v.Interface(); got != tm { |
| 45 |
t.Errorf("got %v, want %v", got, tm) |
| 46 |
} |
| 47 |
var vnil event.Label |
| 48 |
if got := vnil.Interface(); got != nil { |
| 49 |
t.Errorf("got %v, want nil", got) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestEqual(t *testing.T) { |
| 54 |
var x, y int |
| 55 |
vals := []event.Label{ |
| 56 |
{}, |
| 57 |
event.Int64("key", 1), |
| 58 |
event.Int64("key", 2), |
| 59 |
event.Uint64("key", 3), |
| 60 |
event.Uint64("key", 4), |
| 61 |
event.Float64("key", 3.5), |
| 62 |
event.Float64("key", 3.7), |
| 63 |
event.Bool("key", true), |
| 64 |
event.Bool("key", false), |
| 65 |
event.Value("key", &x), |
| 66 |
event.Value("key", &y), |
| 67 |
} |
| 68 |
for i, v1 := range vals { |
| 69 |
for j, v2 := range vals { |
| 70 |
got := v1.Equal(v2) |
| 71 |
want := i == j |
| 72 |
if got != want { |
| 73 |
t.Errorf("%v.Equal(%v): got %t, want %t", v1, v2, got, want) |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func panics(f func()) (b bool) { |
| 80 |
defer func() { |
| 81 |
if x := recover(); x != nil { |
| 82 |
b = true |
| 83 |
} |
| 84 |
}() |
| 85 |
f() |
| 86 |
return false |
| 87 |
} |
| 88 |
|
| 89 |
func TestPanics(t *testing.T) { |
| 90 |
for _, test := range []struct { |
| 91 |
name string |
| 92 |
f func() |
| 93 |
}{ |
| 94 |
{"int64", func() { event.Float64("key", 3).Int64() }}, |
| 95 |
{"uint64", func() { event.Int64("key", 3).Uint64() }}, |
| 96 |
{"float64", func() { event.Uint64("key", 3).Float64() }}, |
| 97 |
{"bool", func() { event.Int64("key", 3).Bool() }}, |
| 98 |
{"duration", func() { event.Value("key", "value").Duration() }}, |
| 99 |
{"bytes", func() { event.String("key", "value").Bytes() }}, |
| 100 |
} { |
| 101 |
if !panics(test.f) { |
| 102 |
t.Errorf("%s: got no panic, want panic", test.name) |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestString(t *testing.T) { |
| 108 |
for _, test := range []struct { |
| 109 |
v event.Label |
| 110 |
want string |
| 111 |
}{ |
| 112 |
{event.Int64("key", -3), "-3"}, |
| 113 |
{event.Uint64("key", 3), "3"}, |
| 114 |
{event.Float64("key", .15), "0.15"}, |
| 115 |
{event.Bool("key", true), "true"}, |
| 116 |
{event.String("key", "foo"), "foo"}, |
| 117 |
{event.Value("key", time.Duration(3*time.Second)), "3s"}, |
| 118 |
} { |
| 119 |
if got := test.v.String(); got != test.want { |
| 120 |
t.Errorf("%#v: got %q, want %q", test.v, got, test.want) |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func TestNoAlloc(t *testing.T) { |
| 126 |
|
| 127 |
var ( |
| 128 |
i int64 |
| 129 |
u uint64 |
| 130 |
f float64 |
| 131 |
b bool |
| 132 |
s string |
| 133 |
x interface{} |
| 134 |
p = &i |
| 135 |
) |
| 136 |
a := int(testing.AllocsPerRun(5, func() { |
| 137 |
i = event.Int64("key", 1).Int64() |
| 138 |
u = event.Uint64("key", 1).Uint64() |
| 139 |
f = event.Float64("key", 1).Float64() |
| 140 |
b = event.Bool("key", true).Bool() |
| 141 |
s = event.String("key", "foo").String() |
| 142 |
x = event.Value("key", p).Interface() |
| 143 |
})) |
| 144 |
if a != 0 { |
| 145 |
t.Errorf("got %d allocs, want zero", a) |
| 146 |
} |
| 147 |
_ = u |
| 148 |
_ = f |
| 149 |
_ = b |
| 150 |
_ = s |
| 151 |
_ = x |
| 152 |
} |