Skip to content

Commit

Permalink
change candid to candidMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Nov 2, 2023
1 parent 9dbc653 commit dfcc099
Show file tree
Hide file tree
Showing 49 changed files with 240 additions and 190 deletions.
6 changes: 3 additions & 3 deletions property_tests/arbitraries/candid/candid_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 Candid<T extends CandidType> = {
export type CandidMeta<T extends CandidType> = {
value: T;
src: {
candidType: string;
Expand All @@ -14,14 +14,14 @@ export type Candid<T extends CandidType> = {
equals(a: T, b: T): boolean;
};

export const CandidArb = <T extends CandidType>(
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): Candid<T> => ({
(value): CandidMeta<T> => ({
src: {
candidType,
imports: new Set([candidType]),
Expand Down
13 changes: 10 additions & 3 deletions property_tests/arbitraries/candid/candid_type_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ 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 { Candid } from './candid_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
Expand All @@ -30,14 +33,18 @@ export type CandidType =
| Uint8Array
| string
| Func
| Opt;
| 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<Candid<CandidType>> = fc.oneof(
export const CandidTypeArb: fc.Arbitrary<CandidMeta<CandidType>> = fc.oneof(
Float32Arb,
Float64Arb,
IntArb,
Expand Down
6 changes: 3 additions & 3 deletions property_tests/arbitraries/candid/constructed/blob_arb.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { CandidArb } from '../candid_arb';
import { CandidMetaArb } from '../candid_arb';
import { blobToSrcLiteral } from '../to_src_literal/blob';

export const BlobArb = fc
.oneof(
CandidArb(fc.uint8Array(), 'Vec(nat8)', blobToSrcLiteral),
CandidArb(fc.uint8Array(), 'blob', blobToSrcLiteral)
CandidMetaArb(fc.uint8Array(), 'Vec(nat8)', blobToSrcLiteral),
CandidMetaArb(fc.uint8Array(), 'blob', blobToSrcLiteral)
)
.map((sample) => ({
...sample,
Expand Down
6 changes: 3 additions & 3 deletions property_tests/arbitraries/candid/constructed/opt_arb.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { CandidType, CandidTypeArb } from '../candid_type_arb';
import { Candid } from '../candid_arb';
import { CandidMeta } from '../candid_arb';

type SomeOrNone = 'Some' | 'None';
type Base = {
someOrNone: SomeOrNone;
candid: Candid<CandidType>;
candid: CandidMeta<CandidType>;
};

type RecursiveOpt<T> = { base: T } | { nextLayer: RecursiveOpt<T> };
Expand Down Expand Up @@ -34,7 +34,7 @@ export const OptArb = fc
})
)
}))
.RecursiveOptArb.map((recursiveOptArb): Candid<Opt> => {
.RecursiveOptArb.map((recursiveOptArb): CandidMeta<Opt> => {
return {
src: {
candidType: generateCandidType(recursiveOptArb),
Expand Down
6 changes: 3 additions & 3 deletions property_tests/arbitraries/candid/constructed/record_arb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fc from 'fast-check';

import { Candid } from '../candid_arb';
import { CandidMeta } from '../candid_arb';
import { CandidType, CandidTypeArb } from '../candid_type_arb';
import { UniqueIdentifierArb } from '../../unique_identifier_arb';
import { JsFunctionNameArb } from '../../js_function_name_arb';
Expand All @@ -9,7 +9,7 @@ export type Record = {
[x: string]: CandidType;
};

type Field = [string, Candid<CandidType>];
type Field = [string, CandidMeta<CandidType>];

export const RecordArb = fc
.tuple(
Expand All @@ -18,7 +18,7 @@ export const RecordArb = fc
selector: (entry) => entry[0]
})
)
.map(([name, fields]): Candid<Record> => {
.map(([name, fields]): CandidMeta<Record> => {
const typeDeclaration = generateTypeDeclaration(name, fields);

const imports = generateImports(fields);
Expand Down
10 changes: 5 additions & 5 deletions property_tests/arbitraries/candid/constructed/tuple_arb.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import fc from 'fast-check';

import { Candid } from '../candid_arb';
import { CandidMeta } from '../candid_arb';
import { CandidType, CandidTypeArb } from '../candid_type_arb';
import { UniqueIdentifierArb } from '../../unique_identifier_arb';

export type Tuple = CandidType[];

export const TupleArb = fc
.tuple(UniqueIdentifierArb('typeDeclaration'), fc.array(CandidTypeArb))
.map(([name, fields]): Candid<Tuple> => {
.map(([name, fields]): CandidMeta<Tuple> => {
const innerTypes = fields.map((field) => field.src.candidType);

const typeDeclaration = `const ${name} = Tuple(${innerTypes.join(
Expand All @@ -35,12 +35,12 @@ export const TupleArb = fc
};
});

function generateImports(fields: Candid<CandidType>[]): Set<string> {
function generateImports(fields: CandidMeta<CandidType>[]): Set<string> {
const fieldImports = fields.flatMap((field) => [...field.src.imports]);
return new Set([...fieldImports, 'Tuple']);
}

function generateValueLiteral(fields: Candid<CandidType>[]) {
function generateValueLiteral(fields: CandidMeta<CandidType>[]) {
const fieldLiterals = fields
.map((field) => field.src.valueLiteral)
.join(',\n');
Expand All @@ -51,7 +51,7 @@ function generateValueLiteral(fields: Candid<CandidType>[]) {
}

function generateEqualsMethod(
fields: Candid<CandidType>[]
fields: CandidMeta<CandidType>[]
): (a: Tuple, b: Tuple) => boolean {
return (a: Tuple, b: Tuple): boolean => {
if (typeof a !== typeof b) {
Expand Down
6 changes: 3 additions & 3 deletions property_tests/arbitraries/candid/constructed/variant_arb.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fc from 'fast-check';
import { Candid } from '../candid_arb';
import { CandidMeta } from '../candid_arb';
import { CandidType, CandidTypeArb } from '../candid_type_arb';
import { UniqueIdentifierArb } from '../../unique_identifier_arb';
import { JsFunctionNameArb } from '../../js_function_name_arb';

export type Variant = {
[x: string]: CandidType;
};
type Field = [string, Candid<CandidType>];
type Field = [string, CandidMeta<CandidType>];

export const VariantArb = fc
.tuple(
Expand All @@ -20,7 +20,7 @@ export const VariantArb = fc
// an empty object.
})
)
.map(([name, fields]): Candid<Variant> => {
.map(([name, fields]): CandidMeta<Variant> => {
const randomIndex = Math.floor(Math.random() * fields.length);

const typeDeclaration = generateTypeDeclaration(name, fields);
Expand Down
39 changes: 22 additions & 17 deletions property_tests/arbitraries/candid/constructed/vec_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,45 @@ import { Nat16Arb } from '../primitive/nats/nat16_arb';
import { Nat32Arb } from '../primitive/nats/nat32_arb';
import { Nat64Arb } from '../primitive/nats/nat64_arb';
import { PrincipalArb } from '../reference/principal_arb';
import { Candid } from '../candid_arb';
import { CandidMeta } from '../candid_arb';
import { BoolArb } from '../primitive/bool';
import { Float32Arb } from '../primitive/floats/float32_arb';
import { Float64Arb } from '../primitive/floats/float64_arb';
import { NullArb } from '../primitive/null';
import { TextArb } from '../primitive/text';
import { deepEqual } from 'fast-equals';
import { BlobArb } from './blob_arb';
import { CandidType } from '../candid_type_arb';

const VecInnerArb = <T>(arb: fc.Arbitrary<Candid<T>>) => {
return fc.tuple(fc.array(arb), arb).map(([sample, src]): Candid<T[]> => {
const equals = generateEquals(sample);
const valueLiteral = generateValueLiteral(sample);
const VecInnerArb = <T extends CandidType>(
arb: fc.Arbitrary<CandidMeta<T>>
) => {
return fc
.tuple(fc.array(arb), arb)
.map(([sample, src]): CandidMeta<T[]> => {
const equals = generateEquals(sample);
const valueLiteral = generateValueLiteral(sample);

return {
value: sample.map((sample) => sample.value),
src: {
candidType: `Vec(${src.src.candidType})`,
imports: new Set([...src.src.imports, 'Vec']),
valueLiteral
},
equals
};
});
return {
value: sample.map((sample) => sample.value),
src: {
candidType: `Vec(${src.src.candidType})`,
imports: new Set([...src.src.imports, 'Vec']),
valueLiteral
},
equals
};
});
};

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

function generateEquals<T>(sample: Candid<T>[]) {
function generateEquals<T extends CandidType>(sample: CandidMeta<T>[]) {
return (a: T[], b: T[]) =>
arraysAreEqual(a, b, sample[0]?.equals ?? deepEqual);
}
Expand Down
4 changes: 2 additions & 2 deletions property_tests/arbitraries/candid/primitive/bool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fc from 'fast-check';
import { CandidArb } from '../candid_arb';
import { CandidMetaArb } from '../candid_arb';
import { booleanToSrcLiteral } from '../to_src_literal/boolean';

export const BoolArb = CandidArb(fc.boolean(), 'bool', booleanToSrcLiteral);
export const BoolArb = CandidMetaArb(fc.boolean(), 'bool', booleanToSrcLiteral);
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import fc from 'fast-check';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { floatToSrcLiteral } from '../../to_src_literal/float';

export const Float32Arb = CandidArb(fc.float(), 'float32', floatToSrcLiteral);
export const Float32Arb = CandidMetaArb(
fc.float(),
'float32',
floatToSrcLiteral
);
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fc from 'fast-check';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { floatToSrcLiteral } from '../../to_src_literal/float';

export const Float64Arb = CandidArb(
export const Float64Arb = CandidMetaArb(
fc.float64Array({ maxLength: 1, minLength: 1 }).map((sample) => sample[0]),
'float64',
floatToSrcLiteral
Expand Down
8 changes: 6 additions & 2 deletions property_tests/arbitraries/candid/primitive/ints/int16_arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { numberToSrcLiteral } from '../../to_src_literal/number';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { NumberArb } from './';

export const Int16Arb = CandidArb(NumberArb(16), 'int16', numberToSrcLiteral);
export const Int16Arb = CandidMetaArb(
NumberArb(16),
'int16',
numberToSrcLiteral
);
8 changes: 6 additions & 2 deletions property_tests/arbitraries/candid/primitive/ints/int32_arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { numberToSrcLiteral } from '../../to_src_literal/number';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { NumberArb } from './';

export const Int32Arb = CandidArb(NumberArb(32), 'int32', numberToSrcLiteral);
export const Int32Arb = CandidMetaArb(
NumberArb(32),
'int32',
numberToSrcLiteral
);
8 changes: 6 additions & 2 deletions property_tests/arbitraries/candid/primitive/ints/int64_arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import fc from 'fast-check';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { bigintToSrcLiteral } from '../../to_src_literal/bigint';

export const Int64Arb = CandidArb(fc.bigIntN(64), 'int64', bigintToSrcLiteral);
export const Int64Arb = CandidMetaArb(
fc.bigIntN(64),
'int64',
bigintToSrcLiteral
);
4 changes: 2 additions & 2 deletions property_tests/arbitraries/candid/primitive/ints/int8_arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { numberToSrcLiteral } from '../../to_src_literal/number';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { NumberArb } from './';

export const Int8Arb = CandidArb(NumberArb(8), 'int8', numberToSrcLiteral);
export const Int8Arb = CandidMetaArb(NumberArb(8), 'int8', numberToSrcLiteral);
4 changes: 2 additions & 2 deletions property_tests/arbitraries/candid/primitive/ints/int_arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fc from 'fast-check';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { bigintToSrcLiteral } from '../../to_src_literal/bigint';

export const IntArb = CandidArb(fc.bigInt(), 'int', bigintToSrcLiteral);
export const IntArb = CandidMetaArb(fc.bigInt(), 'int', bigintToSrcLiteral);
8 changes: 6 additions & 2 deletions property_tests/arbitraries/candid/primitive/nats/nat16_arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { numberToSrcLiteral } from '../../to_src_literal/number';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { UNumberArb } from './index';

export const Nat16Arb = CandidArb(UNumberArb(16), 'nat16', numberToSrcLiteral);
export const Nat16Arb = CandidMetaArb(
UNumberArb(16),
'nat16',
numberToSrcLiteral
);
8 changes: 6 additions & 2 deletions property_tests/arbitraries/candid/primitive/nats/nat32_arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { numberToSrcLiteral } from '../../to_src_literal/number';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { UNumberArb } from './index';

export const Nat32Arb = CandidArb(UNumberArb(32), 'nat32', numberToSrcLiteral);
export const Nat32Arb = CandidMetaArb(
UNumberArb(32),
'nat32',
numberToSrcLiteral
);
8 changes: 6 additions & 2 deletions property_tests/arbitraries/candid/primitive/nats/nat64_arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import fc from 'fast-check';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { bigintToSrcLiteral } from '../../to_src_literal/bigint';

export const Nat64Arb = CandidArb(fc.bigUintN(64), 'nat64', bigintToSrcLiteral);
export const Nat64Arb = CandidMetaArb(
fc.bigUintN(64),
'nat64',
bigintToSrcLiteral
);
4 changes: 2 additions & 2 deletions property_tests/arbitraries/candid/primitive/nats/nat8_arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { numberToSrcLiteral } from '../../to_src_literal/number';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { UNumberArb } from './index';

export const Nat8Arb = CandidArb(UNumberArb(8), 'nat8', numberToSrcLiteral);
export const Nat8Arb = CandidMetaArb(UNumberArb(8), 'nat8', numberToSrcLiteral);
4 changes: 2 additions & 2 deletions property_tests/arbitraries/candid/primitive/nats/nat_arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fc from 'fast-check';
import { CandidArb } from '../../candid_arb';
import { CandidMetaArb } from '../../candid_arb';
import { bigintToSrcLiteral } from '../../to_src_literal/bigint';

export const NatArb = CandidArb(fc.bigUint(), 'nat', bigintToSrcLiteral);
export const NatArb = CandidMetaArb(fc.bigUint(), 'nat', bigintToSrcLiteral);
Loading

0 comments on commit dfcc099

Please sign in to comment.