Skip to content

Commit

Permalink
Merge pull request #1442 from demergent-labs/property_test_deep_equal
Browse files Browse the repository at this point in the history
Property test deep equal
  • Loading branch information
lastmjs authored Nov 7, 2023
2 parents bccef41 + 64fd977 commit e419f88
Show file tree
Hide file tree
Showing 88 changed files with 541 additions and 4,967 deletions.
170 changes: 166 additions & 4 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"homepage": "https://github.com/demergent-labs/azle#readme",
"dependencies": {
"@dfinity/candid": "github:demergent-labs/candid#minimum_viable",
"@dfinity/principal": "^0.19.0",
"@dfinity/principal": "0.19.2",
"@swc/core": "^1.3.86",
"@types/uuid": "^9.0.4",
"buffer": "^6.0.3",
Expand All @@ -39,6 +39,7 @@
"uuid": "^9.0.1"
},
"devDependencies": {
"@dfinity/agent": "^0.19.2",
"@types/fs-extra": "9.0.13",
"eslint": "8.11.0",
"eslint-config-prettier": "8.5.0",
Expand Down
7 changes: 2 additions & 5 deletions property_tests/arbitraries/candid/candid_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ export type CandidMeta<T extends CandidType> = {
imports: Set<string>;
valueLiteral: string;
};
equals(a: T, b: T): boolean;
};

export const CandidMetaArb = <T extends CandidType>(
arb: fc.Arbitrary<T>,
candidType: string,
toLiteral: (value: T) => string,
equals: (a: T, b: T) => boolean = (a: T, b: T) => deepEqual(a, b)
toLiteral: (value: T) => string
) => {
return arb.map(
(value): CandidMeta<T> => ({
Expand All @@ -27,8 +25,7 @@ export const CandidMetaArb = <T extends CandidType>(
imports: new Set([candidType]),
valueLiteral: toLiteral(value)
},
value,
equals
value
})
);
};
10 changes: 9 additions & 1 deletion property_tests/arbitraries/candid/candid_type_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ export type CandidType =
| Variant
| Record
| Tuple
| undefined;
| undefined
| Int16Array
| Int32Array
| Int8Array
| BigInt64Array
| Uint16Array
| Uint32Array
| Uint8Array
| BigUint64Array;

/**
* An arbitrary representing all possible Candid types.
Expand Down
41 changes: 1 addition & 40 deletions property_tests/arbitraries/candid/constructed/opt_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export const OptArb = fc
imports: generateImports(recursiveOptArb),
valueLiteral: generateValueLiteral(recursiveOptArb)
},
value: generateValue(recursiveOptArb),
equals: (a, b) => areOptsEqual(getBaseEquals(recursiveOptArb), a, b)
value: generateValue(recursiveOptArb)
};
});

Expand Down Expand Up @@ -117,41 +116,3 @@ function calculateDepthAndValues(value: [any] | []): {
const result = calculateDepthAndValues(value[0]);
return { ...result, depth: result.depth + 1 };
}

function getBaseEquals(
recursiveOpt: RecursiveOpt<Base>
): (a: any, b: any) => boolean {
if ('base' in recursiveOpt) {
// base case
if (recursiveOpt.base.someOrNone === 'Some') {
return recursiveOpt.base.candid.equals;
} else {
return (a: null, b: null) => a === b;
}
} else {
return getBaseEquals(recursiveOpt.nextLayer);
}
}

function areOptsEqual(
equals: (a: any, b: any) => boolean,
opt1: any,
opt2: any
) {
const { depth: depth1, value: value1 } = calculateDepthAndValues(opt1);
const { depth: depth2, value: value2 } = calculateDepthAndValues(opt2);

if (depth1 !== depth2) {
return false;
}

if (isNone(value1) && isNone(value2)) {
return true;
}

return equals(value1, value2);
}

function isNone(value: any | []) {
return Array.isArray(value) && value.length === 0;
}
Loading

0 comments on commit e419f88

Please sign in to comment.