Skip to content

Commit

Permalink
add type tests for opt and tuple, fix vec type test a bit, add Candid…
Browse files Browse the repository at this point in the history
…Type constraint to params for Opt and Tuple
  • Loading branch information
lastmjs committed Dec 8, 2023
1 parent 25d18df commit 9ca450c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib/candid/types/constructed/opt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function Some<T>(value: T) {
export const None = { None: null };

// TODO what happens if we pass something to Opt() that can't be converted to CandidClass?
export function Opt<T>(t: T) {
export function Opt<T extends CandidType>(t: T) {
return new AzleOpt<T>(t);
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/candid/types/constructed/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class AzleTuple<T extends any[]> {
}
}

export function Tuple<T extends any[]>(...types: T) {
export function Tuple<T extends CandidType[]>(...types: T) {
return new AzleTuple<T>(types);
}
export type Tuple<T> = T;
38 changes: 37 additions & 1 deletion type_tests/candid/constructed/opt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// TODO These tests are just for one type, float32
// TODO it will take a lot of effort (not that much though) to get all types tested with Vec

import { float32, Opt, RequireExactlyOne } from '../../../src/lib';
import {
float32,
int16,
Opt,
Record,
RequireExactlyOne,
text
} from '../../../src/lib';
import {
AssertType,
NotAnyAndExact,
Expand Down Expand Up @@ -51,3 +58,32 @@ export type TestTypeMappingTriple = AssertType<
}>
>
>;

// Crude tests to ensure Opt only accepts CandidType

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

Opt(float32);
Opt(int16);
Opt(OptRecord);

// @ts-expect-error
Opt({});

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

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

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

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

// @ts-expect-error
Opt(Symbol('not CandidType'));
32 changes: 31 additions & 1 deletion type_tests/candid/constructed/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
reserved,
text,
Tuple,
Void
Void,
Record
} from '../../../src/lib';
import { testCandidType, testSerializable } from '../../assert_type';

Expand Down Expand Up @@ -86,3 +87,32 @@ export const ExampleTupleInstance: typeof ExampleTuple.tsType = [
'',
undefined
];

// Crude tests to ensure Tuple only accepts CandidType

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

Tuple(float32);
Tuple(int16);
Tuple(TupleRecord);

// @ts-expect-error
Tuple({});

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

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

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

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

// @ts-expect-error
Tuple(Symbol('not CandidType'));
3 changes: 3 additions & 0 deletions type_tests/candid/constructed/vec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ Vec(float32);
Vec(int16);
Vec(VecRecord);

// @ts-expect-error
Vec({});

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

Expand Down

0 comments on commit 9ca450c

Please sign in to comment.