| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package srcutil_test |
| 17 |
|
| 18 |
import ( |
| 19 |
"fmt" |
| 20 |
"reflect" |
| 21 |
"testing" |
| 22 |
|
| 23 |
. "github.com/jacobsa/oglematchers" |
| 24 |
. "github.com/jacobsa/ogletest" |
| 25 |
"github.com/jacobsa/ogletest/srcutil" |
| 26 |
) |
| 27 |
|
| 28 |
func TestRegisterMethodsTest(t *testing.T) { RunTests(t) } |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
type MethodsTest struct { |
| 35 |
} |
| 36 |
|
| 37 |
func init() { RegisterTestSuite(&MethodsTest{}) } |
| 38 |
|
| 39 |
type OneMethodType int |
| 40 |
|
| 41 |
func (x OneMethodType) Foo() {} |
| 42 |
|
| 43 |
type MultipleMethodsType int |
| 44 |
|
| 45 |
func (x MultipleMethodsType) Foo() {} |
| 46 |
func (x MultipleMethodsType) Bar() {} |
| 47 |
func (x MultipleMethodsType) Baz() {} |
| 48 |
|
| 49 |
type methodNameMatcher struct { |
| 50 |
expected string |
| 51 |
} |
| 52 |
|
| 53 |
func (m *methodNameMatcher) Description() string { |
| 54 |
return fmt.Sprintf("method named %s", m.expected) |
| 55 |
} |
| 56 |
|
| 57 |
func (m *methodNameMatcher) Matches(x interface{}) error { |
| 58 |
method, ok := x.(reflect.Method) |
| 59 |
if !ok { |
| 60 |
panic("Invalid argument.") |
| 61 |
} |
| 62 |
|
| 63 |
if method.Name != m.expected { |
| 64 |
return fmt.Errorf("whose name is %s", method.Name) |
| 65 |
} |
| 66 |
|
| 67 |
return nil |
| 68 |
} |
| 69 |
|
| 70 |
func NameIs(name string) Matcher { |
| 71 |
return &methodNameMatcher{name} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
func (t *MethodsTest) NoMethods() { |
| 79 |
type foo int |
| 80 |
|
| 81 |
methods := srcutil.GetMethodsInSourceOrder(reflect.TypeOf(foo(17))) |
| 82 |
ExpectThat(methods, ElementsAre()) |
| 83 |
} |
| 84 |
|
| 85 |
func (t *MethodsTest) OneMethod() { |
| 86 |
methods := srcutil.GetMethodsInSourceOrder(reflect.TypeOf(OneMethodType(17))) |
| 87 |
ExpectThat( |
| 88 |
methods, |
| 89 |
ElementsAre( |
| 90 |
NameIs("Foo"), |
| 91 |
)) |
| 92 |
} |
| 93 |
|
| 94 |
func (t *MethodsTest) MultipleMethods() { |
| 95 |
methods := srcutil.GetMethodsInSourceOrder(reflect.TypeOf(MultipleMethodsType(17))) |
| 96 |
ExpectThat( |
| 97 |
methods, |
| 98 |
ElementsAre( |
| 99 |
NameIs("Foo"), |
| 100 |
NameIs("Bar"), |
| 101 |
NameIs("Baz"), |
| 102 |
)) |
| 103 |
|
| 104 |
ExpectEq("Foo", methods[0].Name) |
| 105 |
ExpectEq("Bar", methods[1].Name) |
| 106 |
ExpectEq("Baz", methods[2].Name) |
| 107 |
} |