Skip to content

Commit

Permalink
Add InitMethodArb
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren committed Dec 8, 2023
1 parent 56cceea commit 12d7206
Show file tree
Hide file tree
Showing 26 changed files with 1,631 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fc from 'fast-check';
import { CandidType } from '../candid_type';
import { CandidType as AzleCandidType } from '../../../../src/lib';

export type CandidDefinitionArb = fc.Arbitrary<CandidDefinition>;

Expand Down Expand Up @@ -69,6 +70,7 @@ export type ServiceMethodDefinition = {
type CandidMeta = {
candidTypeAnnotation: string; // Either a type reference or type literal
candidTypeObject: string;
azleCandidTypeObject: AzleCandidType;
variableAliasDeclarations: string[];
imports: Set<string>;
candidType: CandidType;
Expand Down
36 changes: 34 additions & 2 deletions property_tests/arbitraries/candid/candid_value_and_meta_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ import { OptArb } from './constructed/opt_arb';
import { VecArb } from './constructed/vec_arb';
import { FuncArb } from './reference/func_arb';
import { CorrespondingJSType } from './corresponding_js_type';
import { CandidType } from '../../../src/lib';

// TODO we're thinking that Candid is not the best name for this. What is better?
export type CandidValueAndMeta<T extends CorrespondingJSType, E = T> = {
agentArgumentValue: T;
agentResponseValue: E;
value: {
agentArgumentValue: T;
agentResponseValue: E;
candidTypeObject: CandidType;
};
src: {
candidTypeAnnotation: string;
candidTypeObject: string;
Expand Down Expand Up @@ -71,3 +75,31 @@ export function CandidValueAndMetaArb(): fc.Arbitrary<
}

// TODO: This needs to support service.

/**
* Azle currently (v0.18.6) cannot accept funcs in init methods.
* See https://github.com/demergent-labs/azle/issues/1474
*/
export function CandidValueAndMetaArbWithoutFuncs(): fc.Arbitrary<
CandidValueAndMeta<CorrespondingJSType>
> {
return fc.oneof(
BlobArb(),
Float32Arb(),
Float64Arb(),
IntArb(),
Int8Arb(),
Int16Arb(),
Int32Arb(),
Int64Arb(),
NatArb(),
Nat8Arb(),
Nat16Arb(),
Nat32Arb(),
Nat64Arb(),
BoolArb(),
NullArb(),
TextArb(),
PrincipalArb()
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function CandidValueAndMetaArbGenerator<
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
azleCandidTypeObject,
variableAliasDeclarations,
imports
}
Expand All @@ -34,8 +35,11 @@ export function CandidValueAndMetaArbGenerator<
imports,
valueLiteral
},
agentArgumentValue,
agentResponseValue
value: {
agentArgumentValue,
agentResponseValue,
candidTypeObject: azleCandidTypeObject
}
};
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fc from 'fast-check';
import { UniqueIdentifierArb } from '../../../unique_identifier_arb';
import { BlobCandidDefinition } from '../../candid_definition_arb/types';
import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb';
import { blob } from '../../../../../src/lib';

export function BlobDefinitionArb(): fc.Arbitrary<BlobCandidDefinition> {
return fc.oneof(SimpleCandidDefinitionArb('blob'), _VecNat8DefinitionArb());
Expand All @@ -19,6 +20,7 @@ export function _VecNat8DefinitionArb(): fc.Arbitrary<BlobCandidDefinition> {
const imports = new Set(['Vec', 'nat8']);
return {
candidMeta: {
azleCandidTypeObject: blob,
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CandidDefinition,
OptCandidDefinition
} from '../../candid_definition_arb/types';
import { CandidType, Opt } from '../../../../../src/lib';

export function OptDefinitionArb(
candidTypeArbForInnerType: fc.Arbitrary<CandidDefinition>
Expand All @@ -27,6 +28,9 @@ export function OptDefinitionArb(
innerType
);

const azleCandidTypeObject =
generateAzleCandidTypeObject(innerType);

const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
Expand All @@ -39,6 +43,7 @@ export function OptDefinitionArb(
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
azleCandidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Opt'
Expand Down Expand Up @@ -90,6 +95,10 @@ function generateCandidTypeObject(
return `Opt(${innerType.candidMeta.candidTypeObject})`;
}

function generateAzleCandidTypeObject(innerType: CandidDefinition): CandidType {
return Opt(innerType.candidMeta.azleCandidTypeObject);
}

function generateImports(innerType: CandidDefinition): Set<string> {
return new Set([...innerType.candidMeta.imports, 'Opt', 'Some', 'None']);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CandidDefinitionArb,
RecordCandidDefinition
} from '../../candid_definition_arb/types';
import { CandidType, Record } from '../../../../../src/lib';

type Field = [string, CandidDefinition];

Expand Down Expand Up @@ -35,6 +36,8 @@ export function RecordDefinitionArb(
fields
);

const azleCandidTypeObject = generateAzleCandidTypeObject(fields);

const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
Expand All @@ -47,6 +50,7 @@ export function RecordDefinitionArb(
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
azleCandidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Record'
Expand Down Expand Up @@ -97,6 +101,17 @@ function generateCandidTypeObject(
.join(',')}})`;
}

function generateAzleCandidTypeObject(fields: Field[]): CandidType {
const azleRecordConstructorObj = fields.reduce<{
[key: string]: CandidType;
}>((acc, [fieldName, fieldDefinition]) => {
acc[fieldName] = fieldDefinition.candidMeta.azleCandidTypeObject;
return acc;
}, {});

return Record(azleRecordConstructorObj);
}

function generateVariableAliasDeclarations(
useTypeDeclaration: boolean,
name: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CandidDefinition,
TupleCandidDefinition
} from '../../candid_definition_arb/types';
import { CandidType, Tuple } from '../../../../../src/lib';

export function TupleDefinitionArb(
candidTypeArbForFields: fc.Arbitrary<CandidDefinition>
Expand All @@ -30,6 +31,8 @@ export function TupleDefinitionArb(
fields
);

const azleCandidTypeObject = generateAzleCandidTypeObject(fields);

const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
Expand All @@ -42,6 +45,7 @@ export function TupleDefinitionArb(
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
azleCandidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Tuple'
Expand Down Expand Up @@ -98,6 +102,14 @@ function generateCandidTypeObject(
return `Tuple(${innerTypes.join(', ')})`;
}

function generateAzleCandidTypeObject(fields: CandidDefinition[]): CandidType {
const innerTypes = fields.map(
(field) => field.candidMeta.azleCandidTypeObject
);

return Tuple(...innerTypes);
}

function generateImports(fields: CandidDefinition[]): Set<string> {
const fieldImports = fields.flatMap((field) => [
...field.candidMeta.imports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
VariantCandidDefinition
} from '../../candid_definition_arb/types';
import { JsFunctionNameArb } from '../../../js_function_name_arb';
import { CandidType, Variant } from '../../../../../src/lib';

type Field = [string, CandidDefinition];

Expand All @@ -30,6 +31,8 @@ export function VariantDefinitionArb(
fields
);

const azleCandidTypeObject = generateAzleCandidTypeObject(fields);

const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
Expand All @@ -42,6 +45,7 @@ export function VariantDefinitionArb(
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
azleCandidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Variant'
Expand Down Expand Up @@ -120,3 +124,14 @@ function generateCandidTypeObject(
)
.join(',')}})`;
}

function generateAzleCandidTypeObject(fields: Field[]): CandidType {
const azleVariantConstructorObj = fields.reduce<{
[key: string]: CandidType;
}>((acc, [fieldName, fieldDefinition]) => {
acc[fieldName] = fieldDefinition.candidMeta.azleCandidTypeObject;
return acc;
}, {});

return Variant(azleVariantConstructorObj);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export function VariantValuesArb(
return [name, values];
});

return fieldValue.map((fieldValues) => {
const valueLiteral = generateValueLiteral(fieldValues);
const agentArgumentValue = generateValue(fieldValues);
const agentResponseValue = generateValue(fieldValues, true);
return fieldValue.map((field) => {
const valueLiteral = generateValueLiteral(field);
const agentArgumentValue = generateValue(field);
const agentResponseValue = generateValue(field, true);

return {
valueLiteral,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CandidDefinition,
VecCandidDefinition
} from '../../candid_definition_arb/types';
import { CandidType, Vec } from '../../../../../src/lib';

export function VecDefinitionArb(
candidTypeArb: fc.Arbitrary<CandidDefinition>
Expand All @@ -27,6 +28,9 @@ export function VecDefinitionArb(
innerType
);

const azleCandidTypeObject =
generateAzleCandidTypeObject(innerType);

const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
Expand All @@ -39,6 +43,7 @@ export function VecDefinitionArb(
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
azleCandidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Vec'
Expand Down Expand Up @@ -93,3 +98,7 @@ function generateCandidTypeObject(

return `Vec(${innerType.candidMeta.candidTypeObject})`;
}

function generateAzleCandidTypeObject(innerType: CandidDefinition): CandidType {
return Vec(innerType.candidMeta.azleCandidTypeObject);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
FuncCandidDefinition
} from '../../candid_definition_arb/types';
import { VoidDefinitionArb } from '../../primitive/void';
import { CandidType, Func } from '../../../../../src/lib';

type Mode = 'query' | 'update' | 'oneway';

Expand Down Expand Up @@ -45,6 +46,12 @@ export function FuncDefinitionArb(
mode
);

const azleCandidTypeObject = generateAzleCandidTypeObject(
params,
returnFunc,
mode
);

const variableAliasDeclarations =
generateVariableAliasDeclarations(
useTypeDeclaration,
Expand All @@ -65,6 +72,7 @@ export function FuncDefinitionArb(
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
azleCandidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Func'
Expand Down Expand Up @@ -131,5 +139,18 @@ function generateCandidTypeObject(
const params = paramCandids
.map((param) => param.candidMeta.candidTypeObject)
.join(', ');

return `Func([${params}], ${returnCandid.candidMeta.candidTypeObject}, '${mode}')`;
}

function generateAzleCandidTypeObject(
paramCandids: CandidDefinition[],
returnCandid: CandidDefinition,
mode: Mode
): CandidType {
const params = paramCandids.map(
(param) => param.candidMeta.azleCandidTypeObject
);

return Func(params, returnCandid.candidMeta.azleCandidTypeObject, mode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { PrincipalArb } from '../principal_arb';
export function FuncValueArb(): fc.Arbitrary<CandidValues<Func>> {
return fc.tuple(TextArb(), PrincipalArb()).map(([name, principal]) => {
const value: Func = [
principal.agentArgumentValue,
name.agentArgumentValue
principal.value.agentArgumentValue,
name.value.agentArgumentValue
];

const valueLiteral = `[${principal.src.valueLiteral}, ${name.src.valueLiteral}]`;
Expand Down
Loading

0 comments on commit 12d7206

Please sign in to comment.