-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathresolve-cadence.test.js
231 lines (178 loc) · 5.14 KB
/
resolve-cadence.test.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import {
initInteraction,
pipe,
put,
makeScript,
} from "../interaction/interaction"
import {resolveCadence} from "./resolve-cadence.js"
import {config} from "@onflow/config"
const idle = () => new Promise(resolve => setTimeout(resolve), 0)
describe("resolveCadence", () => {
describe("0xHelloWorld-style account identifier syntax", () => {
test("cadence is a string", async () => {
const CADENCE = "CADENCE_STRING"
const ix = await pipe([
makeScript,
put("ix.cadence", CADENCE),
resolveCadence,
])(initInteraction())
expect(ix.message.cadence).toBe(CADENCE)
})
test("cadence is a function", async () => {
const CADENCE = async function () {
return "CADENCE_ASYNC_FUNCTION"
}
const ix = await pipe([
makeScript,
put("ix.cadence", CADENCE),
resolveCadence,
])(initInteraction())
expect(ix.message.cadence).toBe(await CADENCE())
})
test("replaces all addresses from config", async () => {
const CADENCE = async function () {
return `
import MyContract from 0xMY_CONTRACT_ADDRESS
access(all) fun main(): Address {
return 0xMY_CONTRACT_ADDRESS
}
`
}
const RESULT = async function () {
return `
import MyContract from 0x123abc
access(all) fun main(): Address {
return 0x123abc
}
`
}
await config.put("0xMY_CONTRACT_ADDRESS", "0x123abc")
const ix = await pipe([
makeScript,
put("ix.cadence", CADENCE),
resolveCadence,
])(initInteraction())
expect(ix.message.cadence).toEqual(await RESULT())
})
test("similar config names do not replace each other", async () => {
const CADENCE = async function () {
return `
import FooBar from 0xFoo
import FooBar from 0xFooBar
access(all) fun main(): Address {
log(0xFoo)
return 0xFoo
}
access(all) fun other(): Address {
log(0xFooBar)
return 0xFooBar
}
access(all) fun otherTwo(): Address {return 0xFoo}
access(all) fun otherThree(): Address {return 0xFooBar}
`
}
const RESULT = async function () {
return `
import FooBar from 0x123
import FooBar from 0x456
access(all) fun main(): Address {
log(0x123)
return 0x123
}
access(all) fun other(): Address {
log(0x456)
return 0x456
}
access(all) fun otherTwo(): Address {return 0x123}
access(all) fun otherThree(): Address {return 0x456}
`
}
config.put("0xFoo", "0x123").put("0xFooBar", "0x456")
const ix = await pipe([
makeScript,
put("ix.cadence", CADENCE),
resolveCadence,
])(initInteraction())
expect(ix.message.cadence).toEqual(await RESULT())
})
})
describe("new Identifier syntax", () => {
test("single import statement", async () => {
const CADENCE = `import "Foo"
access(all) fun main(): Address {
return "Foo"
}`
const expected = `import Foo from 0x1
access(all) fun main(): Address {
return "Foo"
}`
await config().put("system.contracts.Foo", "0x1")
await idle()
const ix = await pipe([
makeScript,
put("ix.cadence", CADENCE),
resolveCadence,
])(initInteraction())
expect(ix.message.cadence).toEqual(expected)
})
test("multiple import statements with only one defined", async () => {
const CADENCE = `import "Foo"
import "Bar"
access(all) fun main(): Address {
return "Foo"
}`
const expected = `import Foo from 0x1
import "Bar"
access(all) fun main(): Address {
return "Foo"
}`
await config().put("system.contracts.Foo", "0x1")
await idle()
const ix = await pipe([
makeScript,
put("ix.cadence", CADENCE),
resolveCadence,
])(initInteraction())
expect(ix.message.cadence).toEqual(expected)
})
test("multiple import statements", async () => {
const CADENCE = `import "Foo"
import "Bar"
access(all) fun main(): Address {
return "Foo"
}`
const expected = `import Foo from 0x1
import Bar from 0x2
access(all) fun main(): Address {
return "Foo"
}`
await config().put("system.contracts.Foo", "0x1")
await config().put("system.contracts.Bar", "0x2")
await idle()
const ix = await pipe([
makeScript,
put("ix.cadence", CADENCE),
resolveCadence,
])(initInteraction())
expect(ix.message.cadence).toEqual(expected)
})
test("should prefix addresses with `0x` if not already present", async () => {
const CADENCE = `import "Foo"
access(all) fun main(): Address {
return "Foo"
}`
const expected = `import Foo from 0x1
access(all) fun main(): Address {
return "Foo"
}`
await config().put("system.contracts.Foo", "1")
await idle()
const ix = await pipe([
makeScript,
put("ix.cadence", CADENCE),
resolveCadence,
])(initInteraction())
expect(ix.message.cadence).toEqual(expected)
})
})
})