Skip to content

Commit

Permalink
Merge pull request #2305 from demergent-labs/icp_api_prop_test_types
Browse files Browse the repository at this point in the history
ICP API prop test types
  • Loading branch information
lastmjs authored Dec 11, 2024
2 parents aef7ea9 + 746984c commit 54cb7ce
Show file tree
Hide file tree
Showing 38 changed files with 10,640 additions and 42,618 deletions.
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

0 comments on commit 54cb7ce

Please sign in to comment.