Skip to content

Commit d950e3f

Browse files
committed
basic tests of variant defaulting under opt
1 parent da86365 commit d950e3f

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/candid/src/idl.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,3 +698,31 @@ test('should decode matching optional fields if wire type contains additional fi
698698
b: ['123'],
699699
});
700700
});
701+
702+
703+
test('IDL opt variant decoding', () => {
704+
testDecode(
705+
IDL.Record({a: IDL.Opt(IDL.Variant({ x: IDL.Null, y: IDL.Null, z: IDL.Null }))}),
706+
{a: [{ x: null }]},
707+
'4449444c036c0161016e026b03787f797f7a7f01000100', // Motoko: {a = ?#x} : {a : ?{#x;#y;#z}}
708+
'same variant under opt x',
709+
);
710+
testDecode(
711+
IDL.Record({a: IDL.Opt(IDL.Variant({ x: IDL.Null, y: IDL.Null, z: IDL.Null }))}),
712+
{a: [{ z: null}]},
713+
'4449444c036c0161016e026b03787f797f7a7f01000102', // Motoko: {a = ?#z} : {a : ?{#x;#y;#z}}
714+
'same variant under opt z',
715+
);
716+
testDecode(
717+
IDL.Record({a: IDL.Opt(IDL.Variant({ x: IDL.Null, y: IDL.Null }))}),
718+
{a: [{ x: null }]},
719+
'4449444c036c0161016e026b03787f797f7a7f01000100', // Motoko: {a = ?#x} : {a : ?{#x;#y;#z}}
720+
'extended variant under opt existing',
721+
);
722+
testDecode(
723+
IDL.Record({a: IDL.Opt(IDL.Variant({ x: IDL.Null, y: IDL.Null }))}),
724+
{a: []},
725+
'4449444c036c0161016e026b03787f797f7a7f01000102', // Motoko: {a = ?#z} : {a : ?{#x;#y;#z}}
726+
'extended variant under opt new - defaulting',
727+
);
728+
});

packages/candid/src/idl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,9 @@ export class OptClass<T> extends ConstructType<[T] | []> {
928928
return [v];
929929
} catch (e : any) {
930930
b.restore(checkpoint);
931-
// skip value at wire typ (to advance b)
931+
// skip value at wire type (to advance b)
932932
let v = opt._type.decodeValue(b, opt._type)
933+
// retun none
933934
return [];
934935
};
935936
default:

0 commit comments

Comments
 (0)