-
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
3 changed files
with
110 additions
and
55 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
85 changes: 85 additions & 0 deletions
85
property_tests/arbitraries/candid/reference/service_arb/service_method_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,85 @@ | ||
import fc from 'fast-check'; | ||
|
||
import { CandidType } from '../../candid_type'; | ||
import { CandidDefinition } from '../../candid_definition_arb/types'; | ||
import { VoidDefinitionArb } from '../../primitive/void'; | ||
import { JsFunctionNameArb } from '../../../js_function_name_arb'; | ||
import { | ||
query, | ||
update | ||
} from '../../../../../src/lib/canister_methods/methods/'; | ||
import { CanisterMethodInfo } from '../../../../../src/lib/canister_methods/types/canister_method_info'; | ||
|
||
type Mode = 'query' | 'update'; | ||
|
||
export type ServiceMethodDefinition = { | ||
name: string; | ||
azleCandidTypeObject: CanisterMethodInfo<CandidType[], CandidType>; | ||
imports: Set<string>; | ||
variableAliasDeclarations: string[]; | ||
src: string; | ||
}; | ||
|
||
export function ServiceMethodArb( | ||
candidDefArb: fc.Arbitrary<CandidDefinition> | ||
): fc.Arbitrary<ServiceMethodDefinition> { | ||
return fc | ||
.tuple( | ||
JsFunctionNameArb, | ||
fc.constantFrom('query', 'update') as fc.Arbitrary<Mode>, | ||
fc.array(candidDefArb), | ||
fc.oneof(candidDefArb, VoidDefinitionArb()) | ||
) | ||
.map(([name, mode, params, returnType]): ServiceMethodDefinition => { | ||
const paramCandidTypeObjects = params.map( | ||
(param) => param.candidMeta.candidTypeObject | ||
); | ||
|
||
const variableAliasDeclarations = params.reduce( | ||
( | ||
acc, | ||
{ candidMeta: { variableAliasDeclarations } } | ||
): string[] => { | ||
return [...acc, ...variableAliasDeclarations]; | ||
}, | ||
returnType.candidMeta.variableAliasDeclarations | ||
); | ||
|
||
const src = `${name}: ${mode}([${paramCandidTypeObjects}], ${returnType.candidMeta.candidTypeObject})`; | ||
|
||
const imports = params.reduce( | ||
(acc, param) => { | ||
return new Set([...acc, ...param.candidMeta.imports]); | ||
}, | ||
new Set([mode, ...returnType.candidMeta.imports]) | ||
); | ||
|
||
const azleCandidTypeObject = generateAzleCandidTypeObject( | ||
mode, | ||
params, | ||
returnType | ||
); | ||
|
||
return { | ||
name, | ||
azleCandidTypeObject: azleCandidTypeObject, | ||
imports, | ||
variableAliasDeclarations: variableAliasDeclarations, | ||
src | ||
}; | ||
}); | ||
} | ||
|
||
function generateAzleCandidTypeObject( | ||
mode: Mode, | ||
params: CandidDefinition[], | ||
returnType: CandidDefinition | ||
) { | ||
const modeFunction = mode === 'query' ? query : update; | ||
const paramCandidTypeObjects = params.map( | ||
(param) => param.candidMeta.azleCandidTypeObject | ||
); | ||
const returnCandidTypeObject = returnType.candidMeta.azleCandidTypeObject; | ||
|
||
return modeFunction(paramCandidTypeObjects, returnCandidTypeObject); | ||
} |