-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,483 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
property_tests/arbitraries/canister_methods/post_upgrade_arb.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import fc from 'fast-check'; | ||
|
||
import { CandidValueAndMeta } from '../candid/candid_value_and_meta_arb'; | ||
import { CorrespondingJSType } from '../candid/corresponding_js_type'; | ||
import { UniqueIdentifierArb } from '../unique_identifier_arb'; | ||
import { Named } from '../..'; | ||
import { | ||
BodyGenerator, | ||
TestsGenerator, | ||
CallbackLocation, | ||
isDefined, | ||
generateCallback | ||
} from '.'; | ||
import { Test } from '../../../test'; | ||
import { VoidArb } from '../candid/primitive/void'; | ||
|
||
export type PostUpgradeMethod< | ||
ParamAgentArgumentValue extends CorrespondingJSType, | ||
ParamAgentResponseValue | ||
> = { | ||
imports: Set<string>; | ||
globalDeclarations: string[]; | ||
params: Named< | ||
CandidValueAndMeta<ParamAgentArgumentValue, ParamAgentResponseValue> | ||
>[]; | ||
sourceCode: string; | ||
tests: Test[][]; | ||
}; | ||
|
||
export function PostUpgradeMethodArb< | ||
ParamAgentArgumentValue extends CorrespondingJSType, | ||
ParamAgentResponseValue | ||
>( | ||
paramTypeArrayArb: fc.Arbitrary< | ||
CandidValueAndMeta<ParamAgentArgumentValue, ParamAgentResponseValue>[] | ||
>, | ||
constraints: { | ||
generateBody: BodyGenerator< | ||
ParamAgentArgumentValue, | ||
ParamAgentResponseValue | ||
>; | ||
generateTests: TestsGenerator< | ||
ParamAgentArgumentValue, | ||
ParamAgentResponseValue | ||
>; | ||
callbackLocation?: CallbackLocation; | ||
} | ||
) { | ||
return fc | ||
.tuple( | ||
UniqueIdentifierArb('canisterMethod'), | ||
paramTypeArrayArb, | ||
VoidArb(), | ||
fc.constantFrom( | ||
'INLINE', | ||
'STANDALONE' | ||
) as fc.Arbitrary<CallbackLocation>, | ||
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, | ||
paramTypes, | ||
returnType, | ||
defaultCallbackLocation, | ||
callbackName | ||
]) => { | ||
// TODO: Add a return type to this map callback of type PostUpgradeMethod<Something, Something> | ||
|
||
const callbackLocation = | ||
constraints.callbackLocation ?? defaultCallbackLocation; | ||
|
||
const imports = new Set([ | ||
'postUpgrade', | ||
...paramTypes.flatMap((param) => [...param.src.imports]) | ||
]); | ||
|
||
const namedParams = paramTypes.map( | ||
<T>(param: T, index: number): Named<T> => ({ | ||
name: `param${index}`, | ||
el: param | ||
}) | ||
); | ||
|
||
const callback = generateCallback( | ||
namedParams, | ||
returnType, | ||
constraints.generateBody, | ||
callbackLocation, | ||
callbackName | ||
); | ||
|
||
const variableAliasDeclarations = paramTypes | ||
.flatMap((param) => param.src.variableAliasDeclarations) | ||
.filter(isDefined); | ||
|
||
const globalDeclarations = | ||
callbackLocation === 'STANDALONE' | ||
? [...variableAliasDeclarations, callback] | ||
: variableAliasDeclarations; | ||
|
||
const sourceCode = generateSourceCode( | ||
functionName, | ||
paramTypes, | ||
callbackLocation === 'STANDALONE' ? callbackName : callback | ||
); | ||
|
||
const tests = constraints.generateTests( | ||
functionName, | ||
namedParams, | ||
returnType | ||
); | ||
|
||
return { | ||
imports, | ||
globalDeclarations, | ||
params: namedParams, | ||
sourceCode, | ||
tests | ||
}; | ||
} | ||
); | ||
} | ||
|
||
function generateSourceCode< | ||
ParamType extends CorrespondingJSType, | ||
ParamAgentType | ||
>( | ||
functionName: string, | ||
paramTypes: CandidValueAndMeta<ParamType, ParamAgentType>[], | ||
callback: string | ||
): string { | ||
const paramCandidTypeObjects = paramTypes | ||
.map((param) => param.src.candidTypeObject) | ||
.join(', '); | ||
|
||
return `${functionName}: postUpgrade([${paramCandidTypeObjects}], ${callback})`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.