Debian Codegraph @

srcutil/methods_test.go

raw symbols SCIP
1 // Copyright 2012 Aaron Jacobs. All Rights Reserved.
2 // Author: [email protected] (Aaron Jacobs)
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
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 // Helpers
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 // Tests
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 }