Skip to content

Commit

Permalink
Update VecArb to support Principals
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren committed Oct 24, 2023
1 parent d4fdbec commit 6d6c058
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
22 changes: 19 additions & 3 deletions property_tests/arbitraries/candid/constructed/vec_arb.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Principal } from '@dfinity/principal';
import fc from 'fast-check';
import { IntArb } from '../primitive/ints/int_arb';
import { Int8Arb } from '../primitive/ints/int8_arb';
Expand All @@ -9,6 +10,7 @@ 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';

// TODO look into making this recursive
// TODO we want to be able to have vecs of vecs
Expand All @@ -35,12 +37,26 @@ export const VecArb = fc.oneof(
.map((sample) => createVecArbWrapper(sample, 'Vec(nat32)')),
fc
.array(Nat64Arb)
.map((sample) => createVecArbWrapper(sample, 'Vec(nat64)'))
.map((sample) => createVecArbWrapper(sample, 'Vec(nat64)')),
fc
.array(PrincipalArb)
.map((sample) =>
createVecArbWrapper(
sample,
'Vec(Principal)',
(a: Principal, b: Principal) => a.toText() === b.toText()
)
)
);

function createVecArbWrapper(sample: any[], candidType: string) {
function createVecArbWrapper(
sample: any[],
candidType: string,
equalityCheck: (a: any, b: any) => boolean = (a, b) => a === b
) {
return {
vec: sample,
candidType
candidType,
equalityCheck
};
}
18 changes: 15 additions & 3 deletions property_tests/tests/vec/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const VecTestArb = fc

const expectedResult = vecWrappers[0]?.vec ?? [];

const equalityCheck =
vecWrappers[0]?.equalityCheck ?? ((a, b) => a === b);

return {
functionName,
imports: [
Expand All @@ -47,6 +50,7 @@ const VecTestArb = fc
'nat16',
'nat32',
'nat64',
'Principal',
'Vec'
],
paramCandidTypes: paramCandidTypes.join(', '),
Expand All @@ -69,7 +73,11 @@ const VecTestArb = fc
);

return {
Ok: primitiveArraysAreEqual(result, expectedResult)
Ok: primitiveArraysAreEqual(
result,
expectedResult,
equalityCheck
)
};
}
}
Expand All @@ -78,15 +86,19 @@ const VecTestArb = fc

runPropTests(VecTestArb);

function primitiveArraysAreEqual(arr1: any, arr2: any) {
function primitiveArraysAreEqual(
arr1: any,
arr2: any,
equalityCheck: (a: any, b: any) => boolean
) {
// Check if both arrays have the same length
if (arr1.length !== arr2.length) {
return false;
}

// Loop through each element to check for equality
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
if (!equalityCheck(arr1[i], arr2[i])) {
return false;
}
}
Expand Down

0 comments on commit 6d6c058

Please sign in to comment.