Skip to content

Commit

Permalink
Merge pull request #1451 from demergent-labs/stable_b_tree_map_proper…
Browse files Browse the repository at this point in the history
…ty_tests

Stable b tree map property tests
  • Loading branch information
lastmjs authored Dec 7, 2023
2 parents ebe5dbe + 55421da commit 956c200
Show file tree
Hide file tree
Showing 66 changed files with 2,385 additions and 191 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ jobs:
"property_tests/tests/principal",
"property_tests/tests/record",
"property_tests/tests/service",
"property_tests/tests/stable_b_tree_map",
"property_tests/tests/text",
"property_tests/tests/tuple",
"property_tests/tests/variant",
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ export type ServiceCandidDefinition = {
export type ServiceMethodDefinition = {
name: string;
imports: Set<string>;
typeAliasDeclarations: string[];
variableAliasDeclarations: string[];
src: string;
};

type CandidMeta = {
typeAnnotation: string; // Either a type reference or type literal
typeAliasDeclarations: string[];
candidTypeAnnotation: string; // Either a type reference or type literal
candidTypeObject: string;
variableAliasDeclarations: string[];
imports: Set<string>;
candidType: CandidType;
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export type CandidValueAndMeta<T extends CorrespondingJSType, E = T> = {
agentArgumentValue: T;
agentResponseValue: E;
src: {
typeAnnotation: string;
typeAliasDeclarations: string[];
candidTypeAnnotation: string;
candidTypeObject: string;
variableAliasDeclarations: string[];
imports: Set<string>;
valueLiteral: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ export function CandidValueAndMetaArbGenerator<
).map(
([
{
candidMeta: { typeAnnotation, typeAliasDeclarations, imports }
candidMeta: {
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports
}
},
{ agentArgumentValue, agentResponseValue, valueLiteral }
]) => {
return {
src: {
typeAnnotation,
typeAliasDeclarations,
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports,
valueLiteral
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ export function _VecNat8DefinitionArb(): fc.Arbitrary<BlobCandidDefinition> {
return fc
.tuple(UniqueIdentifierArb('typeDeclaration'), fc.boolean())
.map(([name, useTypeDeclaration]): BlobCandidDefinition => {
const typeAnnotation = 'Vec(nat8)';
const typeAliasDeclarations = useTypeDeclaration
? [`const ${name} = ${typeAnnotation}`]
const candidTypeAnnotation = 'Vec<nat8>';
const candidTypeObject = 'Vec(nat8)';
const variableAliasDeclarations = useTypeDeclaration
? [`const ${name} = ${candidTypeObject}`]
: [];
const imports = new Set(['Vec', 'nat8']);
return {
candidMeta: {
typeAnnotation,
typeAliasDeclarations,
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'blob'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,31 @@ export function OptDefinitionArb(
fc.boolean()
)
.map(([name, innerType, useTypeDeclaration]): OptCandidDefinition => {
const typeAnnotation = useTypeDeclaration
? name
: generateTypeAnnotation(innerType);
const candidTypeAnnotation = generateCandidTypeAnnotation(
useTypeDeclaration,
name,
innerType
);

const candidTypeObject = generateCandidTypeObject(
useTypeDeclaration,
name,
innerType
);

const typeAliasDeclarations = generateTypeAliasDeclarations(
const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
innerType,
useTypeDeclaration
innerType
);

const imports = generateImports(innerType);

return {
candidMeta: {
typeAnnotation,
typeAliasDeclarations,
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Opt'
},
Expand All @@ -39,22 +48,46 @@ export function OptDefinitionArb(
});
}

function generateTypeAliasDeclarations(
function generateVariableAliasDeclarations(
useTypeDeclaration: boolean,
name: string,
innerType: CandidDefinition,
useTypeDeclaration: boolean
innerType: CandidDefinition
): string[] {
if (useTypeDeclaration) {
return [
...innerType.candidMeta.typeAliasDeclarations,
`const ${name} = ${generateTypeAnnotation(innerType)};`
...innerType.candidMeta.variableAliasDeclarations,
`const ${name} = ${generateCandidTypeObject(
false,
name,
innerType
)};`
];
}
return innerType.candidMeta.typeAliasDeclarations;
return innerType.candidMeta.variableAliasDeclarations;
}

function generateTypeAnnotation(innerType: CandidDefinition): string {
return `Opt(${innerType.candidMeta.typeAnnotation})`;
function generateCandidTypeAnnotation(
useTypeDeclaration: boolean,
name: string,
innerType: CandidDefinition
): string {
if (useTypeDeclaration) {
return `typeof ${name}.tsType`;
}

return `Opt<${innerType.candidMeta.candidTypeAnnotation}>`;
}

function generateCandidTypeObject(
useTypeDeclaration: boolean,
name: string,
innerType: CandidDefinition
): string {
if (useTypeDeclaration === true) {
return name;
}

return `Opt(${innerType.candidMeta.candidTypeObject})`;
}

function generateImports(innerType: CandidDefinition): Set<string> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,31 @@ export function RecordDefinitionArb(
fc.boolean()
)
.map(([name, fields, useTypeDeclaration]): RecordCandidDefinition => {
const typeAnnotation = useTypeDeclaration
? name
: generateTypeAnnotation(fields);
const candidTypeAnnotation = generateCandidTypeAnnotation(
useTypeDeclaration,
name,
fields
);

const candidTypeObject = generateCandidTypeObject(
useTypeDeclaration,
name,
fields
);

const typeAliasDeclarations = generateTypeAliasDeclarations(
const variableAliasDeclarations = generateVariableAliasDeclarations(
useTypeDeclaration,
name,
fields,
useTypeDeclaration
fields
);

const imports = generateImports(fields);

return {
candidMeta: {
typeAnnotation,
typeAliasDeclarations,
candidTypeAnnotation,
candidTypeObject,
variableAliasDeclarations,
imports,
candidType: 'Record'
},
Expand All @@ -54,28 +63,53 @@ function generateImports(fields: Field[]): Set<string> {
return new Set([...fieldImports, 'Record']);
}

function generateTypeAnnotation(fields: Field[]): string {
function generateCandidTypeAnnotation(
useTypeDeclaration: boolean,
name: string,
fields: Field[]
): string {
if (useTypeDeclaration === true) {
return `typeof ${name}.tsType`;
}

return `{${fields
.map(
([fieldName, fieldDefinition]) =>
`${fieldName}: ${fieldDefinition.candidMeta.candidTypeAnnotation}`
)
.join(',')}}`;
}

function generateCandidTypeObject(
useTypeDeclaration: boolean,
name: string,
fields: Field[]
): string {
if (useTypeDeclaration === true) {
return name;
}

return `Record({${fields
.map(
([fieldName, fieldDefinition]) =>
`${fieldName}: ${fieldDefinition.candidMeta.typeAnnotation}`
`${fieldName}: ${fieldDefinition.candidMeta.candidTypeObject}`
)
.join(',')}})`;
}

function generateTypeAliasDeclarations(
function generateVariableAliasDeclarations(
useTypeDeclaration: boolean,
name: string,
fields: Field[],
useTypeDeclaration: boolean
fields: Field[]
): string[] {
const fieldTypeAliasDeclarations = fields.flatMap(
(field) => field[1].candidMeta.typeAliasDeclarations
const fieldVariableAliasDefinitions = fields.flatMap(
(field) => field[1].candidMeta.variableAliasDeclarations
);
if (useTypeDeclaration) {
return [
...fieldTypeAliasDeclarations,
`const ${name} = ${generateTypeAnnotation(fields)};`
...fieldVariableAliasDefinitions,
`const ${name} = ${generateCandidTypeObject(false, name, fields)};`
];
}
return fieldTypeAliasDeclarations;
return fieldVariableAliasDefinitions;
}
Loading

0 comments on commit 956c200

Please sign in to comment.