From 9ca450c6c2e857f8c0143a35b76ad66f7088ae26 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Fri, 8 Dec 2023 11:35:56 -0600 Subject: [PATCH] add type tests for opt and tuple, fix vec type test a bit, add CandidType constraint to params for Opt and Tuple --- src/lib/candid/types/constructed/opt.ts | 2 +- src/lib/candid/types/constructed/tuple.ts | 2 +- type_tests/candid/constructed/opt.ts | 38 ++++++++++++++++++++++- type_tests/candid/constructed/tuple.ts | 32 ++++++++++++++++++- type_tests/candid/constructed/vec.ts | 3 ++ 5 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/lib/candid/types/constructed/opt.ts b/src/lib/candid/types/constructed/opt.ts index e64a71ed0a..f7d71e4a1f 100644 --- a/src/lib/candid/types/constructed/opt.ts +++ b/src/lib/candid/types/constructed/opt.ts @@ -25,7 +25,7 @@ export function Some(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) { +export function Opt(t: T) { return new AzleOpt(t); } diff --git a/src/lib/candid/types/constructed/tuple.ts b/src/lib/candid/types/constructed/tuple.ts index 57716356a6..050535f044 100644 --- a/src/lib/candid/types/constructed/tuple.ts +++ b/src/lib/candid/types/constructed/tuple.ts @@ -35,7 +35,7 @@ export class AzleTuple { } } -export function Tuple(...types: T) { +export function Tuple(...types: T) { return new AzleTuple(types); } export type Tuple = T; diff --git a/type_tests/candid/constructed/opt.ts b/type_tests/candid/constructed/opt.ts index 9ef54dcfb8..4584fe5719 100644 --- a/type_tests/candid/constructed/opt.ts +++ b/type_tests/candid/constructed/opt.ts @@ -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, @@ -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')); diff --git a/type_tests/candid/constructed/tuple.ts b/type_tests/candid/constructed/tuple.ts index 367080f37c..21c0887735 100644 --- a/type_tests/candid/constructed/tuple.ts +++ b/type_tests/candid/constructed/tuple.ts @@ -20,7 +20,8 @@ import { reserved, text, Tuple, - Void + Void, + Record } from '../../../src/lib'; import { testCandidType, testSerializable } from '../../assert_type'; @@ -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')); diff --git a/type_tests/candid/constructed/vec.ts b/type_tests/candid/constructed/vec.ts index 17156f5ef4..3e68639558 100644 --- a/type_tests/candid/constructed/vec.ts +++ b/type_tests/candid/constructed/vec.ts @@ -170,6 +170,9 @@ Vec(float32); Vec(int16); Vec(VecRecord); +// @ts-expect-error +Vec({}); + // @ts-expect-error Vec(5);