Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Dec 1, 2023
1 parent 9ca37ae commit b78ca38
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 119 deletions.
88 changes: 1 addition & 87 deletions property_tests/arbitraries/candid/candid_meta_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ export type CandidMeta = {
candidType: CandidType;
};

export type CandidValues<T extends CorrespondingJSType, E = T> = {
agentArgumentValue: T;
agentResponseValue: E;
valueLiteral: string;
};

export type CandidDefinition =
| MultiTypeConstructedDefinition
| SingleTypeConstructedDefinition
Expand Down Expand Up @@ -72,7 +66,7 @@ export type VecCandidDefinition = SingleTypeConstructedDefinition;
export type RecordCandidDefinition = MultiTypeConstructedDefinition;
export type VariantCandidDefinition = MultiTypeConstructedDefinition;
export type TupleCandidDefinition = UnnamedMultiTypeConstructedDefinition;
export type BlobCandidDefinition = VecCandidDefinition;
export type BlobCandidDefinition = PrimitiveDefinition;

// Primitives
export type FloatCandidDefinition = PrimitiveDefinition;
Expand Down Expand Up @@ -101,83 +95,3 @@ export type ServiceMethodDefinition = {
typeAliasDeclarations: string[];
src: string;
};

export function CandidValueArb(
candidTypeMeta: CandidDefinition
): fc.Arbitrary<CandidValues<CorrespondingJSType>> {
const candidType = candidTypeMeta.candidMeta.candidType;
if (candidType === CandidType.Record) {
return RecordValueArb(candidTypeMeta as RecordCandidDefinition);
}
if (candidType === CandidType.Variant) {
return VariantValueArb(candidTypeMeta as VariantCandidDefinition);
}
if (candidType === CandidType.Tuple) {
return TupleValueArb(candidTypeMeta as TupleCandidDefinition);
}
if (candidType === CandidType.Opt) {
return OptValueArb(candidTypeMeta as OptCandidDefinition);
}
if (candidType === CandidType.Bool) {
return BoolValueArb;
}
if (candidType === CandidType.Vec) {
return VecValueArb(candidTypeMeta as VecCandidDefinition);
}
if (candidType === CandidType.Func) {
return FuncValueArb;
}
if (candidType === CandidType.Text) {
return TextValueArb;
}
if (candidType === CandidType.Null) {
return NullValueArb;
}
if (candidType === CandidType.Float32) {
return Float32ValueArb;
}
if (candidType === CandidType.Float64) {
return Float64ValueArb;
}
if (candidType === CandidType.Int) {
return IntValueArb;
}
if (candidType === CandidType.Int8) {
return Int8ValueArb;
}
if (candidType === CandidType.Int16) {
return Int16ValueArb;
}
if (candidType === CandidType.Int32) {
return Int32ValueArb;
}
if (candidType === CandidType.Int64) {
return Int64ValueArb;
}
if (candidType === CandidType.Nat) {
return NatValueArb;
}
if (candidType === CandidType.Nat8) {
return Nat8ValueArb;
}
if (candidType === CandidType.Nat16) {
return Nat16ValueArb;
}
if (candidType === CandidType.Nat32) {
return Nat32ValueArb;
}
if (candidType === CandidType.Nat64) {
return Nat64ValueArb;
}
if (candidType === CandidType.Principal) {
return PrincipalValueArb;
}
if (candidType === CandidType.Void) {
return VoidValueArb;
}
if (candidType === CandidType.Service) {
return ServiceValueArb(candidTypeMeta as ServiceCandidDefinition);
}
// etc
throw 'Type cannot be converted to CandidValue yet';
}
126 changes: 126 additions & 0 deletions property_tests/arbitraries/candid/candid_values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import fc from 'fast-check';
import { CorrespondingJSType } from './candid_type_arb';
import { RecordValueArb } from './constructed/record_arb/base';
import { BoolValueArb } from './primitive/bool';
import { VecValueArb } from './constructed/vec_arb/base';
import { CandidType } from './candid_type';
import { TextValueArb } from './primitive/text';
import { NullValueArb } from './primitive/null';
import { Float32ValueArb } from './primitive/floats/float32_arb';
import { Float64ValueArb } from './primitive/floats/float64_arb';
import { IntValueArb } from './primitive/ints/int_arb';
import { Int8ValueArb } from './primitive/ints/int8_arb';
import { Int16ValueArb } from './primitive/ints/int16_arb';
import { Int32ValueArb } from './primitive/ints/int32_arb';
import { Int64ValueArb } from './primitive/ints/int64_arb';
import { NatValueArb } from './primitive/nats/nat_arb';
import { Nat8ValueArb } from './primitive/nats/nat8_arb';
import { Nat16ValueArb } from './primitive/nats/nat16_arb';
import { Nat32ValueArb } from './primitive/nats/nat32_arb';
import { Nat64ValueArb } from './primitive/nats/nat64_arb';
import { VariantValueArb } from './constructed/variant_arb/base';
import { TupleValueArb } from './constructed/tuple_arb/base';
import { OptValueArb } from './constructed/opt_arb/base';
import { PrincipalValueArb } from './reference/principal_arb';
import { FuncValueArb } from './reference/func_arb/base';
import { VoidValueArb } from './primitive/void';
import { ServiceValueArb } from './reference/service_arb/base';
import {
CandidDefinition,
OptCandidDefinition,
RecordCandidDefinition,
ServiceCandidDefinition,
TupleCandidDefinition,
VariantCandidDefinition,
VecCandidDefinition
} from './candid_meta_arb';
import { BlobValueArb } from './constructed/blob_arb';

