Skip to content

Commit

Permalink
update the remaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Dec 10, 2024
1 parent 5af5715 commit dd19c43
Show file tree
Hide file tree
Showing 25 changed files with 166 additions and 77 deletions.
6 changes: 5 additions & 1 deletion 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 @@ -100,7 +101,10 @@ export default class {
}

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

it('verifies that caller returns a Principal', async () => {
it('asserts caller static and runtime types', async () => {
const actor = await getCanisterActor<Actor>('canister');
expect(await actor.callerTypesAreCorrect()).toBe(true);
expect(await actor.assertTypes()).toBe(true);
});
};
}
17 changes: 15 additions & 2 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 @@ -12,15 +13,27 @@ export default class {
}

@query([IDL.Vec(IDL.Nat8)], IDL.Bool)
candidDecodeTypesAreCorrect(candidBytes: Uint8Array): boolean {
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)
candidEncodeTypesAreCorrect(candidString: string): boolean {
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
Expand Down
8 changes: 4 additions & 4 deletions tests/property/ic_api/candid/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ export function getTests(): Test {
);
});

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

it('verifies candidEncode returns a Uint8Array and takes a string', async () => {
it('asserts candidEncode static and runtime types', async () => {
const actor = await getCanisterActor<Actor>('canister');
expect(await actor.candidEncodeTypesAreCorrect('("")')).toBe(true);
expect(await actor.assertCandidEncodeTypes('("")')).toBe(true);
});
};
}
6 changes: 5 additions & 1 deletion tests/property/ic_api/canister_version/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
StableBTreeMap,
update
} from 'azle';
import { AssertType, NotAnyAndExact } from 'azle/type_tests/assert_type';

export default class {
initCanisterVersion: bigint | null = null;
Expand Down Expand Up @@ -108,7 +109,10 @@ export default class {
}

@query([], IDL.Bool)
canisterVersionTypesAreCorrect(): boolean {
assertTypes(): boolean {
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof canisterVersion>, bigint>
>;
return typeof canisterVersion() === 'bigint';
}
}
4 changes: 2 additions & 2 deletions tests/property/ic_api/canister_version/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export function getTests(): Test {
);
});

it('verifies that canisterVersion returns a bigint', async () => {
it('asserts canisterVersion static and runtime types', async () => {
const actor = await getCanisterActor<Actor>('canister');
expect(await actor.canisterVersionTypesAreCorrect()).toBe(true);
expect(await actor.assertTypes()).toBe(true);
});
};
}
Expand Down
25 changes: 22 additions & 3 deletions tests/property/ic_api/certified_data/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
StableBTreeMap,
update
} from 'azle';
import { AssertType, NotAnyAndExact } from 'azle/type_tests/assert_type';

