forked from rjeczalik/interfaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
131 lines (112 loc) · 2.92 KB
/
example_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package interfaces_test
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"github.com/rjeczalik/interfaces"
"github.com/rjeczalik/interfaces/testdata/generic"
"github.com/rjeczalik/interfaces/testdata/util"
)
type Generic1[A any] struct {
Value A
}
type Generic2[A any, B any] struct {
Value A
Value2 B
}
type ExampleFoo int
type ExampleBar struct{}
type ExampleBaz struct {
*ExampleBar
}
func (ExampleBar) A(int) int {
return 0
}
func (*ExampleBar) B(*string, io.Writer, ExampleFoo) (*ExampleFoo, int) {
return nil, 0
}
func (ExampleBar) C(map[string]int, *interfaces.Options, *http.Client) (chan []string, error) {
return nil, nil
}
func (ExampleBaz) D(*map[interface{}]struct{}, interface{}) (chan struct{}, []interface{}) {
return nil, nil
}
func (*ExampleBaz) E(*[]map[*flag.FlagSet]struct{}, [3]string) {}
func (*ExampleBaz) F(v Generic1[io.Writer]) (Generic2[io.Writer, ExampleFoo], int) {
return Generic2[io.Writer, ExampleFoo]{}, 0
}
func (*ExampleBaz) G(v Generic2[string, io.Writer]) (Generic1[int], int) {
return Generic1[int]{}, 0
}
func (*ExampleBaz) H(ctx context.Context) (*generic.MyGeneric[util.MyUtil], error) {
return nil, nil
}
func ExampleNew() {
i, err := interfaces.New(`github.com/rjeczalik/interfaces.ExampleBaz`)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Interface:")
for _, fn := range i {
fmt.Println(fn)
}
fmt.Println("Dependencies:")
for _, dep := range i.Deps() {
fmt.Println(dep)
}
// Output: Interface:
// A(int) int
// B(*string, io.Writer, interfaces_test.ExampleFoo) (*interfaces_test.ExampleFoo, int)
// C(map[string]int, *interfaces.Options, *http.Client) (chan []string, error)
// D(*map[interface{}]struct{}, interface{}) (chan struct{}, []interface{})
// E(*[]map[*flag.FlagSet]struct{}, [3]string)
// F(interfaces_test.Generic1[io.Writer]) (interfaces_test.Generic2[io.Writer, interfaces_test.ExampleFoo], int)
// G(interfaces_test.Generic2[string, io.Writer]) (interfaces_test.Generic1[int], int)
// H(context.Context) (*generic.MyGeneric[util.MyUtil], error)
// Dependencies:
// context
// flag
// github.com/rjeczalik/interfaces
// github.com/rjeczalik/interfaces/testdata/generic
// github.com/rjeczalik/interfaces/testdata/util
// github.com/rjeczalik/interfaces_test
// io
// net/http
}
func ExampleNewWithOptions() {
opts := &interfaces.Options{
Query: &interfaces.Query{
Package: "net",
TypeName: "Interface",
},
}
i, err := interfaces.NewWithOptions(opts)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Interface:")
for _, fn := range i {
fmt.Println(fn)
}
fmt.Println("Dependencies:")
for _, dep := range i.Deps() {
fmt.Println(dep)
}
// Output: Interface:
// Addrs() ([]net.Addr, error)
// MulticastAddrs() ([]net.Addr, error)
// Dependencies:
// net
}
func ExampleFunc_String() {
f := interfaces.Func{
Name: "Close",
Outs: []interfaces.Type{{Name: "error"}},
}
fmt.Println(f)
// Output: Close() error
}