Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1497 post upgrade #1556

Merged
merged 8 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ jobs:
"property_tests/tests/nat64",
"property_tests/tests/null",
"property_tests/tests/opt",
"property_tests/tests/post_upgrade_method",
"property_tests/tests/principal",
"property_tests/tests/query_methods",
"property_tests/tests/record",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fc from 'fast-check';
import { CorrespondingJSType } from './corresponding_js_type';
import { CandidDefinition, WithShapes } from './candid_definition_arb/types';
import { CandidValueAndMeta } from './candid_value_and_meta_arb';
import { CandidValues } from './candid_values_arb';
import { CandidValueConstraints, CandidValues } from './candid_values_arb';
import { RecursiveShapes } from './recursive';

export function CandidValueAndMetaArbGenerator<
Expand All @@ -14,9 +14,9 @@ export function CandidValueAndMetaArbGenerator<
ValueArb: (
arb: D,
recursiveShapes: RecursiveShapes,
constraints?: any
constraints?: CandidValueConstraints
) => fc.Arbitrary<V>,
valueConstraints?: any
valueConstraints?: CandidValueConstraints
): fc.Arbitrary<CandidValueAndMeta<any>> {
return DefinitionArb.chain((candidDefinitionAndShapes) => {
const candidDefinition = candidDefinitionAndShapes.definition;
Expand All @@ -25,33 +25,35 @@ export function CandidValueAndMetaArbGenerator<
fc.constant(candidDefinition),
ValueArb(candidDefinition, recursiveShapes, valueConstraints)
);
}).map(
([
{
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
runtimeCandidTypeObject,
variableAliasDeclarations,
imports
}
},
{ agentArgumentValue, agentResponseValue, valueLiteral }
]) => {
return {
src: {
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports,
valueLiteral
},
value: {
agentArgumentValue,
agentResponseValue,
runtimeCandidTypeObject
}
};
}).map(([definition, value]) => {
return definitionAndValueToValueAndMeta(definition, value);
});
}

