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

Class based prop tests prims #2008

Closed
wants to merge 19 commits into from
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fc from 'fast-check';

import { Syntax } from '../../types';
import { BlobDefinitionArb } from '../constructed/blob_arb/definition_arb';
import { OptDefinitionArb } from '../constructed/opt_arb/definition_arb';
import { RecordDefinitionArb } from '../constructed/record_arb/definition_arb';
Expand Down Expand Up @@ -31,6 +32,7 @@ export const COMPLEX_ARB_COUNT = 8;

export function complexCandidDefinitionMemo(
parents: RecursiveCandidName[],
syntax: Syntax,
constraints: DefinitionConstraints = {}
): CandidDefinitionMemo {
const weights = constraints.weights ?? {};
Expand All @@ -45,39 +47,56 @@ export function complexCandidDefinitionMemo(
return fc.memo((depthLevel) => {
return fc.oneof(
{
arbitrary: BlobDefinitionArb(),
arbitrary: BlobDefinitionArb(syntax),
weight: weights.blob ?? 1
},
{
arbitrary: FuncDefinitionArb(
candidDefinitionMemo([], newConstraints)(depthLevel)
candidDefinitionMemo(
[],
syntax,
newConstraints
)(depthLevel),
syntax
),
weight: weights.func ?? 1
},
{
arbitrary: OptDefinitionArb(
candidDefinitionMemo,
parents,
syntax,
newConstraints
),
weight: weights.opt ?? 1
},
{
arbitrary: RecordDefinitionArb(
candidDefinitionMemo([], newConstraints)(depthLevel)
candidDefinitionMemo(
[],
syntax,
newConstraints
)(depthLevel),
syntax
),
weight: weights.record ?? 1
},
{
arbitrary: TupleDefinitionArb(
candidDefinitionMemo([], newConstraints)(depthLevel)
candidDefinitionMemo(
[],
syntax,
newConstraints
)(depthLevel),
syntax
),
weight: weights.tuple ?? 1
},
{
arbitrary: VariantDefinitionArb(
candidDefinitionMemo,
parents,
syntax,
newConstraints
),
weight: weights.variant ?? 1
Expand All @@ -86,13 +105,19 @@ export function complexCandidDefinitionMemo(
arbitrary: VecDefinitionArb(
candidDefinitionMemo,
parents,
syntax,
constraints
),
weight: weights.vec ?? 1
},
{
arbitrary: ServiceDefinitionArb(
candidDefinitionMemo([], newConstraints)(depthLevel)
candidDefinitionMemo(
[],
syntax,
newConstraints
)(depthLevel),
syntax
),
weight: weights.service ?? 0
// TODO Service is disabled until it is more refined. Maybe the
Expand Down
19 changes: 14 additions & 5 deletions property_tests/arbitraries/candid/candid_definition_arb/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fc from 'fast-check';

import { DEFAULT_DEFINITION_MAX_DEPTH } from '../../config';
import { Syntax } from '../../types';
import { RecursiveShapes } from '../recursive';
import {
COMPLEX_ARB_COUNT,
Expand All @@ -24,38 +25,46 @@ import {
export function candidDefinitionArb(
recursiveShapes: RecursiveShapes,
parents: RecursiveCandidName[] = [],
syntax: Syntax,
constraints: DefinitionConstraints = {}
): CandidDefinitionArb {
return candidDefinitionMemo(
parents,
syntax,
constraints
)(constraints.depthLevel ?? DEFAULT_DEFINITION_MAX_DEPTH);
}

export function candidDefinitionMemo(
parents: RecursiveCandidName[],
syntax: Syntax,
constraints: DefinitionConstraints = {}
): CandidDefinitionMemo {
return fc.memo((depthLevel) => {
if (depthLevel <= 1) {
return primitiveCandidDefinitionArb();
return primitiveCandidDefinitionArb(syntax);
}
return fc.oneof(
{
arbitrary: primitiveCandidDefinitionArb(constraints.weights),
arbitrary: primitiveCandidDefinitionArb(
syntax,
constraints.weights
),
weight: PRIM_ARB_COUNT
},
{
arbitrary: complexCandidDefinitionMemo(
parents,
syntax,
constraints
)(depthLevel - 1),
weight: COMPLEX_ARB_COUNT
},
{
arbitrary: recursiveCandidDefinitionMemo(parents)(
depthLevel - 1
),
arbitrary: recursiveCandidDefinitionMemo(
parents,
syntax
)(depthLevel - 1),
weight: REC_ARB_COUNT
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fc from 'fast-check';

import { Syntax } from '../../types';
import { RecursiveDefinitionArb } from '../recursive/definition_arb';
import { complexCandidDefinitionMemo } from './complex_candid_definition_memo';
import { CandidDefinitionMemo, RecursiveCandidName } from './types';
Expand All @@ -12,10 +13,11 @@ export const REC_ARB_COUNT = 0;
// https://github.com/demergent-labs/azle/issues/1525

export function recursiveCandidDefinitionMemo(
parents: RecursiveCandidName[]
parents: RecursiveCandidName[],
syntax: Syntax
): CandidDefinitionMemo {
return fc.memo((depthLevel) =>
RecursiveDefinitionArb(complexCandidDefinitionMemo, parents, {
RecursiveDefinitionArb(complexCandidDefinitionMemo, parents, syntax, {
depthLevel: depthLevel
})
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fc from 'fast-check';

import { Syntax } from '../../types';
import { BoolDefinitionArb } from '../primitive/bool';
import { Float32DefinitionArb } from '../primitive/floats/float32_arb';
import { Float64DefinitionArb } from '../primitive/floats/float64_arb';
Expand Down Expand Up @@ -43,26 +44,51 @@ export const PRIM_ARB_COUNT = 16;
const PRIM_DEF_WEIGHTS_DEFAULT = {};

export function primitiveCandidDefinitionArb(
syntax: Syntax,
constraints: PrimitiveDefinitionWeights = PRIM_DEF_WEIGHTS_DEFAULT
): WithShapesArb<PrimitiveDefinition> {
return fc.oneof(
{ arbitrary: BoolDefinitionArb(), weight: constraints.bool ?? 1 },
{ arbitrary: Float32DefinitionArb(), weight: constraints.float32 ?? 1 },
{ arbitrary: Float64DefinitionArb(), weight: constraints.float64 ?? 1 },
{ arbitrary: IntDefinitionArb(), weight: constraints.int ?? 1 },
{ arbitrary: Int8DefinitionArb(), weight: constraints.int8 ?? 1 },
{ arbitrary: Int16DefinitionArb(), weight: constraints.int16 ?? 1 },
{ arbitrary: Int32DefinitionArb(), weight: constraints.int32 ?? 1 },
{ arbitrary: Int64DefinitionArb(), weight: constraints.int64 ?? 1 },
{ arbitrary: NatDefinitionArb(), weight: constraints.nat ?? 1 },
{ arbitrary: Nat8DefinitionArb(), weight: constraints.nat8 ?? 1 },
{ arbitrary: Nat16DefinitionArb(), weight: constraints.nat16 ?? 1 },
{ arbitrary: Nat32DefinitionArb(), weight: constraints.nat32 ?? 1 },
{ arbitrary: Nat64DefinitionArb(), weight: constraints.nat64 ?? 1 },
{ arbitrary: NullDefinitionArb(), weight: constraints.null ?? 1 },
{ arbitrary: TextDefinitionArb(), weight: constraints.text ?? 1 },
{ arbitrary: BoolDefinitionArb(syntax), weight: constraints.bool ?? 1 },
{
arbitrary: PrincipalDefinitionArb(),
arbitrary: Float32DefinitionArb(syntax),
weight: constraints.float32 ?? 1
},
{
arbitrary: Float64DefinitionArb(syntax),
weight: constraints.float64 ?? 1
},
{ arbitrary: IntDefinitionArb(syntax), weight: constraints.int ?? 1 },
{ arbitrary: Int8DefinitionArb(syntax), weight: constraints.int8 ?? 1 },
{
arbitrary: Int16DefinitionArb(syntax),
weight: constraints.int16 ?? 1
},
{
arbitrary: Int32DefinitionArb(syntax),
weight: constraints.int32 ?? 1
},
{
arbitrary: Int64DefinitionArb(syntax),
weight: constraints.int64 ?? 1
},
{ arbitrary: NatDefinitionArb(syntax), weight: constraints.nat ?? 1 },
{ arbitrary: Nat8DefinitionArb(syntax), weight: constraints.nat8 ?? 1 },
{
arbitrary: Nat16DefinitionArb(syntax),
weight: constraints.nat16 ?? 1
},
{
arbitrary: Nat32DefinitionArb(syntax),
weight: constraints.nat32 ?? 1
},
{
arbitrary: Nat64DefinitionArb(syntax),
weight: constraints.nat64 ?? 1
},
{ arbitrary: NullDefinitionArb(syntax), weight: constraints.null ?? 1 },
{ arbitrary: TextDefinitionArb(syntax), weight: constraints.text ?? 1 },
{
arbitrary: PrincipalDefinitionArb(syntax),
weight: constraints.principal ?? 1
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fc from 'fast-check';

import { CandidType as RuntimeCandidType } from '../../../../src/lib/experimental/candid/candid_type';
import { Syntax } from '../../types';
import { CandidType } from '../candid_type';
import { RecursiveShapes } from '../recursive';
import { ServiceMethodDefinition } from '../reference/service_arb/service_method_arb';
Expand All @@ -12,6 +13,7 @@ export type CandidDefinitionMemo = (
) => WithShapesArb<CandidDefinition>;
export type RecursiveCandidDefinitionMemo = (
parents: RecursiveCandidName[],
syntax: Syntax,
constraints?: DefinitionConstraints
) => CandidDefinitionMemo;

Expand Down Expand Up @@ -108,4 +110,5 @@ type CandidMeta = {
variableAliasDeclarations: string[];
imports: Set<string>;
candidType: CandidType;
idl: string;
};
11 changes: 6 additions & 5 deletions property_tests/arbitraries/candid/candid_return_type_arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fc from 'fast-check';

import { Syntax } from '../types';
import {
CandidValueAndMeta,
CandidValueAndMetaArb
Expand All @@ -9,11 +10,11 @@ import { VoidArb } from './primitive/void';

export type CandidReturnType = CorrespondingJSType | undefined;

export function CandidReturnTypeArb(): fc.Arbitrary<
CandidValueAndMeta<CandidReturnType>
> {
export function CandidReturnTypeArb(
syntax: Syntax
): fc.Arbitrary<CandidValueAndMeta<CandidReturnType>> {
return fc.oneof(
{ arbitrary: CandidValueAndMetaArb(), weight: 17 },
{ arbitrary: VoidArb(), weight: 1 }
{ arbitrary: CandidValueAndMetaArb(syntax), weight: 17 },
{ arbitrary: VoidArb(syntax), weight: 1 }
);
}
31 changes: 31 additions & 0 deletions property_tests/arbitraries/candid/candid_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,34 @@ export type SimpleCandidType =
| 'Principal'
| 'text'
| 'Void';

export function simpleCandidTypeToTsType(type: SimpleCandidType): string {
switch (type) {
case 'Null':
return 'null';
case 'Principal':
return 'Principal';
case 'Void':
return 'void';
case 'blob':
return 'number[]';
case 'bool':
return 'boolean';
case 'float32':
case 'float64':
case 'int8':
case 'int16':
case 'int32':
case 'nat16':
case 'nat32':
case 'nat8':
return 'number';
case 'int':
case 'int64':
case 'nat':
case 'nat64':
return 'bigint';
case 'text':
return 'string';
}
}
Loading
Loading