/
mock.go
83 lines (68 loc) · 2.39 KB
/
mock.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
package erc20
import (
"sync/atomic"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
)
// ReadOnlyContract defines an ERC20 read-only contract.
type ReadOnlyContract interface {
Decimals(*bind.CallOpts) (uint8, error)
Symbol(*bind.CallOpts) (string, error)
Name(*bind.CallOpts) (string, error)
}
// ReadOnlyContractMock is a read only contract mock.
type ReadOnlyContractMock struct {
decimalsFn func(*bind.CallOpts) (uint8, error)
decimalsCalled uint64
symbolFn func(*bind.CallOpts) (string, error)
symbolCalled uint64
nameFn func(*bind.CallOpts) (string, error)
nameCalled uint64
}
// OnDecimals replaces makes mock to call fn when Decimals function is called.
func (m *ReadOnlyContractMock) OnDecimals(fn func(*bind.CallOpts) (uint8, error)) {
m.decimalsFn = fn
}
// DecimalsCalled return the number of times the function was called in this instance.
func (m *ReadOnlyContractMock) DecimalsCalled() uint64 {
return atomic.LoadUint64(&m.decimalsCalled)
}
// Decimals implement the ReadOnlyContract interface.
func (m *ReadOnlyContractMock) Decimals(callOpts *bind.CallOpts) (uint8, error) {
atomic.AddUint64(&m.decimalsCalled, 1)
if m.decimalsFn != nil {
return m.decimalsFn(callOpts)
}
return 0, nil
}
// OnSymbol replaces makes mock to call fn when Symbol function is called.
func (m *ReadOnlyContractMock) OnSymbol(fn func(*bind.CallOpts) (string, error)) {
m.symbolFn = fn
}
// SymbolCalled return the number of times the function was called in this instance.
func (m *ReadOnlyContractMock) SymbolCalled() uint64 {
return atomic.LoadUint64(&m.symbolCalled)
}
// Symbol implement the ReadOnlyContract interface.
func (m *ReadOnlyContractMock) Symbol(callOpts *bind.CallOpts) (string, error) {
atomic.AddUint64(&m.symbolCalled, 1)
if m.symbolFn != nil {
return m.symbolFn(callOpts)
}
return "", nil
}
// OnName replaces makes mock to call fn when Symbol function is called.
func (m *ReadOnlyContractMock) OnName(fn func(*bind.CallOpts) (string, error)) {
m.nameFn = fn
}
// NameCalled return the number of times the function was called in this instance.
func (m *ReadOnlyContractMock) NameCalled() uint64 {
return atomic.LoadUint64(&m.nameCalled)
}
// Name implement the ReadOnlyContract interface.
func (m *ReadOnlyContractMock) Name(callOpts *bind.CallOpts) (string, error) {
atomic.AddUint64(&m.nameCalled, 1)
if m.nameFn != nil {
return m.nameFn(callOpts)
}
return "", nil
}