-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinteracton_test.go
162 lines (121 loc) Β· 4.23 KB
/
interacton_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package misc
import (
"fmt"
"testing"
"github.com/onflow/cadence/runtime/parser"
)
func TestUnknownInteraction(t *testing.T) {
interaction := parseAndBuildInteraction("lorem ipsum")
if interaction.Kind != InteractionKindUnknown {
t.Error("Expected transaction kind unknown")
}
if len(interaction.Parameters) != 0 {
t.Error("Expected empty parameters list")
}
if interaction.Transaction != nil {
t.Error("Expected null transaction")
}
}
func TestSimpleInteraction(t *testing.T) {
interaction := parseAndBuildInteraction("transaction (addr: Address) {}")
if interaction.Kind != InteractionKindTransaction {
t.Error("Expected transaction kind transaction")
}
if len(interaction.Parameters) != 1 {
t.Error("Expected a single parameter")
}
if interaction.Parameters[0].Identifier != "addr" {
t.Error("Expected 'addr' identifier")
}
if interaction.Parameters[0].Type.Kind != CadenceTypeAddress {
t.Error("Expected Address parameter kind")
}
if interaction.Parameters[0].Type.Optional {
t.Error("Expected required")
}
}
func TestInteractionWithOptionalParameter(t *testing.T) {
interaction := parseAndBuildInteraction("transaction (addr: [Address?]?) {}")
if interaction.Parameters[0].Identifier != "addr" {
t.Error("Expected 'addr' identifier")
}
if interaction.Parameters[0].Type.Kind != CadenceTypeArray {
t.Error("Expected Array parameter kind")
}
if !interaction.Parameters[0].Type.Optional {
t.Error("Expected optional")
}
if interaction.Parameters[0].Type.ArrayType.Element.Kind != CadenceTypeAddress {
t.Error("Expected Address element kind")
}
if !interaction.Parameters[0].Type.ArrayType.Element.Optional {
t.Error("Expected optional array element")
}
}
func TestVariableArrayParameter(t *testing.T) {
interaction := parseAndBuildInteraction("transaction (addresses: [Address]) {}")
if interaction.Parameters[0].Identifier != "addresses" {
t.Error("Expected 'addresses' identifier")
}
if interaction.Parameters[0].Type.Kind != CadenceTypeArray {
t.Error("Expected Array parameter kind")
}
if interaction.Parameters[0].Type.ArrayType == nil {
t.Error("Expected array sub-field to be set")
}
if interaction.Parameters[0].Type.ArrayType.Element.Kind != CadenceTypeAddress {
t.Error("Expected Address parameter kind")
}
if interaction.Parameters[0].Type.ArrayType.Size != -1 {
t.Error("Expected size -1")
}
}
func TestConstantArrayParameter(t *testing.T) {
interaction := parseAndBuildInteraction("transaction (addresses: [Address; 3]) {}")
if interaction.Parameters[0].Identifier != "addresses" {
t.Error("Expected 'addresses' identifier")
}
if interaction.Parameters[0].Type.Kind != CadenceTypeArray {
t.Error("Expected Array parameter kind")
}
if interaction.Parameters[0].Type.ArrayType == nil {
t.Error("Expected array sub-field to be set")
}
if interaction.Parameters[0].Type.ArrayType.Element.Kind != CadenceTypeAddress {
t.Error("Expected Address element kind")
}
if interaction.Parameters[0].Type.ArrayType.Size != 3 {
t.Error("Expected size 3")
}
}
func TestDictionaryParameter(t *testing.T) {
interaction := parseAndBuildInteraction("transaction (addressLookupById: {String: Address}) {}")
if interaction.Parameters[0].Identifier != "addressLookupById" {
t.Error("Expected 'addressLookupById' identifier")
}
if interaction.Parameters[0].Type.Kind != CadenceTypeDictionary {
t.Error("Expected Dictionary parameter kind")
}
if interaction.Parameters[0].Type.DictionaryType == nil {
t.Error("Expected dictionary sub-field to be set")
}
if interaction.Parameters[0].Type.DictionaryType.Key.Kind != CadenceTypeTextual {
t.Error("Expected String key kind")
}
if interaction.Parameters[0].Type.DictionaryType.Value.Kind != CadenceTypeAddress {
t.Error("Expected Address value kind")
}
}
func TestAuthorizerCount(t *testing.T) {
interaction := parseAndBuildInteraction("transaction { prepare (acct: AuthAccount) {} }")
if interaction.Transaction.AuthorizerCount != 1 {
t.Error("Expected 'AuthorizerCount' to be 1")
}
}
func parseAndBuildInteraction(code string) *Interaction {
program, err := parser.ParseProgram(nil, []byte(code), parser.Config{})
if err != nil {
fmt.Printf("Interaction parsing error: %s\n", err)
}
return buildInteraction(program)
}