Skip to content

Commit 3c45968

Browse files
committed
chore: improve tests, fix constructor
1 parent d051ae3 commit 3c45968

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

__tests__/utils/CairoTypes/uint256.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable no-new */
2+
import { Uint256 } from '../../../src';
23
import {
34
CairoUint256,
45
UINT_256_HIGH_MAX,
@@ -100,6 +101,32 @@ describe('CairoUint256 class test', () => {
100101
expect(case1).toEqual(case4);
101102
});
102103

104+
test('constructor 2 should support Uint256 {low, high}', () => {
105+
const cases: Uint256[] = [];
106+
cases[cases.length] = new CairoUint256({ low: 0, high: 0 });
107+
cases[cases.length] = new CairoUint256({ low: '0', high: '0' });
108+
cases[cases.length] = new CairoUint256({ low: 0n, high: 0n });
109+
cases[cases.length] = new CairoUint256({ low: '0x0', high: '0x0' });
110+
111+
const cases2: Uint256[] = [];
112+
cases2[cases2.length] = new CairoUint256({ low: 10000, high: 10000 });
113+
cases2[cases2.length] = new CairoUint256({ low: '10000', high: '10000' });
114+
cases2[cases2.length] = new CairoUint256({ low: 10000n, high: 10000n });
115+
cases2[cases2.length] = new CairoUint256({ low: '0x2710', high: '0x2710' });
116+
117+
expect(
118+
cases.every((it) => {
119+
return it.low === 0n && it.high === 0n;
120+
})
121+
).toEqual(true);
122+
123+
expect(
124+
cases2.every((it) => {
125+
return it.low === 10000n && it.high === 10000n;
126+
})
127+
).toEqual(true);
128+
});
129+
103130
test('should convert UINT_256_MAX to Uint256 dec struct', () => {
104131
const u256 = new CairoUint256(UINT_256_MAX);
105132
const u256Hex = u256.toUint256DecimalString();

src/utils/cairoDataTypes/uint256.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class CairoUint256 {
3737
public constructor(uint256: Uint256);
3838

3939
public constructor(...arr: any[]) {
40-
if (typeof arr === 'object' && arr.length === 1 && arr[0].low && arr[0].high) {
40+
if (typeof arr[0] === 'object' && arr.length === 1 && 'low' in arr[0] && 'high' in arr[0]) {
4141
const props = CairoUint256.validateProps(arr[0].low, arr[0].high);
4242
this.low = props.low;
4343
this.high = props.high;

0 commit comments

Comments
 (0)