diff --git a/property_tests/arbitraries/candid/candid_arb.ts b/property_tests/arbitraries/candid/candid_arb.ts deleted file mode 100644 index ebb3303cb6..0000000000 --- a/property_tests/arbitraries/candid/candid_arb.ts +++ /dev/null @@ -1,34 +0,0 @@ -import fc from 'fast-check'; -import { CandidType } from './candid_type_arb'; - -// TODO we're thinking that Candid is not the best name for this. What is better? -export type CandidMeta = { - agentArgumentValue: T; - agentResponseValue: E; - src: Src; -}; - -export type Src = { - candidType: string; - typeDeclaration?: string; - imports: Set; - valueLiteral: string; -}; - -export const CandidMetaArb = ( - arb: fc.Arbitrary, - candidType: string, - toLiteral: (value: T) => string -) => { - return arb.map( - (agentArgumentValue): CandidMeta => ({ - src: { - candidType, - imports: new Set([candidType]), - valueLiteral: toLiteral(agentArgumentValue) - }, - agentArgumentValue, - agentResponseValue: agentArgumentValue - }) - ); -}; diff --git a/property_tests/arbitraries/candid/candid_definition_arb/index.ts b/property_tests/arbitraries/candid/candid_definition_arb/index.ts new file mode 100644 index 0000000000..0ac60d9217 --- /dev/null +++ b/property_tests/arbitraries/candid/candid_definition_arb/index.ts @@ -0,0 +1,84 @@ +import fc from 'fast-check'; +import { BoolDefinitionArb } from '../primitive/bool'; +import { Float32DefinitionArb } from '../primitive/floats/float32_arb'; +import { Float64DefinitionArb } from '../primitive/floats/float64_arb'; +import { FuncDefinitionArb } from '../reference/func_arb/definition_arb'; +import { Int16DefinitionArb } from '../primitive/ints/int16_arb'; +import { Int32DefinitionArb } from '../primitive/ints/int32_arb'; +import { Int64DefinitionArb } from '../primitive/ints/int64_arb'; +import { Int8DefinitionArb } from '../primitive/ints/int8_arb'; +import { IntDefinitionArb } from '../primitive/ints/int_arb'; +import { Nat16DefinitionArb } from '../primitive/nats/nat16_arb'; +import { Nat32DefinitionArb } from '../primitive/nats/nat32_arb'; +import { Nat64DefinitionArb } from '../primitive/nats/nat64_arb'; +import { Nat8DefinitionArb } from '../primitive/nats/nat8_arb'; +import { NatDefinitionArb } from '../primitive/nats/nat_arb'; +import { OptDefinitionArb } from '../constructed/opt_arb/definition_arb'; +import { PrincipalDefinitionArb } from '../reference/principal_arb'; +import { RecordDefinitionArb } from '../constructed/record_arb/definition_arb'; +import { TextDefinitionArb } from '../primitive/text'; +import { TupleDefinitionArb } from '../constructed/tuple_arb/definition_arb'; +import { VariantDefinitionArb } from '../constructed/variant_arb/definition_arbs'; +import { VecDefinitionArb } from '../constructed/vec_arb/definition_arb'; +import { + CandidDefinition, + FuncCandidDefinition, + OptCandidDefinition, + RecordCandidDefinition, + TupleCandidDefinition, + VariantCandidDefinition, + VecCandidDefinition +} from './types'; +import { BlobDefinitionArb } from '../constructed/blob_arb/definition_arb'; + +export const CandidDefinitionArb: fc.Arbitrary = fc.letrec( + (tie) => ({ + CandidDefinition: fc.oneof( + BlobDefinitionArb(), + tie('Opt').map((sample) => sample as OptCandidDefinition), + tie('Record').map((sample) => sample as RecordCandidDefinition), + tie('Tuple').map((sample) => sample as TupleCandidDefinition), + tie('Variant').map((sample) => sample as VariantCandidDefinition), + tie('Vec').map((sample) => sample as VecCandidDefinition), + BoolDefinitionArb(), + Float32DefinitionArb(), + Float64DefinitionArb(), + IntDefinitionArb(), + Int8DefinitionArb(), + Int16DefinitionArb(), + Int32DefinitionArb(), + Int64DefinitionArb(), + NatDefinitionArb(), + Nat8DefinitionArb(), + Nat16DefinitionArb(), + Nat32DefinitionArb(), + Nat64DefinitionArb(), + // NullDefinitionArb(), // Must be excluded until https://github.com/demergent-labs/azle/issues/1453 gets resolved + TextDefinitionArb(), + tie('Func').map((sample) => sample as FuncCandidDefinition), + PrincipalDefinitionArb() + // tie('Service').map((sample) => sample as ServiceCandidDefinition) // Services Aren't working with deep equals + ), + Func: FuncDefinitionArb( + tie('CandidDefinition') as fc.Arbitrary + ), + Opt: OptDefinitionArb( + tie('CandidDefinition') as fc.Arbitrary + ), + Record: RecordDefinitionArb( + tie('CandidDefinition') as fc.Arbitrary + ), + // Service: ServiceDefinitionArb( + // tie('CandidDefinition') as fc.Arbitrary + // ), + Tuple: TupleDefinitionArb( + tie('CandidDefinition') as fc.Arbitrary + ), + Variant: VariantDefinitionArb( + tie('CandidDefinition') as fc.Arbitrary + ), + Vec: VecDefinitionArb( + tie('CandidDefinition') as fc.Arbitrary + ) + }) +).CandidDefinition; diff --git a/property_tests/arbitraries/candid/candid_definition_arb/types.ts b/property_tests/arbitraries/candid/candid_definition_arb/types.ts new file mode 100644 index 0000000000..89a9b732fb --- /dev/null +++ b/property_tests/arbitraries/candid/candid_definition_arb/types.ts @@ -0,0 +1,71 @@ +import { CandidType } from '../candid_type'; + +export type CandidDefinition = + | MultiTypeConstructedDefinition + | SingleTypeConstructedDefinition + | PrimitiveDefinition + | UnnamedMultiTypeConstructedDefinition + | FuncCandidDefinition + | ServiceCandidDefinition; + +export type MultiTypeConstructedDefinition = { + candidMeta: CandidMeta; + innerTypes: [string, CandidDefinition][]; +}; + +export type UnnamedMultiTypeConstructedDefinition = { + candidMeta: CandidMeta; + innerTypes: CandidDefinition[]; +}; + +export type SingleTypeConstructedDefinition = { + candidMeta: CandidMeta; + innerType: CandidDefinition; +}; + +export type PrimitiveDefinition = { + candidMeta: CandidMeta; +}; + +// Constructed +export type OptCandidDefinition = SingleTypeConstructedDefinition; +export type VecCandidDefinition = SingleTypeConstructedDefinition; +export type RecordCandidDefinition = MultiTypeConstructedDefinition; +export type VariantCandidDefinition = MultiTypeConstructedDefinition; +export type TupleCandidDefinition = UnnamedMultiTypeConstructedDefinition; +export type BlobCandidDefinition = PrimitiveDefinition; + +// Primitives +export type FloatCandidDefinition = PrimitiveDefinition; +export type IntCandidDefinition = PrimitiveDefinition; +export type NatCandidDefinition = PrimitiveDefinition; +export type BoolCandidDefinition = PrimitiveDefinition; +export type NullCandidDefinition = PrimitiveDefinition; +export type TextCandidDefinition = PrimitiveDefinition; +export type VoidCandidDefinition = PrimitiveDefinition; + +// Reference +export type FuncCandidDefinition = { + candidMeta: CandidMeta; + paramCandidMeta: CandidDefinition[]; + returnCandidMeta: CandidDefinition; +}; +export type PrincipalCandidDefinition = PrimitiveDefinition; +export type ServiceCandidDefinition = { + name: string; + candidMeta: CandidMeta; + funcs: ServiceMethodDefinition[]; +}; +export type ServiceMethodDefinition = { + name: string; + imports: Set; + typeAliasDeclarations: string[]; + src: string; +}; + +type CandidMeta = { + candidTypeObject: string; // Either a type reference or type literal + typeAliasDeclarations: string[]; + imports: Set; + candidType: CandidType; +}; diff --git a/property_tests/arbitraries/candid/candid_return_type_arb.ts b/property_tests/arbitraries/candid/candid_return_type_arb.ts index 44182006ef..109abe4840 100644 --- a/property_tests/arbitraries/candid/candid_return_type_arb.ts +++ b/property_tests/arbitraries/candid/candid_return_type_arb.ts @@ -1,12 +1,19 @@ import fc from 'fast-check'; -import { CandidMeta } from './candid_arb'; -import { CandidType, CandidTypeArb } from './candid_type_arb'; +import { + CandidValueAndMeta, + CandidValueAndMetaArb +} from './candid_value_and_meta_arb'; import { VoidArb } from './primitive/void'; +import { CorrespondingJSType } from './corresponding_js_type'; -export type CandidReturnType = CandidType | undefined; +export type CandidReturnType = CorrespondingJSType | undefined; -export const CandidReturnTypeArb = fc.oneof( - { arbitrary: CandidTypeArb, weight: 17 }, - { arbitrary: VoidArb, weight: 1 } -) as fc.Arbitrary>; +export function CandidReturnTypeArb(): fc.Arbitrary< + CandidValueAndMeta +> { + return fc.oneof( + { arbitrary: CandidValueAndMetaArb(), weight: 17 }, + { arbitrary: VoidArb(), weight: 1 } + ); +} diff --git a/property_tests/arbitraries/candid/candid_type.ts b/property_tests/arbitraries/candid/candid_type.ts new file mode 100644 index 0000000000..cb8b48a836 --- /dev/null +++ b/property_tests/arbitraries/candid/candid_type.ts @@ -0,0 +1,30 @@ +export type CandidType = ComplexCandidType | SimpleCandidType; + +export type ComplexCandidType = + | 'Func' + | 'Opt' + | 'Record' + | 'Service' + | 'Tuple' + | 'Variant' + | 'Vec'; + +export type SimpleCandidType = + | 'blob' + | 'bool' + | 'float32' + | 'float64' + | 'int' + | 'int16' + | 'int32' + | 'int64' + | 'int8' + | 'nat' + | 'nat16' + | 'nat32' + | 'nat64' + | 'nat8' + | 'Null' + | 'Principal' + | 'text' + | 'Void'; diff --git a/property_tests/arbitraries/candid/candid_type_arb.ts b/property_tests/arbitraries/candid/candid_type_arb.ts deleted file mode 100644 index 26b09856bd..0000000000 --- a/property_tests/arbitraries/candid/candid_type_arb.ts +++ /dev/null @@ -1,106 +0,0 @@ -import fc from 'fast-check'; -import { IntArb } from './primitive/ints/int_arb'; -import { Int8Arb } from './primitive/ints/int8_arb'; -import { Int16Arb } from './primitive/ints/int16_arb'; -import { Int32Arb } from './primitive/ints/int32_arb'; -import { Int64Arb } from './primitive/ints/int64_arb'; -import { NatArb } from './primitive/nats/nat_arb'; -import { Nat8Arb } from './primitive/nats/nat8_arb'; -import { Nat16Arb } from './primitive/nats/nat16_arb'; -import { Nat32Arb } from './primitive/nats/nat32_arb'; -import { Nat64Arb } from './primitive/nats/nat64_arb'; -import { NullArb } from './primitive/null'; -import { BoolArb } from './primitive/bool'; -import { Principal } from '@dfinity/principal'; -import { PrincipalArb } from './reference/principal_arb'; -import { Float32Arb } from './primitive/floats/float32_arb'; -import { Float64Arb } from './primitive/floats/float64_arb'; -import { TextArb } from './primitive/text'; -import { BlobArb } from './constructed/blob_arb'; -import { CandidMeta } from './candid_arb'; -import { Func } from './reference/func_arb'; -import { Opt } from './constructed/opt_arb'; -import { Variant } from './constructed/variant_arb'; -import { BaseVariantArb } from './constructed/variant_arb/base'; -import { Record } from './constructed/record_arb'; -import { Tuple } from './constructed/tuple_arb'; -import { RecordArb } from './constructed/record_arb/base'; -import { TupleArb } from './constructed/tuple_arb/base'; -import { OptArb } from './constructed/opt_arb/base'; -import { Vec, VecArb } from './constructed/vec_arb'; -import { FuncArb } from './reference/func_arb/base'; - -export type CandidType = - | number - | bigint - | null - | boolean - | Principal - | Uint8Array - | string - | Func - | Opt - | Variant - | Record - | Tuple - | undefined - | Int16Array - | Int32Array - | Int8Array - | BigInt64Array - | Uint16Array - | Uint32Array - | Uint8Array - | BigUint64Array - | Vec; - -/** - * An arbitrary representing all possible Candid types. - * - * **Note:** This currently only supports ints, nats, and null arbitraries - */ -export const CandidTypeArb: fc.Arbitrary> = fc.letrec( - (tie) => ({ - CandidType: fc.oneof( - BlobArb, - tie('Opt').map((sample) => sample as CandidMeta), - tie('Record').map((sample) => sample as CandidMeta), - tie('Tuple').map((sample) => sample as CandidMeta), - tie('Variant').map((sample) => sample as CandidMeta), - tie('Vec').map((sample) => sample as CandidMeta), - Float32Arb, - Float64Arb, - IntArb, - Int8Arb, - Int16Arb, - Int32Arb, - Int64Arb, - NatArb, - Nat8Arb, - Nat16Arb, - Nat32Arb, - Nat64Arb, - BoolArb, - NullArb, - TextArb, - tie('Func').map((sample) => sample as CandidMeta), - PrincipalArb - ), - Func: FuncArb( - tie('CandidType') as fc.Arbitrary> - ), - Vec: VecArb, - Opt: OptArb(tie('CandidType') as fc.Arbitrary>), - Variant: BaseVariantArb( - tie('CandidType') as fc.Arbitrary> - ), - Tuple: TupleArb( - tie('CandidType') as fc.Arbitrary> - ), - Record: RecordArb( - tie('CandidType') as fc.Arbitrary> - ) - }) -).CandidType; - -// TODO: This needs to support service. diff --git a/property_tests/arbitraries/candid/candid_value_and_meta_arb.ts b/property_tests/arbitraries/candid/candid_value_and_meta_arb.ts new file mode 100644 index 0000000000..ecde5a0ac5 --- /dev/null +++ b/property_tests/arbitraries/candid/candid_value_and_meta_arb.ts @@ -0,0 +1,72 @@ +import fc from 'fast-check'; +import { IntArb } from './primitive/ints/int_arb'; +import { Int8Arb } from './primitive/ints/int8_arb'; +import { Int16Arb } from './primitive/ints/int16_arb'; +import { Int32Arb } from './primitive/ints/int32_arb'; +import { Int64Arb } from './primitive/ints/int64_arb'; +import { NatArb } from './primitive/nats/nat_arb'; +import { Nat8Arb } from './primitive/nats/nat8_arb'; +import { Nat16Arb } from './primitive/nats/nat16_arb'; +import { Nat32Arb } from './primitive/nats/nat32_arb'; +import { Nat64Arb } from './primitive/nats/nat64_arb'; +import { NullArb } from './primitive/null'; +import { BoolArb } from './primitive/bool'; +import { PrincipalArb } from './reference/principal_arb'; +import { Float32Arb } from './primitive/floats/float32_arb'; +import { Float64Arb } from './primitive/floats/float64_arb'; +import { TextArb } from './primitive/text'; +import { BlobArb } from './constructed/blob_arb'; +import { VariantArb } from './constructed/variant_arb'; +import { RecordArb } from './constructed/record_arb'; +import { TupleArb } from './constructed/tuple_arb'; +import { OptArb } from './constructed/opt_arb'; +import { VecArb } from './constructed/vec_arb'; +import { FuncArb } from './reference/func_arb'; +import { CorrespondingJSType } from './corresponding_js_type'; + +// TODO we're thinking that Candid is not the best name for this. What is better? +export type CandidValueAndMeta = { + agentArgumentValue: T; + agentResponseValue: E; + src: { + candidTypeObject: string; + typeAliasDeclarations: string[]; + imports: Set; + valueLiteral: string; + }; +}; + +/** + * An arbitrary representing all possible Candid types. + */ +export function CandidValueAndMetaArb(): fc.Arbitrary< + CandidValueAndMeta +> { + return fc.oneof( + BlobArb(), + OptArb(), + RecordArb(), + TupleArb(), + VariantArb(), + VecArb(), + Float32Arb(), + Float64Arb(), + IntArb(), + Int8Arb(), + Int16Arb(), + Int32Arb(), + Int64Arb(), + NatArb(), + Nat8Arb(), + Nat16Arb(), + Nat32Arb(), + Nat64Arb(), + BoolArb(), + NullArb(), + TextArb(), + FuncArb(), + PrincipalArb() + ); +} + +// TODO: This needs to support service. diff --git a/property_tests/arbitraries/candid/candid_value_and_meta_arb_generator.ts b/property_tests/arbitraries/candid/candid_value_and_meta_arb_generator.ts new file mode 100644 index 0000000000..cf050e924b --- /dev/null +++ b/property_tests/arbitraries/candid/candid_value_and_meta_arb_generator.ts @@ -0,0 +1,36 @@ +import fc from 'fast-check'; +import { CorrespondingJSType } from './corresponding_js_type'; +import { CandidDefinition } from './candid_definition_arb/types'; +import { CandidValueAndMeta } from './candid_value_and_meta_arb'; +import { CandidValues } from './candid_values_arb'; + +export function CandidValueAndMetaArbGenerator< + T extends CorrespondingJSType, + D extends CandidDefinition, + V extends CandidValues +>( + DefinitionArb: fc.Arbitrary, + ValueArb: (arb: D) => fc.Arbitrary +): fc.Arbitrary> { + return DefinitionArb.chain((candidDefinition) => + fc.tuple(fc.constant(candidDefinition), ValueArb(candidDefinition)) + ).map( + ([ + { + candidMeta: { candidTypeObject, typeAliasDeclarations, imports } + }, + { agentArgumentValue, agentResponseValue, valueLiteral } + ]) => { + return { + src: { + candidTypeObject, + typeAliasDeclarations, + imports, + valueLiteral + }, + agentArgumentValue, + agentResponseValue + }; + } + ); +} diff --git a/property_tests/arbitraries/candid/candid_values_arb.ts b/property_tests/arbitraries/candid/candid_values_arb.ts new file mode 100644 index 0000000000..d4ba790994 --- /dev/null +++ b/property_tests/arbitraries/candid/candid_values_arb.ts @@ -0,0 +1,124 @@ +import fc from 'fast-check'; +import { RecordValuesArb } from './constructed/record_arb/values_arb'; +import { BoolValueArb } from './primitive/bool'; +import { VecValuesArb } from './constructed/vec_arb/values_arb'; +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 { VariantValuesArb } from './constructed/variant_arb/values_arb'; +import { TupleValuesArb } from './constructed/tuple_arb/values_arbs'; +import { OptValuesArb } from './constructed/opt_arb/values_arb'; +import { PrincipalValueArb } from './reference/principal_arb'; +import { FuncValueArb } from './reference/func_arb/values_arb'; +import { VoidValueArb } from './primitive/void'; +import { ServiceValueArb } from './reference/service_arb/values_arb'; +import { + CandidDefinition, + OptCandidDefinition, + RecordCandidDefinition, + ServiceCandidDefinition, + TupleCandidDefinition, + VariantCandidDefinition, + VecCandidDefinition +} from './candid_definition_arb/types'; +import { BlobValuesArb } from './constructed/blob_arb/values_arb'; +import { CorrespondingJSType } from './corresponding_js_type'; + +export type CandidValues = { + agentArgumentValue: T; + agentResponseValue: E; + valueLiteral: string; +}; + +export function CandidValueArb( + candidTypeMeta: CandidDefinition +): fc.Arbitrary> { + const candidType = candidTypeMeta.candidMeta.candidType; + if (candidType === 'blob') { + return BlobValuesArb(); + } + if (candidType === 'Opt') { + return OptValuesArb(candidTypeMeta as OptCandidDefinition); + } + if (candidType === 'Record') { + return RecordValuesArb(candidTypeMeta as RecordCandidDefinition); + } + if (candidType === 'Tuple') { + return TupleValuesArb(candidTypeMeta as TupleCandidDefinition); + } + if (candidType === 'Variant') { + return VariantValuesArb(candidTypeMeta as VariantCandidDefinition); + } + if (candidType === 'Vec') { + return VecValuesArb(candidTypeMeta as VecCandidDefinition); + } + if (candidType === 'bool') { + return BoolValueArb(); + } + if (candidType === 'float32') { + return Float32ValueArb(); + } + if (candidType === 'float64') { + return Float64ValueArb(); + } + if (candidType === 'int') { + return IntValueArb(); + } + if (candidType === 'int8') { + return Int8ValueArb(); + } + if (candidType === 'int16') { + return Int16ValueArb(); + } + if (candidType === 'int32') { + return Int32ValueArb(); + } + if (candidType === 'int64') { + return Int64ValueArb(); + } + if (candidType === 'nat') { + return NatValueArb(); + } + if (candidType === 'nat8') { + return Nat8ValueArb(); + } + if (candidType === 'nat16') { + return Nat16ValueArb(); + } + if (candidType === 'nat32') { + return Nat32ValueArb(); + } + if (candidType === 'nat64') { + return Nat64ValueArb(); + } + if (candidType === 'Null') { + return NullValueArb(); + } + if (candidType === 'text') { + return TextValueArb(); + } + if (candidType === 'Void') { + return VoidValueArb(); + } + if (candidType === 'Func') { + return FuncValueArb(); + } + if (candidType === 'Principal') { + return PrincipalValueArb(); + } + if (candidType === 'Service') { + return ServiceValueArb(candidTypeMeta as ServiceCandidDefinition); + } + throw new Error('Unreachable'); +} diff --git a/property_tests/arbitraries/candid/constructed/blob_arb.ts b/property_tests/arbitraries/candid/constructed/blob_arb.ts deleted file mode 100644 index bee4bcd2d8..0000000000 --- a/property_tests/arbitraries/candid/constructed/blob_arb.ts +++ /dev/null @@ -1,15 +0,0 @@ -import fc from 'fast-check'; -import { CandidMeta, CandidMetaArb } from '../candid_arb'; -import { blobToSrcLiteral } from '../to_src_literal/blob'; - -export const BlobArb = fc - .oneof( - CandidMetaArb(fc.uint8Array(), 'Vec(nat8)', blobToSrcLiteral), - CandidMetaArb(fc.uint8Array(), 'blob', blobToSrcLiteral) - ) - .map( - (sample): CandidMeta => ({ - ...sample, - src: { ...sample.src, imports: new Set(['blob', 'nat8', 'Vec']) } - }) - ); diff --git a/property_tests/arbitraries/candid/constructed/blob_arb/definition_arb.ts b/property_tests/arbitraries/candid/constructed/blob_arb/definition_arb.ts new file mode 100644 index 0000000000..6259e59fb1 --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/blob_arb/definition_arb.ts @@ -0,0 +1,28 @@ +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'; + +export function BlobDefinitionArb(): fc.Arbitrary { + return fc.oneof(SimpleCandidDefinitionArb('blob'), _VecNat8DefinitionArb()); +} + +export function _VecNat8DefinitionArb(): fc.Arbitrary { + return 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: { + candidTypeObject: typeAnnotation, + typeAliasDeclarations, + imports, + candidType: 'blob' + } + }; + }); +} diff --git a/property_tests/arbitraries/candid/constructed/blob_arb/index.ts b/property_tests/arbitraries/candid/constructed/blob_arb/index.ts new file mode 100644 index 0000000000..e8dc86a93f --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/blob_arb/index.ts @@ -0,0 +1,12 @@ +import fc from 'fast-check'; +import { BlobValuesArb } from './values_arb'; +import { BlobDefinitionArb, _VecNat8DefinitionArb } from './definition_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; + +export function BlobArb(): fc.Arbitrary> { + return fc.oneof( + CandidValueAndMetaArbGenerator(_VecNat8DefinitionArb(), BlobValuesArb), + CandidValueAndMetaArbGenerator(BlobDefinitionArb(), BlobValuesArb) + ); +} diff --git a/property_tests/arbitraries/candid/constructed/blob_arb/values_arb.ts b/property_tests/arbitraries/candid/constructed/blob_arb/values_arb.ts new file mode 100644 index 0000000000..9cd184272e --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/blob_arb/values_arb.ts @@ -0,0 +1,9 @@ +import fc from 'fast-check'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { blobToSrcLiteral } from '../../to_src_literal/blob'; +import { CandidValues } from '../../candid_values_arb'; +import { _VecNat8DefinitionArb } from './definition_arb'; + +export function BlobValuesArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(fc.uint8Array(), blobToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/constructed/opt_arb/base.ts b/property_tests/arbitraries/candid/constructed/opt_arb/base.ts deleted file mode 100644 index 479aea4691..0000000000 --- a/property_tests/arbitraries/candid/constructed/opt_arb/base.ts +++ /dev/null @@ -1,89 +0,0 @@ -import fc from 'fast-check'; -import { CandidType } from '../../candid_type_arb'; -import { CandidMeta } from '../../candid_arb'; -import { Opt } from './index'; -import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; - -type SomeOrNone = 'Some' | 'None'; - -export function OptArb( - candidTypeArb: fc.Arbitrary> -): fc.Arbitrary> { - return fc - .tuple( - UniqueIdentifierArb('typeDeclaration'), - fc.constantFrom('Some', 'None') as fc.Arbitrary, - candidTypeArb, - fc.boolean() - ) - .map(([name, someOrNone, innerType, useTypeDeclaration]) => { - const candidType = useTypeDeclaration - ? name - : generateCandidType(innerType); - - const typeDeclaration = generateTypeDeclaration( - name, - innerType, - useTypeDeclaration - ); - - return { - src: { - candidType, - imports: generateImports(innerType), - typeDeclaration, - valueLiteral: generateValueLiteral(someOrNone, innerType) - }, - agentArgumentValue: generateValue(someOrNone, innerType), - agentResponseValue: generateValue(someOrNone, innerType, true) - }; - }); -} - -function generateTypeDeclaration( - name: string, - innerType: CandidMeta, - useTypeDeclaration: boolean -) { - if (useTypeDeclaration) { - return `${ - innerType.src.typeDeclaration ?? '' - }\nconst ${name} = ${generateCandidType(innerType)}`; - } - return innerType.src.typeDeclaration; -} - -function generateCandidType(innerType: CandidMeta): string { - return `Opt(${innerType.src.candidType})`; -} - -function generateImports(innerType: CandidMeta): Set { - return new Set([...innerType.src.imports, 'Opt', 'Some', 'None']); -} - -function generateValue( - someOrNone: SomeOrNone, - innerType: CandidMeta, - useAgentResponseValue: boolean = false -): Opt { - if (someOrNone === 'Some') { - return [ - useAgentResponseValue - ? innerType.agentResponseValue - : innerType.agentArgumentValue - ]; - } else { - return []; - } -} - -function generateValueLiteral( - someOrNone: SomeOrNone, - innerType: CandidMeta -): string { - if (someOrNone === 'Some') { - return `Some(${innerType.src.valueLiteral})`; - } else { - return `None`; - } -} diff --git a/property_tests/arbitraries/candid/constructed/opt_arb/definition_arb.ts b/property_tests/arbitraries/candid/constructed/opt_arb/definition_arb.ts new file mode 100644 index 0000000000..8a09235fcf --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/opt_arb/definition_arb.ts @@ -0,0 +1,62 @@ +import fc from 'fast-check'; +import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; +import { + CandidDefinition, + OptCandidDefinition +} from '../../candid_definition_arb/types'; + +export function OptDefinitionArb( + candidTypeArbForInnerType: fc.Arbitrary +): fc.Arbitrary { + return fc + .tuple( + UniqueIdentifierArb('typeDeclaration'), + candidTypeArbForInnerType, + fc.boolean() + ) + .map(([name, innerType, useTypeDeclaration]): OptCandidDefinition => { + const typeAnnotation = useTypeDeclaration + ? name + : generateTypeAnnotation(innerType); + + const typeAliasDeclarations = generateTypeAliasDeclarations( + name, + innerType, + useTypeDeclaration + ); + + const imports = generateImports(innerType); + + return { + candidMeta: { + candidTypeObject: typeAnnotation, + typeAliasDeclarations, + imports, + candidType: 'Opt' + }, + innerType + }; + }); +} + +function generateTypeAliasDeclarations( + name: string, + innerType: CandidDefinition, + useTypeDeclaration: boolean +): string[] { + if (useTypeDeclaration) { + return [ + ...innerType.candidMeta.typeAliasDeclarations, + `const ${name} = ${generateTypeAnnotation(innerType)};` + ]; + } + return innerType.candidMeta.typeAliasDeclarations; +} + +function generateTypeAnnotation(innerType: CandidDefinition): string { + return `Opt(${innerType.candidMeta.candidTypeObject})`; +} + +function generateImports(innerType: CandidDefinition): Set { + return new Set([...innerType.candidMeta.imports, 'Opt', 'Some', 'None']); +} diff --git a/property_tests/arbitraries/candid/constructed/opt_arb/index.ts b/property_tests/arbitraries/candid/constructed/opt_arb/index.ts index c92da68bec..eb732a38a0 100644 --- a/property_tests/arbitraries/candid/constructed/opt_arb/index.ts +++ b/property_tests/arbitraries/candid/constructed/opt_arb/index.ts @@ -1,6 +1,19 @@ -import { CandidType, CandidTypeArb } from '../../candid_type_arb'; -import { OptArb as Base } from './base'; +import fc from 'fast-check'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { CandidDefinition } from '../../candid_definition_arb/types'; +import { OptDefinitionArb } from './definition_arb'; +import { OptValuesArb } from './values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { CandidDefinitionArb } from '../../candid_definition_arb'; +import { CorrespondingJSType } from '../../corresponding_js_type'; -export type Opt = [CandidType] | never[]; +export type Opt = [CorrespondingJSType] | never[]; -export const OptArb = Base(CandidTypeArb); +export function OptArb( + candidDefinitionArb: fc.Arbitrary = CandidDefinitionArb +): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator( + OptDefinitionArb(candidDefinitionArb), + OptValuesArb + ); +} diff --git a/property_tests/arbitraries/candid/constructed/opt_arb/values_arb.ts b/property_tests/arbitraries/candid/constructed/opt_arb/values_arb.ts new file mode 100644 index 0000000000..6fe6c9a4a4 --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/opt_arb/values_arb.ts @@ -0,0 +1,55 @@ +import fc from 'fast-check'; +import { Opt } from '.'; +import { CorrespondingJSType } from '../../corresponding_js_type'; +import { OptCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues, CandidValueArb } from '../../candid_values_arb'; + +type SomeOrNone = 'Some' | 'None'; + +export function OptValuesArb( + optDefinition: OptCandidDefinition +): fc.Arbitrary> { + const innerValue = fc.tuple( + fc.constantFrom('Some', 'None') as fc.Arbitrary, + CandidValueArb(optDefinition.innerType) + ); + + return innerValue.map(([someOrNone, innerType]) => { + const valueLiteral = generateValueLiteral(someOrNone, innerType); + const agentArgumentValue = generateValue(someOrNone, innerType); + const agentResponseValue = generateValue(someOrNone, innerType, true); + + return { + valueLiteral, + agentArgumentValue, + agentResponseValue + }; + }); +} + +function generateValue( + someOrNone: SomeOrNone, + innerType: CandidValues, + useAgentResponseValue: boolean = false +): Opt { + if (someOrNone === 'Some') { + return [ + useAgentResponseValue + ? innerType.agentResponseValue + : innerType.agentArgumentValue + ]; + } else { + return []; + } +} + +function generateValueLiteral( + someOrNone: SomeOrNone, + innerType: CandidValues +): string { + if (someOrNone === 'Some') { + return `Some(${innerType.valueLiteral})`; + } else { + return `None`; + } +} diff --git a/property_tests/arbitraries/candid/constructed/record_arb/base.ts b/property_tests/arbitraries/candid/constructed/record_arb/base.ts deleted file mode 100644 index cb8b0a8d6f..0000000000 --- a/property_tests/arbitraries/candid/constructed/record_arb/base.ts +++ /dev/null @@ -1,110 +0,0 @@ -import fc from 'fast-check'; - -import { CandidMeta } from '../../candid_arb'; -import { CandidType } from '../../candid_type_arb'; -import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; -import { JsFunctionNameArb } from '../../../js_function_name_arb'; -import { Record } from './index'; - -type Field = [string, CandidMeta]; - -export function RecordArb(candidTypeArb: fc.Arbitrary>) { - return fc - .tuple( - UniqueIdentifierArb('typeDeclaration'), - fc.uniqueArray(fc.tuple(JsFunctionNameArb, candidTypeArb), { - selector: (entry) => entry[0] - }), - fc.boolean() - ) - .map(([name, fields, useTypeDeclaration]): CandidMeta => { - const candidType = useTypeDeclaration - ? name - : generateCandidType(fields); - - const typeDeclaration = generateTypeDeclaration( - name, - fields, - useTypeDeclaration - ); - - const imports = generateImports(fields); - - const valueLiteral = generateValueLiteral(fields); - - const agentArgumentValue = generateValue(fields); - - const agentResponseValue = generateValue(fields, true); - - return { - src: { - candidType, - typeDeclaration, - imports, - valueLiteral - }, - agentArgumentValue, - agentResponseValue - }; - }); -} - -function generateImports(fields: Field[]): Set { - const fieldImports = fields.flatMap((field) => [...field[1].src.imports]); - return new Set([...fieldImports, 'Record']); -} - -function generateCandidType(fields: Field[]): string { - return `Record({${fields - .map( - ([fieldName, fieldDataType]) => - `${fieldName}: ${fieldDataType.src.candidType}` - ) - .join(',')}})`; -} - -function generateTypeDeclaration( - name: string, - fields: Field[], - useTypeDeclaration: boolean -): string { - const fieldTypeDeclarations = fields - .map((field) => field[1].src.typeDeclaration) - .join('\n'); - if (useTypeDeclaration) { - return `${fieldTypeDeclarations}\nconst ${name} = ${generateCandidType( - fields - )};`; - } - return fieldTypeDeclarations; -} - -function generateValue(fields: Field[], returned: boolean = false): Record { - return fields.length === 0 - ? {} - : fields.reduce((record, [fieldName, fieldDataType]) => { - return { - ...record, - [fieldName]: returned - ? fieldDataType.agentResponseValue - : fieldDataType.agentArgumentValue - }; - }, {}); -} - -function generateValueLiteral(fields: Field[]): string { - if (fields.length === 0) { - return '{}'; - } - - const fieldLiterals = fields - .map( - ([fieldName, fieldValue]) => - `${fieldName}: ${fieldValue.src.valueLiteral}` - ) - .join(',\n'); - - return `{ - ${fieldLiterals} - }`; -} diff --git a/property_tests/arbitraries/candid/constructed/record_arb/definition_arb.ts b/property_tests/arbitraries/candid/constructed/record_arb/definition_arb.ts new file mode 100644 index 0000000000..ff2eba9b18 --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/record_arb/definition_arb.ts @@ -0,0 +1,80 @@ +import fc from 'fast-check'; +import { JsFunctionNameArb } from '../../../js_function_name_arb'; +import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; +import { + CandidDefinition, + RecordCandidDefinition +} from '../../candid_definition_arb/types'; + +type Field = [string, CandidDefinition]; + +export function RecordDefinitionArb( + fieldCandidDefArb: fc.Arbitrary +): fc.Arbitrary { + return fc + .tuple( + UniqueIdentifierArb('typeDeclaration'), + fc.uniqueArray(fc.tuple(JsFunctionNameArb, fieldCandidDefArb), { + selector: ([name, _]) => name, + minLength: 1 // Zero length records are giving that same null error 'vec length of zero sized values too large' // I don't know if that's the same error but it seems like it is + // https://github.com/demergent-labs/azle/issues/1453 + }), + fc.boolean() + ) + .map(([name, fields, useTypeDeclaration]): RecordCandidDefinition => { + const typeAnnotation = useTypeDeclaration + ? name + : generateTypeAnnotation(fields); + + const typeAliasDeclarations = generateTypeAliasDeclarations( + name, + fields, + useTypeDeclaration + ); + + const imports = generateImports(fields); + + return { + candidMeta: { + candidTypeObject: typeAnnotation, + typeAliasDeclarations, + imports, + candidType: 'Record' + }, + innerTypes: fields + }; + }); +} + +function generateImports(fields: Field[]): Set { + const fieldImports = fields.flatMap((field) => [ + ...field[1].candidMeta.imports + ]); + return new Set([...fieldImports, 'Record']); +} + +function generateTypeAnnotation(fields: Field[]): string { + return `Record({${fields + .map( + ([fieldName, fieldDefinition]) => + `${fieldName}: ${fieldDefinition.candidMeta.candidTypeObject}` + ) + .join(',')}})`; +} + +function generateTypeAliasDeclarations( + name: string, + fields: Field[], + useTypeDeclaration: boolean +): string[] { + const fieldTypeAliasDeclarations = fields.flatMap( + (field) => field[1].candidMeta.typeAliasDeclarations + ); + if (useTypeDeclaration) { + return [ + ...fieldTypeAliasDeclarations, + `const ${name} = ${generateTypeAnnotation(fields)};` + ]; + } + return fieldTypeAliasDeclarations; +} diff --git a/property_tests/arbitraries/candid/constructed/record_arb/index.ts b/property_tests/arbitraries/candid/constructed/record_arb/index.ts index 93db47cd24..937e9e3aac 100644 --- a/property_tests/arbitraries/candid/constructed/record_arb/index.ts +++ b/property_tests/arbitraries/candid/constructed/record_arb/index.ts @@ -1,8 +1,22 @@ -import { CandidType, CandidTypeArb } from '../../candid_type_arb'; -import { RecordArb as Base } from './base'; +import fc from 'fast-check'; + +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { CandidDefinition } from '../../candid_definition_arb/types'; +import { RecordDefinitionArb } from './definition_arb'; +import { RecordValuesArb } from './values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { CorrespondingJSType } from '../../corresponding_js_type'; +import { CandidDefinitionArb } from '../../candid_definition_arb'; export type Record = { - [x: string]: CandidType; + [x: string]: CorrespondingJSType; }; -export const RecordArb = Base(CandidTypeArb); +export function RecordArb( + candidDefinitionArb: fc.Arbitrary = CandidDefinitionArb +): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator( + RecordDefinitionArb(candidDefinitionArb), + RecordValuesArb + ); +} diff --git a/property_tests/arbitraries/candid/constructed/record_arb/values_arb.ts b/property_tests/arbitraries/candid/constructed/record_arb/values_arb.ts new file mode 100644 index 0000000000..ece496386d --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/record_arb/values_arb.ts @@ -0,0 +1,62 @@ +import fc from 'fast-check'; +import { Record } from './index'; +import { RecordCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues, CandidValueArb } from '../../candid_values_arb'; +import { CorrespondingJSType } from '../../corresponding_js_type'; + +type Field = [string, CandidValues]; +type ArbField = [string, fc.Arbitrary>]; + +export function RecordValuesArb( + recordDefinition: RecordCandidDefinition +): fc.Arbitrary> { + const fieldValues = recordDefinition.innerTypes.map(([name, innerType]) => { + const result: ArbField = [name, CandidValueArb(innerType)]; + return result; + }); + const arbitraryFieldValues = fieldValues.map(([key, arbValue]) => + arbValue.map((value): Field => [key, value]) + ); + + return fc.tuple(...arbitraryFieldValues).map((fieldValues) => { + const valueLiteral = generateValueLiteral(fieldValues); + const agentArgumentValue = generateValue(fieldValues); + const agentResponseValue = generateValue(fieldValues, true); + + return { + valueLiteral, + agentArgumentValue, + agentResponseValue + }; + }); +} + +function generateValue(fields: Field[], returned: boolean = false): Record { + return fields.length === 0 + ? {} + : fields.reduce((record, [fieldName, fieldCandidValues]) => { + return { + ...record, + [fieldName]: returned + ? fieldCandidValues.agentResponseValue + : fieldCandidValues.agentArgumentValue + }; + }, {}); +} + +function generateValueLiteral(fields: Field[]): string { + if (fields.length === 0) { + return '{}'; + } + + const fieldLiterals = fields + .map( + ([fieldName, fieldCandidValues]) => + `${fieldName}: ${fieldCandidValues.valueLiteral}` + ) + .join(',\n'); + + return `{ + ${fieldLiterals} + }`; +} diff --git a/property_tests/arbitraries/candid/constructed/tuple_arb/base.ts b/property_tests/arbitraries/candid/constructed/tuple_arb/base.ts deleted file mode 100644 index 504da6b963..0000000000 --- a/property_tests/arbitraries/candid/constructed/tuple_arb/base.ts +++ /dev/null @@ -1,98 +0,0 @@ -import fc from 'fast-check'; - -import { CandidMeta } from '../../candid_arb'; -import { CandidType } from '../../candid_type_arb'; -import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; -import { ReturnTuple, Tuple } from './index'; - -export function TupleArb(candidTypeArb: fc.Arbitrary>) { - return fc - .tuple( - UniqueIdentifierArb('typeDeclaration'), - fc.array(candidTypeArb), - fc.boolean() - ) - .map( - ([name, fields, useTypeDeclaration]): CandidMeta< - Tuple, - ReturnTuple - > => { - const candidType = useTypeDeclaration - ? name - : generateCandidType(fields); - - const typeDeclaration = generateTypeDeclaration( - name, - fields, - useTypeDeclaration - ); - - const imports = generateImports(fields); - - const valueLiteral = generateValueLiteral(fields); - - const agentArgumentValue = generateVale(fields); - - const agentResponseValue = generateExpectedValue(fields); - - return { - src: { - candidType, - typeDeclaration, - imports, - valueLiteral - }, - agentArgumentValue, - agentResponseValue - }; - } - ); -} - -function generateVale(fields: CandidMeta[]) { - return fields.map((field) => field.agentArgumentValue); -} - -function generateExpectedValue(fields: CandidMeta[]): ReturnTuple { - if (fields.length === 0) { - return {}; - } - return fields.map((field) => field.agentResponseValue); -} - -function generateTypeDeclaration( - name: string, - fields: CandidMeta[], - useTypeDeclaration: boolean -): string { - const fieldTypeDeclarations = fields - .map((field) => field.src.typeDeclaration) - .join('\n'); - if (useTypeDeclaration) { - return `${fieldTypeDeclarations}\nconst ${name} = ${generateCandidType( - fields - )};`; - } - return fieldTypeDeclarations; -} - -function generateCandidType(fields: CandidMeta[]) { - const innerTypes = fields.map((field) => field.src.candidType); - - return `Tuple(${innerTypes.join(', ')})`; -} - -function generateImports(fields: CandidMeta[]): Set { - const fieldImports = fields.flatMap((field) => [...field.src.imports]); - return new Set([...fieldImports, 'Tuple']); -} - -function generateValueLiteral(fields: CandidMeta[]) { - const fieldLiterals = fields - .map((field) => field.src.valueLiteral) - .join(',\n'); - - return `[ - ${fieldLiterals} - ]`; -} diff --git a/property_tests/arbitraries/candid/constructed/tuple_arb/definition_arb.ts b/property_tests/arbitraries/candid/constructed/tuple_arb/definition_arb.ts new file mode 100644 index 0000000000..a8f73c226c --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/tuple_arb/definition_arb.ts @@ -0,0 +1,73 @@ +import fc from 'fast-check'; +import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; +import { + CandidDefinition, + TupleCandidDefinition +} from '../../candid_definition_arb/types'; + +export function TupleDefinitionArb( + candidTypeArbForFields: fc.Arbitrary +): fc.Arbitrary { + return fc + .tuple( + UniqueIdentifierArb('typeDeclaration'), + fc.array(candidTypeArbForFields, { minLength: 1 }), + // Although no minLength is technically required (according to the + // spec), there are some issues with vecs of empty objects that are causing some problems + // https://github.com/demergent-labs/azle/issues/1453 + fc.boolean() + ) + .map(([name, fields, useTypeDeclaration]): TupleCandidDefinition => { + const typeAnnotation = useTypeDeclaration + ? name + : generateTypeAnnotation(fields); + + const typeAliasDeclarations = generateTypeAliasDeclarations( + name, + fields, + useTypeDeclaration + ); + + const imports = generateImports(fields); + + return { + candidMeta: { + candidTypeObject: typeAnnotation, + typeAliasDeclarations, + imports, + candidType: 'Tuple' + }, + innerTypes: fields + }; + }); +} + +function generateTypeAliasDeclarations( + name: string, + fields: CandidDefinition[], + useTypeDeclaration: boolean +): string[] { + const fieldTypeAliasDeclarations = fields.flatMap( + (field) => field.candidMeta.typeAliasDeclarations + ); + if (useTypeDeclaration) { + return [ + ...fieldTypeAliasDeclarations, + `const ${name} = ${generateTypeAnnotation(fields)};` + ]; + } + return fieldTypeAliasDeclarations; +} + +function generateTypeAnnotation(fields: CandidDefinition[]) { + const innerTypes = fields.map((field) => field.candidMeta.candidTypeObject); + + return `Tuple(${innerTypes.join(', ')})`; +} + +function generateImports(fields: CandidDefinition[]): Set { + const fieldImports = fields.flatMap((field) => [ + ...field.candidMeta.imports + ]); + return new Set([...fieldImports, 'Tuple']); +} diff --git a/property_tests/arbitraries/candid/constructed/tuple_arb/index.ts b/property_tests/arbitraries/candid/constructed/tuple_arb/index.ts index 7b32141484..1f4b0e952d 100644 --- a/property_tests/arbitraries/candid/constructed/tuple_arb/index.ts +++ b/property_tests/arbitraries/candid/constructed/tuple_arb/index.ts @@ -1,7 +1,20 @@ -import { CandidType, CandidTypeArb } from '../../candid_type_arb'; -import { TupleArb as Base } from './base'; +import { CorrespondingJSType } from '../../corresponding_js_type'; +import { CandidDefinitionArb } from '../../candid_definition_arb'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { CandidDefinition } from '../../candid_definition_arb/types'; +import { TupleDefinitionArb } from './definition_arb'; +import { TupleValuesArb } from './values_arbs'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import fc from 'fast-check'; -export type Tuple = CandidType[]; +export type Tuple = CorrespondingJSType[]; export type ReturnTuple = Tuple | {}; -export const TupleArb = Base(CandidTypeArb); +export function TupleArb( + candidDefinitionArb: fc.Arbitrary = CandidDefinitionArb +): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator( + TupleDefinitionArb(candidDefinitionArb), + TupleValuesArb + ); +} diff --git a/property_tests/arbitraries/candid/constructed/tuple_arb/values_arbs.ts b/property_tests/arbitraries/candid/constructed/tuple_arb/values_arbs.ts new file mode 100644 index 0000000000..6e23b01fcf --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/tuple_arb/values_arbs.ts @@ -0,0 +1,50 @@ +import fc from 'fast-check'; +import { Tuple, ReturnTuple } from '.'; +import { CorrespondingJSType } from '../../corresponding_js_type'; +import { TupleCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues, CandidValueArb } from '../../candid_values_arb'; + +export function TupleValuesArb( + tupleDefinition: TupleCandidDefinition +): fc.Arbitrary> { + const fieldValues = tupleDefinition.innerTypes.map((innerType) => { + const result: fc.Arbitrary> = + CandidValueArb(innerType); + return result; + }); + + return fc.tuple(...fieldValues).map((fieldValues) => { + const valueLiteral = generateValueLiteral(fieldValues); + const agentArgumentValue = generateAgentArgumentValue(fieldValues); + const agentResponseValue = generateAgentResponseValue(fieldValues); + + return { + valueLiteral, + agentArgumentValue, + agentResponseValue + }; + }); +} + +function generateAgentArgumentValue( + fields: CandidValues[] +): Tuple { + return fields.map((field) => field.agentArgumentValue); +} + +function generateAgentResponseValue( + fields: CandidValues[] +): ReturnTuple { + if (fields.length === 0) { + return {}; + } + return fields.map((field) => field.agentResponseValue); +} + +function generateValueLiteral(fields: CandidValues[]) { + const fieldLiterals = fields.map((field) => field.valueLiteral).join(',\n'); + + return `[ + ${fieldLiterals} + ]`; +} diff --git a/property_tests/arbitraries/candid/constructed/variant_arb/base.ts b/property_tests/arbitraries/candid/constructed/variant_arb/base.ts deleted file mode 100644 index bd6bc3d912..0000000000 --- a/property_tests/arbitraries/candid/constructed/variant_arb/base.ts +++ /dev/null @@ -1,128 +0,0 @@ -import fc from 'fast-check'; -import { CandidMeta } from '../../candid_arb'; -import { CandidType } from '../../candid_type_arb'; -import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; -import { JsFunctionNameArb } from '../../../js_function_name_arb'; -import { Variant } from '.'; - -type Field = [string, CandidMeta]; - -function VariantFieldsArb( - candidTypeArb: fc.Arbitrary> -): fc.Arbitrary { - return fc.uniqueArray(fc.tuple(JsFunctionNameArb, candidTypeArb), { - selector: (entry) => entry[0], - minLength: 1 - // Although no minLength is technically required (according to the - // spec), the DFX CLI itself currently errors out trying to pass - // an empty object. - }); -} - -export function BaseVariantArb( - candidTypeArb: fc.Arbitrary> -): fc.Arbitrary> { - return fc - .tuple( - UniqueIdentifierArb('typeDeclaration'), - VariantFieldsArb(candidTypeArb), - fc.boolean() - ) - .map(([name, fields, useTypeDeclaration]): CandidMeta => { - const randomIndex = Math.floor(Math.random() * fields.length); - - const candidType = useTypeDeclaration - ? name - : generateCandidType(fields); - - const typeDeclaration = generateTypeDeclaration( - name, - fields, - useTypeDeclaration - ); - - const imports = generateImports(fields); - - const valueLiteral = generateValueLiteral(randomIndex, fields); - - const agentArgumentValue = generateValue(randomIndex, fields); - - const agentResponseValue = generateValue(randomIndex, fields, true); - - return { - src: { - candidType, - typeDeclaration, - imports, - valueLiteral - }, - agentArgumentValue, - agentResponseValue - }; - }); -} - -function generateImports(fields: Field[]): Set { - const fieldImports = fields.flatMap((field) => [...field[1].src.imports]); - return new Set([...fieldImports, 'Variant']); -} - -function generateTypeDeclaration( - name: string, - fields: Field[], - useTypeDeclaration: boolean -): string { - const fieldTypeDeclarations = fields - .map((field) => field[1].src.typeDeclaration) - .join('\n'); - if (useTypeDeclaration) { - return `${fieldTypeDeclarations}\nconst ${name} = ${generateCandidType( - fields - )};`; - } - return fieldTypeDeclarations; -} - -function generateCandidType(fields: Field[]): string { - return `Variant({${fields - .map( - ([fieldName, fieldDataType]) => - `${fieldName}: ${fieldDataType.src.candidType}` - ) - .join(',')}})`; -} - -function generateValue( - index: number, - fields: Field[], - returned: boolean = false -): Variant { - if (fields.length === 0) { - return {}; - } - const [ - randomFieldName, - { - agentArgumentValue: randomFieldValue, - agentResponseValue: randomFieldExpectedValue - } - ] = fields[index]; - - return { - [randomFieldName]: returned - ? randomFieldExpectedValue - : randomFieldValue - }; -} - -function generateValueLiteral(index: number, fields: Field[]): string { - if (fields.length === 0) { - return '{}'; - } - - const [fieldName, fieldValue] = fields[index]; - - return `{ - ${fieldName}: ${fieldValue.src.valueLiteral} - }`; -} diff --git a/property_tests/arbitraries/candid/constructed/variant_arb/definition_arbs.ts b/property_tests/arbitraries/candid/constructed/variant_arb/definition_arbs.ts new file mode 100644 index 0000000000..a4bfca1214 --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/variant_arb/definition_arbs.ts @@ -0,0 +1,88 @@ +import fc from 'fast-check'; +import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; +import { + CandidDefinition, + VariantCandidDefinition +} from '../../candid_definition_arb/types'; +import { JsFunctionNameArb } from '../../../js_function_name_arb'; + +type Field = [string, CandidDefinition]; + +export function VariantDefinitionArb( + candidTypeArbForFields: fc.Arbitrary +): fc.Arbitrary { + return fc + .tuple( + UniqueIdentifierArb('typeDeclaration'), + VariantFieldsArb(candidTypeArbForFields), + fc.boolean() + ) + .map(([name, fields, useTypeDeclaration]): VariantCandidDefinition => { + const typeAnnotation = useTypeDeclaration + ? name + : generateTypeAnnotation(fields); + + const typeAliasDeclarations = generateTypeAliasDeclarations( + name, + fields, + useTypeDeclaration + ); + + const imports = generateImports(fields); + + return { + candidMeta: { + candidTypeObject: typeAnnotation, + typeAliasDeclarations, + imports, + candidType: 'Variant' + }, + innerTypes: fields + }; + }); +} + +function VariantFieldsArb( + candidTypeArb: fc.Arbitrary +): fc.Arbitrary { + return fc.uniqueArray(fc.tuple(JsFunctionNameArb, candidTypeArb), { + selector: ([name, _]) => name, + minLength: 1 + // Although no minLength is technically required (according to the + // spec), the DFX CLI itself currently errors out trying to pass + // an empty object. + }); +} + +function generateImports(fields: Field[]): Set { + const fieldImports = fields.flatMap((field) => [ + ...field[1].candidMeta.imports + ]); + return new Set([...fieldImports, 'Variant']); +} + +function generateTypeAliasDeclarations( + name: string, + fields: Field[], + useTypeDeclaration: boolean +): string[] { + const fieldTypeDeclarations = fields.flatMap( + (field) => field[1].candidMeta.typeAliasDeclarations + ); + if (useTypeDeclaration) { + return [ + ...fieldTypeDeclarations, + `const ${name} = ${generateTypeAnnotation(fields)};` + ]; + } + return fieldTypeDeclarations; +} + +function generateTypeAnnotation(fields: Field[]): string { + return `Variant({${fields + .map( + ([fieldName, fieldDataType]) => + `${fieldName}: ${fieldDataType.candidMeta.candidTypeObject}` + ) + .join(',')}})`; +} diff --git a/property_tests/arbitraries/candid/constructed/variant_arb/index.ts b/property_tests/arbitraries/candid/constructed/variant_arb/index.ts index d4a5ee7ebb..0f93c527a6 100644 --- a/property_tests/arbitraries/candid/constructed/variant_arb/index.ts +++ b/property_tests/arbitraries/candid/constructed/variant_arb/index.ts @@ -1,8 +1,22 @@ -import { CandidType, CandidTypeArb } from '../../candid_type_arb'; -import { BaseVariantArb } from './base'; +import fc from 'fast-check'; + +import { CandidDefinitionArb } from '../../candid_definition_arb'; +import { CorrespondingJSType } from '../../corresponding_js_type'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { CandidDefinition } from '../../candid_definition_arb/types'; +import { VariantDefinitionArb } from './definition_arbs'; +import { VariantValuesArb } from './values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; export type Variant = { - [x: string]: CandidType; + [x: string]: CorrespondingJSType; }; -export const VariantArb = BaseVariantArb(CandidTypeArb); +export function VariantArb( + candidDefinitionArb: fc.Arbitrary = CandidDefinitionArb +): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator( + VariantDefinitionArb(candidDefinitionArb), + VariantValuesArb + ); +} diff --git a/property_tests/arbitraries/candid/constructed/variant_arb/values_arb.ts b/property_tests/arbitraries/candid/constructed/variant_arb/values_arb.ts new file mode 100644 index 0000000000..dcef8de1e7 --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/variant_arb/values_arb.ts @@ -0,0 +1,56 @@ +import fc from 'fast-check'; +import { Variant } from '.'; +import { VariantCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues, CandidValueArb } from '../../candid_values_arb'; +import { CorrespondingJSType } from '../../corresponding_js_type'; + +type Field = [string, CandidValues]; + +export function VariantValuesArb( + variantDefinition: VariantCandidDefinition +): fc.Arbitrary> { + if (variantDefinition.innerTypes.length === 0) { + return fc.constant({ + valueLiteral: '{}', + agentArgumentValue: {}, + agentResponseValue: {} + }); + } + const randomIndex = Math.floor( + Math.random() * variantDefinition.innerTypes.length + ); + + const [name, innerType] = variantDefinition.innerTypes[randomIndex]; + + const fieldValue = 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 { + valueLiteral, + agentArgumentValue, + agentResponseValue + }; + }); +} + +function generateValue(field: Field, returned: boolean = false): Variant { + const [fieldName, { agentArgumentValue, agentResponseValue }] = field; + + return { + [fieldName]: returned ? agentResponseValue : agentArgumentValue + }; +} + +function generateValueLiteral(field: Field): string { + const [fieldName, fieldValue] = field; + + return `{ + ${fieldName}: ${fieldValue.valueLiteral} + }`; +} diff --git a/property_tests/arbitraries/candid/constructed/vec_arb/base.ts b/property_tests/arbitraries/candid/constructed/vec_arb/base.ts deleted file mode 100644 index 8f9b489d5d..0000000000 --- a/property_tests/arbitraries/candid/constructed/vec_arb/base.ts +++ /dev/null @@ -1,222 +0,0 @@ -import fc, { sample } from 'fast-check'; -import { CandidMeta, Src } from '../../candid_arb'; -import { CandidType } from '../../candid_type_arb'; -import { Vec } from './index'; -import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; - -export function VecInnerArb( - arb: fc.Arbitrary> -): fc.Arbitrary> { - return fc - .tuple( - UniqueIdentifierArb('typeDeclaration'), - fc.array(arb), - arb, - fc.boolean() - ) - .map( - ([ - name, - vecOfInnerType, - { src: innerTypeSrc }, - useTypeDeclaration - ]): CandidMeta => { - const valueLiteral = generateValueLiteral( - vecOfInnerType, - innerTypeSrc.candidType - ); - const candidType = useTypeDeclaration - ? name - : generateCandidType(innerTypeSrc); - - const imports = generateImports(innerTypeSrc); - - const typeDeclaration = generateTypeDeclaration( - name, - innerTypeSrc, - useTypeDeclaration - ); - - const agentArgumentValue = generateValue( - vecOfInnerType, - innerTypeSrc.candidType - ); - - const agentResponseValue = generateValue( - vecOfInnerType, - innerTypeSrc.candidType - ); - - return { - agentArgumentValue, - agentResponseValue, - src: { - candidType, - imports, - typeDeclaration, - valueLiteral - } - }; - } - ); -} - -// Exploration for making VecArb use CandidTypeArb -// export function VecArb( -// candidTypeArb: fc.Arbitrary> -// ): fc.Arbitrary> { -// candidTypeArb.chain((innerType) => { -// return fc.array(fc.constant(innerType)); -// }); -// return fc -// .tuple( -// UniqueIdentifierArb('typeDeclaration'), -// fc.array(candidTypeArb), -// fc.boolean() -// ) -// .map(([name, innerType, useTypeDeclaration]) => { -// const candidType = useTypeDeclaration -// ? name -// : generateCandidType(innerType); - -// const imports = generateImports(innerType); - -// const typeDeclaration = generateTypeDeclaration( -// name, -// innerType, -// useTypeDeclaration -// ); - -// const valueLiteral = generateValueLiteral(innerType); - -// return { -// src: { -// candidType, -// imports, -// typeDeclaration, -// valueLiteral -// }, -// value, -// expectedValue -// }; -// }); -// } - -function generateTypeDeclaration( - name: string, - innerTypeSrc: Src, - useTypeDeclaration: boolean -) { - if (useTypeDeclaration) { - return `${ - innerTypeSrc.typeDeclaration ?? '' - }\nconst ${name} = ${generateCandidType(innerTypeSrc)}`; - } - return innerTypeSrc.typeDeclaration; -} - -function generateImports(innerTypeSrc: Src): Set { - // Hack until https://github.com/demergent-labs/azle/issues/1453 gets fixed - if (innerTypeSrc.candidType === 'Null') { - return new Set([...innerTypeSrc.imports, 'Vec', 'bool']); - } - return new Set([...innerTypeSrc.imports, 'Vec']); -} - -function generateCandidType(innerTypeSrc: Src): string { - // Hack until https://github.com/demergent-labs/azle/issues/1453 gets fixed - if (innerTypeSrc.candidType === 'Null') { - return `Vec(bool)`; - } - return `Vec(${innerTypeSrc.candidType})`; -} - -function generateValue( - array: CandidMeta[], - candidType: string, - returned: boolean = false -): Vec { - // Hack until https://github.com/demergent-labs/azle/issues/1453 gets fixed - if (candidType === 'Null') { - return Array(array.length).fill(false); - } - const value = array.map((sample) => - returned ? sample.agentResponseValue : sample.agentArgumentValue - ); - - if (candidType === 'int8') { - return new Int8Array(value as number[]); - } - if (candidType === 'int16') { - return new Int16Array(value as number[]); - } - if (candidType === 'int32') { - return new Int32Array(value as number[]); - } - if (candidType === 'int64') { - return new BigInt64Array(value as bigint[]); - } - if (candidType === 'nat8') { - return new Uint8Array(value as number[]); - } - if (candidType === 'nat16') { - return new Uint16Array(value as number[]); - } - if (candidType === 'nat32') { - return new Uint32Array(value as number[]); - } - if (candidType === 'nat64') { - return new BigUint64Array(value as bigint[]); - } - - return value; -} - -function generateValueLiteral( - sample: CandidMeta[], - candidType: string -) { - // Hack until https://github.com/demergent-labs/azle/issues/1453 gets fixed - if (candidType === 'Null') { - return `[${Array(sample.length).fill(false)}]`; - } - const valueLiterals = sample - .map((sample) => sample.src.valueLiteral) - .join(','); - - const valueLiteral = `[${valueLiterals}]`; - - if (candidType === 'int64') { - return `BigInt64Array.from(${valueLiteral})`; - } - - if (candidType === 'int32') { - return `Int32Array.from(${valueLiteral})`; - } - - if (candidType === 'int16') { - return `Int16Array.from(${valueLiteral})`; - } - - if (candidType === 'int8') { - return `Int8Array.from(${valueLiteral})`; - } - - if (candidType === 'nat64') { - return `BigUint64Array.from(${valueLiteral})`; - } - - if (candidType === 'nat32') { - return `Uint32Array.from(${valueLiteral})`; - } - - if (candidType === 'nat16') { - return `Uint16Array.from(${valueLiteral})`; - } - - if (candidType === 'nat8') { - return `Uint8Array.from(${valueLiteral})`; - } - - return valueLiteral; -} diff --git a/property_tests/arbitraries/candid/constructed/vec_arb/definition_arb.ts b/property_tests/arbitraries/candid/constructed/vec_arb/definition_arb.ts new file mode 100644 index 0000000000..d16a5be129 --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/vec_arb/definition_arb.ts @@ -0,0 +1,62 @@ +import fc from 'fast-check'; +import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; +import { + CandidDefinition, + VecCandidDefinition +} from '../../candid_definition_arb/types'; + +export function VecDefinitionArb( + candidTypeArb: fc.Arbitrary +): fc.Arbitrary { + return fc + .tuple( + UniqueIdentifierArb('typeDeclaration'), + candidTypeArb, + fc.boolean() + ) + .map(([name, innerType, useTypeDeclaration]): VecCandidDefinition => { + const typeAnnotation = useTypeDeclaration + ? name + : generateTypeAnnotation(innerType); + + const typeAliasDeclarations = generateTypeAliasDeclarations( + name, + innerType, + useTypeDeclaration + ); + + const imports = generateImports(innerType); + + return { + candidMeta: { + candidTypeObject: typeAnnotation, + typeAliasDeclarations, + imports, + candidType: 'Vec' + }, + innerType: innerType + }; + }); +} + +function generateTypeAliasDeclarations( + name: string, + innerType: CandidDefinition, + useTypeDeclaration: boolean +): string[] { + if (useTypeDeclaration) { + return [ + ...innerType.candidMeta.typeAliasDeclarations, + `const ${name} = ${generateTypeAnnotation(innerType)};` + ]; + } + return innerType.candidMeta.typeAliasDeclarations; +} + +function generateImports(innerType: CandidDefinition): Set { + return new Set([...innerType.candidMeta.imports, 'Vec']); +} + +function generateTypeAnnotation(innerType: CandidDefinition): string { + return `Vec(${innerType.candidMeta.candidTypeObject})`; +} diff --git a/property_tests/arbitraries/candid/constructed/vec_arb/index.ts b/property_tests/arbitraries/candid/constructed/vec_arb/index.ts index 7ba7555c0c..92fdd4e660 100644 --- a/property_tests/arbitraries/candid/constructed/vec_arb/index.ts +++ b/property_tests/arbitraries/candid/constructed/vec_arb/index.ts @@ -1,26 +1,15 @@ import fc from 'fast-check'; -import { IntArb } from '../../primitive/ints/int_arb'; -import { Int8Arb } from '../../primitive/ints/int8_arb'; -import { Int16Arb } from '../../primitive/ints/int16_arb'; -import { Int32Arb } from '../../primitive/ints/int32_arb'; -import { Int64Arb } from '../../primitive/ints/int64_arb'; -import { NatArb } from '../../primitive/nats/nat_arb'; -import { Nat8Arb } from '../../primitive/nats/nat8_arb'; -import { Nat16Arb } from '../../primitive/nats/nat16_arb'; -import { Nat32Arb } from '../../primitive/nats/nat32_arb'; -import { Nat64Arb } from '../../primitive/nats/nat64_arb'; -import { PrincipalArb } from '../../reference/principal_arb'; -import { BoolArb } from '../../primitive/bool'; -import { Float32Arb } from '../../primitive/floats/float32_arb'; -import { Float64Arb } from '../../primitive/floats/float64_arb'; -import { NullArb } from '../../primitive/null'; -import { TextArb } from '../../primitive/text'; -import { BlobArb } from '../blob_arb'; -import { VecInnerArb } from './base'; -import { CandidType } from '../../candid_type_arb'; + +import { CandidDefinitionArb } from '../../candid_definition_arb'; +import { CorrespondingJSType } from '../../corresponding_js_type'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { CandidDefinition } from '../../candid_definition_arb/types'; +import { VecDefinitionArb } from './definition_arb'; +import { VecValuesArb } from './values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; export type Vec = - | CandidType[] + | CorrespondingJSType[] | Uint16Array | Uint32Array | Uint8Array @@ -30,31 +19,11 @@ export type Vec = | BigUint64Array | BigInt64Array; -// TODO we have a big problem here. If we try to make a vec of CandidTypeArb you -// get a vec of multiple different types of candidTypeArbs, doing it this way -// makes it so you for sure have a homogeneous vec... but it doesn't benefit -// from code reuse -// The Problem goes even deeper than we first thought. If you pass in something -// like an opt for example, Opt gets an arbitrary inner value, so every index in -// the array will be an opt with a different inner value, so instead of having a -// homogenous vec of opt nat16 you will get a vec of opt nat16 and opt int and -// opt bool etc -export const VecArb = fc.oneof( - VecInnerArb(Float32Arb), - VecInnerArb(Float64Arb), - VecInnerArb(IntArb), - VecInnerArb(Int8Arb), - VecInnerArb(Int16Arb), - VecInnerArb(Int32Arb), - VecInnerArb(Int64Arb), - VecInnerArb(NatArb), - VecInnerArb(Nat8Arb), - VecInnerArb(Nat16Arb), - VecInnerArb(Nat32Arb), - VecInnerArb(Nat64Arb), - VecInnerArb(BoolArb), - VecInnerArb(NullArb), - VecInnerArb(TextArb), - VecInnerArb(PrincipalArb), - VecInnerArb(BlobArb) -); +export function VecArb( + candidDefinitionArb: fc.Arbitrary = CandidDefinitionArb +): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator( + VecDefinitionArb(candidDefinitionArb), + VecValuesArb + ); +} diff --git a/property_tests/arbitraries/candid/constructed/vec_arb/values_arb.ts b/property_tests/arbitraries/candid/constructed/vec_arb/values_arb.ts new file mode 100644 index 0000000000..8ab1e6a195 --- /dev/null +++ b/property_tests/arbitraries/candid/constructed/vec_arb/values_arb.ts @@ -0,0 +1,116 @@ +import fc from 'fast-check'; +import { Vec } from '.'; +import { CandidType } from '../../candid_type'; +import { CorrespondingJSType } from '../../corresponding_js_type'; +import { VecCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues, CandidValueArb } from '../../candid_values_arb'; + +export function VecValuesArb( + vecDefinition: VecCandidDefinition +): fc.Arbitrary> { + const arbitraryMemberValues = fc + .tuple( + fc.array(fc.constant(null)), + fc.constant(vecDefinition.innerType) + ) + .chain(([arrayTemplate, innerType]) => + fc.tuple(...arrayTemplate.map(() => CandidValueArb(innerType))) + ); + + const innerCandidType = vecDefinition.innerType.candidMeta.candidType; + + return arbitraryMemberValues.map((fieldValues) => { + const valueLiteral = generateValueLiteral(fieldValues, innerCandidType); + const agentArgumentValue = generateValue(fieldValues, innerCandidType); + const agentResponseValue = generateValue( + fieldValues, + innerCandidType, + true + ); + return { + valueLiteral, + agentArgumentValue, + agentResponseValue + }; + }); +} + +function generateValue( + array: CandidValues[], + candidType: CandidType, + returned: boolean = false +): Vec { + const value = array.map((sample) => + returned ? sample.agentResponseValue : sample.agentArgumentValue + ); + + if (candidType === 'int8') { + return new Int8Array(value as number[]); + } + if (candidType === 'int16') { + return new Int16Array(value as number[]); + } + if (candidType === 'int32') { + return new Int32Array(value as number[]); + } + if (candidType === 'int64') { + return new BigInt64Array(value as bigint[]); + } + if (candidType === 'nat8') { + return new Uint8Array(value as number[]); + } + if (candidType === 'nat16') { + return new Uint16Array(value as number[]); + } + if (candidType === 'nat32') { + return new Uint32Array(value as number[]); + } + if (candidType === 'nat64') { + return new BigUint64Array(value as bigint[]); + } + + return value; +} + +function generateValueLiteral( + sample: CandidValues[], + innerCandidType: CandidType +) { + const valueLiterals = sample.map((sample) => sample.valueLiteral).join(','); + + const valueLiteral = `[${valueLiterals}]`; + + if (innerCandidType === 'int64') { + return `BigInt64Array.from(${valueLiteral})`; + } + + if (innerCandidType === 'int32') { + return `Int32Array.from(${valueLiteral})`; + } + + if (innerCandidType === 'int16') { + return `Int16Array.from(${valueLiteral})`; + } + + if (innerCandidType === 'int8') { + return `Int8Array.from(${valueLiteral})`; + } + + if (innerCandidType === 'nat64') { + return `BigUint64Array.from(${valueLiteral})`; + } + + if (innerCandidType === 'nat32') { + return `Uint32Array.from(${valueLiteral})`; + } + + if (innerCandidType === 'nat16') { + return `Uint16Array.from(${valueLiteral})`; + } + + if (innerCandidType === 'nat8') { + return `Uint8Array.from(${valueLiteral})`; + } + + return valueLiteral; +} diff --git a/property_tests/arbitraries/candid/corresponding_js_type.ts b/property_tests/arbitraries/candid/corresponding_js_type.ts new file mode 100644 index 0000000000..116e0d6993 --- /dev/null +++ b/property_tests/arbitraries/candid/corresponding_js_type.ts @@ -0,0 +1,31 @@ +import { Principal } from '@dfinity/principal'; +import { Func } from './reference/func_arb'; +import { Opt } from './constructed/opt_arb'; +import { Variant } from './constructed/variant_arb'; +import { Tuple } from './constructed/tuple_arb'; +import { Vec } from './constructed/vec_arb'; +import { Record } from './constructed/record_arb'; + +export type CorrespondingJSType = + | number + | bigint + | null + | boolean + | Principal + | Uint8Array + | string + | Func + | Opt + | Variant + | Record + | Tuple + | undefined + | Int16Array + | Int32Array + | Int8Array + | BigInt64Array + | Uint16Array + | Uint32Array + | Uint8Array + | BigUint64Array + | Vec; diff --git a/property_tests/arbitraries/candid/primitive/bool.ts b/property_tests/arbitraries/candid/primitive/bool.ts index b2c914b567..43f9beea75 100644 --- a/property_tests/arbitraries/candid/primitive/bool.ts +++ b/property_tests/arbitraries/candid/primitive/bool.ts @@ -1,5 +1,19 @@ import fc from 'fast-check'; -import { CandidMetaArb } from '../candid_arb'; +import { SimpleCandidDefinitionArb } from '../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../simple_type_arbs/values_arb'; import { booleanToSrcLiteral } from '../to_src_literal/boolean'; +import { CandidValueAndMetaArbGenerator } from '../candid_value_and_meta_arb_generator'; +import { BoolCandidDefinition } from '../candid_definition_arb/types'; +import { CandidValues } from '../candid_values_arb'; -export const BoolArb = CandidMetaArb(fc.boolean(), 'bool', booleanToSrcLiteral); +export function BoolArb(): fc.Arbitrary { + return CandidValueAndMetaArbGenerator(BoolDefinitionArb(), BoolValueArb); +} + +export function BoolDefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('bool'); +} + +export function BoolValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(fc.boolean(), booleanToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/floats/float32_arb.ts b/property_tests/arbitraries/candid/primitive/floats/float32_arb.ts index 5fe72a91fa..e103f310ba 100644 --- a/property_tests/arbitraries/candid/primitive/floats/float32_arb.ts +++ b/property_tests/arbitraries/candid/primitive/floats/float32_arb.ts @@ -1,9 +1,23 @@ import fc from 'fast-check'; -import { CandidMetaArb } from '../../candid_arb'; import { floatToSrcLiteral } from '../../to_src_literal/float'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { FloatCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues } from '../../candid_values_arb'; -export const Float32Arb = CandidMetaArb( - fc.float(), - 'float32', - floatToSrcLiteral -); +export function Float32Arb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator( + Float32DefinitionArb(), + Float32ValueArb + ); +} + +export function Float32DefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('float32'); +} + +export function Float32ValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(fc.float(), floatToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/floats/float64_arb.ts b/property_tests/arbitraries/candid/primitive/floats/float64_arb.ts index 4f4078c972..5a83c958a2 100644 --- a/property_tests/arbitraries/candid/primitive/floats/float64_arb.ts +++ b/property_tests/arbitraries/candid/primitive/floats/float64_arb.ts @@ -1,9 +1,29 @@ import fc from 'fast-check'; -import { CandidMetaArb } from '../../candid_arb'; import { floatToSrcLiteral } from '../../to_src_literal/float'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { FloatCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues } from '../../candid_values_arb'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; -export const Float64Arb = CandidMetaArb( - fc.float64Array({ maxLength: 1, minLength: 1 }).map((sample) => sample[0]), - 'float64', - floatToSrcLiteral -); +export function Float64Arb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator( + Float64DefinitionArb(), + Float64ValueArb + ); +} + +export function Float64DefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('float64'); +} + +export function Float64ValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(float64(), floatToSrcLiteral); +} + +function float64(): fc.Arbitrary { + return fc + .float64Array({ maxLength: 1, minLength: 1 }) + .map((sample) => sample[0]); +} diff --git a/property_tests/arbitraries/candid/primitive/ints/int16_arb.ts b/property_tests/arbitraries/candid/primitive/ints/int16_arb.ts index 6c7a54032a..c44ab4c678 100644 --- a/property_tests/arbitraries/candid/primitive/ints/int16_arb.ts +++ b/property_tests/arbitraries/candid/primitive/ints/int16_arb.ts @@ -1,9 +1,21 @@ import { numberToSrcLiteral } from '../../to_src_literal/number'; -import { CandidMetaArb } from '../../candid_arb'; import { NumberArb } from './'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import fc from 'fast-check'; +import { IntCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues } from '../../candid_values_arb'; -export const Int16Arb = CandidMetaArb( - NumberArb(16), - 'int16', - numberToSrcLiteral -); +export function Int16Arb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(Int16DefinitionArb(), Int16ValueArb); +} + +export function Int16DefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('int16'); +} + +export function Int16ValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(NumberArb(16), numberToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/ints/int32_arb.ts b/property_tests/arbitraries/candid/primitive/ints/int32_arb.ts index 6396827d36..0f9fbef5d0 100644 --- a/property_tests/arbitraries/candid/primitive/ints/int32_arb.ts +++ b/property_tests/arbitraries/candid/primitive/ints/int32_arb.ts @@ -1,9 +1,21 @@ import { numberToSrcLiteral } from '../../to_src_literal/number'; -import { CandidMetaArb } from '../../candid_arb'; import { NumberArb } from './'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import fc from 'fast-check'; +import { IntCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues } from '../../candid_values_arb'; -export const Int32Arb = CandidMetaArb( - NumberArb(32), - 'int32', - numberToSrcLiteral -); +export function Int32Arb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(Int32DefinitionArb(), Int32ValueArb); +} + +export function Int32DefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('int32'); +} + +export function Int32ValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(NumberArb(32), numberToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/ints/int64_arb.ts b/property_tests/arbitraries/candid/primitive/ints/int64_arb.ts index 88b269430c..f07001546c 100644 --- a/property_tests/arbitraries/candid/primitive/ints/int64_arb.ts +++ b/property_tests/arbitraries/candid/primitive/ints/int64_arb.ts @@ -1,9 +1,20 @@ import fc from 'fast-check'; -import { CandidMetaArb } from '../../candid_arb'; import { bigintToSrcLiteral } from '../../to_src_literal/bigint'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { IntCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues } from '../../candid_values_arb'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; -export const Int64Arb = CandidMetaArb( - fc.bigIntN(64), - 'int64', - bigintToSrcLiteral -); +export function Int64Arb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(Int64DefinitionArb(), Int64ValueArb); +} + +export function Int64DefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('int64'); +} + +export function Int64ValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(fc.bigIntN(64), bigintToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/ints/int8_arb.ts b/property_tests/arbitraries/candid/primitive/ints/int8_arb.ts index d2506756ac..fa85774b44 100644 --- a/property_tests/arbitraries/candid/primitive/ints/int8_arb.ts +++ b/property_tests/arbitraries/candid/primitive/ints/int8_arb.ts @@ -1,5 +1,21 @@ import { numberToSrcLiteral } from '../../to_src_literal/number'; -import { CandidMetaArb } from '../../candid_arb'; import { NumberArb } from './'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import fc from 'fast-check'; +import { CandidValues } from '../../candid_values_arb'; +import { IntCandidDefinition } from '../../candid_definition_arb/types'; -export const Int8Arb = CandidMetaArb(NumberArb(8), 'int8', numberToSrcLiteral); +export function Int8Arb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(Int8DefinitionArb(), Int8ValueArb); +} + +export function Int8DefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('int8'); +} + +export function Int8ValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(NumberArb(8), numberToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/ints/int_arb.ts b/property_tests/arbitraries/candid/primitive/ints/int_arb.ts index 92a310ce64..7574655e6d 100644 --- a/property_tests/arbitraries/candid/primitive/ints/int_arb.ts +++ b/property_tests/arbitraries/candid/primitive/ints/int_arb.ts @@ -1,5 +1,20 @@ import fc from 'fast-check'; -import { CandidMetaArb } from '../../candid_arb'; import { bigintToSrcLiteral } from '../../to_src_literal/bigint'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { CandidValues } from '../../candid_values_arb'; +import { IntCandidDefinition } from '../../candid_definition_arb/types'; -export const IntArb = CandidMetaArb(fc.bigInt(), 'int', bigintToSrcLiteral); +export function IntArb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(IntDefinitionArb(), IntValueArb); +} + +export function IntDefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('int'); +} + +export function IntValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(fc.bigInt(), bigintToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/nats/nat16_arb.ts b/property_tests/arbitraries/candid/primitive/nats/nat16_arb.ts index 9e108e1380..a4ba69b142 100644 --- a/property_tests/arbitraries/candid/primitive/nats/nat16_arb.ts +++ b/property_tests/arbitraries/candid/primitive/nats/nat16_arb.ts @@ -1,9 +1,21 @@ import { numberToSrcLiteral } from '../../to_src_literal/number'; -import { CandidMetaArb } from '../../candid_arb'; import { UNumberArb } from './index'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import fc from 'fast-check'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { NatCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues } from '../../candid_values_arb'; -export const Nat16Arb = CandidMetaArb( - UNumberArb(16), - 'nat16', - numberToSrcLiteral -); +export function Nat16Arb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(Nat16DefinitionArb(), Nat16ValueArb); +} + +export function Nat16DefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('nat16'); +} + +export function Nat16ValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(UNumberArb(16), numberToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/nats/nat32_arb.ts b/property_tests/arbitraries/candid/primitive/nats/nat32_arb.ts index 83671ce36f..988333391a 100644 --- a/property_tests/arbitraries/candid/primitive/nats/nat32_arb.ts +++ b/property_tests/arbitraries/candid/primitive/nats/nat32_arb.ts @@ -1,9 +1,21 @@ import { numberToSrcLiteral } from '../../to_src_literal/number'; -import { CandidMetaArb } from '../../candid_arb'; import { UNumberArb } from './index'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import fc from 'fast-check'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { NatCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues } from '../../candid_values_arb'; -export const Nat32Arb = CandidMetaArb( - UNumberArb(32), - 'nat32', - numberToSrcLiteral -); +export function Nat32Arb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(Nat32DefinitionArb(), Nat32ValueArb); +} + +export function Nat32DefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('nat32'); +} + +export function Nat32ValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(UNumberArb(32), numberToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/nats/nat64_arb.ts b/property_tests/arbitraries/candid/primitive/nats/nat64_arb.ts index e616bfec8a..0310c8614e 100644 --- a/property_tests/arbitraries/candid/primitive/nats/nat64_arb.ts +++ b/property_tests/arbitraries/candid/primitive/nats/nat64_arb.ts @@ -1,9 +1,20 @@ import fc from 'fast-check'; -import { CandidMetaArb } from '../../candid_arb'; import { bigintToSrcLiteral } from '../../to_src_literal/bigint'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { NatCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues } from '../../candid_values_arb'; -export const Nat64Arb = CandidMetaArb( - fc.bigUintN(64), - 'nat64', - bigintToSrcLiteral -); +export function Nat64Arb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(Nat64DefinitionArb(), Nat64ValueArb); +} + +export function Nat64DefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('nat64'); +} + +export function Nat64ValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(fc.bigUintN(64), bigintToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/nats/nat8_arb.ts b/property_tests/arbitraries/candid/primitive/nats/nat8_arb.ts index 7ca00e794b..f6d219daba 100644 --- a/property_tests/arbitraries/candid/primitive/nats/nat8_arb.ts +++ b/property_tests/arbitraries/candid/primitive/nats/nat8_arb.ts @@ -1,5 +1,21 @@ import { numberToSrcLiteral } from '../../to_src_literal/number'; -import { CandidMetaArb } from '../../candid_arb'; import { UNumberArb } from './index'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import fc from 'fast-check'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { NatCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues } from '../../candid_values_arb'; -export const Nat8Arb = CandidMetaArb(UNumberArb(8), 'nat8', numberToSrcLiteral); +export function Nat8Arb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(Nat8DefinitionArb(), Nat8ValueArb); +} + +export function Nat8DefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('nat8'); +} + +export function Nat8ValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(UNumberArb(8), numberToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/nats/nat_arb.ts b/property_tests/arbitraries/candid/primitive/nats/nat_arb.ts index e1592dc47b..720af1ad98 100644 --- a/property_tests/arbitraries/candid/primitive/nats/nat_arb.ts +++ b/property_tests/arbitraries/candid/primitive/nats/nat_arb.ts @@ -1,5 +1,20 @@ import fc from 'fast-check'; -import { CandidMetaArb } from '../../candid_arb'; import { bigintToSrcLiteral } from '../../to_src_literal/bigint'; +import { SimpleCandidDefinitionArb } from '../../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { NatCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues } from '../../candid_values_arb'; -export const NatArb = CandidMetaArb(fc.bigUint(), 'nat', bigintToSrcLiteral); +export function NatArb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(NatDefinitionArb(), NatValueArb); +} + +export function NatDefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('nat'); +} + +export function NatValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(fc.bigUint(), bigintToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/null.ts b/property_tests/arbitraries/candid/primitive/null.ts index c98466cd17..a7d3e7f700 100644 --- a/property_tests/arbitraries/candid/primitive/null.ts +++ b/property_tests/arbitraries/candid/primitive/null.ts @@ -1,9 +1,20 @@ import fc from 'fast-check'; -import { CandidMetaArb } from '../candid_arb'; +import { SimpleCandidDefinitionArb } from '../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../simple_type_arbs/values_arb'; import { nullToSrcLiteral } from '../to_src_literal/null'; +import { CandidValueAndMetaArbGenerator } from '../candid_value_and_meta_arb_generator'; +import { NullCandidDefinition } from '../candid_definition_arb/types'; +import { CandidValues } from '../candid_values_arb'; +import { CandidValueAndMeta } from '../candid_value_and_meta_arb'; -export const NullArb = CandidMetaArb( - fc.constant(null), - 'Null', - nullToSrcLiteral -); +export function NullArb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(NullDefinitionArb(), NullValueArb); +} + +export function NullDefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('Null'); +} + +export function NullValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(fc.constant(null), nullToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/text.ts b/property_tests/arbitraries/candid/primitive/text.ts index b68a28d6a8..aa50d5047d 100644 --- a/property_tests/arbitraries/candid/primitive/text.ts +++ b/property_tests/arbitraries/candid/primitive/text.ts @@ -1,5 +1,20 @@ import fc from 'fast-check'; -import { CandidMetaArb } from '../candid_arb'; 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'; -export const TextArb = CandidMetaArb(fc.string(), 'text', stringToSrcLiteral); +export function TextArb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(TextDefinitionArb(), TextValueArb); +} + +export function TextDefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('text'); +} + +export function TextValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(fc.string(), stringToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/primitive/void.ts b/property_tests/arbitraries/candid/primitive/void.ts index d39c1339d2..bd97f4b315 100644 --- a/property_tests/arbitraries/candid/primitive/void.ts +++ b/property_tests/arbitraries/candid/primitive/void.ts @@ -1,9 +1,20 @@ import fc from 'fast-check'; -import { CandidMetaArb } from '../candid_arb'; import { voidToSrcLiteral } from '../to_src_literal/void'; +import { SimpleCandidDefinitionArb } from '../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../simple_type_arbs/values_arb'; +import { CandidValueAndMetaArbGenerator } from '../candid_value_and_meta_arb_generator'; +import { CandidValueAndMeta } from '../candid_value_and_meta_arb'; +import { VoidCandidDefinition } from '../candid_definition_arb/types'; +import { CandidValues } from '../candid_values_arb'; -export const VoidArb = CandidMetaArb( - fc.constant(undefined), - 'Void', - voidToSrcLiteral -); +export function VoidArb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator(VoidDefinitionArb(), VoidValueArb); +} + +export function VoidDefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('Void'); +} + +export function VoidValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(fc.constant(undefined), voidToSrcLiteral); +} diff --git a/property_tests/arbitraries/candid/reference/func_arb/base.ts b/property_tests/arbitraries/candid/reference/func_arb/base.ts deleted file mode 100644 index 971f53ede1..0000000000 --- a/property_tests/arbitraries/candid/reference/func_arb/base.ts +++ /dev/null @@ -1,71 +0,0 @@ -import fc from 'fast-check'; - -import { PrincipalArb } from '../principal_arb'; -import { VoidArb } from '../../primitive/void'; -import { CandidMeta } from '../../candid_arb'; -import { CandidType } from '../../candid_type_arb'; -import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; -import { Func } from './index'; - -type Mode = 'query' | 'update' | 'oneway'; - -export function FuncArb(candidTypeArb: fc.Arbitrary>) { - return (fc.constantFrom('query', 'update', 'oneway') as fc.Arbitrary) - .chain((mode) => { - const returnType = mode === 'oneway' ? VoidArb : candidTypeArb; - - return fc.tuple( - UniqueIdentifierArb('typeDeclaration'), - fc.array(candidTypeArb), - returnType, - fc.constant(mode), - PrincipalArb - ); - }) - .map( - ([name, params, returnFunc, mode, principal]): CandidMeta => { - const typeDeclaration = generateTypeDeclaration( - name, - params, - returnFunc, - mode - ); - - const imports = new Set([ - ...params.flatMap((param) => [...param.src.imports]), - ...returnFunc.src.imports, - 'Func' - ]); - - const value: Func = [principal.agentArgumentValue, name]; - - const valueLiteral = `[${principal.src.valueLiteral}, '${name}']`; - - return { - src: { - candidType: name, - typeDeclaration, - imports, - valueLiteral - }, - agentArgumentValue: value, - agentResponseValue: value - }; - } - ); -} - -function generateTypeDeclaration( - name: string, - paramCandids: CandidMeta[], - returnCandid: CandidMeta, - mode: Mode -): string { - const paramTypeDeclarations = paramCandids - .map((param) => param.src.typeDeclaration ?? '') - .join('\n'); - const returnTypeDeclaration = returnCandid.src.typeDeclaration ?? ''; - const params = paramCandids.map((param) => param.src.candidType).join(', '); - - return `${paramTypeDeclarations}\n${returnTypeDeclaration}\nconst ${name} = Func([${params}], ${returnCandid.src.candidType}, '${mode}')`; -} diff --git a/property_tests/arbitraries/candid/reference/func_arb/definition_arb.ts b/property_tests/arbitraries/candid/reference/func_arb/definition_arb.ts new file mode 100644 index 0000000000..9fa0122a0f --- /dev/null +++ b/property_tests/arbitraries/candid/reference/func_arb/definition_arb.ts @@ -0,0 +1,103 @@ +import fc from 'fast-check'; +import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; +import { + CandidDefinition, + FuncCandidDefinition +} from '../../candid_definition_arb/types'; +import { VoidDefinitionArb } from '../../primitive/void'; + +type Mode = 'query' | 'update' | 'oneway'; + +export function FuncDefinitionArb( + candidDefArb: fc.Arbitrary +): fc.Arbitrary { + return (fc.constantFrom('query', 'update', 'oneway') as fc.Arbitrary) + .chain((mode) => { + const returnType = + mode === 'oneway' ? VoidDefinitionArb() : candidDefArb; + + return fc.tuple( + UniqueIdentifierArb('typeDeclaration'), + fc.array(candidDefArb), + returnType, + fc.constant(mode), + fc.boolean() + ); + }) + .map( + ([ + name, + params, + returnFunc, + mode, + useTypeDeclaration + ]): FuncCandidDefinition => { + const typeAliasDeclarations = generateTypeAliasDeclarations( + name, + params, + returnFunc, + mode, + useTypeDeclaration + ); + + const typeAnnotation = useTypeDeclaration + ? name + : generateTypeAnnotation(params, returnFunc, mode); + + const imports = new Set([ + ...params.flatMap((param) => [...param.candidMeta.imports]), + ...returnFunc.candidMeta.imports, + 'Func' + ]); + + return { + candidMeta: { + candidTypeObject: typeAnnotation, + typeAliasDeclarations, + imports, + candidType: 'Func' + }, + paramCandidMeta: params, + returnCandidMeta: returnFunc + }; + } + ); +} + +function generateTypeAliasDeclarations( + name: string, + paramCandids: CandidDefinition[], + returnCandid: CandidDefinition, + mode: Mode, + useTypeDeclaration: boolean +): string[] { + const paramTypeDeclarations = paramCandids.flatMap( + (param) => param.candidMeta.typeAliasDeclarations + ); + const returnTypeDeclaration = returnCandid.candidMeta.typeAliasDeclarations; + + if (useTypeDeclaration) { + return [ + ...paramTypeDeclarations, + ...returnTypeDeclaration, + `const ${name} = ${generateTypeAnnotation( + paramCandids, + returnCandid, + mode + )}` + ]; + } + + return [...paramTypeDeclarations, ...returnTypeDeclaration]; +} + +function generateTypeAnnotation( + paramCandids: CandidDefinition[], + returnCandid: CandidDefinition, + mode: Mode +): string { + const params = paramCandids + .map((param) => param.candidMeta.candidTypeObject) + .join(', '); + return `Func([${params}], ${returnCandid.candidMeta.candidTypeObject}, '${mode}')`; +} diff --git a/property_tests/arbitraries/candid/reference/func_arb/index.ts b/property_tests/arbitraries/candid/reference/func_arb/index.ts index 52c204e1cd..1e1d8c3a72 100644 --- a/property_tests/arbitraries/candid/reference/func_arb/index.ts +++ b/property_tests/arbitraries/candid/reference/func_arb/index.ts @@ -1,8 +1,20 @@ +import fc from 'fast-check'; import { Principal } from '@dfinity/principal'; -import { CandidTypeArb } from '../../candid_type_arb'; -import { FuncArb as Base } from './base'; +import { CandidDefinitionArb } from '../../candid_definition_arb'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { CandidDefinition } from '../../candid_definition_arb/types'; +import { FuncDefinitionArb } from './definition_arb'; +import { FuncValueArb } from './values_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; export type Func = [Principal, string]; -export const FuncArb = Base(CandidTypeArb); +export function FuncArb( + candidDefinitionArb: fc.Arbitrary = CandidDefinitionArb +): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator( + FuncDefinitionArb(candidDefinitionArb), + FuncValueArb + ); +} diff --git a/property_tests/arbitraries/candid/reference/func_arb/values_arb.ts b/property_tests/arbitraries/candid/reference/func_arb/values_arb.ts new file mode 100644 index 0000000000..956965ce53 --- /dev/null +++ b/property_tests/arbitraries/candid/reference/func_arb/values_arb.ts @@ -0,0 +1,22 @@ +import fc from 'fast-check'; +import { Func } from '.'; +import { TextArb } from '../../primitive/text'; +import { CandidValues } from '../../candid_values_arb'; +import { PrincipalArb } from '../principal_arb'; + +export function FuncValueArb(): fc.Arbitrary> { + return fc.tuple(TextArb(), PrincipalArb()).map(([name, principal]) => { + const value: Func = [ + principal.agentArgumentValue, + name.agentArgumentValue + ]; + + const valueLiteral = `[${principal.src.valueLiteral}, ${name.src.valueLiteral}]`; + + return { + valueLiteral, + agentArgumentValue: value, + agentResponseValue: value + }; + }); +} diff --git a/property_tests/arbitraries/candid/reference/principal_arb.ts b/property_tests/arbitraries/candid/reference/principal_arb.ts index caece7129e..74b2444298 100644 --- a/property_tests/arbitraries/candid/reference/principal_arb.ts +++ b/property_tests/arbitraries/candid/reference/principal_arb.ts @@ -1,15 +1,33 @@ import fc from 'fast-check'; import { Principal } from '@dfinity/principal'; -import { CandidMetaArb } from '../candid_arb'; import { principalToSrcLiteral } from '../to_src_literal/principal'; +import { SimpleCandidDefinitionArb } from '../simple_type_arbs/definition_arb'; +import { SimpleCandidValuesArb } from '../simple_type_arbs/values_arb'; +import { PrincipalCandidDefinition } from '../candid_definition_arb/types'; +import { CandidValues } from '../candid_values_arb'; +import { CandidValueAndMeta } from '../candid_value_and_meta_arb'; +import { CandidValueAndMetaArbGenerator } from '../candid_value_and_meta_arb_generator'; -export const PrincipalArb = CandidMetaArb( - fc +export function PrincipalArb(): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator( + PrincipalDefinitionArb(), + PrincipalValueArb + ); +} + +export function PrincipalDefinitionArb(): fc.Arbitrary { + return SimpleCandidDefinitionArb('Principal'); +} + +export function PrincipalValueArb(): fc.Arbitrary> { + return SimpleCandidValuesArb(principal(), principalToSrcLiteral); +} + +function principal() { + return fc .uint8Array({ minLength: 29, maxLength: 29 }) - .map((sample) => Principal.fromUint8Array(sample)), - 'Principal', - principalToSrcLiteral -); + .map((sample) => Principal.fromUint8Array(sample)); +} diff --git a/property_tests/arbitraries/candid/reference/service_arb.ts b/property_tests/arbitraries/candid/reference/service_arb.ts deleted file mode 100644 index 4bac0c608b..0000000000 --- a/property_tests/arbitraries/candid/reference/service_arb.ts +++ /dev/null @@ -1,112 +0,0 @@ -import fc from 'fast-check'; -import { Principal } from '@dfinity/principal'; - -import { PrincipalArb } from './principal_arb'; -import { VoidArb } from '../primitive/void'; -import { CandidMeta } from '../candid_arb'; -import { CandidTypeArb } from '../candid_type_arb'; -import { UniqueIdentifierArb } from '../../unique_identifier_arb'; -import { JsFunctionNameArb } from '../../js_function_name_arb'; - -// TODO: -// - services that are more than type-definitions, i.e. have functionality -// - async service methods -// - non-query methods -// - actually using the service - -// Example Service: -// const SomeService = Canister({ -// method1: query([], Void), -// method2: query([text, text, nat64], nat64), -// }); - -type ServiceMethod = { - name: string; - imports: Set; - typeDeclarations: string[]; - src: string; -}; - -const ServiceMethodArb = fc - .tuple( - JsFunctionNameArb, - fc.constantFrom('query', 'update'), - fc.array(CandidTypeArb), - fc.oneof(CandidTypeArb, VoidArb) - ) - .map(([name, mode, params, returnType]): ServiceMethod => { - const paramCandidTypes = params.map((param) => param.src.candidType); - - const typeDeclarations = params.reduce( - (acc, { src: { typeDeclaration } }) => { - return typeDeclaration ? [...acc, typeDeclaration] : acc; - }, - returnType.src.typeDeclaration - ? [returnType.src.typeDeclaration] - : new Array() - ); - - const src = `${name}: ${mode}([${paramCandidTypes}], ${returnType.src.candidType})`; - - const imports = params.reduce( - (acc, param) => { - return new Set([...acc, ...param.src.imports]); - }, - new Set([mode, ...returnType.src.imports]) - ); - - return { - name, - imports, - typeDeclarations, - src - }; - }); - -export const ServiceArb = fc - .tuple( - UniqueIdentifierArb('typeDeclaration'), - fc.uniqueArray(ServiceMethodArb, { selector: (entry) => entry.name }), - PrincipalArb - ) - .map(([name, serviceMethods, principal]): CandidMeta => { - const imports = new Set([ - ...serviceMethods.flatMap((method) => [...method.imports]), - 'Canister', - 'query' - ]); - - const typeDeclaration = generateTypeDeclaration(name, serviceMethods); - - const typeDeclarationAndChildren = [ - ...serviceMethods.flatMap((method) => method.typeDeclarations), - typeDeclaration - ].join('\n'); - - const valueLiteral = `${name}(${principal.src.valueLiteral})`; - - const value = principal.agentArgumentValue; - - return { - src: { - candidType: name, - typeDeclaration: typeDeclarationAndChildren, - imports, - valueLiteral - }, - agentArgumentValue: value, - agentResponseValue: value - }; - }); - -function generateTypeDeclaration( - name: string, - serviceMethods: ServiceMethod[] -): string { - const methods = serviceMethods - .map((serviceMethod) => serviceMethod.src) - .filter((typeDeclaration) => typeDeclaration) - .join(',\n'); - - return `const ${name} = Canister({${methods}});`; -} diff --git a/property_tests/arbitraries/candid/reference/service_arb/definition_arb.ts b/property_tests/arbitraries/candid/reference/service_arb/definition_arb.ts new file mode 100644 index 0000000000..5aa463e5a9 --- /dev/null +++ b/property_tests/arbitraries/candid/reference/service_arb/definition_arb.ts @@ -0,0 +1,125 @@ +import fc from 'fast-check'; +import { JsFunctionNameArb } from '../../../js_function_name_arb'; +import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; +import { + CandidDefinition, + ServiceCandidDefinition, + ServiceMethodDefinition +} from '../../candid_definition_arb/types'; +import { VoidDefinitionArb } from '../../primitive/void'; + +export function ServiceDefinitionArb( + fieldCandidDefArb: fc.Arbitrary +): fc.Arbitrary { + return fc + .tuple( + UniqueIdentifierArb('typeDeclaration'), + fc.uniqueArray(ServiceMethodArb(fieldCandidDefArb), { + selector: (entry) => entry.name + }), + fc.boolean() + ) + .map(([name, fields, useTypeDeclaration]): ServiceCandidDefinition => { + useTypeDeclaration = true; + const typeAnnotation = useTypeDeclaration + ? name + : generateTypeAnnotation(fields); + + const typeAliasDeclarations = generateTypeAliasDeclarations( + name, + fields, + useTypeDeclaration + ); + + const imports = generateImports(fields); + + return { + name, + candidMeta: { + candidTypeObject: typeAnnotation, + typeAliasDeclarations, + imports, + candidType: 'Service' + }, + funcs: fields + }; + }); +} + +function ServiceMethodArb( + candidDefArb: fc.Arbitrary +): fc.Arbitrary { + return fc + .tuple( + JsFunctionNameArb, + fc.constantFrom('query', 'update'), + fc.array(candidDefArb), + fc.oneof(candidDefArb, VoidDefinitionArb()) + ) + .map(([name, mode, params, returnType]): ServiceMethodDefinition => { + const paramCandidTypes = params.map( + (param) => param.candidMeta.candidTypeObject + ); + + const typeAliasDeclarations = params.reduce( + (acc, { candidMeta: { typeAliasDeclarations } }): string[] => { + return [...acc, ...typeAliasDeclarations]; + }, + returnType.candidMeta.typeAliasDeclarations + ); + + const src = `${name}: ${mode}([${paramCandidTypes}], ${returnType.candidMeta.candidTypeObject})`; + + const imports = params.reduce( + (acc, param) => { + return new Set([...acc, ...param.candidMeta.imports]); + }, + new Set([mode, ...returnType.candidMeta.imports]) + ); + + return { + name, + imports, + typeAliasDeclarations, + src + }; + }); +} + +function generateTypeAliasDeclarations( + name: string, + serviceMethods: ServiceMethodDefinition[], + useTypeDeclaration: boolean +): string[] { + const serviceMethodTypeAliasDecls = serviceMethods.flatMap( + (serviceMethod) => serviceMethod.typeAliasDeclarations + ); + if (useTypeDeclaration) { + return [ + ...serviceMethodTypeAliasDecls, + `const ${name} = ${generateTypeAnnotation(serviceMethods)};` + ]; + } + return serviceMethodTypeAliasDecls; +} + +function generateTypeAnnotation(serviceMethods: ServiceMethodDefinition[]) { + const methods = serviceMethods + .map((serviceMethod) => serviceMethod.src) + .filter((typeDeclaration) => typeDeclaration) + .join(',\n'); + + return `Canister({${methods}})`; +} + +function generateImports( + serviceMethods: ServiceMethodDefinition[] +): Set { + return new Set([ + ...serviceMethods.flatMap((serviceMethod) => + Array.from(serviceMethod.imports) + ), + 'Canister', + 'query' + ]); +} diff --git a/property_tests/arbitraries/candid/reference/service_arb/index.ts b/property_tests/arbitraries/candid/reference/service_arb/index.ts new file mode 100644 index 0000000000..28a9cbcc89 --- /dev/null +++ b/property_tests/arbitraries/candid/reference/service_arb/index.ts @@ -0,0 +1,30 @@ +import fc from 'fast-check'; +import { Principal } from '@dfinity/principal'; + +import { CandidDefinitionArb } from '../../candid_definition_arb'; +import { CandidValueAndMeta } from '../../candid_value_and_meta_arb'; +import { CandidDefinition } from '../../candid_definition_arb/types'; +import { ServiceValueArb } from './values_arb'; +import { ServiceDefinitionArb } from './definition_arb'; +import { CandidValueAndMetaArbGenerator } from '../../candid_value_and_meta_arb_generator'; + +// TODO: +// - services that are more than type-definitions, i.e. have functionality +// - async service methods +// - non-query methods +// - actually using the service + +// Example Service: +// const SomeService = Canister({ +// method1: query([], Void), +// method2: query([text, text, nat64], nat64), +// }); + +export function ServiceArb( + candidDefinitionArb: fc.Arbitrary = CandidDefinitionArb +): fc.Arbitrary> { + return CandidValueAndMetaArbGenerator( + ServiceDefinitionArb(candidDefinitionArb), + ServiceValueArb + ); +} diff --git a/property_tests/arbitraries/candid/reference/service_arb/values_arb.ts b/property_tests/arbitraries/candid/reference/service_arb/values_arb.ts new file mode 100644 index 0000000000..5b5d84fa25 --- /dev/null +++ b/property_tests/arbitraries/candid/reference/service_arb/values_arb.ts @@ -0,0 +1,20 @@ +import { Principal } from '@dfinity/principal'; +import fc from 'fast-check'; +import { ServiceCandidDefinition } from '../../candid_definition_arb/types'; +import { CandidValues } from '../../candid_values_arb'; +import { PrincipalValueArb } from '../principal_arb'; + +export function ServiceValueArb( + serviceDefinition: ServiceCandidDefinition +): fc.Arbitrary> { + return PrincipalValueArb().map((principal) => { + const valueLiteral = `${serviceDefinition.name}(${principal.valueLiteral})`; + const value = principal.agentArgumentValue; + + return { + valueLiteral, + agentArgumentValue: value, + agentResponseValue: value + }; + }); +} diff --git a/property_tests/arbitraries/candid/simple_type_arbs/definition_arb.ts b/property_tests/arbitraries/candid/simple_type_arbs/definition_arb.ts new file mode 100644 index 0000000000..2bdd80ba28 --- /dev/null +++ b/property_tests/arbitraries/candid/simple_type_arbs/definition_arb.ts @@ -0,0 +1,39 @@ +import fc from 'fast-check'; +import { PrimitiveDefinition } from '../candid_definition_arb/types'; +import { SimpleCandidType } from '../candid_type'; +import { UniqueIdentifierArb } from '../../unique_identifier_arb'; + +export function SimpleCandidDefinitionArb( + candidType: SimpleCandidType +): fc.Arbitrary { + return fc + .tuple(UniqueIdentifierArb('typeDeclaration'), fc.boolean()) + .map(([name, useTypeDeclaration]) => { + const candidTypeObject = useTypeDeclaration ? name : candidType; + const imports = new Set([candidType]); + const typeAliasDeclarations = generateTypeAliasDeclarations( + name, + candidType, + useTypeDeclaration + ); + return { + candidMeta: { + candidType, + candidTypeObject, + imports, + typeAliasDeclarations + } + }; + }); +} + +function generateTypeAliasDeclarations( + name: string, + candidType: string, + useTypeDeclaration: boolean +): string[] { + if (useTypeDeclaration) { + return [`const ${name} = ${candidType};`]; + } + return []; +} diff --git a/property_tests/arbitraries/candid/simple_type_arbs/values_arb.ts b/property_tests/arbitraries/candid/simple_type_arbs/values_arb.ts new file mode 100644 index 0000000000..cfd517045e --- /dev/null +++ b/property_tests/arbitraries/candid/simple_type_arbs/values_arb.ts @@ -0,0 +1,16 @@ +import fc from 'fast-check'; +import { CorrespondingJSType } from '../corresponding_js_type'; +import { CandidValues } from '../candid_values_arb'; + +export function SimpleCandidValuesArb( + arb: fc.Arbitrary, + toLiteral: (value: T) => string +): fc.Arbitrary> { + return arb.map((value): CandidValues => { + return { + valueLiteral: toLiteral(value), + agentArgumentValue: value, + agentResponseValue: value + }; + }); +} diff --git a/property_tests/arbitraries/query_method_arb.ts b/property_tests/arbitraries/query_method_arb.ts index 0147d8fec9..70649a74f8 100644 --- a/property_tests/arbitraries/query_method_arb.ts +++ b/property_tests/arbitraries/query_method_arb.ts @@ -1,11 +1,11 @@ import fc from 'fast-check'; -import { CandidMeta } from './candid/candid_arb'; import { CandidReturnType } from './candid/candid_return_type_arb'; -import { CandidType } from './candid/candid_type_arb'; +import { CorrespondingJSType } from './candid/corresponding_js_type'; import { UniqueIdentifierArb } from './unique_identifier_arb'; import { Test } from '../../test'; import { Named } from '../'; +import { CandidValueAndMeta } from './candid/candid_value_and_meta_arb'; export type QueryMethod = { imports: Set; @@ -15,31 +15,31 @@ export type QueryMethod = { }; export type BodyGenerator< - ParamAgentArgumentValue extends CandidType, + ParamAgentArgumentValue extends CorrespondingJSType, ParamAgentResponseValue, - ReturnTypeAgentArgumentValue extends CandidType, + ReturnTypeAgentArgumentValue extends CorrespondingJSType, ReturnTypeAgentResponseValue > = ( namedParams: Named< - CandidMeta + CandidValueAndMeta >[], - returnType: CandidMeta< + returnType: CandidValueAndMeta< ReturnTypeAgentArgumentValue, ReturnTypeAgentResponseValue > ) => string; export type TestsGenerator< - ParamAgentArgumentValue extends CandidType, + ParamAgentArgumentValue extends CorrespondingJSType, ParamAgentResponseValue, - ReturnTypeAgentArgumentValue extends CandidType, + ReturnTypeAgentArgumentValue extends CorrespondingJSType, ReturnTypeAgentResponseValue > = ( methodName: string, namedParams: Named< - CandidMeta + CandidValueAndMeta >[], - returnType: CandidMeta< + returnType: CandidValueAndMeta< ReturnTypeAgentArgumentValue, ReturnTypeAgentResponseValue > @@ -48,16 +48,19 @@ export type TestsGenerator< export type CallbackLocation = 'INLINE' | 'STANDALONE'; export function QueryMethodArb< - ParamAgentArgumentValue extends CandidType, + ParamAgentArgumentValue extends CorrespondingJSType, ParamAgentResponseValue, - ReturnTypeAgentArgumentValue extends CandidType, + ReturnTypeAgentArgumentValue extends CorrespondingJSType, ReturnTypeAgentResponseValue >( paramTypeArrayArb: fc.Arbitrary< - CandidMeta[] + CandidValueAndMeta[] >, returnTypeArb: fc.Arbitrary< - CandidMeta + CandidValueAndMeta< + ReturnTypeAgentArgumentValue, + ReturnTypeAgentResponseValue + > >, constraints: { generateBody: BodyGenerator< @@ -121,8 +124,10 @@ export function QueryMethodArb< ); const candidTypeDeclarations = [ - ...paramTypes.map((param) => param.src.typeDeclaration), - returnType.src.typeDeclaration + ...paramTypes.flatMap( + (param) => param.src.typeAliasDeclarations + ), + ...returnType.src.typeAliasDeclarations ].filter(isDefined); const globalDeclarations = @@ -158,13 +163,13 @@ function isDefined(value: T | undefined): value is T { } function generateCallback< - ParamType extends CandidType, + ParamType extends CorrespondingJSType, ParamAgentType, ReturnType extends CandidReturnType, ReturnAgentType >( - namedParams: Named>[], - returnType: CandidMeta, + namedParams: Named>[], + returnType: CandidValueAndMeta, generateBody: BodyGenerator< ParamType, ParamAgentType, @@ -192,21 +197,21 @@ function generateCallback< } function generateSourceCode< - ParamType extends CandidType, + ParamType extends CorrespondingJSType, ParamAgentType, ReturnType extends CandidReturnType, ReturnAgentType >( functionName: string, - paramTypes: CandidMeta[], - returnType: CandidMeta, + paramTypes: CandidValueAndMeta[], + returnType: CandidValueAndMeta, callback: string ): string { const paramCandidTypes = paramTypes - .map((param) => param.src.candidType) + .map((param) => param.src.candidTypeObject) .join(', '); - const returnCandidType = returnType.src.candidType; + const returnCandidType = returnType.src.candidTypeObject; return `${functionName}: query([${paramCandidTypes}], ${returnCandidType}, ${callback})`; } diff --git a/property_tests/are_params_correctly_ordered.ts b/property_tests/are_params_correctly_ordered.ts index b078f3972c..398dd31693 100644 --- a/property_tests/are_params_correctly_ordered.ts +++ b/property_tests/are_params_correctly_ordered.ts @@ -1,9 +1,9 @@ -import { CandidMeta } from './arbitraries/candid/candid_arb'; -import { CandidType } from './arbitraries/candid/candid_type_arb'; +import { CandidValueAndMeta } from './arbitraries/candid/candid_value_and_meta_arb'; +import { CorrespondingJSType } from './arbitraries/candid/corresponding_js_type'; import { Named } from '.'; -export function areParamsCorrectlyOrdered( - params: Named>[] +export function areParamsCorrectlyOrdered( + params: Named>[] ) { return params .map(({ name, el }) => { diff --git a/property_tests/tests/blob/test/generate_body.ts b/property_tests/tests/blob/test/generate_body.ts index bcc0f66e66..13864f6bc3 100644 --- a/property_tests/tests/blob/test/generate_body.ts +++ b/property_tests/tests/blob/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamBlobs: Named>[], - returnBlob: CandidMeta + namedParamBlobs: Named>[], + returnBlob: CandidValueAndMeta ): string { // TODO these checks should be much more precise probably, imagine checking the elements inside of the arrays const paramsAreUint8Arrays = namedParamBlobs diff --git a/property_tests/tests/blob/test/generate_tests.ts b/property_tests/tests/blob/test/generate_tests.ts index 739b609bb8..4b01823c76 100644 --- a/property_tests/tests/blob/test/generate_tests.ts +++ b/property_tests/tests/blob/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - paramBlobs: Named>[], - returnBlob: CandidMeta + paramBlobs: Named>[], + returnBlob: CandidValueAndMeta ): Test[] { const expectedResult = Uint8Array.from( paramBlobs diff --git a/property_tests/tests/blob/test/test.ts b/property_tests/tests/blob/test/test.ts index 4836a897bb..63535a7439 100644 --- a/property_tests/tests/blob/test/test.ts +++ b/property_tests/tests/blob/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllBlobsQueryMethod = QueryMethodArb(fc.array(BlobArb), BlobArb, { +const AllBlobsQueryMethod = QueryMethodArb(fc.array(BlobArb()), BlobArb(), { generateBody, generateTests }); diff --git a/property_tests/tests/bool/test/generate_body.ts b/property_tests/tests/bool/test/generate_body.ts index 28b40c0f80..1731b949f2 100644 --- a/property_tests/tests/bool/test/generate_body.ts +++ b/property_tests/tests/bool/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamBools: Named>[], - returnBool: CandidMeta + namedParamBools: Named>[], + returnBool: CandidValueAndMeta ): string { // TODO do we want to encapsulate 'boolean' in the CandidArb? Like an agentType instead of a candidType, like azleValue and agentValue? // TODO or will this not matter anymore once we start using a deep equal library diff --git a/property_tests/tests/bool/test/generate_tests.ts b/property_tests/tests/bool/test/generate_tests.ts index ca7062bea9..06fade587b 100644 --- a/property_tests/tests/bool/test/generate_tests.ts +++ b/property_tests/tests/bool/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamBools: Named>[], - returnBool: CandidMeta + namedParamBools: Named>[], + returnBool: CandidValueAndMeta ): Test[] { const expectedResult = namedParamBools.reduce( (acc, param) => acc && param.el.agentResponseValue, diff --git a/property_tests/tests/bool/test/test.ts b/property_tests/tests/bool/test/test.ts index d900fb9a5e..b842e2ce9b 100644 --- a/property_tests/tests/bool/test/test.ts +++ b/property_tests/tests/bool/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllBoolsQueryMethod = QueryMethodArb(fc.array(BoolArb), BoolArb, { +const AllBoolsQueryMethod = QueryMethodArb(fc.array(BoolArb()), BoolArb(), { generateBody, generateTests }); diff --git a/property_tests/tests/float32/test/generate_body.ts b/property_tests/tests/float32/test/generate_body.ts index edb9dda4c7..e30bddd47f 100644 --- a/property_tests/tests/float32/test/generate_body.ts +++ b/property_tests/tests/float32/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamFloat32s: Named>[], - returnFloat32: CandidMeta + namedParamFloat32s: Named>[], + returnFloat32: CandidValueAndMeta ): string { const paramsAreNumbers = namedParamFloat32s .map((param) => { diff --git a/property_tests/tests/float32/test/generate_tests.ts b/property_tests/tests/float32/test/generate_tests.ts index 466faf2884..e2e891e2b7 100644 --- a/property_tests/tests/float32/test/generate_tests.ts +++ b/property_tests/tests/float32/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamFloat32s: Named>[], - returnFloat32: CandidMeta + namedParamFloat32s: Named>[], + returnFloat32: CandidValueAndMeta ): Test[] { const expectedResult = namedParamFloat32s.length === 0 diff --git a/property_tests/tests/float32/test/test.ts b/property_tests/tests/float32/test/test.ts index a70655d041..0b17a293e0 100644 --- a/property_tests/tests/float32/test/test.ts +++ b/property_tests/tests/float32/test/test.ts @@ -9,8 +9,8 @@ import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; const AllFloat32sQueryMethod = QueryMethodArb( - fc.array(Float32Arb), - Float32Arb, + fc.array(Float32Arb()), + Float32Arb(), { generateBody, generateTests diff --git a/property_tests/tests/float64/test/generate_body.ts b/property_tests/tests/float64/test/generate_body.ts index bd8fa38ac7..5693308553 100644 --- a/property_tests/tests/float64/test/generate_body.ts +++ b/property_tests/tests/float64/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamFloat64s: Named>[], - returnFloat64: CandidMeta + namedParamFloat64s: Named>[], + returnFloat64: CandidValueAndMeta ): string { const paramsAreNumbers = namedParamFloat64s .map((param) => { diff --git a/property_tests/tests/float64/test/generate_tests.ts b/property_tests/tests/float64/test/generate_tests.ts index a232a6c8ab..9f1429ac9e 100644 --- a/property_tests/tests/float64/test/generate_tests.ts +++ b/property_tests/tests/float64/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamFloat64s: Named>[], - returnFloat64: CandidMeta + namedParamFloat64s: Named>[], + returnFloat64: CandidValueAndMeta ): Test[] { const count = namedParamFloat64s.length + 1; const expectedResult = diff --git a/property_tests/tests/float64/test/test.ts b/property_tests/tests/float64/test/test.ts index ee728dbf72..e58d2f1bf4 100644 --- a/property_tests/tests/float64/test/test.ts +++ b/property_tests/tests/float64/test/test.ts @@ -9,8 +9,8 @@ import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; const AllFloat64sQueryMethod = QueryMethodArb( - fc.array(Float64Arb), - Float64Arb, + fc.array(Float64Arb()), + Float64Arb(), { generateBody, generateTests diff --git a/property_tests/tests/func/test/generate_body.ts b/property_tests/tests/func/test/generate_body.ts index 2e5aa2fb8b..1a43c3195d 100644 --- a/property_tests/tests/func/test/generate_body.ts +++ b/property_tests/tests/func/test/generate_body.ts @@ -1,11 +1,11 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Func } from 'azle/property_tests/arbitraries/candid/reference/func_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamFuncs: Named>[], - returnFunc: CandidMeta + namedParamFuncs: Named>[], + returnFunc: CandidValueAndMeta ): string { const paramsAreFuncs = namedParamFuncs .map(({ name }) => { diff --git a/property_tests/tests/func/test/generate_tests.ts b/property_tests/tests/func/test/generate_tests.ts index 84d4149ce7..ee6e49c55f 100644 --- a/property_tests/tests/func/test/generate_tests.ts +++ b/property_tests/tests/func/test/generate_tests.ts @@ -1,14 +1,14 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Func } from 'azle/property_tests/arbitraries/candid/reference/func_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamFuncs: Named>[], - returnFunc: CandidMeta + namedParamFuncs: Named>[], + returnFunc: CandidValueAndMeta ): Test[] { return [ { diff --git a/property_tests/tests/func/test/test.ts b/property_tests/tests/func/test/test.ts index 6eae8b940a..a37bfa49c0 100644 --- a/property_tests/tests/func/test/test.ts +++ b/property_tests/tests/func/test/test.ts @@ -9,8 +9,10 @@ import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; const AllFuncsQueryMethod = QueryMethodArb( - fc.uniqueArray(FuncArb, { selector: (entry) => entry.src.candidType }), - FuncArb, + fc.uniqueArray(FuncArb(), { + selector: (entry) => entry.src.candidTypeObject + }), + FuncArb(), { generateBody, generateTests diff --git a/property_tests/tests/int/test/generate_body.ts b/property_tests/tests/int/test/generate_body.ts index d0d10b3515..88171062e8 100644 --- a/property_tests/tests/int/test/generate_body.ts +++ b/property_tests/tests/int/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamInts: Named>[], - returnInt: CandidMeta + namedParamInts: Named>[], + returnInt: CandidValueAndMeta ): string { const paramsAreBigInts = namedParamInts .map((param) => { diff --git a/property_tests/tests/int/test/generate_tests.ts b/property_tests/tests/int/test/generate_tests.ts index 136ac7206b..1b01b2a30b 100644 --- a/property_tests/tests/int/test/generate_tests.ts +++ b/property_tests/tests/int/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamInts: Named>[], - returnInt: CandidMeta + namedParamInts: Named>[], + returnInt: CandidValueAndMeta ): Test[] { const expectedResult = namedParamInts.reduce( (acc, param) => acc + param.el.agentResponseValue, diff --git a/property_tests/tests/int/test/test.ts b/property_tests/tests/int/test/test.ts index 4293bf6110..fbf73f70da 100644 --- a/property_tests/tests/int/test/test.ts +++ b/property_tests/tests/int/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllIntsQueryMethod = QueryMethodArb(fc.array(IntArb), IntArb, { +const AllIntsQueryMethod = QueryMethodArb(fc.array(IntArb()), IntArb(), { generateBody, generateTests }); diff --git a/property_tests/tests/int16/test/generate_body.ts b/property_tests/tests/int16/test/generate_body.ts index 8564972c07..e17bc72bfe 100644 --- a/property_tests/tests/int16/test/generate_body.ts +++ b/property_tests/tests/int16/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamInt16s: Named>[], - returnInt16: CandidMeta + namedParamInt16s: Named>[], + returnInt16: CandidValueAndMeta ): string { const paramsAreNumbers = namedParamInt16s .map((param) => { diff --git a/property_tests/tests/int16/test/generate_tests.ts b/property_tests/tests/int16/test/generate_tests.ts index a7d368f18f..1372b4352e 100644 --- a/property_tests/tests/int16/test/generate_tests.ts +++ b/property_tests/tests/int16/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamInt16s: Named>[], - returnInt16: CandidMeta + namedParamInt16s: Named>[], + returnInt16: CandidValueAndMeta ): Test[] { const count = namedParamInt16s.length + 1; const expectedResult = Math.floor( diff --git a/property_tests/tests/int16/test/test.ts b/property_tests/tests/int16/test/test.ts index 454adcb7eb..7fcc4063a1 100644 --- a/property_tests/tests/int16/test/test.ts +++ b/property_tests/tests/int16/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllInt16sQueryMethod = QueryMethodArb(fc.array(Int16Arb), Int16Arb, { +const AllInt16sQueryMethod = QueryMethodArb(fc.array(Int16Arb()), Int16Arb(), { generateBody, generateTests }); diff --git a/property_tests/tests/int32/test/generate_body.ts b/property_tests/tests/int32/test/generate_body.ts index 358e355a4d..16b9a29550 100644 --- a/property_tests/tests/int32/test/generate_body.ts +++ b/property_tests/tests/int32/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamInt32s: Named>[], - returnInt32: CandidMeta + namedParamInt32s: Named>[], + returnInt32: CandidValueAndMeta ): string { const paramsAreNumbers = namedParamInt32s .map((param) => { diff --git a/property_tests/tests/int32/test/generate_tests.ts b/property_tests/tests/int32/test/generate_tests.ts index ce108815cb..748ebae928 100644 --- a/property_tests/tests/int32/test/generate_tests.ts +++ b/property_tests/tests/int32/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamInt32s: Named>[], - returnInt32: CandidMeta + namedParamInt32s: Named>[], + returnInt32: CandidValueAndMeta ): Test[] { const count = namedParamInt32s.length + 1; const expectedResult = Math.floor( diff --git a/property_tests/tests/int32/test/test.ts b/property_tests/tests/int32/test/test.ts index e6f7bb0f99..97f6681d94 100644 --- a/property_tests/tests/int32/test/test.ts +++ b/property_tests/tests/int32/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllInt32sQueryMethod = QueryMethodArb(fc.array(Int32Arb), Int32Arb, { +const AllInt32sQueryMethod = QueryMethodArb(fc.array(Int32Arb()), Int32Arb(), { generateBody, generateTests }); diff --git a/property_tests/tests/int64/test/generate_body.ts b/property_tests/tests/int64/test/generate_body.ts index ca5308dcc6..90bafab51e 100644 --- a/property_tests/tests/int64/test/generate_body.ts +++ b/property_tests/tests/int64/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamInt64s: Named>[], - returnInt64: CandidMeta + namedParamInt64s: Named>[], + returnInt64: CandidValueAndMeta ): string { const paramsAreBigInts = namedParamInt64s .map((param) => { diff --git a/property_tests/tests/int64/test/generate_tests.ts b/property_tests/tests/int64/test/generate_tests.ts index 7023c94f05..958bd40c94 100644 --- a/property_tests/tests/int64/test/generate_tests.ts +++ b/property_tests/tests/int64/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamInt64s: Named>[], - returnInt64: CandidMeta + namedParamInt64s: Named>[], + returnInt64: CandidValueAndMeta ): Test[] { const count = namedParamInt64s.length + 1; const expectedResult = diff --git a/property_tests/tests/int64/test/test.ts b/property_tests/tests/int64/test/test.ts index 3dceb15bdc..4d5b2649b5 100644 --- a/property_tests/tests/int64/test/test.ts +++ b/property_tests/tests/int64/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllInt64sQueryMethod = QueryMethodArb(fc.array(Int64Arb), Int64Arb, { +const AllInt64sQueryMethod = QueryMethodArb(fc.array(Int64Arb()), Int64Arb(), { generateBody, generateTests }); diff --git a/property_tests/tests/int8/test/generate_body.ts b/property_tests/tests/int8/test/generate_body.ts index 61e93d39cf..86ec62ff08 100644 --- a/property_tests/tests/int8/test/generate_body.ts +++ b/property_tests/tests/int8/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamInt8s: Named>[], - returnInt8: CandidMeta + namedParamInt8s: Named>[], + returnInt8: CandidValueAndMeta ): string { const paramsAreNumbers = namedParamInt8s .map((param) => { diff --git a/property_tests/tests/int8/test/generate_tests.ts b/property_tests/tests/int8/test/generate_tests.ts index a0237976e5..43f5c8fc3b 100644 --- a/property_tests/tests/int8/test/generate_tests.ts +++ b/property_tests/tests/int8/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamInt8s: Named>[], - returnInt8: CandidMeta + namedParamInt8s: Named>[], + returnInt8: CandidValueAndMeta ): Test[] { const count = namedParamInt8s.length + 1; const expectedResult = Math.floor( diff --git a/property_tests/tests/int8/test/test.ts b/property_tests/tests/int8/test/test.ts index 7ee01da265..fd5909f68b 100644 --- a/property_tests/tests/int8/test/test.ts +++ b/property_tests/tests/int8/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllInt8sQueryMethod = QueryMethodArb(fc.array(Int8Arb), Int8Arb, { +const AllInt8sQueryMethod = QueryMethodArb(fc.array(Int8Arb()), Int8Arb(), { generateBody, generateTests }); diff --git a/property_tests/tests/nat/test/generate_body.ts b/property_tests/tests/nat/test/generate_body.ts index 5b0741b2da..fb4f9c7810 100644 --- a/property_tests/tests/nat/test/generate_body.ts +++ b/property_tests/tests/nat/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamNats: Named>[], - returnNat: CandidMeta + namedParamNats: Named>[], + returnNat: CandidValueAndMeta ): string { const paramsAreBigInts = namedParamNats .map((param) => { diff --git a/property_tests/tests/nat/test/generate_tests.ts b/property_tests/tests/nat/test/generate_tests.ts index 77ad9e4474..31a0047d76 100644 --- a/property_tests/tests/nat/test/generate_tests.ts +++ b/property_tests/tests/nat/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamNats: Named>[], - returnNat: CandidMeta + namedParamNats: Named>[], + returnNat: CandidValueAndMeta ): Test[] { const expectedResult = namedParamNats.reduce( (acc, param) => acc + param.el.agentResponseValue, diff --git a/property_tests/tests/nat/test/test.ts b/property_tests/tests/nat/test/test.ts index 0f8a528086..1f95ad31dc 100644 --- a/property_tests/tests/nat/test/test.ts +++ b/property_tests/tests/nat/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllNatsQueryMethod = QueryMethodArb(fc.array(NatArb), NatArb, { +const AllNatsQueryMethod = QueryMethodArb(fc.array(NatArb()), NatArb(), { generateBody, generateTests }); diff --git a/property_tests/tests/nat16/test/generate_body.ts b/property_tests/tests/nat16/test/generate_body.ts index 01fefac5b3..116fb89d0c 100644 --- a/property_tests/tests/nat16/test/generate_body.ts +++ b/property_tests/tests/nat16/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamNat16s: Named>[], - returnNat16: CandidMeta + namedParamNat16s: Named>[], + returnNat16: CandidValueAndMeta ): string { const paramsAreNumbers = namedParamNat16s .map((param) => { diff --git a/property_tests/tests/nat16/test/generate_tests.ts b/property_tests/tests/nat16/test/generate_tests.ts index 1bf2afbc33..6adc8ebffe 100644 --- a/property_tests/tests/nat16/test/generate_tests.ts +++ b/property_tests/tests/nat16/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamNat16s: Named>[], - returnNat16: CandidMeta + namedParamNat16s: Named>[], + returnNat16: CandidValueAndMeta ): Test[] { const count = namedParamNat16s.length + 1; const expectedResult = Math.floor( diff --git a/property_tests/tests/nat16/test/test.ts b/property_tests/tests/nat16/test/test.ts index dd11eb8e8a..19db5a3c0d 100644 --- a/property_tests/tests/nat16/test/test.ts +++ b/property_tests/tests/nat16/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllNat16sQueryMethod = QueryMethodArb(fc.array(Nat16Arb), Nat16Arb, { +const AllNat16sQueryMethod = QueryMethodArb(fc.array(Nat16Arb()), Nat16Arb(), { generateBody, generateTests }); diff --git a/property_tests/tests/nat32/test/generate_body.ts b/property_tests/tests/nat32/test/generate_body.ts index 2754c62538..c96a9cd2e3 100644 --- a/property_tests/tests/nat32/test/generate_body.ts +++ b/property_tests/tests/nat32/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamNat32s: Named>[], - returnNat32: CandidMeta + namedParamNat32s: Named>[], + returnNat32: CandidValueAndMeta ): string { const paramsAreNumbers = namedParamNat32s .map((param) => { diff --git a/property_tests/tests/nat32/test/generate_tests.ts b/property_tests/tests/nat32/test/generate_tests.ts index 90570ee9a7..c1f484d48c 100644 --- a/property_tests/tests/nat32/test/generate_tests.ts +++ b/property_tests/tests/nat32/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamNat32s: Named>[], - returnNat32: CandidMeta + namedParamNat32s: Named>[], + returnNat32: CandidValueAndMeta ): Test[] { const count = namedParamNat32s.length + 1; const expectedResult = Math.floor( diff --git a/property_tests/tests/nat32/test/test.ts b/property_tests/tests/nat32/test/test.ts index 5826beda5c..165b40fb44 100644 --- a/property_tests/tests/nat32/test/test.ts +++ b/property_tests/tests/nat32/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllNat32sQueryMethod = QueryMethodArb(fc.array(Nat32Arb), Nat32Arb, { +const AllNat32sQueryMethod = QueryMethodArb(fc.array(Nat32Arb()), Nat32Arb(), { generateBody, generateTests }); diff --git a/property_tests/tests/nat64/test/generate_body.ts b/property_tests/tests/nat64/test/generate_body.ts index 524c7bcde3..de3eaa9eba 100644 --- a/property_tests/tests/nat64/test/generate_body.ts +++ b/property_tests/tests/nat64/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamNat64s: Named>[], - returnNat64: CandidMeta + namedParamNat64s: Named>[], + returnNat64: CandidValueAndMeta ): string { const paramsAreBigInts = namedParamNat64s .map((param) => { diff --git a/property_tests/tests/nat64/test/generate_tests.ts b/property_tests/tests/nat64/test/generate_tests.ts index 963143ce2e..e6cb87ca50 100644 --- a/property_tests/tests/nat64/test/generate_tests.ts +++ b/property_tests/tests/nat64/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamNat64s: Named>[], - returnNat64: CandidMeta + namedParamNat64s: Named>[], + returnNat64: CandidValueAndMeta ): Test[] { const count = namedParamNat64s.length + 1; const expectedResult = diff --git a/property_tests/tests/nat64/test/test.ts b/property_tests/tests/nat64/test/test.ts index ee00f5889c..a845a8f4d3 100644 --- a/property_tests/tests/nat64/test/test.ts +++ b/property_tests/tests/nat64/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllNat64sQueryMethod = QueryMethodArb(fc.array(Nat64Arb), Nat64Arb, { +const AllNat64sQueryMethod = QueryMethodArb(fc.array(Nat64Arb()), Nat64Arb(), { generateBody, generateTests }); diff --git a/property_tests/tests/nat8/test/generate_body.ts b/property_tests/tests/nat8/test/generate_body.ts index 9591220de7..0b9479d261 100644 --- a/property_tests/tests/nat8/test/generate_body.ts +++ b/property_tests/tests/nat8/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamNat8s: Named>[], - returnNat8: CandidMeta + namedParamNat8s: Named>[], + returnNat8: CandidValueAndMeta ): string { const paramsAreNumbers = namedParamNat8s .map((param) => { diff --git a/property_tests/tests/nat8/test/generate_tests.ts b/property_tests/tests/nat8/test/generate_tests.ts index ac2937734c..60f29c0a2e 100644 --- a/property_tests/tests/nat8/test/generate_tests.ts +++ b/property_tests/tests/nat8/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamNat8s: Named>[], - returnNat8: CandidMeta + namedParamNat8s: Named>[], + returnNat8: CandidValueAndMeta ): Test[] { const count = namedParamNat8s.length + 1; const expectedResult = Math.floor( diff --git a/property_tests/tests/nat8/test/test.ts b/property_tests/tests/nat8/test/test.ts index 4d61c6f73f..29ace5f58b 100644 --- a/property_tests/tests/nat8/test/test.ts +++ b/property_tests/tests/nat8/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllNat8sQueryMethod = QueryMethodArb(fc.array(Nat8Arb), Nat8Arb, { +const AllNat8sQueryMethod = QueryMethodArb(fc.array(Nat8Arb()), Nat8Arb(), { generateBody, generateTests }); diff --git a/property_tests/tests/null/test/generate_body.ts b/property_tests/tests/null/test/generate_body.ts index 23db849a81..553eaeb441 100644 --- a/property_tests/tests/null/test/generate_body.ts +++ b/property_tests/tests/null/test/generate_body.ts @@ -1,9 +1,9 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; export function generateBody( - namedParamNulls: Named>[], - returnNull: CandidMeta + namedParamNulls: Named>[], + returnNull: CandidValueAndMeta ): string { const areAllNull = namedParamNulls.reduce((acc, { name }) => { return `${acc} && ${name} === null`; diff --git a/property_tests/tests/null/test/generate_tests.ts b/property_tests/tests/null/test/generate_tests.ts index c769d27dce..0c19d354d3 100644 --- a/property_tests/tests/null/test/generate_tests.ts +++ b/property_tests/tests/null/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamNulls: Named>[], - _returnNull: CandidMeta + namedParamNulls: Named>[], + _returnNull: CandidValueAndMeta ): Test[] { return [ { diff --git a/property_tests/tests/null/test/test.ts b/property_tests/tests/null/test/test.ts index ccd91f029c..bcaffcc72f 100644 --- a/property_tests/tests/null/test/test.ts +++ b/property_tests/tests/null/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllNullsQueryMethod = QueryMethodArb(fc.array(NullArb), NullArb, { +const AllNullsQueryMethod = QueryMethodArb(fc.array(NullArb()), NullArb(), { generateBody, generateTests }); diff --git a/property_tests/tests/opt/test/generate_body.ts b/property_tests/tests/opt/test/generate_body.ts index 9a837262b8..fe74aa8e7e 100644 --- a/property_tests/tests/opt/test/generate_body.ts +++ b/property_tests/tests/opt/test/generate_body.ts @@ -1,11 +1,11 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Opt } from 'azle/property_tests/arbitraries/candid/constructed/opt_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamOpts: Named>[], - returnOpt: CandidMeta + namedParamOpts: Named>[], + returnOpt: CandidValueAndMeta ): string { const areParamsOpts = namedParamOpts .map((param) => { diff --git a/property_tests/tests/opt/test/generate_tests.ts b/property_tests/tests/opt/test/generate_tests.ts index 9a8a4046bd..772d58eca0 100644 --- a/property_tests/tests/opt/test/generate_tests.ts +++ b/property_tests/tests/opt/test/generate_tests.ts @@ -1,14 +1,14 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Opt } from 'azle/property_tests/arbitraries/candid/constructed/opt_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamOpts: Named>[], - returnOpt: CandidMeta + namedParamOpts: Named>[], + returnOpt: CandidValueAndMeta ): Test[] { const expectedResult = returnOpt.agentResponseValue; diff --git a/property_tests/tests/opt/test/test.ts b/property_tests/tests/opt/test/test.ts index 890102c7cc..3a3920c8e8 100644 --- a/property_tests/tests/opt/test/test.ts +++ b/property_tests/tests/opt/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllOptsQueryMethod = QueryMethodArb(fc.array(OptArb), OptArb, { +const AllOptsQueryMethod = QueryMethodArb(fc.array(OptArb()), OptArb(), { generateBody, generateTests }); diff --git a/property_tests/tests/principal/test/generate_body.ts b/property_tests/tests/principal/test/generate_body.ts index 3f4a75739c..4119207627 100644 --- a/property_tests/tests/principal/test/generate_body.ts +++ b/property_tests/tests/principal/test/generate_body.ts @@ -1,12 +1,12 @@ import { Principal } from '@dfinity/principal'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamPrincipals: Named>[], - returnPrincipal: CandidMeta + namedParamPrincipals: Named>[], + returnPrincipal: CandidValueAndMeta ): string { const paramsArePrincipals = namedParamPrincipals .map((param) => { diff --git a/property_tests/tests/principal/test/generate_tests.ts b/property_tests/tests/principal/test/generate_tests.ts index 87df7bc478..5433012364 100644 --- a/property_tests/tests/principal/test/generate_tests.ts +++ b/property_tests/tests/principal/test/generate_tests.ts @@ -2,13 +2,13 @@ import { Principal } from '@dfinity/principal'; import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamPrincipals: Named>[], - returnPrincipal: CandidMeta + namedParamPrincipals: Named>[], + returnPrincipal: CandidValueAndMeta ): Test[] { const expectedResult = namedParamPrincipals.length > 0 diff --git a/property_tests/tests/principal/test/test.ts b/property_tests/tests/principal/test/test.ts index 566d06dc17..e7da053967 100644 --- a/property_tests/tests/principal/test/test.ts +++ b/property_tests/tests/principal/test/test.ts @@ -9,8 +9,8 @@ import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; const AllPrincipalsQueryMethod = QueryMethodArb( - fc.array(PrincipalArb), - PrincipalArb, + fc.array(PrincipalArb()), + PrincipalArb(), { generateBody, generateTests diff --git a/property_tests/tests/query_methods/test/generate_body.ts b/property_tests/tests/query_methods/test/generate_body.ts index fcbb578333..7241c2e015 100644 --- a/property_tests/tests/query_methods/test/generate_body.ts +++ b/property_tests/tests/query_methods/test/generate_body.ts @@ -1,13 +1,12 @@ import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; -import { CandidType } from 'azle/property_tests/arbitraries/candid/candid_type_arb'; -import { BodyGenerator } from 'azle/property_tests/arbitraries/query_method_arb'; -import { CandidReturnType } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CorrespondingJSType } from '../../../arbitraries/candid/corresponding_js_type'; +import { CandidReturnType } from '../../../arbitraries/candid/candid_return_type_arb'; +import { CandidValueAndMeta } from '../../../arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; export function generateBody( - namedParams: Named>[], - returnType: CandidMeta + namedParams: Named>[], + returnType: CandidValueAndMeta ): string { const paramsAreCorrectlyOrdered = areParamsCorrectlyOrdered(namedParams); diff --git a/property_tests/tests/query_methods/test/generate_tests.ts b/property_tests/tests/query_methods/test/generate_tests.ts index c74d39beb2..a2b0f1ccf3 100644 --- a/property_tests/tests/query_methods/test/generate_tests.ts +++ b/property_tests/tests/query_methods/test/generate_tests.ts @@ -1,15 +1,15 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidType } from 'azle/property_tests/arbitraries/candid/candid_type_arb'; -import { CandidReturnType } from '../../../arbitraries/candid/candid_return_type_arb'; -import { CandidMeta } from '../../../arbitraries/candid/candid_arb'; +import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; +import { CandidReturnType } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - params: Named>[], - returnType: CandidMeta + params: Named>[], + returnType: CandidValueAndMeta ): Test[] { const paramValues = params.map((param) => param.el.agentArgumentValue); const expectedResult = returnType.agentResponseValue; diff --git a/property_tests/tests/query_methods/test/test.ts b/property_tests/tests/query_methods/test/test.ts index a178b210e2..875ba4ac74 100644 --- a/property_tests/tests/query_methods/test/test.ts +++ b/property_tests/tests/query_methods/test/test.ts @@ -1,7 +1,7 @@ import fc from 'fast-check'; import { runPropTests } from 'azle/property_tests'; -import { CandidTypeArb } from 'azle/property_tests/arbitraries/candid/candid_type_arb'; +import { CandidValueAndMetaArb } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CandidReturnTypeArb } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; import { CanisterArb } from 'azle/property_tests/arbitraries/canister_arb'; import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb'; @@ -16,8 +16,8 @@ import { generateTests } from './generate_tests'; // TODO update methods const HeterogeneousQueryMethod = QueryMethodArb( - fc.array(CandidTypeArb), - CandidReturnTypeArb, + fc.array(CandidValueAndMetaArb()), + CandidReturnTypeArb(), { generateBody, generateTests diff --git a/property_tests/tests/record/test/generate_body.ts b/property_tests/tests/record/test/generate_body.ts index 874a0ce634..c4f7d5e942 100644 --- a/property_tests/tests/record/test/generate_body.ts +++ b/property_tests/tests/record/test/generate_body.ts @@ -1,11 +1,11 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Record } from 'azle/property_tests/arbitraries/candid/constructed/record_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamRecords: Named>[], - returnRecord: CandidMeta + namedParamRecords: Named>[], + returnRecord: CandidValueAndMeta ): string { const paramsAreRecords = namedParamRecords .map((param) => { diff --git a/property_tests/tests/record/test/generate_tests.ts b/property_tests/tests/record/test/generate_tests.ts index 2439d78371..803eceb80a 100644 --- a/property_tests/tests/record/test/generate_tests.ts +++ b/property_tests/tests/record/test/generate_tests.ts @@ -1,14 +1,14 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Record } from 'azle/property_tests/arbitraries/candid/constructed/record_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamRecords: Named>[], - returnRecord: CandidMeta + namedParamRecords: Named>[], + returnRecord: CandidValueAndMeta ): Test[] { return [ { diff --git a/property_tests/tests/record/test/test.ts b/property_tests/tests/record/test/test.ts index 3605baa67d..8f8a29669a 100644 --- a/property_tests/tests/record/test/test.ts +++ b/property_tests/tests/record/test/test.ts @@ -8,9 +8,13 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllRecordsQueryMethod = QueryMethodArb(fc.array(RecordArb), RecordArb, { - generateBody, - generateTests -}); +const AllRecordsQueryMethod = QueryMethodArb( + fc.array(RecordArb()), + RecordArb(), + { + generateBody, + generateTests + } +); runPropTests(CanisterArb(AllRecordsQueryMethod)); diff --git a/property_tests/tests/service/test/generate_body.ts b/property_tests/tests/service/test/generate_body.ts index 4fd2908cbd..34fb22ecbd 100644 --- a/property_tests/tests/service/test/generate_body.ts +++ b/property_tests/tests/service/test/generate_body.ts @@ -1,11 +1,11 @@ import { Principal } from '@dfinity/principal'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; export function generateBody( - namedParamServices: Named>[], - returnService: CandidMeta + namedParamServices: Named>[], + returnService: CandidValueAndMeta ): string { const paramsAreServices = namedParamServices .map((param) => { diff --git a/property_tests/tests/service/test/generate_tests.ts b/property_tests/tests/service/test/generate_tests.ts index 6c2c59cc27..d3590a364f 100644 --- a/property_tests/tests/service/test/generate_tests.ts +++ b/property_tests/tests/service/test/generate_tests.ts @@ -1,14 +1,14 @@ import { Principal } from '@dfinity/principal'; import { execSync } from 'child_process'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamServices: Named>[], - returnService: CandidMeta + namedParamServices: Named>[], + returnService: CandidValueAndMeta ): Test[] { return [ { diff --git a/property_tests/tests/service/test/test.ts b/property_tests/tests/service/test/test.ts index 60b53fc161..02a42962b0 100644 --- a/property_tests/tests/service/test/test.ts +++ b/property_tests/tests/service/test/test.ts @@ -9,8 +9,10 @@ import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; const AllServicesQueryMethod = QueryMethodArb( - fc.uniqueArray(ServiceArb, { selector: (entry) => entry.src.candidType }), - ServiceArb, + fc.uniqueArray(ServiceArb(), { + selector: (entry) => entry.src.candidTypeObject + }), + ServiceArb(), { generateBody, generateTests diff --git a/property_tests/tests/text/test/generate_body.ts b/property_tests/tests/text/test/generate_body.ts index 3075ff364d..14cb6370d0 100644 --- a/property_tests/tests/text/test/generate_body.ts +++ b/property_tests/tests/text/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamTexts: Named>[], - returnText: CandidMeta + namedParamTexts: Named>[], + returnText: CandidValueAndMeta ): string { const paramsAreStrings = namedParamTexts .map((param) => { diff --git a/property_tests/tests/text/test/generate_tests.ts b/property_tests/tests/text/test/generate_tests.ts index 293d95c2bd..5b603660c7 100644 --- a/property_tests/tests/text/test/generate_tests.ts +++ b/property_tests/tests/text/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamTexts: Named>[], - returnTexts: CandidMeta + namedParamTexts: Named>[], + returnTexts: CandidValueAndMeta ): Test[] { const expectedResult = namedParamTexts.reduce( (acc, param) => acc + param.el.agentResponseValue, diff --git a/property_tests/tests/text/test/test.ts b/property_tests/tests/text/test/test.ts index 6a294917cd..e656cad545 100644 --- a/property_tests/tests/text/test/test.ts +++ b/property_tests/tests/text/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllTextsQueryMethod = QueryMethodArb(fc.array(TextArb), TextArb, { +const AllTextsQueryMethod = QueryMethodArb(fc.array(TextArb()), TextArb(), { generateBody, generateTests }); diff --git a/property_tests/tests/tuple/test/generate_body.ts b/property_tests/tests/tuple/test/generate_body.ts index a248a3f899..a304a1238c 100644 --- a/property_tests/tests/tuple/test/generate_body.ts +++ b/property_tests/tests/tuple/test/generate_body.ts @@ -1,4 +1,4 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Tuple, ReturnTuple @@ -7,8 +7,8 @@ import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamTuples: Named>[], - returnTuple: CandidMeta + namedParamTuples: Named>[], + returnTuple: CandidValueAndMeta ): string { const paramsAreTuples = namedParamTuples .map((param) => { diff --git a/property_tests/tests/tuple/test/generate_tests.ts b/property_tests/tests/tuple/test/generate_tests.ts index 87f0e0a2dd..b26960de6b 100644 --- a/property_tests/tests/tuple/test/generate_tests.ts +++ b/property_tests/tests/tuple/test/generate_tests.ts @@ -1,7 +1,7 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Tuple, ReturnTuple @@ -10,8 +10,8 @@ import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamTuples: Named>[], - returnTuple: CandidMeta + namedParamTuples: Named>[], + returnTuple: CandidValueAndMeta ): Test[] { const expectedResult = returnTuple.agentResponseValue; diff --git a/property_tests/tests/tuple/test/test.ts b/property_tests/tests/tuple/test/test.ts index 864983bd50..af82d4c8a3 100644 --- a/property_tests/tests/tuple/test/test.ts +++ b/property_tests/tests/tuple/test/test.ts @@ -9,8 +9,10 @@ import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; const AllTuplesQueryMethod = QueryMethodArb( - fc.uniqueArray(TupleArb, { selector: (entry) => entry.src.candidType }), - TupleArb, + fc.uniqueArray(TupleArb(), { + selector: (entry) => entry.src.candidTypeObject + }), + TupleArb(), { generateBody, generateTests diff --git a/property_tests/tests/variant/test/generate_body.ts b/property_tests/tests/variant/test/generate_body.ts index 48b8b8ca1d..c6c2ed6a69 100644 --- a/property_tests/tests/variant/test/generate_body.ts +++ b/property_tests/tests/variant/test/generate_body.ts @@ -1,11 +1,11 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Variant } from 'azle/property_tests/arbitraries/candid/constructed/variant_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamVariants: Named>[], - returnVariant: CandidMeta + namedParamVariants: Named>[], + returnVariant: CandidValueAndMeta ): string { const paramsAreVariants = namedParamVariants .map((param) => { diff --git a/property_tests/tests/variant/test/generate_tests.ts b/property_tests/tests/variant/test/generate_tests.ts index 50f1dcffae..4bf5262aa4 100644 --- a/property_tests/tests/variant/test/generate_tests.ts +++ b/property_tests/tests/variant/test/generate_tests.ts @@ -1,14 +1,14 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Variant } from 'azle/property_tests/arbitraries/candid/constructed/variant_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamVariants: Named>[], - returnVariant: CandidMeta + namedParamVariants: Named>[], + returnVariant: CandidValueAndMeta ): Test[] { const expectedResult = returnVariant.agentResponseValue; diff --git a/property_tests/tests/variant/test/test.ts b/property_tests/tests/variant/test/test.ts index cedb2ffe62..977f16646d 100644 --- a/property_tests/tests/variant/test/test.ts +++ b/property_tests/tests/variant/test/test.ts @@ -9,8 +9,10 @@ import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; const AllVariantsQueryMethod = QueryMethodArb( - fc.uniqueArray(VariantArb, { selector: (entry) => entry.src.candidType }), - VariantArb, + fc.uniqueArray(VariantArb(), { + selector: (entry) => entry.src.candidTypeObject + }), + VariantArb(), { generateBody, generateTests diff --git a/property_tests/tests/vec/test/generate_body.ts b/property_tests/tests/vec/test/generate_body.ts index fada3786de..5ebd339506 100644 --- a/property_tests/tests/vec/test/generate_body.ts +++ b/property_tests/tests/vec/test/generate_body.ts @@ -1,10 +1,10 @@ -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Named } from 'azle/property_tests'; import { areParamsCorrectlyOrdered } from 'azle/property_tests/are_params_correctly_ordered'; export function generateBody( - namedParamVecs: Named>[], - returnVec: CandidMeta + namedParamVecs: Named>[], + returnVec: CandidValueAndMeta ): string { const paramsAreArrays = namedParamVecs .map((param) => { diff --git a/property_tests/tests/vec/test/generate_tests.ts b/property_tests/tests/vec/test/generate_tests.ts index 7c95b8deae..1c0e7400a4 100644 --- a/property_tests/tests/vec/test/generate_tests.ts +++ b/property_tests/tests/vec/test/generate_tests.ts @@ -1,13 +1,13 @@ import { deepEqual } from 'fast-equals'; import { getActor, Named } from 'azle/property_tests'; -import { CandidMeta } from 'azle/property_tests/arbitraries/candid/candid_arb'; +import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Test } from 'azle/test'; export function generateTests( functionName: string, - namedParamVecs: Named>[], - returnVec: CandidMeta + namedParamVecs: Named>[], + returnVec: CandidValueAndMeta ): Test[] { const expectedResult = returnVec.agentResponseValue; diff --git a/property_tests/tests/vec/test/test.ts b/property_tests/tests/vec/test/test.ts index 3b5b4d94d2..0fc5742edb 100644 --- a/property_tests/tests/vec/test/test.ts +++ b/property_tests/tests/vec/test/test.ts @@ -8,7 +8,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/query_method_arb import { generateBody } from './generate_body'; import { generateTests } from './generate_tests'; -const AllVecsQueryMethod = QueryMethodArb(fc.array(VecArb), VecArb, { +const AllVecsQueryMethod = QueryMethodArb(fc.array(VecArb()), VecArb(), { generateBody, generateTests });