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 all 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 RuntimeCandidType } 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;
runtimeCandidTypeObject: RuntimeCandidType;
variableAliasDeclarations: string[];
imports: Set<string>;
candidType: CandidType;
Expand Down
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 as RuntimeCandidType } 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;
runtimeCandidTypeObject: RuntimeCandidType;
};
src: {
candidTypeAnnotation: string;
candidTypeObject: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ export function CandidValueAndMetaArbGenerator<
V extends CandidValues<T>
>(
DefinitionArb: fc.Arbitrary<D>,
ValueArb: (arb: D) => fc.Arbitrary<V>
ValueArb: (arb: D, constraints?: any) => fc.Arbitrary<V>,
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
valueConstraints?: any
): fc.Arbitrary<CandidValueAndMeta<any>> {
return DefinitionArb.chain((candidDefinition) =>
fc.tuple(fc.constant(candidDefinition), ValueArb(candidDefinition))
fc.tuple(
fc.constant(candidDefinition),
ValueArb(candidDefinition, valueConstraints)
)
).map(
([
{
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
runtimeCandidTypeObject,
variableAliasDeclarations,
imports
}
Expand All @@ -34,8 +39,11 @@ export function CandidValueAndMetaArbGenerator<
imports,
valueLiteral
},
agentArgumentValue,
agentResponseValue
value: {
agentArgumentValue,
agentResponseValue,
runtimeCandidTypeObject
}
};
}
);
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: {
runtimeCandidTypeObject: 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 runtimeCandidTypeObject =
generateRuntimeCandidTypeObject(innerType);

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

function generateRuntimeCandidTypeObject(
innerType: CandidDefinition
): CandidType {
return Opt(innerType.candidMeta.runtimeCandidTypeObject);
}

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,9 +6,14 @@ import {
CandidDefinitionArb,
RecordCandidDefinition
} from '../../candid_definition_arb/types';
import { CandidType, Record } from '../../../../../src/lib';

type Field = [string, CandidDefinition];

type RuntimeRecord = {
[key: string]: CandidType;
};

export function RecordDefinitionArb(
fieldCandidDefArb: CandidDefinitionArb
): fc.Arbitrary<RecordCandidDefinition> {
Expand All @@ -35,6 +40,9 @@ export function RecordDefinitionArb(
fields
);

const runtimeCandidTypeObject =
generateRuntimeCandidTypeObject(fields);

const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
Expand All @@ -47,6 +55,7 @@ export function RecordDefinitionArb(
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
runtimeCandidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Record'
Expand All @@ -57,7 +66,7 @@ export function RecordDefinitionArb(
}

function generateImports(fields: Field[]): Set<string> {
const fieldImports = fields.flatMap((field) => [
const fieldImports = fields.flatMap((field): string[] => [
...field[1].candidMeta.imports
]);
return new Set([...fieldImports, 'Record']);
Expand Down Expand Up @@ -97,13 +106,27 @@ function generateCandidTypeObject(
.join(',')}})`;
}

function generateRuntimeCandidTypeObject(fields: Field[]): CandidType {
const azleRecordConstructorObj = fields.reduce(
(acc, [fieldName, fieldDefinition]): RuntimeRecord => {
return {
...acc,
[fieldName]: fieldDefinition.candidMeta.runtimeCandidTypeObject
};
},
{}
);

return Record(azleRecordConstructorObj);
}

function generateVariableAliasDeclarations(
useTypeDeclaration: boolean,
name: string,
fields: Field[]
): string[] {
const fieldVariableAliasDefinitions = fields.flatMap(
(field) => field[1].candidMeta.variableAliasDeclarations
(field): string[] => field[1].candidMeta.variableAliasDeclarations
);
if (useTypeDeclaration) {
return [
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,9 @@ export function TupleDefinitionArb(
fields
);

const runtimeCandidTypeObject =
generateRuntimeCandidTypeObject(fields);

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

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

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,9 +5,14 @@ import {
VariantCandidDefinition
} from '../../candid_definition_arb/types';
import { JsFunctionNameArb } from '../../../js_function_name_arb';
import { CandidType, Variant } from '../../../../../src/lib';

type Field = [string, CandidDefinition];

type RuntimeVariant = {
[key: string]: CandidType;
};

export function VariantDefinitionArb(
candidTypeArbForFields: fc.Arbitrary<CandidDefinition>
): fc.Arbitrary<VariantCandidDefinition> {
Expand All @@ -30,6 +35,9 @@ export function VariantDefinitionArb(
fields
);

const runtimeCandidTypeObject =
generateRuntimeCandidTypeObject(fields);

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

function generateRuntimeCandidTypeObject(fields: Field[]): CandidType {
const azleVariantConstructorObj = fields.reduce(
(acc, [fieldName, fieldDefinition]): RuntimeVariant => {
return {
...acc,
[fieldName]: fieldDefinition.candidMeta.runtimeCandidTypeObject
};
},
{}
);

return Variant(azleVariantConstructorObj);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export function VariantValuesArb(

const [name, innerType] = variantDefinition.innerTypes[randomIndex];

const fieldValue = CandidValueArb(innerType).map((values): Field => {
const fieldValues = CandidValueArb(innerType).map((values): Field => {
return [name, values];
});

return fieldValue.map((fieldValues) => {
const valueLiteral = generateValueLiteral(fieldValues);
const agentArgumentValue = generateValue(fieldValues);
const agentResponseValue = generateValue(fieldValues, true);
return fieldValues.map((fieldValue) => {
const valueLiteral = generateValueLiteral(fieldValue);
const agentArgumentValue = generateValue(fieldValue);
const agentResponseValue = generateValue(fieldValue, 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 runtimeCandidTypeObject =
generateRuntimeCandidTypeObject(innerType);

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

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

function generateRuntimeCandidTypeObject(
innerType: CandidDefinition
): CandidType {
return Vec(innerType.candidMeta.runtimeCandidTypeObject);
}
Loading
Loading