Skip to content

Commit

Permalink
improve vec typing
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Oct 27, 2023
1 parent e7d3890 commit 24d87dd
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 11 deletions.
4 changes: 2 additions & 2 deletions property_tests/arbitraries/candid/constructed/vec_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import { Float64Arb } from '../primitive/floats/float64_arb';
import { NullArb } from '../primitive/null';
import { TextArb } from '../primitive/text';

const VecInnerArb = (arb: fc.Arbitrary<Candid<any>>) => {
const VecInnerArb = <T>(arb: fc.Arbitrary<Candid<T>>) => {
return fc.tuple(fc.array(arb), arb).map(
([sample, src]): Candid<any[]> => ({
([sample, src]): Candid<T[]> => ({
value: sample.map((sample) => sample.value),
src: {
candidType: `Vec(${src.src.candidType})`,
Expand Down
10 changes: 2 additions & 8 deletions property_tests/tests/vec/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const VecTestArb = fc
.tuple(createUniquePrimitiveArb(JsFunctionNameArb), fc.array(VecArb))
.map(([functionName, vecs]): TestSample => {
const paramCandidTypes = vecs.map((vec) => vec.src.candidType);
const returnCandidType = vecs[0]?.src?.candidType ?? 'Vec(int8)';
const returnCandidType = vecs[0]?.src?.candidType ?? 'Vec(nat8)';
const paramNames = vecs.map((_, index) => `param${index}`);

// TODO these checks should be much more precise probably, imagine checking the elements inside of the arrays
Expand All @@ -29,7 +29,7 @@ const VecTestArb = fc
})
.join('\n');

const returnStatement = paramNames[0] ?? `[]`;
const returnStatement = paramNames[0] ?? `new Uint8Array()`;

const expectedResult = vecs[0]?.value ?? [];

Expand Down Expand Up @@ -59,14 +59,8 @@ const VecTestArb = fc
test: async () => {
const actor = getActor('./tests/vec/test');

console.log('Expected Result');
console.log(JSON.stringify(expectedResult, replacer));
const params = vecs.map((vec) => vec.value);
console.log('Params');
console.log(JSON.stringify(params, replacer));
const result = await actor[functionName](...params);
console.log('Actual Result');
console.log(JSON.stringify(result, replacer));

return {
Ok: primitiveArraysAreEqual(
Expand Down
2 changes: 1 addition & 1 deletion src/lib/candid/type_mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export type TypeMapping<T, RecursionLevel = 0> = RecursionLevel extends 10
: 10
>;
}
: T extends AzleVec<AzleNat8>
: T extends AzleVec<AzleNat8> // TODO this isn't correct. It's capturing everything that's an AzleVec not just AzleVec<AzleNat8>
? Uint8Array
: T extends AzleVec<infer U>
? TypeMapping<U>[]
Expand Down

0 comments on commit 24d87dd

Please sign in to comment.