From 7a04176cf925c4d847ac4eaa98b23fdcb972656c Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Sat, 18 Dec 2021 16:16:09 +0100 Subject: [PATCH] test(napi): add tests for hashmap <-> object --- examples/napi/__test__/typegen.spec.ts.md | 2 ++ examples/napi/__test__/typegen.spec.ts.snap | Bin 2075 -> 2094 bytes examples/napi/__test__/values.spec.ts | 7 +++++++ examples/napi/index.d.ts | 2 ++ examples/napi/src/lib.rs | 1 + examples/napi/src/map.rs | 14 ++++++++++++++ 6 files changed, 26 insertions(+) create mode 100644 examples/napi/src/map.rs diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index f2a985cc..54bd26ac 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -68,6 +68,8 @@ Generated by [AVA](https://avajs.dev). export function mutateExternal(external: ExternalObject, newVal: number): void␊ export function tsRename(a: { foo: number }): string[]␊ export function xxh64Alias(input: Buffer): BigInt␊ + export function getMapping(): Record␊ + export function sumMapping(nums: Record): number␊ export function mapOption(val?: number | undefined | null): number | undefined | null␊ export function returnNull(): null␊ export function returnUndefined(): void␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 7132e138178a4d146c329322e0d5ea2e719460d4..2249df9d222003ec2628f9930a4135d2dbdd3b96 100644 GIT binary patch delta 2065 zcmV+s2=4cr5Uvn^K~_N^Q*L2!b7*gLAa*he0szgmUrewK`GLi^IblB(#wQ<(2mk;8 z00003tys-(8^;x=8x#R_>!~?T(nFR8rRXbaO4u?eS#lIxlp#@#13`#qcZcMNyEB`e zSxKxTpnpP7J@itbm-dh7Ehx}?&;1vIoN~^4GrQz0cUKgDf*6K9JM+Hg&F_7Yv|6o4 zt@r-=+yDLd`>+1}&rg5<(+06b^gI8kBL?h7rGmA zMg548$DgdgCNrC>&L-~COv}{~6D*~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#j^^YW)8sh8l?SMN+?_wpN`kxPtRYYJ;m9+5jU=aqq(-w zf}SQ!4#~)D)R8WCg=s{rOFvP4$EouK-HRd{&H{KQg&>k{D_%fCN9?xf*JO&61&m*^ zibixJh^ksriz)Y_P8#ezKkA(r+j~Wt-ounNx5K?N;GKn#53>BefEObGmNUH6++ZkP z**k=}2oKvfS6YX#TvQ`Gu>*Ss)#JKZW_Y|DtK4!{f{$1jo<;&_DhSEW~P@aPN8O+?+A<$KSfC8ta z4T21A1&E%Vo9-QdR*mJjB5N@3c>@1;katKmjb6x*{m$Zkvx)EkZ0}$u zH?yiHzXTtDPUdGrx811c08m5v(WhIRro0wi$|)qcd~4~HUG4%0P*y`Lt6h@Q(5X4} zx$eVAQb&0)W3G?~K z{D=n~6eDpCx&pjHVi^%0CqRmogE-=m?k{IJ*Lu!w`+UV66U^@}J;7rd&1M=i;6R$E zcH3n5*)J}WUIr(BxlHrGJaZJ`NW<_NOMwa?W@LM9rxvMVvqmfaUgW5@pE3UYu^Qoc z6UaD*S-|e2axAbcmKU7-_h0|~#_XecS7S+IUQ+Ac$yyK3rF0pv7KTQ1op$b1I{}`= za)N!8X3)JLI&<*Dc6A-3-qd0D zGn8Wo{Cx+5HSLVWP2Rwj?Q$tQCBpWiRv3LR=pajV#F~1heUntk6bEU66GNJX8sGmM}YR8O5b;9Gc&!{TB!QffI(BsdMX#o$aQP(|Q<)Ha0(+GZ~lG zNCKkU1-(W)t>0W3cIS0*i7JRKcb0@0Xv0bJuZlh_Qv>ikHQgYPz7-? { t.is(sumNums([1, 2, 3, 4, 5]), 15) }) +test('map', (t) => { + t.deepEqual(getMapping(), { a: 101, b: 102 }) + t.is(sumMapping({ a: 101, b: 102 }), 203) +}) + test('enum', (t) => { t.deepEqual([Kind.Dog, Kind.Cat, Kind.Duck], [0, 1, 2]) t.is(enumToI32(CustomNumEnum.Eight), 8) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index da358ea6..6282cf8f 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -58,6 +58,8 @@ export function getExternal(external: ExternalObject): number export function mutateExternal(external: ExternalObject, newVal: number): void export function tsRename(a: { foo: number }): string[] export function xxh64Alias(input: Buffer): BigInt +export function getMapping(): Record +export function sumMapping(nums: Record): number export function mapOption(val?: number | undefined | null): number | undefined | null export function returnNull(): null export function returnUndefined(): void diff --git a/examples/napi/src/lib.rs b/examples/napi/src/lib.rs index 047c5add..5e0333b2 100644 --- a/examples/napi/src/lib.rs +++ b/examples/napi/src/lib.rs @@ -22,6 +22,7 @@ mod error; mod external; mod fn_ts_override; mod js_mod; +mod map; mod nullable; mod number; mod object; diff --git a/examples/napi/src/map.rs b/examples/napi/src/map.rs new file mode 100644 index 00000000..c4d0bd43 --- /dev/null +++ b/examples/napi/src/map.rs @@ -0,0 +1,14 @@ +use std::collections::HashMap; + +#[napi] +fn get_mapping() -> HashMap { + let mut map = HashMap::new(); + map.insert("a".to_string(), 101); + map.insert("b".to_string(), 102); + map +} + +#[napi] +fn sum_mapping(nums: HashMap) -> u32 { + nums.into_values().sum() +}