Skip to content

Commit

Permalink
Add InspectMessageMethodArb
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren authored and bdemann committed Jan 19, 2024
1 parent 22e6bdb commit ce93db4
Show file tree
Hide file tree
Showing 9 changed files with 1,256 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ jobs:
"property_tests/tests/blob",
"property_tests/tests/bool",
"property_tests/tests/canister_methods/init",
"property_tests/tests/canister_methods/inspect_message",
"property_tests/tests/canister_methods/post_upgrade",
"property_tests/tests/canister_methods/pre_upgrade",
"property_tests/tests/canister_methods/query",
Expand Down
8 changes: 7 additions & 1 deletion property_tests/arbitraries/canister_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { QueryMethod } from './canister_methods/query_method_arb';
import { Test } from '../../test';
import { UpdateMethod } from './canister_methods/update_method_arb';
import { InitMethod } from './canister_methods/init_method_arb';
import { InspectMessageMethod } from './canister_methods/inspect_message_method_arb';
import { PostUpgradeMethod } from './canister_methods/post_upgrade_arb';
import { PreUpgradeMethod } from './canister_methods/pre_upgrade_method_arb';
import { CorrespondingJSType } from './candid/corresponding_js_type';
Expand Down Expand Up @@ -39,14 +40,16 @@ export type CanisterMethod<
| UpdateMethod
| InitMethod<ParamAgentArgumentValue, ParamAgentResponseValue>
| PostUpgradeMethod<ParamAgentArgumentValue, ParamAgentResponseValue>
| PreUpgradeMethod;
| PreUpgradeMethod
| InspectMessageMethod;

export type CanisterConfig<
ParamAgentArgumentValue extends CorrespondingJSType = undefined,
ParamAgentResponseValue = undefined
> = {
globalDeclarations?: string[];
initMethod?: InitMethod<ParamAgentArgumentValue, ParamAgentResponseValue>;
inspectMessageMethod?: InspectMessageMethod;
postUpgradeMethod?: PostUpgradeMethod<
ParamAgentArgumentValue,
ParamAgentResponseValue
Expand All @@ -71,6 +74,9 @@ export function CanisterArb<
ParamAgentResponseValue
>[] = [
...(config.initMethod ? [config.initMethod] : []),
...(config.inspectMessageMethod
? [config.inspectMessageMethod]
: []),
...(config.postUpgradeMethod ? [config.postUpgradeMethod] : []),
...(config.preUpgradeMethod ? [config.preUpgradeMethod] : []),
...(config.queryMethods ?? []),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import fc from 'fast-check';

import { UniqueIdentifierArb } from '../unique_identifier_arb';
import {
BodyGenerator,
TestsGenerator,
CallbackLocation,
generateCallback,
CallbackLocationArb
} from '.';
import { Test } from '../../../test';
import { VoidArb } from '../candid/primitive/void';

export type InspectMessageMethod = {
imports: Set<string>;
globalDeclarations: string[];
sourceCode: string;
tests: Test[][];
};

export function InspectMessageMethodArb(constraints: {
generateBody: BodyGenerator;
generateTests: TestsGenerator;
callbackLocation?: CallbackLocation;
}) {
return fc
.tuple(
UniqueIdentifierArb('canisterMethod'),
VoidArb(),
CallbackLocationArb,
UniqueIdentifierArb('typeDeclaration')
// TODO: This unique id would be better named globalScope or something
// But needs to match the same scope as typeDeclarations so I'm using
// that for now.
)
.map(
([
functionName,
returnType,
defaultCallbackLocation,
callbackName
]): InspectMessageMethod => {
const callbackLocation =
constraints.callbackLocation ?? defaultCallbackLocation;

const imports = new Set(['inspectMessage', 'ic']);

const callback = generateCallback(
[],
returnType,
constraints.generateBody,
callbackLocation,
callbackName
);

const globalDeclarations =
callbackLocation === 'STANDALONE' ? [callback] : [];

const sourceCode = `${functionName}: inspectMessage(${
callbackLocation === 'STANDALONE' ? callbackName : callback
})`;

const tests = constraints.generateTests(
functionName,
[],
returnType
);

return {
imports,
globalDeclarations,
sourceCode,
tests
};
}
);
}
16 changes: 16 additions & 0 deletions property_tests/tests/canister_methods/inspect_message/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"canisters": {
"canister": {
"type": "custom",
"main": "src/index.ts",
"candid": "src/index.did",
"build": "npx azle canister",
"wasm": ".azle/canister/canister.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/canister",
"node_compatibility": true
}
}
}
}
Loading

0 comments on commit ce93db4

Please sign in to comment.