/
instanceSmartContractCallerTest.go
97 lines (79 loc) · 3.31 KB
/
instanceSmartContractCallerTest.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
package testcommon
import (
"testing"
"github.com/ElrondNetwork/arwen-wasm-vm/arwen"
contextmock "github.com/ElrondNetwork/arwen-wasm-vm/mock/context"
"github.com/ElrondNetwork/elrond-go/core/vmcommon"
)
// InstanceTestSmartContract represents the config data for the smart contract instance to be tested
type InstanceTestSmartContract struct {
testSmartContract
code []byte
}
// CreateInstanceContract build a contract to be used in a test creted with BuildInstanceCallTest
func CreateInstanceContract(address []byte) *InstanceTestSmartContract {
return &InstanceTestSmartContract{
testSmartContract: testSmartContract{
address: address,
},
}
}
// WithBalance provides the balance for the InstanceTestSmartContract
func (mockSC *InstanceTestSmartContract) WithBalance(balance int64) *InstanceTestSmartContract {
mockSC.balance = balance
return mockSC
}
// WithConfig provides the config object for the InstanceTestSmartContract
func (mockSC *InstanceTestSmartContract) WithConfig(config interface{}) *InstanceTestSmartContract {
mockSC.config = config
return mockSC
}
// WithCode provides the code for the InstanceTestSmartContract
func (mockSC *InstanceTestSmartContract) WithCode(code []byte) *InstanceTestSmartContract {
mockSC.code = code
return mockSC
}
// InstancesTestTemplate holds the data to build a contract call test
type InstancesTestTemplate struct {
testTemplateConfig
contracts []*InstanceTestSmartContract
setup func(arwen.VMHost, *contextmock.BlockchainHookStub)
assertResults func(arwen.VMHost, *contextmock.BlockchainHookStub, *VMOutputVerifier)
}
// BuildInstanceCallTest starts the building process for a contract call test
func BuildInstanceCallTest(t *testing.T) *InstancesTestTemplate {
return &InstancesTestTemplate{
testTemplateConfig: testTemplateConfig{
t: t,
useMocks: false,
},
setup: func(arwen.VMHost, *contextmock.BlockchainHookStub) {},
}
}
// WithContracts provides the contracts to be used by the contract call test
func (callerTest *InstancesTestTemplate) WithContracts(usedContracts ...*InstanceTestSmartContract) *InstancesTestTemplate {
callerTest.contracts = usedContracts
return callerTest
}
// WithInput provides the ContractCallInput to be used by the contract call test
func (callerTest *InstancesTestTemplate) WithInput(input *vmcommon.ContractCallInput) *InstancesTestTemplate {
callerTest.input = input
return callerTest
}
// WithSetup provides the setup function to be used by the contract call test
func (callerTest *InstancesTestTemplate) WithSetup(setup func(arwen.VMHost, *contextmock.BlockchainHookStub)) *InstancesTestTemplate {
callerTest.setup = setup
return callerTest
}
// AndAssertResults provides the function that will aserts the results
func (callerTest *InstancesTestTemplate) AndAssertResults(assertResults func(arwen.VMHost, *contextmock.BlockchainHookStub, *VMOutputVerifier)) {
callerTest.assertResults = assertResults
runTestWithInstances(callerTest)
}
func runTestWithInstances(callerTest *InstancesTestTemplate) {
host, blockchainHookStub := defaultTestArwenForContracts(callerTest.t, callerTest.contracts)
callerTest.setup(host, blockchainHookStub)
vmOutput, err := host.RunSmartContractCall(callerTest.input)
verify := NewVMOutputVerifier(callerTest.t, vmOutput, err)
callerTest.assertResults(host, blockchainHookStub, verify)
}