Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Added init-method property test #1492

Merged
merged 19 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ jobs:
"property_tests/tests/float32",
"property_tests/tests/float64",
"property_tests/tests/func",
"property_tests/tests/init_method",
"property_tests/tests/int",
"property_tests/tests/int8",
"property_tests/tests/int16",
Expand All @@ -147,12 +148,15 @@ jobs:
"property_tests/tests/nat32",
"property_tests/tests/nat64",
"property_tests/tests/null",
"property_tests/tests/opt",
"property_tests/tests/principal",
"property_tests/tests/query_methods",
"property_tests/tests/record",
"property_tests/tests/service",
"property_tests/tests/stable_b_tree_map",
"property_tests/tests/text",
"property_tests/tests/tuple",
"property_tests/tests/update_methods",
"property_tests/tests/variant",
"property_tests/tests/vec"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fc from 'fast-check';
import { CandidType } from '../candid_type';
import { CandidType as AzleCandidType } from '../../../../src/lib/candid/candid_type';
import { ServiceMethodDefinition } from '../reference/service_arb/service_method_arb';

export type CandidDefinitionArb = fc.Arbitrary<CandidDefinition>;

Expand Down Expand Up @@ -59,16 +61,11 @@ export type ServiceCandidDefinition = {
candidMeta: CandidMeta;
funcs: ServiceMethodDefinition[];
};
export type ServiceMethodDefinition = {
name: string;
imports: Set<string>;
variableAliasDeclarations: string[];
src: string;
};

type CandidMeta = {
candidTypeAnnotation: string; // Either a type reference or type literal
candidTypeObject: string;
azleCandidTypeObject: AzleCandidType;
bdemann marked this conversation as resolved.
Show resolved Hide resolved
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
bdemann marked this conversation as resolved.
Show resolved Hide resolved
*/
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
bdemann marked this conversation as resolved.
Show resolved Hide resolved
}
};
}
);
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]) => {
bdemann marked this conversation as resolved.
Show resolved Hide resolved
acc[fieldName] = fieldDefinition.candidMeta.azleCandidTypeObject;
bdemann marked this conversation as resolved.
Show resolved Hide resolved
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]) => {
bdemann marked this conversation as resolved.
Show resolved Hide resolved
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) => {
bdemann marked this conversation as resolved.
Show resolved Hide resolved
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);
}
Loading
Loading