-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_test.go
More file actions
76 lines (60 loc) · 1.06 KB
/
base_test.go
File metadata and controls
76 lines (60 loc) · 1.06 KB
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
package base
import (
"testing"
"github.com/stretchr/testify/assert"
)
type Bird struct {
Base
}
// BirdI defines the virtual functions of Bird.
type BirdI interface {
BaseI
Call() string
}
func NewBird() *Bird {
b := new(Bird)
b.Init(b)
return b
}
func (a *Bird) GetCall() string {
return a.this().Call()
}
// Call can be overridden by a subclass.
func (a *Bird) Call() string {
return "chirp"
}
func (a *Bird) this() BirdI {
return a.Self().(BirdI)
}
type Duck struct {
Bird
}
type DuckI interface {
BirdI
}
func NewDuck() *Duck {
d := new(Duck)
d.Init(d)
return d
}
func (a *Duck) Call() string {
return "quack"
}
func TestBase(t *testing.T) {
assert.Equal(t, "chirp", NewBird().GetCall())
assert.Equal(t, "quack", NewDuck().GetCall())
}
func TestBase_String(t *testing.T) {
assert.Equal(t, "base.Bird", NewBird().String())
assert.Equal(t, "base.Duck", NewDuck().String())
}
func TestBase_Init(t *testing.T) {
assert.Panics(t, func() {
d := NewDuck()
d.Init(NewBird())
})
assert.NotPanics(t, func() {
d := NewDuck()
d.Init(d)
})
}