Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICP API prop test types #2305

Merged
merged 17 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/property/ic_api/arg_data_raw/test/generate_canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ${imports
.map((importDeclaration) => `import { ${importDeclaration} } from 'azle';`)
.join('\n')}
${variableAliasDeclarations.join('\n')}
import { AssertType, NotAnyAndExact } from 'azle/type_tests/assert_type';

export default class {
initArgDataRaw: Uint8Array | null = null;
Expand Down Expand Up @@ -68,6 +69,14 @@ export default class {
candidEncode(arg: string): Uint8Array {
return candidEncode(arg);
}

@query([], IDL.Bool)
assertTypes(): boolean {
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof argDataRaw>, Uint8Array>
>;
return argDataRaw() instanceof Uint8Array;
}
}
`;
}
5 changes: 5 additions & 0 deletions tests/property/ic_api/arg_data_raw/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export function getTests(): Test {
defaultPropTestParams()
);
});

it('asserts argDataRaw static and runtime types', async () => {
const actor = await getCanisterActor<Actor>('canister');
expect(await actor.assertTypes()).toBe(true);
});
};
}

Expand Down
9 changes: 9 additions & 0 deletions tests/property/ic_api/caller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
StableBTreeMap,
update
} from 'azle';
import { AssertType, NotAnyAndExact } from 'azle/type_tests/assert_type';

export default class {
initCaller: Principal | null = null;
Expand Down Expand Up @@ -98,4 +99,12 @@ export default class {
getUpdateCaller(): Principal {
return caller();
}

@query([], IDL.Bool)
assertTypes(): boolean {
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof caller>, Principal>
>;
return caller() instanceof Principal;
}
}
5 changes: 5 additions & 0 deletions tests/property/ic_api/caller/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,10 @@ export function getTests(): Test {
defaultPropTestParams()
);
});

it('asserts caller static and runtime types', async () => {
const actor = await getCanisterActor<Actor>('canister');
expect(await actor.assertTypes()).toBe(true);
});
};
}
29 changes: 29 additions & 0 deletions tests/property/ic_api/candid/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { candidDecode, candidEncode, IDL, query } from 'azle';
import { AssertType, NotAnyAndExact } from 'azle/type_tests/assert_type';

export default class {
@query([IDL.Vec(IDL.Nat8)], IDL.Text)
Expand All @@ -10,4 +11,32 @@ export default class {
candidEncodeQuery(candidString: string): Uint8Array {
return candidEncode(candidString);
}

@query([IDL.Vec(IDL.Nat8)], IDL.Bool)
assertCandidDecodeTypes(candidBytes: Uint8Array): boolean {
type _AssertParamType = AssertType<
NotAnyAndExact<Parameters<typeof candidDecode>[0], Uint8Array>
>;
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof candidDecode>, string>
>;
return (
candidBytes instanceof Uint8Array &&
typeof candidDecode(candidBytes) === 'string'
);
}

@query([IDL.Text], IDL.Bool)
assertCandidEncodeTypes(candidString: string): boolean {
type _AssertParamType = AssertType<
NotAnyAndExact<Parameters<typeof candidEncode>[0], string>
>;
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof candidEncode>, Uint8Array>
>;
return (
typeof candidString === 'string' &&
candidEncode(candidString) instanceof Uint8Array
);
}
}
14 changes: 14 additions & 0 deletions tests/property/ic_api/candid/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,19 @@ export function getTests(): Test {
defaultPropTestParams()
);
});

it('asserts candidDecode static and runtime types', async () => {
const actor = await getCanisterActor<Actor>('canister');
expect(
await actor.assertCandidDecodeTypes(
new Uint8Array([68, 73, 68, 76, 0, 1, 113, 0])
)
).toBe(true);
});

it('asserts candidEncode static and runtime types', async () => {
const actor = await getCanisterActor<Actor>('canister');
expect(await actor.assertCandidEncodeTypes('("")')).toBe(true);
});
};
}
Loading
Loading