Skip to content

Commit cec0ca3

Browse files
committed
feat: add tests for setting items in struct back in
1 parent c71995b commit cec0ca3

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

tests/arc4/struct.spec.ts

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getABIEncodedValue } from '@algorandfoundation/algokit-utils/types/app-
22
import { Bytes } from '@algorandfoundation/algorand-typescript'
33
import { Bool, DynamicArray, interpretAsArc4, StaticArray, Str, Struct, Tuple, UintN } from '@algorandfoundation/algorand-typescript/arc4'
44
import { encodingUtil } from '@algorandfoundation/puya-ts'
5-
import { describe, expect, test } from 'vitest'
5+
import { describe, expect, it, test } from 'vitest'
66
import type { StubBytesCompat } from '../../src/impl/primitives'
77
import { AccountCls } from '../../src/impl/reference'
88
import type { DeliberateAny } from '../../src/typescript-helpers'
@@ -21,42 +21,42 @@ class Swapped1 extends Struct<{
2121
c: Bool
2222
d: Str
2323
a: Tuple<[UintN<64>, Bool, Bool]>
24-
}> {}
24+
}> { }
2525

2626
class Swapped2 extends Struct<{
2727
b: UintN<64>
2828
c: Bool
2929
d: Str
3030
a: Tuple<[Tuple<[UintN<64>, Bool, Bool]>, Tuple<[UintN<64>, Bool, Bool]>]>
31-
}> {}
31+
}> { }
3232

3333
class Swapped3 extends Struct<{
3434
b: UintN<64>
3535
c: Bool
3636
d: Str
3737
a: Tuple<[DynamicArray<Str>, DynamicArray<Str>, Str, UintN<64>, Bool, StaticArray<UintN<64>, 3>]>
38-
}> {}
38+
}> { }
3939

4040
class Swapped4 extends Struct<{
4141
b: UintN<64>
4242
c: Bool
4343
d: Str
4444
a: Tuple<[Tuple<[Bool, DynamicArray<Str>, Str]>, UintN<64>, StaticArray<UintN<64>, 3>]>
45-
}> {}
45+
}> { }
4646

4747
class Swapped5 extends Struct<{
4848
b: UintN<64>
4949
c: Bool
5050
d: Str
5151
a: Tuple<[Tuple<[Bool, DynamicArray<Str>, Str]>, Tuple<[UintN<64>, StaticArray<UintN<64>, 3>]>]>
52-
}> {}
52+
}> { }
5353

5454
class Swapped6 extends Struct<{
5555
b: UintN<64>
5656
c: Bool
5757
d: Str
5858
a: Tuple<[Tuple<[Bool, Tuple<[DynamicArray<Str>, Str]>]>, Tuple<[UintN<64>, StaticArray<UintN<64>, 3>]>]>
59-
}> {}
59+
}> { }
6060

6161
const testData = [
6262
{
@@ -252,6 +252,52 @@ describe('arc4.Struct', async () => {
252252
compareARC4AndABIValue(result.d, nativeValues[i++])
253253
compareARC4AndABIValue(result.a, nativeValues[i++])
254254
})
255+
256+
it('set item in struct', async () => {
257+
const data = testData[5]
258+
259+
const nativeValues = data.nativeValues() as DeliberateAny
260+
nativeValues[0] = 43
261+
nativeValues[2] = 'world'
262+
nativeValues[3][0][1][0][1] = 'hello, world'
263+
nativeValues[3][0][1][0].push('test')
264+
nativeValues[3][1][1][0] = 24
265+
266+
const sdkResult = getABIEncodedValue(nativeValues, data.abiTypeString, {})
267+
268+
const abiValues = data.struct() as Swapped6
269+
abiValues.b = new UintN<64>(43)
270+
abiValues.d = new Str('world')
271+
abiValues.a.at(0).at(1).at(0)[1] = new Str('hello, world')
272+
abiValues.a.at(0).at(1).at(0).push(new Str('test'))
273+
abiValues.a.at(1).at(1)[0] = new UintN<64>(24)
274+
const result = abiValues.bytes
275+
276+
expect(result).toEqual(Bytes(sdkResult))
277+
})
278+
279+
it('set item in struct created from bytes', async () => {
280+
const data = testData[5]
281+
const nativeValues = data.nativeValues() as DeliberateAny
282+
nativeValues[0] = 43
283+
nativeValues[2] = 'world'
284+
nativeValues[3][0][1][0][1] = 'hello, world'
285+
nativeValues[3][0][1][0].push('test')
286+
nativeValues[3][1][1][0] = 24
287+
288+
const sdkResult = getABIEncodedValue(nativeValues, data.abiTypeString, {})
289+
const bytes = Bytes(getABIEncodedValue(data.nativeValues(), data.abiTypeString, {}))
290+
291+
const abiValues = data.create(bytes) as Swapped6
292+
abiValues.b = new UintN<64>(43)
293+
abiValues.d = new Str('world')
294+
abiValues.a.at(0).at(1).at(0)[1] = new Str('hello, world')
295+
abiValues.a.at(0).at(1).at(0).push(new Str('test'))
296+
abiValues.a.at(1).at(1)[0] = new UintN<64>(24)
297+
const result = abiValues.bytes
298+
299+
expect(result).toEqual(Bytes(sdkResult))
300+
})
255301
})
256302

257303
const compareARC4AndABIValue = (arc4Value: DeliberateAny, nativeValue: DeliberateAny) => {

0 commit comments

Comments
 (0)