diff --git a/property_tests/arbitraries/candid/candid_definition_arb/types.ts b/property_tests/arbitraries/candid/candid_definition_arb/types.ts index d4f0e42714..1bf1865b32 100644 --- a/property_tests/arbitraries/candid/candid_definition_arb/types.ts +++ b/property_tests/arbitraries/candid/candid_definition_arb/types.ts @@ -1,6 +1,7 @@ import fc from 'fast-check'; import { CandidType } from '../candid_type'; -import { CandidType as AzleCandidType } from '../../../../src/lib'; +import { CandidType as AzleCandidType } from '../../../../src/lib/candid/candid_type'; +import { ServiceMethodDefinition } from '../reference/service_arb/service_method_arb'; export type CandidDefinitionArb = fc.Arbitrary; @@ -60,12 +61,6 @@ export type ServiceCandidDefinition = { candidMeta: CandidMeta; funcs: ServiceMethodDefinition[]; }; -export type ServiceMethodDefinition = { - name: string; - imports: Set; - variableAliasDeclarations: string[]; - src: string; -}; type CandidMeta = { candidTypeAnnotation: string; // Either a type reference or type literal diff --git a/property_tests/arbitraries/candid/reference/service_arb/definition_arb.ts b/property_tests/arbitraries/candid/reference/service_arb/definition_arb.ts index 6af47cac4b..d83d2832b5 100644 --- a/property_tests/arbitraries/candid/reference/service_arb/definition_arb.ts +++ b/property_tests/arbitraries/candid/reference/service_arb/definition_arb.ts @@ -1,13 +1,15 @@ import fc from 'fast-check'; -import { JsFunctionNameArb } from '../../../js_function_name_arb'; -import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; import { - CandidDefinition, - ServiceCandidDefinition, + ServiceMethodArb, ServiceMethodDefinition +} from './service_method_arb'; +import { + CandidDefinition, + ServiceCandidDefinition } from '../../candid_definition_arb/types'; -import { VoidDefinitionArb } from '../../primitive/void'; +import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; +import { Canister } from '../../../../../src/lib/candid/types/reference/service'; export function ServiceDefinitionArb( fieldCandidDefArb: fc.Arbitrary @@ -34,6 +36,8 @@ export function ServiceDefinitionArb( fields ); + const azleCandidTypeObject = generateAzleCandidTypeObject(fields); + const variableAliasDeclarations = generateVariableAliasDeclarations( useTypeDeclaration, name, @@ -47,6 +51,7 @@ export function ServiceDefinitionArb( candidMeta: { candidTypeAnnotation, candidTypeObject, + azleCandidTypeObject, variableAliasDeclarations, imports, candidType: 'Service' @@ -56,49 +61,6 @@ export function ServiceDefinitionArb( }); } -function ServiceMethodArb( - candidDefArb: fc.Arbitrary -): fc.Arbitrary { - return fc - .tuple( - JsFunctionNameArb, - fc.constantFrom('query', 'update'), - 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]) - ); - - return { - name, - imports, - variableAliasDeclarations: variableAliasDeclarations, - src - }; - }); -} - function generateVariableAliasDeclarations( useTypeDeclaration: boolean, name: string, @@ -148,6 +110,19 @@ function generateCandidTypeObject( return `Canister({${methods}})`; } +function generateAzleCandidTypeObject( + serviceMethods: ServiceMethodDefinition[] +) { + const methods = serviceMethods.reduce((acc, serviceMethod) => { + return { + ...acc, + [serviceMethod.name]: serviceMethod.azleCandidTypeObject + }; + }, {}); + + return Canister(methods); +} + function generateImports( serviceMethods: ServiceMethodDefinition[] ): Set { diff --git a/property_tests/arbitraries/candid/reference/service_arb/service_method_arb.ts b/property_tests/arbitraries/candid/reference/service_arb/service_method_arb.ts new file mode 100644 index 0000000000..88c996e30c --- /dev/null +++ b/property_tests/arbitraries/candid/reference/service_arb/service_method_arb.ts @@ -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; + imports: Set; + variableAliasDeclarations: string[]; + src: string; +}; + +export function ServiceMethodArb( + candidDefArb: fc.Arbitrary +): fc.Arbitrary { + return fc + .tuple( + JsFunctionNameArb, + fc.constantFrom('query', 'update') as fc.Arbitrary, + 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); +}