export function definitionAndValueToValueAndMeta(
definition: CandidDefinition,
value: CandidValues<CorrespondingJSType>
): CandidValueAndMeta<CorrespondingJSType> {
const {
candidTypeAnnotation,
candidTypeObject,
imports,
variableAliasDeclarations,
runtimeCandidTypeObject
} = definition.candidMeta;
const { valueLiteral, agentArgumentValue, agentResponseValue } = value;
return {
src: {
candidTypeAnnotation,
candidTypeObject,
imports,
valueLiteral,
variableAliasDeclarations
},
value: {
agentArgumentValue,
agentResponseValue,
runtimeCandidTypeObject
}
);
};
}
37 changes: 25 additions & 12 deletions property_tests/arbitraries/candid/candid_values_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ 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 { TextConstraints, TextValueArb } from './primitive/text';
import { NullValueArb } from './primitive/null';
import { Float32ValueArb } from './primitive/floats/float32_arb';
import { Float64ValueArb } from './primitive/floats/float64_arb';
import {
Float32Constraints,
Float32ValueArb
} from './primitive/floats/float32_arb';
import {
Float64Constraints,
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';
Expand Down Expand Up @@ -45,10 +51,17 @@ export type CandidValues<T extends CorrespondingJSType, E = T> = {
valueLiteral: string;
};

export interface CandidValueConstraints
extends TextConstraints,
Float32Constraints,
Float64Constraints {
depthLevel?: number;
}

export function CandidValueArb(
candidTypeMeta: CandidDefinition,
recursiveShapes: RecursiveShapes,
depthLevel: number
constraints: CandidValueConstraints
): fc.Arbitrary<CandidValues<CorrespondingJSType>> {
const candidType = candidTypeMeta.candidMeta.candidType;
if (candidType === 'blob') {
Expand All @@ -58,45 +71,45 @@ export function CandidValueArb(
return OptValuesArb(
candidTypeMeta as OptCandidDefinition,
recursiveShapes,
depthLevel
constraints
);
}
if (candidType === 'Record') {
return RecordValuesArb(
candidTypeMeta as RecordCandidDefinition,
recursiveShapes,
depthLevel
constraints
);
}
if (candidType === 'Tuple') {
return TupleValuesArb(
candidTypeMeta as TupleCandidDefinition,
recursiveShapes,
depthLevel
constraints
);
}
if (candidType === 'Variant') {
return VariantValuesArb(
candidTypeMeta as VariantCandidDefinition,
recursiveShapes,
depthLevel
constraints
);
}
if (candidType === 'Vec') {
return VecValuesArb(
candidTypeMeta as VecCandidDefinition,
recursiveShapes,
depthLevel
constraints
);
}
if (candidType === 'bool') {
return BoolValueArb();
}
if (candidType === 'float32') {
return Float32ValueArb();
return Float32ValueArb(undefined, undefined, constraints);
}
if (candidType === 'float64') {
return Float64ValueArb();
return Float64ValueArb(undefined, undefined, constraints);
}
if (candidType === 'int') {
return IntValueArb();
Expand Down Expand Up @@ -150,7 +163,7 @@ export function CandidValueArb(
return RecursiveNameValuesArb(
candidTypeMeta as RecursiveCandidName | RecursiveCandidDefinition,
recursiveShapes,
depthLevel
constraints
);
}
throw new Error('Unreachable');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@ 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';
import {
CandidValues,
CandidValueArb,
CandidValueConstraints
} from '../../candid_values_arb';
import { RecursiveShapes } from '../../recursive';
import { DEFAULT_VALUE_MAX_DEPTH } from '../../../config';

type SomeOrNone = 'Some' | 'None';

export function OptValuesArb(
optDefinition: OptCandidDefinition,
recursiveShapes: RecursiveShapes,
depthLevel: number
constraints: CandidValueConstraints = {
depthLevel: DEFAULT_VALUE_MAX_DEPTH
}
): fc.Arbitrary<CandidValues<Opt>> {
const depthLevel = constraints?.depthLevel ?? DEFAULT_VALUE_MAX_DEPTH;
if (depthLevel < 1) {
return fc.constant(generateNoneValue());
}
const innerValue = fc.tuple(
fc.constantFrom('Some', 'None') as fc.Arbitrary<SomeOrNone>,
CandidValueArb(optDefinition.innerType, recursiveShapes, depthLevel - 1)
CandidValueArb(optDefinition.innerType, recursiveShapes, {
...constraints,
depthLevel: depthLevel - 1
})
);

return innerValue.map(([someOrNone, innerType]) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import fc from 'fast-check';
import { Record } from './index';
import { RecordCandidDefinition } from '../../candid_definition_arb/types';
import { CandidValues, CandidValueArb } from '../../candid_values_arb';
import {
CandidValues,
CandidValueArb,
CandidValueConstraints
} from '../../candid_values_arb';
import { CorrespondingJSType } from '../../corresponding_js_type';
import { RecursiveShapes } from '../../recursive';
import { DEFAULT_VALUE_MAX_DEPTH } from '../../../config';

type Field = [string, CandidValues<CorrespondingJSType>];
type ArbField = [string, fc.Arbitrary<CandidValues<CorrespondingJSType>>];

export function RecordValuesArb(
recordDefinition: RecordCandidDefinition,
recursiveShapes: RecursiveShapes,
depthLevel: number
constraints?: CandidValueConstraints
): fc.Arbitrary<CandidValues<Record>> {
const depthLevel = constraints?.depthLevel ?? DEFAULT_VALUE_MAX_DEPTH;
const fieldValues = recordDefinition.innerTypes.map(([name, innerType]) => {
const result: ArbField = [
name,
CandidValueArb(innerType, recursiveShapes, depthLevel - 1)
CandidValueArb(innerType, recursiveShapes, {
...constraints,
depthLevel: depthLevel - 1
})
];
return result;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ 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';
import {
CandidValues,
CandidValueArb,
CandidValueConstraints
} from '../../candid_values_arb';
import { RecursiveShapes } from '../../recursive';
import { DEFAULT_VALUE_MAX_DEPTH } from '../../../config';

export function TupleValuesArb(
tupleDefinition: TupleCandidDefinition,
recursiveShapes: RecursiveShapes,
depthLevel: number
constraints?: CandidValueConstraints
): fc.Arbitrary<CandidValues<Tuple, ReturnTuple>> {
const depthLevel = constraints?.depthLevel ?? DEFAULT_VALUE_MAX_DEPTH;
const fieldValues = tupleDefinition.innerTypes.map((innerType) => {
const result: fc.Arbitrary<CandidValues<CorrespondingJSType>> =
CandidValueArb(innerType, recursiveShapes, depthLevel - 1);
CandidValueArb(innerType, recursiveShapes, {
...constraints,
depthLevel: depthLevel - 1
});
return result;
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import fc from 'fast-check';
import { Variant } from '.';
import { VariantCandidDefinition } from '../../candid_definition_arb/types';
import { CandidValues, CandidValueArb } from '../../candid_values_arb';
import {
CandidValues,
CandidValueArb,
CandidValueConstraints
} from '../../candid_values_arb';
import { CorrespondingJSType } from '../../corresponding_js_type';
import { RecursiveShapes } from '../../recursive';
import { DEFAULT_VALUE_MAX_DEPTH } from '../../../config';

type Field = [string, CandidValues<CorrespondingJSType>];

export function VariantValuesArb(
variantDefinition: VariantCandidDefinition,
recursiveShapes: RecursiveShapes,
depthLevel: number
constraints?: CandidValueConstraints
): fc.Arbitrary<CandidValues<Variant>> {
const depthLevel = constraints?.depthLevel ?? DEFAULT_VALUE_MAX_DEPTH;
return fc
.nat(depthLevel < 1 ? 0 : variantDefinition.innerTypes.length - 1)
.chain((randomIndex) => {
Expand All @@ -25,11 +31,10 @@ export function VariantValuesArb(

const [name, innerType] = variantDefinition.innerTypes[randomIndex];

const fieldValues = CandidValueArb(
innerType,
recursiveShapes,
depthLevel - 1
).map((values): Field => {
const fieldValues = CandidValueArb(innerType, recursiveShapes, {
...constraints,
depthLevel: depthLevel - 1
}).map((values): Field => {
return [name, values];
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import {
VariantCandidDefinition,
VecCandidDefinition
} from '../../candid_definition_arb/types';
import { CandidValues, CandidValueArb } from '../../candid_values_arb';
import {
CandidValues,
CandidValueArb,
CandidValueConstraints
} from '../../candid_values_arb';
import { RecursiveShapes } from '../../recursive';
import { DEFAULT_VALUE_MAX_DEPTH } from '../../../config';

/*
https://github.com/dfinity/candid/blob/491969f34dd791e51f69c5f8d3c6192ae405b839/spec/Candid.md#memory
Expand All @@ -24,8 +29,11 @@ const EMPTYISH_VEC_SIZE_LIMIT = 0;
export function VecValuesArb(
vecDefinition: VecCandidDefinition,
recursiveShapes: RecursiveShapes,
depthLevel: number
constraints: CandidValueConstraints = {
depthLevel: DEFAULT_VALUE_MAX_DEPTH
}
): fc.Arbitrary<CandidValues<Vec>> {
const depthLevel = constraints.depthLevel ?? DEFAULT_VALUE_MAX_DEPTH;
if (depthLevel < 1) {
return fc.constant(
generateEmptyVec(vecDefinition.innerType.candidMeta.candidType)
Expand All @@ -39,7 +47,10 @@ export function VecValuesArb(
.chain(([arrayTemplate, innerType]) =>
fc.tuple(
...arrayTemplate.map(() =>
CandidValueArb(innerType, recursiveShapes, depthLevel - 1)
CandidValueArb(innerType, recursiveShapes, {
...constraints,
depthLevel: depthLevel - 1
})
)
)
);
Expand Down
Loading
Loading