const CertifiedData = IDL.Record({
data: IDL.Vec(IDL.Nat8),
Expand Down Expand Up @@ -95,17 +96,35 @@ export default class {
}

@query([], IDL.Bool)
getDataCertificateTypesAreCorrectInQuery(): boolean {
assertGetDataCertificateTypesInQuery(): boolean {
type _AssertReturnType = AssertType<
NotAnyAndExact<
ReturnType<typeof dataCertificate>,
Uint8Array | undefined
>
>;
return dataCertificate() instanceof Uint8Array;
}

@update([], IDL.Bool)
getDataCertificateTypesAreCorrectInUpdate(): boolean {
assertGetDataCertificateTypesInUpdate(): boolean {
type _AssertReturnType = AssertType<
NotAnyAndExact<
ReturnType<typeof dataCertificate>,
Uint8Array | undefined
>
>;
return dataCertificate() === undefined;
}

@update([IDL.Vec(IDL.Nat8)], IDL.Bool)
verifyThatSetCertifiedDataTypesAreCorrect(data: Uint8Array): boolean {
assertSetCertifiedDataTypes(data: Uint8Array): boolean {
type _AssertParamType = AssertType<
NotAnyAndExact<Parameters<typeof setCertifiedData>[0], Uint8Array>
>;
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof setCertifiedData>, void>
>;
return (
data instanceof Uint8Array && setCertifiedData(data) === undefined
);
Expand Down
14 changes: 6 additions & 8 deletions tests/property/ic_api/certified_data/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,26 +298,24 @@ export function getTests(): Test {
);
});

it('verifies that dataCertificate returns a Uint8Array in query', async () => {
it('asserts dataCertificate static and runtime types in query', async () => {
const actor = await getCanisterActor<Actor>(CANISTER_NAME);
expect(await actor.getDataCertificateTypesAreCorrectInQuery()).toBe(
expect(await actor.assertGetDataCertificateTypesInQuery()).toBe(
true
);
});

it('verifies that dataCertificate returns undefined in update', async () => {
const actor = await getCanisterActor<Actor>(CANISTER_NAME);
expect(
await actor.getDataCertificateTypesAreCorrectInUpdate()
).toBe(true);
expect(await actor.assertGetDataCertificateTypesInUpdate()).toBe(
true
);
});

it('verifies that setCertifiedData returns undefined and takes a Uint8Array', async () => {
const actor = await getCanisterActor<Actor>(CANISTER_NAME);
expect(
await actor.verifyThatSetCertifiedDataTypesAreCorrect(
new Uint8Array()
)
await actor.assertSetCertifiedDataTypes(new Uint8Array())
).toBe(true);
});
};
Expand Down
14 changes: 12 additions & 2 deletions tests/property/ic_api/cycles/src/cycles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
msgCyclesAvailable,
update
} from 'azle';
import { AssertType, NotAnyAndExact } from 'azle/type_tests/assert_type';

import { CyclesResult } from '../types';

Expand All @@ -30,15 +31,24 @@ export default class {
}

@update([IDL.Nat64], IDL.Bool)
msgCyclesAcceptTypesAreCorrect(amount: bigint): boolean {
assertMsgCyclesAcceptTypes(amount: bigint): boolean {
type _AssertParamType = AssertType<
NotAnyAndExact<Parameters<typeof msgCyclesAccept>[0], bigint>
>;
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof msgCyclesAccept>, bigint>
>;
return (
typeof amount === 'bigint' &&
typeof msgCyclesAccept(amount) === 'bigint'
);
}

@update([], IDL.Bool)
msgCyclesAvailableTypesAreCorrect(): boolean {
assertMsgCyclesAvailableTypes(): boolean {
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof msgCyclesAvailable>, bigint>
>;
return typeof msgCyclesAvailable() === 'bigint';
}
}
Expand Down
33 changes: 18 additions & 15 deletions tests/property/ic_api/cycles/src/intermediary/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
query,
update
} from 'azle';
import { AssertType, NotAnyAndExact } from 'azle/type_tests/assert_type';

import { CyclesResult } from '../types';

Expand Down Expand Up @@ -67,42 +68,44 @@ export default class {
}

@query([], IDL.Bool)
canisterBalanceTypesAreCorrect(): boolean {
assertCanisterBalanceTypes(): boolean {
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof canisterBalance>, bigint>
>;
return typeof canisterBalance() === 'bigint';
}

@update([IDL.Nat64], IDL.Bool)
async msgCyclesAcceptTypesAreCorrect(amount: bigint): Promise<boolean> {
return await call(
this.cyclesPrincipal,
'msgCyclesAcceptTypesAreCorrect',
{
paramIdlTypes: [IDL.Nat64],
returnIdlType: IDL.Bool,
args: [amount]
}
);
async assertMsgCyclesAcceptTypes(amount: bigint): Promise<boolean> {
return await call(this.cyclesPrincipal, 'assertMsgCyclesAcceptTypes', {
paramIdlTypes: [IDL.Nat64],
returnIdlType: IDL.Bool,
args: [amount]
});
}

@update([], IDL.Bool)
async msgCyclesAvailableTypesAreCorrect(): Promise<boolean> {
async assertMsgCyclesAvailableTypes(): Promise<boolean> {
return await call(
this.cyclesPrincipal,
'msgCyclesAvailableTypesAreCorrect',
'assertMsgCyclesAvailableTypes',
{
returnIdlType: IDL.Bool
}
);
}

@update([IDL.Nat64], IDL.Bool)
async msgCyclesRefundedTypesAreCorrect(amount: bigint): Promise<boolean> {
await call(this.cyclesPrincipal, 'msgCyclesAcceptTypesAreCorrect', {
async assertMsgCyclesRefundedTypes(amount: bigint): Promise<boolean> {
await call(this.cyclesPrincipal, 'assertMsgCyclesAcceptTypes', {
paramIdlTypes: [IDL.Nat64],
returnIdlType: IDL.Bool,
args: [amount]
});
// NOTE: if there is no cross canister call, msgCyclesRefunded() cannot be called
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof msgCyclesRefunded>, bigint>
>;
return typeof msgCyclesRefunded() === 'bigint';
}
}
Expand Down
16 changes: 8 additions & 8 deletions tests/property/ic_api/cycles/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,35 +79,35 @@ export function getTests(): Test {
);
});

it('verifies canisterBalance returns a bigint', async () => {
it('asserts canisterBalance static and runtime types', async () => {
const intermediaryCanister =
await getCanisterActor<Actor>('intermediary');
const result =
await intermediaryCanister.canisterBalanceTypesAreCorrect();
await intermediaryCanister.assertCanisterBalanceTypes();
expect(result).toBe(true);
});

it('verifies msgCyclesAccept returns a bigint and accepts a bigint', async () => {
it('asserts msgCyclesAccept static and runtime types', async () => {
const intermediaryCanister =
await getCanisterActor<Actor>('intermediary');
const result =
await intermediaryCanister.msgCyclesAcceptTypesAreCorrect(0n);
await intermediaryCanister.assertMsgCyclesAcceptTypes(0n);
expect(result).toBe(true);
});

it('verifies msgCyclesAvailable returns a bigint', async () => {
it('asserts msgCyclesAvailable static and runtime types', async () => {
const intermediaryCanister =
await getCanisterActor<Actor>('intermediary');
const result =
await intermediaryCanister.msgCyclesAvailableTypesAreCorrect();
await intermediaryCanister.assertMsgCyclesAvailableTypes();
expect(result).toBe(true);
});

it('verifies msgCyclesRefunded returns a bigint', async () => {
it('asserts msgCyclesRefunded static and runtime types', async () => {
const intermediaryCanister =
await getCanisterActor<Actor>('intermediary');
const result =
await intermediaryCanister.msgCyclesRefundedTypesAreCorrect(0n);
await intermediaryCanister.assertMsgCyclesRefundedTypes(0n);
expect(result).toBe(true);
});
};
Expand Down
14 changes: 12 additions & 2 deletions tests/property/ic_api/cycles_burn/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { canisterBalance, cyclesBurn, IDL, query, update } from 'azle';
import { AssertType, NotAnyAndExact } from 'azle/type_tests/assert_type';

export default class {
@query([], IDL.Nat)
Expand All @@ -12,12 +13,21 @@ export default class {
}

@query([], IDL.Bool)
canisterBalanceTypesAreCorrect(): boolean {
assertCanisterBalanceTypes(): boolean {
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof canisterBalance>, bigint>
>;
return typeof canisterBalance() === 'bigint';
}

@update([IDL.Nat], IDL.Bool)
cyclesBurnTypesAreCorrect(amount: bigint): boolean {
assertCyclesBurnTypes(amount: bigint): boolean {
type _AssertParamType = AssertType<
NotAnyAndExact<Parameters<typeof cyclesBurn>[0], bigint>
>;
type _AssertReturnType = AssertType<
NotAnyAndExact<ReturnType<typeof cyclesBurn>, bigint>
>;
return typeof cyclesBurn(amount) === 'bigint';
}
}
8 changes: 4 additions & 4 deletions tests/property/ic_api/cycles_burn/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ export function getTests(): Test {
);
});

it('verifies canisterBalance returns a bigint', async () => {
it('asserts canisterBalance static and runtime types', async () => {
const actor = await getCanisterActor<Actor>('canister');
const result = await actor.canisterBalanceTypesAreCorrect();
const result = await actor.assertCanisterBalanceTypes();
expect(result).toBe(true);
});

it('verifies updateCyclesBurn returns a bigint and accepts a bigint', async () => {
it('asserts updateCyclesBurn static and runtime types', async () => {
const actor = await getCanisterActor<Actor>('canister');
const result = await actor.cyclesBurnTypesAreCorrect(0n);
const result = await actor.assertCyclesBurnTypes(0n);
expect(result).toBe(true);
});
};
Expand Down
Loading

0 comments on commit dd19c43

Please sign in to comment.