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 6 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fc from 'fast-check';
import { CandidType } from '../candid_type';
import { CandidType as AzleCandidType } from '../../../../src/lib/candid/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 @@ -65,7 +65,7 @@ export type ServiceCandidDefinition = {
type CandidMeta = {
candidTypeAnnotation: string; // Either a type reference or type literal
candidTypeObject: string;
azleCandidTypeObject: AzleCandidType;
runtimeCandidTypeObject: RuntimeCandidType;
variableAliasDeclarations: string[];
imports: Set<string>;
candidType: CandidType;
Expand Down
32 changes: 2 additions & 30 deletions property_tests/arbitraries/candid/candid_value_and_meta_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ 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';
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> = {
value: {
agentArgumentValue: T;
agentResponseValue: E;
candidTypeObject: CandidType;
runtimeCandidTypeObject: RuntimeCandidType;
};
src: {
candidTypeAnnotation: string;
Expand Down Expand Up @@ -75,31 +75,3 @@ 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 @@ -10,17 +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,
azleCandidTypeObject,
runtimeCandidTypeObject,
variableAliasDeclarations,
imports
}
Expand All @@ -38,7 +42,7 @@ export function CandidValueAndMetaArbGenerator<
value: {
agentArgumentValue,
agentResponseValue,
candidTypeObject: azleCandidTypeObject
runtimeCandidTypeObject
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function _VecNat8DefinitionArb(): fc.Arbitrary<BlobCandidDefinition> {
const imports = new Set(['Vec', 'nat8']);
return {
candidMeta: {
azleCandidTypeObject: blob,
runtimeCandidTypeObject: blob,
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export function OptDefinitionArb(
innerType
);

const azleCandidTypeObject =
generateAzleCandidTypeObject(innerType);
const runtimeCandidTypeObject =
generateRuntimeCandidTypeObject(innerType);

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

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

function generateImports(innerType: CandidDefinition): Set<string> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { CandidType, Record } from '../../../../../src/lib';

type Field = [string, CandidDefinition];

type RunTimeRecord = {
bdemann marked this conversation as resolved.
Show resolved Hide resolved
[key: string]: CandidType;
};

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

const azleCandidTypeObject = generateAzleCandidTypeObject(fields);
const runtimeCandidTypeObject =
generateRuntimeCandidTypeObject(fields);

const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
Expand All @@ -50,7 +55,7 @@ export function RecordDefinitionArb(
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
azleCandidTypeObject,
runtimeCandidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Record'
Expand All @@ -61,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 @@ -101,13 +106,16 @@ 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;
}, {});
function generateRuntimeCandidTypeObject(fields: Field[]): CandidType {
const azleRecordConstructorObj = fields.reduce(
(acc, [fieldName, fieldDefinition]): RunTimeRecord => {
return {
...acc,
[fieldName]: fieldDefinition.candidMeta.runtimeCandidTypeObject
};
},
{}
);

return Record(azleRecordConstructorObj);
}
Expand All @@ -118,7 +126,7 @@ function generateVariableAliasDeclarations(
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 @@ -31,7 +31,8 @@ export function TupleDefinitionArb(
fields
);

const azleCandidTypeObject = generateAzleCandidTypeObject(fields);
const runtimeCandidTypeObject =
generateRuntimeCandidTypeObject(fields);

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

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

return Tuple(...innerTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { CandidType, Variant } from '../../../../../src/lib';

type Field = [string, CandidDefinition];

type RunTimeVariant = {
bdemann marked this conversation as resolved.
Show resolved Hide resolved
[key: string]: CandidType;
};

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

const azleCandidTypeObject = generateAzleCandidTypeObject(fields);
const runtimeCandidTypeObject =
generateRuntimeCandidTypeObject(fields);

const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
Expand All @@ -45,7 +50,7 @@ export function VariantDefinitionArb(
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
azleCandidTypeObject,
runtimeCandidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Variant'
Expand Down Expand Up @@ -125,13 +130,16 @@ 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;
}, {});
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((field) => {
const valueLiteral = generateValueLiteral(field);
const agentArgumentValue = generateValue(field);
const agentResponseValue = generateValue(field, 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 @@ -28,8 +28,8 @@ export function VecDefinitionArb(
innerType
);

const azleCandidTypeObject =
generateAzleCandidTypeObject(innerType);
const runtimeCandidTypeObject =
generateRuntimeCandidTypeObject(innerType);

const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
Expand All @@ -43,7 +43,7 @@ export function VecDefinitionArb(
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
azleCandidTypeObject,
runtimeCandidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Vec'
Expand Down Expand Up @@ -99,6 +99,8 @@ function generateCandidTypeObject(
return `Vec(${innerType.candidMeta.candidTypeObject})`;
}

function generateAzleCandidTypeObject(innerType: CandidDefinition): CandidType {
return Vec(innerType.candidMeta.azleCandidTypeObject);
function generateRuntimeCandidTypeObject(
innerType: CandidDefinition
): CandidType {
return Vec(innerType.candidMeta.runtimeCandidTypeObject);
}
32 changes: 27 additions & 5 deletions property_tests/arbitraries/candid/primitive/text.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
import fc from 'fast-check';
import fc, { StringSharedConstraints } from 'fast-check';
import { stringToSrcLiteral } from '../to_src_literal/string';
import { TextCandidDefinition } from '../candid_definition_arb/types';
import { SimpleCandidValuesArb } from '../simple_type_arbs/values_arb';
import { SimpleCandidDefinitionArb } from '../simple_type_arbs/definition_arb';
import { CandidValues } from '../candid_values_arb';
import { CandidValueAndMeta } from '../candid_value_and_meta_arb';
import { CandidValueAndMetaArbGenerator } from '../candid_value_and_meta_arb_generator';
import { JsFunctionNameArb } from '../../js_function_name_arb';

export function TextArb(): fc.Arbitrary<CandidValueAndMeta<string>> {
return CandidValueAndMetaArbGenerator(TextDefinitionArb(), TextValueArb);
export interface TextConstraints extends StringSharedConstraints {
canStartWithDigit?: boolean;
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
isJsFunctionName?: boolean;
}

export function TextArb(
constraints?: TextConstraints
): fc.Arbitrary<CandidValueAndMeta<string>> {
return CandidValueAndMetaArbGenerator(
TextDefinitionArb(),
TextValueArb,
constraints
);
}

export function TextDefinitionArb(): fc.Arbitrary<TextCandidDefinition> {
return SimpleCandidDefinitionArb('text');
}

export function TextValueArb(): fc.Arbitrary<CandidValues<string>> {
return SimpleCandidValuesArb(fc.string(), stringToSrcLiteral);
export function TextValueArb(
_?: TextCandidDefinition,
constraints?: TextConstraints
): fc.Arbitrary<CandidValues<string>> {
const canStartWithDigit = constraints?.canStartWithDigit ?? true;
const isJSName = constraints?.isJsFunctionName ?? false;
const textArb = isJSName
? JsFunctionNameArb
: fc
.string(constraints)
.filter((string) => canStartWithDigit || /^\d/.test(string));
return SimpleCandidValuesArb(textArb, stringToSrcLiteral);
}
Loading
Loading