Skip to content

Commit

Permalink
WIP make sure tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Jan 17, 2024
1 parent 3b45dfc commit 0197741
Show file tree
Hide file tree
Showing 20 changed files with 55 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const COMPLEX_ARB_COUNT = 8;

export function complexCandidDefinitionMemo(
parents: RecursiveCandidName[],
recursiveShapes: RecursiveShapes,
constraints: DefinitionConstraints = {}
): CandidDefinitionMemo {
const weights = constraints.weights ?? {};
Expand All @@ -51,51 +50,34 @@ export function complexCandidDefinitionMemo(
},
{
arbitrary: FuncDefinitionArb(
candidDefinitionMemo(
[],
recursiveShapes,
newConstraints
)(depthLevel),
recursiveShapes
candidDefinitionMemo([], newConstraints)(depthLevel)
),
weight: weights.func ?? 1
},
{
arbitrary: OptDefinitionArb(
candidDefinitionMemo,
parents,
recursiveShapes,
newConstraints
),
weight: weights.opt ?? 1
},
{
arbitrary: RecordDefinitionArb(
candidDefinitionMemo(
[],
recursiveShapes,
newConstraints
)(depthLevel),
recursiveShapes
candidDefinitionMemo([], newConstraints)(depthLevel)
),
weight: weights.record ?? 1
},
{
arbitrary: TupleDefinitionArb(
candidDefinitionMemo(
[],
recursiveShapes,
newConstraints
)(depthLevel),
recursiveShapes
candidDefinitionMemo([], newConstraints)(depthLevel)
),
weight: weights.tuple ?? 1
},
{
arbitrary: VariantDefinitionArb(
candidDefinitionMemo,
parents,
recursiveShapes,
newConstraints
),
weight: weights.variant ?? 1
Expand All @@ -104,19 +86,13 @@ export function complexCandidDefinitionMemo(
arbitrary: VecDefinitionArb(
candidDefinitionMemo,
parents,
recursiveShapes,
constraints
),
weight: weights.vec ?? 1
},
{
arbitrary: ServiceDefinitionArb(
candidDefinitionMemo(
[],
recursiveShapes,
newConstraints
)(depthLevel),
recursiveShapes
candidDefinitionMemo([], newConstraints)(depthLevel)
),
weight: weights.service ?? 0
// TODO Service is disabled until it is more refined. Maybe the
Expand Down
10 changes: 3 additions & 7 deletions property_tests/arbitraries/candid/candid_definition_arb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ export function candidDefinitionArb(
): CandidDefinitionArb {
return candidDefinitionMemo(
parents,
recursiveShapes,
constraints
)(constraints.depthLevel ?? DEFAULT_DEFINITION_MAX_DEPTH);
}

export function candidDefinitionMemo(
parents: RecursiveCandidName[],
recursiveShapes: RecursiveShapes,
constraints: DefinitionConstraints = {}
): CandidDefinitionMemo {
return fc.memo((depthLevel) => {
Expand All @@ -49,16 +47,14 @@ export function candidDefinitionMemo(
{
arbitrary: complexCandidDefinitionMemo(
parents,
recursiveShapes,
constraints
)(depthLevel - 1),
weight: COMPLEX_ARB_COUNT
},
{
arbitrary: recursiveCandidDefinitionMemo(
parents,
recursiveShapes
)(depthLevel - 1),
arbitrary: recursiveCandidDefinitionMemo(parents)(
depthLevel - 1
),
weight: REC_ARB_COUNT
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@ export const REC_ARB_COUNT = 0;
// https://github.com/demergent-labs/azle/issues/1525

export function recursiveCandidDefinitionMemo(
parents: RecursiveCandidName[],
recursiveShapes: RecursiveShapes
parents: RecursiveCandidName[]
): CandidDefinitionMemo {
return fc.memo((depthLevel) =>
RecursiveDefinitionArb(
complexCandidDefinitionMemo,
parents,
recursiveShapes,
{
depthLevel: depthLevel
}
)
RecursiveDefinitionArb(complexCandidDefinitionMemo, parents, {
depthLevel: depthLevel
})
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export type CandidDefinitionMemo = (
) => WithShapesArb<CandidDefinition>;
export type RecursiveCandidDefinitionMemo = (
parents: RecursiveCandidName[],
recursiveShapes: RecursiveShapes,
constraints?: DefinitionConstraints
) => CandidDefinitionMemo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { RecursiveShapes } from '../../recursive';
export function OptDefinitionArb(
candidTypeArbForInnerType: RecursiveCandidDefinitionMemo,
parents: RecursiveCandidName[],
recursiveShapes: RecursiveShapes,
constraints: DefinitionConstraints
): WithShapesArb<OptCandidDefinition> {
return fc
Expand All @@ -24,7 +23,6 @@ export function OptDefinitionArb(
possiblyRecursiveArb(
candidTypeArbForInnerType,
parents,
recursiveShapes,
constraints
),
fc.boolean()
Expand Down Expand Up @@ -82,15 +80,14 @@ export function OptDefinitionArb(
function possiblyRecursiveArb(
candidArb: RecursiveCandidDefinitionMemo,
parents: RecursiveCandidName[],
recursiveShapes: RecursiveShapes,
constraints: DefinitionConstraints
): WithShapesArb<CandidDefinition> {
const depthLevel = constraints.depthLevel ?? 0;
return fc.nat(Math.max(parents.length - 1, 0)).chain((randomIndex) => {
if (parents.length === 0 || depthLevel < 1) {
// If there are no recursive parents or we have reached a depth
// level of 0 just do a regular arb inner type
return candidArb(parents, recursiveShapes)(depthLevel);
return candidArb(parents)(depthLevel);
}
return fc.oneof(
{
Expand All @@ -101,7 +98,7 @@ function possiblyRecursiveArb(
weight: 1
},
{
arbitrary: candidArb(parents, recursiveShapes)(depthLevel),
arbitrary: candidArb(parents)(depthLevel),
weight: 1
}
);
Expand Down
11 changes: 3 additions & 8 deletions property_tests/arbitraries/candid/constructed/opt_arb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ export type Opt = [CorrespondingJSType] | never[];

export function OptArb(): fc.Arbitrary<CandidValueAndMeta<Opt>> {
return CandidValueAndMetaArbGenerator(
OptDefinitionArb(
candidDefinitionMemo,
[],
{},
{
depthLevel: DEFAULT_DEFINITION_MAX_DEPTH
}
),
OptDefinitionArb(candidDefinitionMemo, [], {
depthLevel: DEFAULT_DEFINITION_MAX_DEPTH
}),
OptValuesArb
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ type RuntimeRecord = {
};

export function RecordDefinitionArb(
fieldCandidDefArb: CandidDefinitionArb,
recShapes: RecursiveShapes
fieldCandidDefArb: CandidDefinitionArb
): WithShapesArb<RecordCandidDefinition> {
return fc
.tuple(
Expand All @@ -44,7 +43,7 @@ export function RecordDefinitionArb(
(acc, field): RecursiveShapes => {
return { ...acc, ...field[1].recursiveShapes };
},
recShapes
{}
);
const candidTypeAnnotation = generateCandidTypeAnnotation(
useTypeDeclaration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function RecordArb(
arb: CandidDefinitionArb = candidDefinitionArb({})
): fc.Arbitrary<CandidValueAndMeta<Record>> {
return CandidValueAndMetaArbGenerator(
RecordDefinitionArb(arb, {}),
RecordDefinitionArb(arb),
RecordValuesArb
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { CandidType, Tuple } from '../../../../../src/lib';
import { RecursiveShapes } from '../../recursive';

export function TupleDefinitionArb(
candidTypeArbForFields: WithShapesArb<CandidDefinition>,
recShapes: RecursiveShapes
candidTypeArbForFields: WithShapesArb<CandidDefinition>
): WithShapesArb<TupleCandidDefinition> {
return fc
.tuple(
Expand All @@ -35,7 +34,7 @@ export function TupleDefinitionArb(
(acc, field): RecursiveShapes => {
return { ...acc, ...field.recursiveShapes };
},
recShapes
{}
);
const candidTypeAnnotation = generateCandidTypeAnnotation(
useTypeDeclaration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function TupleArb(
fieldCandidDefinitionArb = candidDefinitionArb({})
): fc.Arbitrary<CandidValueAndMeta<Tuple, ReturnTuple>> {
return CandidValueAndMetaArbGenerator(
TupleDefinitionArb(fieldCandidDefinitionArb, {}),
TupleDefinitionArb(fieldCandidDefinitionArb),
TupleValuesArb
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@ type RuntimeVariant = {
export function VariantDefinitionArb(
candidTypeArbForFields: RecursiveCandidDefinitionMemo,
parents: RecursiveCandidName[],
recShapes: RecursiveShapes,
constraints: DefinitionConstraints
): WithShapesArb<VariantCandidDefinition> {
return fc
.tuple(
UniqueIdentifierArb('typeDeclaration'),
VariantFieldsArb(
candidTypeArbForFields,
parents,
recShapes,
constraints
),
VariantFieldsArb(candidTypeArbForFields, parents, constraints),
fc.boolean()
)
.map(
Expand All @@ -50,7 +44,7 @@ export function VariantDefinitionArb(
(acc, field): RecursiveShapes => {
return { ...acc, ...field[1].recursiveShapes };
},
recShapes
{}
);
const candidTypeAnnotation = generateCandidTypeAnnotation(
useTypeDeclaration,
Expand Down Expand Up @@ -97,7 +91,6 @@ export function VariantDefinitionArb(
function VariantFieldsArb(
candidTypeArb: RecursiveCandidDefinitionMemo,
parents: RecursiveCandidName[],
recursiveShapes: RecursiveShapes,
constraints: DefinitionConstraints
): fc.Arbitrary<FieldAndShapes[]> {
// Although no minLength is technically required (according to the
Expand All @@ -117,7 +110,6 @@ function VariantFieldsArb(
candidTypeArb,
index,
parents,
recursiveShapes,
constraints
)
)
Expand All @@ -138,14 +130,13 @@ function possiblyRecursiveArb(
candidArb: RecursiveCandidDefinitionMemo,
index: number,
parents: RecursiveCandidName[],
recursiveShapes: RecursiveShapes,
constraints: DefinitionConstraints
): WithShapesArb<CandidDefinition> {
const depthLevel = constraints?.depthLevel ?? 0;
return fc.nat(Math.max(parents.length - 1, 0)).chain((randomIndex) => {
if (parents.length === 0 || index < 1) {
// If there are no recursive parents or this is the first variant field just do a regular arb field
return candidArb(parents, recursiveShapes)(depthLevel);
return candidArb(parents)(depthLevel);
}
return fc.oneof(
{
Expand All @@ -156,7 +147,7 @@ function possiblyRecursiveArb(
weight: 1
},
{
arbitrary: candidArb(parents, recursiveShapes)(depthLevel),
arbitrary: candidArb(parents)(depthLevel),
weight: 1
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ export type Variant = {

export function VariantArb(): fc.Arbitrary<CandidValueAndMeta<Variant>> {
return CandidValueAndMetaArbGenerator(
VariantDefinitionArb(
candidDefinitionMemo,
[],
{},
{
depthLevel: DEFAULT_DEFINITION_MAX_DEPTH - 1
}
),
VariantDefinitionArb(candidDefinitionMemo, [], {
depthLevel: DEFAULT_DEFINITION_MAX_DEPTH - 1
}),
VariantValuesArb
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,12 @@ import { RecursiveShapes } from '../../recursive';
export function VecDefinitionArb(
candidTypeArb: RecursiveCandidDefinitionMemo,
parents: RecursiveCandidName[],
recursiveShapes: RecursiveShapes,
constraints: DefinitionConstraints
): WithShapesArb<VecCandidDefinition> {
return fc
.tuple(
UniqueIdentifierArb('typeDeclaration'),
possiblyRecursiveArb(
candidTypeArb,
parents,
recursiveShapes,
constraints
),
possiblyRecursiveArb(candidTypeArb, parents, constraints),
fc.boolean()
)
.map(
Expand Down Expand Up @@ -82,14 +76,13 @@ export function VecDefinitionArb(
function possiblyRecursiveArb(
candidArb: RecursiveCandidDefinitionMemo,
parents: RecursiveCandidName[],
recursiveShapes: RecursiveShapes,
constraints: DefinitionConstraints
): WithShapesArb<CandidDefinition> {
const depthLevel = constraints?.depthLevel ?? 0;
return fc.nat(Math.max(parents.length - 1, 0)).chain((randomIndex) => {
if (parents.length === 0) {
// If there are no recursive parents or this is the first variant field just do a regular arb field
return candidArb(parents, recursiveShapes)(depthLevel);
return candidArb(parents)(depthLevel);
}
return fc.oneof(
{
Expand All @@ -100,7 +93,7 @@ function possiblyRecursiveArb(
weight: 1
},
{
arbitrary: candidArb(parents, recursiveShapes)(depthLevel),
arbitrary: candidArb(parents)(depthLevel),
weight: 1
}
);
Expand Down
Loading

0 comments on commit 0197741

Please sign in to comment.