Skip to content

Commit

Permalink
chore: add enum cases tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasaric committed Sep 12, 2024
1 parent 7ece011 commit dddd65a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
96 changes: 94 additions & 2 deletions __tests__/utils/calldata/requestParser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { parseCalldataField } from '../../../src/utils/calldata/requestParser';
import { getAbiEnums, getAbiStructs, getAbiEntry } from '../../factories/abi';
import { CairoOption, ETH_ADDRESS, NON_ZERO_PREFIX } from '../../../src';
import {
CairoCustomEnum,
CairoOption,
CairoResult,
ETH_ADDRESS,
NON_ZERO_PREFIX,
} from '../../../src';

describe('requestParser', () => {
describe('parseCalldataField', () => {
Expand Down Expand Up @@ -100,7 +106,7 @@ describe('requestParser', () => {
expect(parsedField).toEqual(['252', '0']);
});

test('should return parsed calldata field for Enum type', () => {
test('should return parsed calldata field for Enum Option type None', () => {
const args = [new CairoOption<string>(1, 'content')];
const argsIterator = args[Symbol.iterator]();
const parsedField = parseCalldataField(
Expand All @@ -112,6 +118,92 @@ describe('requestParser', () => {
expect(parsedField).toEqual('1');
});

test('should return parsed calldata field for Enum Option type Some', () => {
const args = [new CairoOption<string>(0, 'content')];
const argsIterator = args[Symbol.iterator]();
const abiEnum = getAbiEnums().enum;
abiEnum.variants.push({
name: 'Some',
type: 'cairo_struct_variant',
offset: 1,
});
const parsedField = parseCalldataField(
argsIterator,
getAbiEntry('core::option::Option::core::bool'),
getAbiStructs(),
{ 'core::option::Option::core::bool': abiEnum }
);
expect(parsedField).toEqual(['0', '27988542884245108']);
});

test('should throw an error for Enum Option has no "Some" variant', () => {
const args = [new CairoOption<string>(0, 'content')];
const argsIterator = args[Symbol.iterator]();
expect(() =>
parseCalldataField(
argsIterator,
getAbiEntry('core::option::Option::core::bool'),
getAbiStructs(),
{ 'core::option::Option::core::bool': getAbiEnums().enum }
)
).toThrow(new Error(`Error in abi : Option has no 'Some' variant.`));
});

test('should return parsed calldata field for Enum Result type Ok', () => {
const args = [new CairoResult<string, string>(0, 'Ok')];
const argsIterator = args[Symbol.iterator]();
const abiEnum = getAbiEnums().enum;
abiEnum.variants.push({
name: 'Ok',
type: 'cairo_struct_variant',
offset: 1,
});
const parsedField = parseCalldataField(
argsIterator,
getAbiEntry('core::result::Result::core::bool'),
getAbiStructs(),
{ 'core::result::Result::core::bool': abiEnum }
);
expect(parsedField).toEqual(['0', '20331']);
});

test('should throw an error for Enum Result has no "Ok" variant', () => {
const args = [new CairoResult<string, string>(0, 'Ok')];
const argsIterator = args[Symbol.iterator]();
expect(() =>
parseCalldataField(
argsIterator,
getAbiEntry('core::result::Result::core::bool'),
getAbiStructs(),
{ 'core::result::Result::core::bool': getAbiEnums().enum }
)
).toThrow(new Error(`Error in abi : Result has no 'Ok' variant.`));
});

test('should return parsed calldata field for Custom Enum type', () => {
const activeVariantName = 'custom_enum';
const args = [new CairoCustomEnum({ [activeVariantName]: 'content' })];
const argsIterator = args[Symbol.iterator]();
const abiEnum = getAbiEnums().enum;
abiEnum.variants.push({
name: activeVariantName,
type: 'cairo_struct_variant',
offset: 1,
});
const parsedField = parseCalldataField(argsIterator, getAbiEntry('enum'), getAbiStructs(), {
enum: abiEnum,
});
expect(parsedField).toEqual(['1', '27988542884245108']);
});

test('should throw an error for Custon Enum type when there is not active variant', () => {
const args = [new CairoCustomEnum({ test: 'content' })];
const argsIterator = args[Symbol.iterator]();
expect(() =>
parseCalldataField(argsIterator, getAbiEntry('enum'), getAbiStructs(), getAbiEnums())
).toThrow(new Error(`Not find in abi : Enum has no 'test' variant.`));
});

test('should throw an error for CairoUint256 abi type when wrong arg is provided', () => {
const args = ['test'];
const argsIterator = args[Symbol.iterator]();
Expand Down
1 change: 1 addition & 0 deletions src/utils/calldata/requestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ function parseCalldataValue(
}
return [CairoResultVariant.Ok.toString(), parsedParameter];
}

// is Result::Err
const listTypeVariant = variants.find((variant) => variant.name === 'Err');
if (isUndefined(listTypeVariant)) {
Expand Down

0 comments on commit dddd65a

Please sign in to comment.