test(napi): add tests for hashmap <-> object

This commit is contained in:
Niklas Mischkulnig 2021-12-18 16:16:09 +01:00 committed by LongYinan
parent b7b405a49b
commit 7a04176cf9
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
6 changed files with 26 additions and 0 deletions

View file

@ -68,6 +68,8 @@ Generated by [AVA](https://avajs.dev).
export function mutateExternal(external: ExternalObject<number>, newVal: number): void␊
export function tsRename(a: { foo: number }): string[]␊
export function xxh64Alias(input: Buffer): BigInt␊
export function getMapping(): Record<string, number>
export function sumMapping(nums: Record<string, number>): number␊
export function mapOption(val?: number | undefined | null): number | undefined | null␊
export function returnNull(): null␊
export function returnUndefined(): void␊

View file

@ -14,6 +14,8 @@ import {
getNums,
getWords,
sumNums,
getMapping,
sumMapping,
getCwd,
Animal,
Kind,
@ -104,6 +106,11 @@ test('array', (t) => {
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)

View file

@ -58,6 +58,8 @@ export function getExternal(external: ExternalObject<number>): number
export function mutateExternal(external: ExternalObject<number>, newVal: number): void
export function tsRename(a: { foo: number }): string[]
export function xxh64Alias(input: Buffer): BigInt
export function getMapping(): Record<string, number>
export function sumMapping(nums: Record<string, number>): number
export function mapOption(val?: number | undefined | null): number | undefined | null
export function returnNull(): null
export function returnUndefined(): void

View file

@ -22,6 +22,7 @@ mod error;
mod external;
mod fn_ts_override;
mod js_mod;
mod map;
mod nullable;
mod number;
mod object;

14
examples/napi/src/map.rs Normal file
View file

@ -0,0 +1,14 @@
use std::collections::HashMap;
#[napi]
fn get_mapping() -> HashMap<String, u32> {
let mut map = HashMap::new();
map.insert("a".to_string(), 101);
map.insert("b".to_string(), 102);
map
}
#[napi]
fn sum_mapping(nums: HashMap<String, u32>) -> u32 {
nums.into_values().sum()
}