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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
package web
import (
"testing"
)
// To tell you the truth, I'm not actually sure how many of these cases are
// needed. Presumably someone with more patience than I could comb through
// http://golang.org/s/go11func and figure out what all the different cases I
// ought to test are, but I think this test includes all the cases I care about
// and is at least reasonably thorough.
func a() string {
return "A"
}
func b() string {
return "B"
}
func mkFn(s string) func() string {
return func() string {
return s
}
}
var c = mkFn("C")
var d = mkFn("D")
var e = a
var f = c
var g = mkFn("D")
type Type string
func (t *Type) String() string {
return string(*t)
}
var t1 = Type("hi")
var t2 = Type("bye")
var t1f = t1.String
var t2f = t2.String
var funcEqualTests = []struct {
a, b func() string
result bool
}{
{a, a, true},
{a, b, false},
{b, b, true},
{a, c, false},
{c, c, true},
{c, d, false},
{a, e, true},
{a, f, false},
{c, f, true},
{e, f, false},
{d, g, false},
{t1f, t1f, true},
{t1f, t2f, false},
}
func TestFuncEqual(t *testing.T) {
t.Parallel()
for _, test := range funcEqualTests {
r := funcEqual(test.a, test.b)
if r != test.result {
t.Errorf("funcEqual(%p, %p) should have been %v",
test.a, test.b, test.result)
}
}
h := mkFn("H")
i := h
j := mkFn("H")
k := a
if !funcEqual(h, i) {
t.Errorf("h and i should have been equal")
}
if funcEqual(h, j) {
t.Errorf("h and j should not have been equal")
}
if !funcEqual(a, k) {
t.Errorf("a and k should have been equal")
}
}
|