-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain_test.go
81 lines (65 loc) · 1.43 KB
/
chain_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
package chain
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewChain(t *testing.T) {
foo := NewFoo()
s1 := foo.Foo("foo")
assert.Equal(t, "foo & f1 & f2 & f3", s1)
s2 := foo.Bar("bar")
assert.Equal(t, "bar & b1 & b2", s2)
}
func NewFoo() FooInterface {
return New(
func(next interface{}) interface{} {
return &f1{FooInterface: next.(FooInterface)}
},
func(next interface{}) interface{} {
return &f2{FooInterface: next.(FooInterface)}
},
func(next interface{}) interface{} {
return &f3{FooInterface: next.(FooInterface)}
},
)(&f{}).(FooInterface)
}
type FooInterface interface {
Foo(s string) string
Bar(s string) string
}
type f struct {
}
func (f) Foo(s string) string {
return s
}
func (f) Bar(s string) string {
return s
}
type f1 struct {
FooInterface
}
func (r f1) Foo(s string) string {
return r.FooInterface.Foo(fmt.Sprintf("%s & %s", s, "f1"))
}
func (r f1) Bar(s string) string {
return r.FooInterface.Bar(fmt.Sprintf("%s & %s", s, "b1"))
}
type f2 struct {
FooInterface
}
func (r f2) Foo(s string) string {
return r.FooInterface.Foo(fmt.Sprintf("%s & %s", s, "f2"))
}
func (r f2) Bar(s string) string {
return fmt.Sprintf("%s & %s", s, "b2")
}
type f3 struct {
FooInterface
}
func (r f3) Foo(s string) string {
return r.FooInterface.Foo(fmt.Sprintf("%s & %s", s, "f3"))
}
func (r f3) Bar(s string) string {
return r.FooInterface.Bar(fmt.Sprintf("%s & %s", s, "b3"))
}