export type CandidValues<T extends CorrespondingJSType, E = T> = {
agentArgumentValue: T;
agentResponseValue: E;
valueLiteral: string;
};

export function CandidValueArb(
candidTypeMeta: CandidDefinition
): fc.Arbitrary<CandidValues<CorrespondingJSType>> {
const candidType = candidTypeMeta.candidMeta.candidType;
if (candidType === CandidType.Blob) {
return BlobValueArb;
}
if (candidType === CandidType.Record) {
return RecordValueArb(candidTypeMeta as RecordCandidDefinition);
}
if (candidType === CandidType.Variant) {
return VariantValueArb(candidTypeMeta as VariantCandidDefinition);
}
if (candidType === CandidType.Tuple) {
return TupleValueArb(candidTypeMeta as TupleCandidDefinition);
}
if (candidType === CandidType.Opt) {
return OptValueArb(candidTypeMeta as OptCandidDefinition);
}
if (candidType === CandidType.Bool) {
return BoolValueArb;
}
if (candidType === CandidType.Vec) {
return VecValueArb(candidTypeMeta as VecCandidDefinition);
}
if (candidType === CandidType.Func) {
return FuncValueArb;
}
if (candidType === CandidType.Text) {
return TextValueArb;
}
if (candidType === CandidType.Null) {
return NullValueArb;
}
if (candidType === CandidType.Float32) {
return Float32ValueArb;
}
if (candidType === CandidType.Float64) {
return Float64ValueArb;
}
if (candidType === CandidType.Int) {
return IntValueArb;
}
if (candidType === CandidType.Int8) {
return Int8ValueArb;
}
if (candidType === CandidType.Int16) {
return Int16ValueArb;
}
if (candidType === CandidType.Int32) {
return Int32ValueArb;
}
if (candidType === CandidType.Int64) {
return Int64ValueArb;
}
if (candidType === CandidType.Nat) {
return NatValueArb;
}
if (candidType === CandidType.Nat8) {
return Nat8ValueArb;
}
if (candidType === CandidType.Nat16) {
return Nat16ValueArb;
}
if (candidType === CandidType.Nat32) {
return Nat32ValueArb;
}
if (candidType === CandidType.Nat64) {
return Nat64ValueArb;
}
if (candidType === CandidType.Principal) {
return PrincipalValueArb;
}
if (candidType === CandidType.Void) {
return VoidValueArb;
}
if (candidType === CandidType.Service) {
return ServiceValueArb(candidTypeMeta as ServiceCandidDefinition);
}
// etc
throw 'Type cannot be converted to CandidValue yet';
}
60 changes: 53 additions & 7 deletions property_tests/arbitraries/candid/constructed/blob_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,64 @@ import fc from 'fast-check';
import { SimpleCandidValueAndMetaArb } from '../simple_type_arbs/value_and_meta_arb';
import { blobToSrcLiteral } from '../to_src_literal/blob';
import { CandidType } from '../candid_type';
import { OldSimpleCandidValueAndMetaArb } from '../simple_type_arbs/old_value_and_meta_arb';
import { BlobCandidDefinition } from '../candid_meta_arb';
import { SimpleCandidShapeArb } from '../simple_type_arbs/shape_arb';
import { CandidValues } from '../candid_values';
import { SimpleCandidValueArb } from '../simple_type_arbs/value_arb';
import { UniqueIdentifierArb } from '../../unique_identifier_arb';
import { CandidValueAndMeta } from '../candid_value_and_meta';

