Skip to content

Commit

Permalink
Merge pull request #1410 from demergent-labs/property_tests_opt
Browse files Browse the repository at this point in the history
add property tests for opt
  • Loading branch information
lastmjs authored Oct 20, 2023
2 parents b9f4cda + f10f211 commit 7b9c07f
Show file tree
Hide file tree
Showing 7 changed files with 1,394 additions and 0 deletions.
73 changes: 73 additions & 0 deletions property_tests/arbitraries/candid/constructed/opt_arb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import fc from 'fast-check';
import { IntArb } from '../primitive/ints/int_arb';
import { Int8Arb } from '../primitive/ints/int8_arb';
import { Int16Arb } from '../primitive/ints/int16_arb';
import { Int32Arb } from '../primitive/ints/int32_arb';
import { Int64Arb } from '../primitive/ints/int64_arb';
import { NatArb } from '../primitive/nats/nat_arb';
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';

const InnerOptArb = (arb: fc.Arbitrary<any>) => {
return fc
.oneof(fc.constant('Some'), fc.constant('None'))
.chain((keySample) => {
return arb.map((innerValueSample) => {
if (keySample === 'Some') {
return {
Some: innerValueSample
};
} else {
return {
None: null
};
}
});
});
};

// TODO look into making this recursive
// TODO we want to be able to have opts of opts
// TODO we also need to add vecs in here
// TODO we need to add all constructed and reference types
export const OptArb = fc.oneof(
InnerOptArb(IntArb).map((sample) =>
createOptArbWrapper(sample, 'Opt(int)')
),
InnerOptArb(Int8Arb).map((sample) =>
createOptArbWrapper(sample, 'Opt(int8)')
),
InnerOptArb(Int16Arb).map((sample) =>
createOptArbWrapper(sample, 'Opt(int16)')
),
InnerOptArb(Int32Arb).map((sample) =>
createOptArbWrapper(sample, 'Opt(int32)')
),
InnerOptArb(Int64Arb).map((sample) =>
createOptArbWrapper(sample, 'Opt(int64)')
),
InnerOptArb(NatArb).map((sample) =>
createOptArbWrapper(sample, 'Opt(nat)')
),
InnerOptArb(Nat8Arb).map((sample) =>
createOptArbWrapper(sample, 'Opt(nat8)')
),
InnerOptArb(Nat16Arb).map((sample) =>
createOptArbWrapper(sample, 'Opt(nat16)')
),
InnerOptArb(Nat32Arb).map((sample) =>
createOptArbWrapper(sample, 'Opt(nat32)')
),
InnerOptArb(Nat64Arb).map((sample) =>
createOptArbWrapper(sample, 'Opt(nat64)')
)
);

function createOptArbWrapper(sample: any, candidType: string) {
return {
opt: sample,
candidType
};
}
1 change: 1 addition & 0 deletions property_tests/arbitraries/candid/constructed/vec_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Nat64Arb } from '../primitive/nats/nat64_arb';

// TODO look into making this recursive
// TODO we want to be able to have vecs of vecs
// TODO we need to add all constructed and reference types
export const VecArb = fc.oneof(
fc.array(IntArb).map((sample) => createVecArbWrapper(sample, 'Vec(int)')),
fc.array(Int8Arb).map((sample) => createVecArbWrapper(sample, 'Vec(int8)')),
Expand Down
16 changes: 16 additions & 0 deletions property_tests/tests/opt/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"canisters": {
"canister": {
"type": "custom",
"main": "src/index.ts",
"candid": "src/index.did",
"build": "npx azle canister",
"wasm": ".azle/canister/canister.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/canister",
"node_compatibility": true
}
}
}
}
Loading

0 comments on commit 7b9c07f

Please sign in to comment.