Skip to content

Commit

Permalink
add some final polish, add Vec() CandidType type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Dec 8, 2023
1 parent cb4dbb9 commit 25d18df
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 54 deletions.
17 changes: 13 additions & 4 deletions property_tests/tests/stable_b_tree_map/test/contains_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ function generateBody(stableBTreeMapName: string): string {
`;
}

function generateTests(functionName: string, param0Value: any): Test[][] {
function generateTests(
functionName: string,
keySampleAgentArgumentValue: StableBTreeMap['keySample']['agentArgumentValue']
): Test[][] {
return [
[
{
name: `containsKey after first deploy ${functionName}`,
test: async () => {
const actor = getActor('./tests/stable_b_tree_map/test');

const result = await actor[functionName](param0Value);
const result = await actor[functionName](
keySampleAgentArgumentValue
);

return {
Ok: deepEqual(result, true)
Expand All @@ -63,7 +68,9 @@ function generateTests(functionName: string, param0Value: any): Test[][] {
test: async () => {
const actor = getActor('./tests/stable_b_tree_map/test');

const result = await actor[functionName](param0Value);
const result = await actor[functionName](
keySampleAgentArgumentValue
);

return {
Ok: deepEqual(result, true)
Expand All @@ -77,7 +84,9 @@ function generateTests(functionName: string, param0Value: any): Test[][] {
test: async () => {
const actor = getActor('./tests/stable_b_tree_map/test');

const result = await actor[functionName](param0Value);
const result = await actor[functionName](
keySampleAgentArgumentValue
);

return {
Ok: deepEqual(result, false)
Expand Down
20 changes: 13 additions & 7 deletions property_tests/tests/stable_b_tree_map/test/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ function generateBody(stableBTreeMapName: string): string {

function generateTests(
functionName: string,
param0Value: any,
param1Value: any
keySampleAgentArgumentValue: StableBTreeMap['keySample']['agentArgumentValue'],
valueSampleAgentArgumentValue: StableBTreeMap['valueSample']['agentArgumentValue']
): Test[][] {
return [
[
Expand All @@ -55,10 +55,12 @@ function generateTests(
test: async () => {
const actor = getActor('./tests/stable_b_tree_map/test');

const result = await actor[functionName](param0Value);
const result = await actor[functionName](
keySampleAgentArgumentValue
);

return {
Ok: deepEqual(result, [param1Value])
Ok: deepEqual(result, [valueSampleAgentArgumentValue])
};
}
}
Expand All @@ -69,10 +71,12 @@ function generateTests(
test: async () => {
const actor = getActor('./tests/stable_b_tree_map/test');

const result = await actor[functionName](param0Value);
const result = await actor[functionName](
keySampleAgentArgumentValue
);

return {
Ok: deepEqual(result, [param1Value])
Ok: deepEqual(result, [valueSampleAgentArgumentValue])
};
}
}
Expand All @@ -83,7 +87,9 @@ function generateTests(
test: async () => {
const actor = getActor('./tests/stable_b_tree_map/test');

const result = await actor[functionName](param0Value);
const result = await actor[functionName](
keySampleAgentArgumentValue
);

return {
Ok: deepEqual(result, [])
Expand Down
8 changes: 4 additions & 4 deletions property_tests/tests/stable_b_tree_map/test/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ function generateBody(stableBTreeMapName: string): string {

function generateTests(
functionName: string,
param0Value: any,
param1Value: any
keySampleAgentArgumentValue: StableBTreeMap['keySample']['agentArgumentValue'],
valueSampleAgentArgumentValue: StableBTreeMap['valueSample']['agentArgumentValue']
): Test[][] {
return [
[
Expand All @@ -57,8 +57,8 @@ function generateTests(
const actor = getActor('./tests/stable_b_tree_map/test');

const result = await actor[functionName](
param0Value,
param1Value
keySampleAgentArgumentValue,
valueSampleAgentArgumentValue
);

return {
Expand Down
18 changes: 14 additions & 4 deletions property_tests/tests/stable_b_tree_map/test/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ function generateBody(stableBTreeMapName: string): string {

function generateTests(
functionName: string,
param0Value: any,
param1Value: any
keySampleAgentArgumentValue: StableBTreeMap['keySample']['agentArgumentValue'],
valueSampleAgentArgumentValue: StableBTreeMap['valueSample']['agentArgumentValue']
): Test[][] {
return [
[
Expand All @@ -58,7 +58,12 @@ function generateTests(
const result = await actor[functionName]();

return {
Ok: deepEqual(result, [[param0Value, param1Value]])
Ok: deepEqual(result, [
[
keySampleAgentArgumentValue,
valueSampleAgentArgumentValue
]
])
};
}
}
Expand All @@ -72,7 +77,12 @@ function generateTests(
const result = await actor[functionName]();

return {
Ok: deepEqual(result, [[param0Value, param1Value]])
Ok: deepEqual(result, [
[
keySampleAgentArgumentValue,
valueSampleAgentArgumentValue
]
])
};
}
}
Expand Down
20 changes: 9 additions & 11 deletions property_tests/tests/stable_b_tree_map/test/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { deepEqual } from 'fast-equals';
import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { CandidValueAndMeta } from '../../../arbitraries/candid/candid_value_and_meta_arb';
import { CorrespondingJSType } from '../../../arbitraries/candid/corresponding_js_type';
import { getArrayForCandidType, getArrayStringForCandidType } from './utils';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
Expand Down Expand Up @@ -48,7 +46,7 @@ function generateBody(

function generateTests(
functionName: string,
param0: CandidValueAndMeta<CorrespondingJSType>
keySample: StableBTreeMap['keySample']
): Test[][] {
return [
[
Expand All @@ -62,11 +60,11 @@ function generateTests(
return {
Ok: deepEqual(
getArrayForCandidType(
param0.src.candidTypeAnnotation
keySample.src.candidTypeAnnotation
).from(result),
getArrayForCandidType(
param0.src.candidTypeAnnotation
).from([param0.agentArgumentValue])
keySample.src.candidTypeAnnotation
).from([keySample.agentArgumentValue])
)
};
}
Expand All @@ -83,11 +81,11 @@ function generateTests(
return {
Ok: deepEqual(
getArrayForCandidType(
param0.src.candidTypeAnnotation
keySample.src.candidTypeAnnotation
).from(result),
getArrayForCandidType(
param0.src.candidTypeAnnotation
).from([param0.agentArgumentValue])
keySample.src.candidTypeAnnotation
).from([keySample.agentArgumentValue])
)
};
}
Expand All @@ -104,10 +102,10 @@ function generateTests(
return {
Ok: deepEqual(
getArrayForCandidType(
param0.src.candidTypeAnnotation
keySample.src.candidTypeAnnotation
).from(result),
getArrayForCandidType(
param0.src.candidTypeAnnotation
keySample.src.candidTypeAnnotation
).from([])
)
};
Expand Down
15 changes: 7 additions & 8 deletions property_tests/tests/stable_b_tree_map/test/remove.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import {
StableBTreeMap,
StableBTreeMapArb
} from '../../../arbitraries/stable_b_tree_map_arb';
import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
Expand Down Expand Up @@ -48,8 +45,8 @@ function generateBody(stableBTreeMapName: string): string {

function generateTests(
functionName: string,
param0Value: any,
param1Value: any
keySampleAgentArgumentValue: StableBTreeMap['keySample']['agentArgumentValue'],
valueSampleAgentArgumentValue: StableBTreeMap['valueSample']['agentArgumentValue']
): Test[][] {
return [
[],
Expand All @@ -59,10 +56,12 @@ function generateTests(
test: async () => {
const actor = getActor('./tests/stable_b_tree_map/test');

const result = await actor[functionName](param0Value);
const result = await actor[functionName](
keySampleAgentArgumentValue
);

return {
Ok: deepEqual(result, [param1Value])
Ok: deepEqual(result, [valueSampleAgentArgumentValue])
};
}
}
Expand Down
4 changes: 0 additions & 4 deletions property_tests/tests/stable_b_tree_map/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// TODO I need to figure out how to test the stable part of this
// TODO I am not sure I can do that without having the ability to create multiple
// TODO different kinds of methods per test

import fc from 'fast-check';

import { runPropTests } from '../../../../property_tests';
Expand Down
20 changes: 9 additions & 11 deletions property_tests/tests/stable_b_tree_map/test/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { getArrayForCandidType, getArrayStringForCandidType } from './utils';
import { CandidValueAndMeta } from '../../../arbitraries/candid/candid_value_and_meta_arb';
import { CorrespondingJSType } from '../../../arbitraries/candid/corresponding_js_type';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';

Expand Down Expand Up @@ -51,7 +49,7 @@ function generateBody(

function generateTests(
functionName: string,
param1: CandidValueAndMeta<CorrespondingJSType>
valueSample: StableBTreeMap['valueSample']
): Test[][] {
return [
[
Expand All @@ -65,11 +63,11 @@ function generateTests(
return {
Ok: deepEqual(
getArrayForCandidType(
param1.src.candidTypeAnnotation
valueSample.src.candidTypeAnnotation
).from(result),
getArrayForCandidType(
param1.src.candidTypeAnnotation
).from([param1.agentArgumentValue])
valueSample.src.candidTypeAnnotation
).from([valueSample.agentArgumentValue])
)
};
}
Expand All @@ -86,11 +84,11 @@ function generateTests(
return {
Ok: deepEqual(
getArrayForCandidType(
param1.src.candidTypeAnnotation
valueSample.src.candidTypeAnnotation
).from(result),
getArrayForCandidType(
param1.src.candidTypeAnnotation
).from([param1.agentArgumentValue])
valueSample.src.candidTypeAnnotation
).from([valueSample.agentArgumentValue])
)
};
}
Expand All @@ -107,10 +105,10 @@ function generateTests(
return {
Ok: deepEqual(
getArrayForCandidType(
param1.src.candidTypeAnnotation
valueSample.src.candidTypeAnnotation
).from(result),
getArrayForCandidType(
param1.src.candidTypeAnnotation
valueSample.src.candidTypeAnnotation
).from([])
)
};
Expand Down
30 changes: 29 additions & 1 deletion type_tests/candid/constructed/vec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
nat32,
nat16,
nat8,
Vec
Vec,
Record,
text
} from '../../../src/lib';
import {
AssertType,
Expand Down Expand Up @@ -156,3 +158,29 @@ export type TestVecTypeInt16 = AssertType<
>;

export type TestVecTypeInt8 = AssertType<NotAnyAndExact<Vec<int8>, Int8Array>>;

// Crude tests to ensure Vec only accepts CandidType

const VecRecord = Record({
id: text,
username: text
});

Vec(float32);
Vec(int16);
Vec(VecRecord);

// @ts-expect-error
Vec(5);

// @ts-expect-error
Vec('not CandidType');

// @ts-expect-error
Vec(null);

// @ts-expect-error
Vec(undefined);

// @ts-expect-error
Vec(Symbol('not CandidType'));

0 comments on commit 25d18df

Please sign in to comment.