const VecNat8DefinitionArb: fc.Arbitrary<BlobCandidDefinition> = fc
.tuple(UniqueIdentifierArb('typeDeclaration'), fc.boolean())
.map(([name, useTypeDeclaration]): BlobCandidDefinition => {
const typeAnnotation = 'Vec(nat8)';
const typeAliasDeclarations = useTypeDeclaration
? [`const ${name} = ${typeAnnotation}`]
: [];
const imports = new Set(['Vec', 'nat8']);
return {
candidMeta: {
typeAnnotation,
typeAliasDeclarations,
imports,
candidType: CandidType.Blob
}
};
});

export const BlobValueArb: fc.Arbitrary<CandidValues<Uint8Array>> =
SimpleCandidValueArb(fc.uint8Array(), blobToSrcLiteral);

const VecNat8CandidValueAndMetaArb: fc.Arbitrary<
CandidValueAndMeta<Uint8Array>
> = fc.tuple(VecNat8DefinitionArb, BlobValueArb).map(
([
{
candidMeta: { typeAnnotation, typeAliasDeclarations, imports }
},
{ agentArgumentValue, agentResponseValue, valueLiteral }
]): CandidValueAndMeta<Uint8Array> => ({
src: {
typeAnnotation,
imports,
typeAliasDeclarations,
valueLiteral
},
agentArgumentValue,
agentResponseValue
})
);

export const BlobArb = fc.oneof(
OldSimpleCandidValueAndMetaArb(
fc.uint8Array(),
'Vec(nat8)',
new Set(['Vec', 'nat8']),
blobToSrcLiteral
),
VecNat8CandidValueAndMetaArb,
SimpleCandidValueAndMetaArb(
fc.uint8Array(),
CandidType.Blob,
blobToSrcLiteral
)
);

export const BlobDefinitionArb: fc.Arbitrary<BlobCandidDefinition> = fc.oneof(
SimpleCandidShapeArb(CandidType.Blob),
VecNat8DefinitionArb
);
3 changes: 2 additions & 1 deletion property_tests/arbitraries/candid/primitive/bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { SimpleCandidValueAndMetaArb } from '../simple_type_arbs/value_and_meta_
import { SimpleCandidShapeArb } from '../simple_type_arbs/shape_arb';
import { SimpleCandidValueArb } from '../simple_type_arbs/value_arb';
import { booleanToSrcLiteral } from '../to_src_literal/boolean';
import { BoolCandidDefinition, CandidValues } from '../candid_meta_arb';
import { BoolCandidDefinition } from '../candid_meta_arb';
import { CandidType } from '../candid_type';
import { CandidValues } from '../candid_values';

export const BoolArb = SimpleCandidValueAndMetaArb(
fc.boolean(),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '../candid_type';
import { UniqueIdentifierArb } from '../../unique_identifier_arb';

export function SimpleCandidShapeArb(
export function SimpleCandidShapeArb( // TODO rename this to definition
candidType: CandidType
): fc.Arbitrary<PrimitiveDefinition> {
return fc
Expand Down

0 comments on commit b78ca38

Please sign in to comment.