Skip to content

Commit

Permalink
Merge pull request #1425 from demergent-labs/property_test_candid_types
Browse files Browse the repository at this point in the history
Property test candid types
  • Loading branch information
lastmjs authored Nov 2, 2023
2 parents ff972ef + 4abdee5 commit bccef41
Show file tree
Hide file tree
Showing 113 changed files with 8,180 additions and 1,245 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ jobs:
"property_tests/tests/bool",
"property_tests/tests/float32",
"property_tests/tests/float64",
"property_tests/tests/func",
"property_tests/tests/int",
"property_tests/tests/int8",
"property_tests/tests/int16",
Expand All @@ -146,7 +147,10 @@ jobs:
"property_tests/tests/nat64",
"property_tests/tests/null",
"property_tests/tests/principal",
"property_tests/tests/record",
"property_tests/tests/text",
"property_tests/tests/tuple",
"property_tests/tests/variant",
"property_tests/tests/vec"
]
END
Expand Down Expand Up @@ -219,15 +223,15 @@ jobs:
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
shell: bash -l {0}
working-directory: ${{ matrix.example_directories }}
run: AZLE_NUM_PROPTEST_RUNS=10 npm test
run: AZLE_PROPTEST_NUM_RUNS=10 AZLE_PROPTEST_VERBOSE=true npm test
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && contains(github.head_ref, 'release--') }}
shell: bash -l {0}
working-directory: ${{ matrix.example_directories }}
run: AZLE_NUM_PROPTEST_RUNS=100 npm test
run: AZLE_PROPTEST_NUM_RUNS=100 AZLE_PROPTEST_VERBOSE=true npm test
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && (github.ref == 'refs/heads/main' && contains(github.event.head_commit.message, 'Merge pull request') && contains(github.event.head_commit.message, 'demergent-labs/release--')) }}
shell: bash -l {0}
working-directory: ${{ matrix.example_directories }}
run: AZLE_NUM_PROPTEST_RUNS=100 npm test
run: AZLE_PROPTEST_NUM_RUNS=100 AZLE_PROPTEST_VERBOSE=true npm test

check-basic-integration-tests-success:
needs: basic-integration-tests
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"eslint": "8.11.0",
"eslint-config-prettier": "8.5.0",
"fast-check": "^3.13.1",
"fast-equals": "5.0.1",
"husky": "7.0.4",
"lint-staged": "12.3.7",
"prettier": "^3.0.3"
Expand Down
34 changes: 34 additions & 0 deletions property_tests/arbitraries/candid/candid_arb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';
import { CandidType } from './candid_type_arb';

// TODO we're thinking that Candid is not the best name for this. What is better?
export type CandidMeta<T extends CandidType> = {
value: T;
src: {
candidType: string;
typeDeclaration?: string;
imports: Set<string>;
valueLiteral: string;
};
equals(a: T, b: T): boolean;
};

export const CandidMetaArb = <T extends CandidType>(
arb: fc.Arbitrary<T>,
candidType: string,
toLiteral: (value: T) => string,
equals: (a: T, b: T) => boolean = (a: T, b: T) => deepEqual(a, b)
) => {
return arb.map(
(value): CandidMeta<T> => ({
src: {
candidType,
imports: new Set([candidType]),
valueLiteral: toLiteral(value)
},
value,
equals
})
);
};
66 changes: 66 additions & 0 deletions property_tests/arbitraries/candid/candid_type_arb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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';
import { NullArb } from './primitive/null';
import { BoolArb } from './primitive/bool';
import { Principal } from '@dfinity/principal';
import { PrincipalArb } from './reference/principal_arb';
import { Float32Arb } from './primitive/floats/float32_arb';
import { Float64Arb } from './primitive/floats/float64_arb';
import { TextArb } from './primitive/text';
import { BlobArb } from './constructed/blob_arb';
import { CandidMeta } from './candid_arb';
import { Func } from './reference/func_arb';
import { Opt } from './constructed/opt_arb';
import { Variant } from './constructed/variant_arb';
import { Record } from './constructed/record_arb';
import { Tuple } from './constructed/tuple_arb';

export type CandidType =
| number
| bigint
| null
| boolean
| Principal
| Uint8Array
| string
| Func
| Opt
| Variant
| Record
| Tuple
| undefined;

/**
* An arbitrary representing all possible Candid types.
*
* **Note:** This currently only supports ints, nats, and null arbitraries
*/
export const CandidTypeArb: fc.Arbitrary<CandidMeta<CandidType>> = fc.oneof(
Float32Arb,
Float64Arb,
IntArb,
Int8Arb,
Int16Arb,
Int32Arb,
Int64Arb,
NatArb,
Nat8Arb,
Nat16Arb,
Nat32Arb,
Nat64Arb,
BoolArb,
NullArb,
TextArb,
PrincipalArb,
BlobArb
);
// TODO: This needs to support ALL valid candid types, including records, variants, etc.
15 changes: 11 additions & 4 deletions property_tests/arbitraries/candid/constructed/blob_arb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import fc from 'fast-check';
import { CandidMetaArb } from '../candid_arb';
import { blobToSrcLiteral } from '../to_src_literal/blob';

export const BlobArb = fc.tuple(
fc.uint8Array(),
fc.oneof(fc.constant('blob'), fc.constant('Vec(nat8)'))
);
export const BlobArb = fc
.oneof(
CandidMetaArb(fc.uint8Array(), 'Vec(nat8)', blobToSrcLiteral),
CandidMetaArb(fc.uint8Array(), 'blob', blobToSrcLiteral)
)
.map((sample) => ({
...sample,
src: { ...sample.src, imports: new Set(['blob', 'nat8', 'Vec']) }
}));
Loading

0 comments on commit bccef41

Please sign in to comment.