Skip to content

Commit

Permalink
fix principal check
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Nov 7, 2023
1 parent aaf52b0 commit f412b8e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 32 deletions.
61 changes: 40 additions & 21 deletions property_tests/arbitraries/candid/constructed/vec_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ const VecInnerArb = <T extends CandidType>(
return fc
.tuple(fc.array(arb), arb)
.map(([sample, src]): CandidMeta<Vec<T>> => {
const valueLiteral = generateValueLiteral(sample);
const valueLiteral = generateValueLiteral(
sample,
src.src.candidType
);

return {
value: generateValue(sample, src.src.candidType),
Expand All @@ -49,37 +52,23 @@ const VecInnerArb = <T extends CandidType>(
});
};

function toNumberArray<T extends CandidType>(array: T[]): number[] {
if (array.every((item) => typeof item === 'number')) {
return array as number[];
}
throw new Error('array is not a number array');
}

function toBigintArray<T extends CandidType>(array: T[]): bigint[] {
if (array.every((item) => typeof item === 'bigint')) {
return array as bigint[];
}
throw new Error('array is not a bigint array');
}

function generateValue<T extends CandidType>(
array: CandidMeta<T>[],
candidType: string
): Vec<T> {
const value = array.map((sample) => sample.value);

if (candidType === 'int8') {
return new Int8Array(toNumberArray(value));
return new Int8Array(value as number[]);
}
if (candidType === 'int16') {
return new Int16Array(toNumberArray(value));
return new Int16Array(value as number[]);
}
if (candidType === 'int32') {
return new Int32Array(toNumberArray(value));
return new Int32Array(value as number[]);
}
if (candidType === 'int64') {
return new BigInt64Array(toBigintArray(value));
return new BigInt64Array(value as bigint[]);
}
if (candidType === 'nat8') {
return new Uint8Array(value as number[]);
Expand All @@ -97,11 +86,41 @@ function generateValue<T extends CandidType>(
return value;
}

function generateValueLiteral<T extends CandidType>(sample: CandidMeta<T>[]) {
function generateValueLiteral<T extends CandidType>(
sample: CandidMeta<T>[],
candidType: string
) {
const valueLiterals = sample
.map((sample) => sample.src.valueLiteral)
.join(',');
return `[${valueLiterals}]`;

const valueLiteral = `[${valueLiterals}]`;

if (candidType === 'int32') {
return `Int32Array.from(${valueLiteral})`;
}

if (candidType === 'int16') {
return `Int16Array.from(${valueLiteral})`;
}

if (candidType === 'int8') {
return `Int8Array.from(${valueLiteral})`;
}

if (candidType === 'nat32') {
return `Uint32Array.from(${valueLiteral})`;
}

if (candidType === 'nat16') {
return `Uint16Array.from(${valueLiteral})`;
}

if (candidType === 'nat8') {
return `Uint8Array.from(${valueLiteral})`;
}

return valueLiteral;
}

export const VecArb = fc.oneof(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Principal } from '@dfinity/principal';

export function principalToSrcLiteral(value: Principal): string {
return `Principal.fromText('${value.toText()}')`;
return `DfinityPrincipal.fromText('${value.toText()}')`;
}
1 change: 1 addition & 0 deletions property_tests/arbitraries/canister_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function CanisterArb(testArb: fc.Arbitrary<TestSample>) {
sourceCode: `
import { Canister, query, ${imports.join(', ')} } from 'azle';
import { deepEqual } from 'fast-equals';
import { Principal as DfinityPrincipal } from '@dfinity/principal';
${candidTypeDeclarations}
Expand Down
7 changes: 1 addition & 6 deletions property_tests/tests/func/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,9 @@ function generateBody(
.map((func, paramIndex): string => {
const paramName = `param${paramIndex}`;

const [principal, methodName] = func.value;

const principalValueIsCorrect = `${paramName}[0].toText() === '${principal.toText()}'`;
const methodNameIsCorrect = `${paramName}[1] === '${methodName}'`;
const throwError = `throw new Error('${paramName} is incorrectly ordered')`;

const debug = `console.log(${paramName})\nconsole.log(${func.src.valueLiteral})\n`;
return `${debug}if (!deepEqual(${paramName}[0].toText(), ${func.src.valueLiteral}[0].toText())) ${throwError}`;
return `if (!deepEqual(${paramName}, ${func.src.valueLiteral})) ${throwError}`;
})
.join('\n');

Expand Down
2 changes: 1 addition & 1 deletion property_tests/tests/opt/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function generateBody(

const areParamsCorrectlyOrdered = paramNames
.map((paramName, index) => {
return `if (false && !deepEqual(${paramName}, ${paramLiterals[index]})) throw new Error('${paramName} is incorrectly ordered')`;
return `if (!deepEqual(${paramName}, ${paramLiterals[index]})) throw new Error('${paramName} is incorrectly ordered')`;
})
.join('\n');

Expand Down
4 changes: 2 additions & 2 deletions property_tests/tests/principal/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ function generateBody(
const paramsCorrectlyOrdered = paramNames
.map((paramName, index) => {
const areEqual = `deepEqual(
${paramName}.toText(),
${paramPrincipals[index].src.valueLiteral}.toText()
${paramName},
${paramPrincipals[index].src.valueLiteral}
)`;

return `if (!${areEqual}) throw new Error('${paramName} is incorrectly ordered')`;
Expand Down
2 changes: 1 addition & 1 deletion property_tests/tests/vec/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function generateTest(
test: async () => {
const actor = getActor('./tests/vec/test');

const params = paramVecs.map((vec) => [...vec.value]);
const params = paramVecs.map((vec) => vec.value);
const result = await actor[functionName](...params);

return {
Expand Down

0 comments on commit f412b8e

Please sign in to comment.