Merge pull request #946 from mischnic/test-hashmap

Add tests for hashmap <-> object
This commit is contained in:
LongYinan 2021-12-20 00:47:55 +08:00 committed by GitHub
commit 163ddb69ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 mutateExternal(external: ExternalObject<number>, newVal: number): void␊
export function tsRename(a: { foo: number }): string[]␊ export function tsRename(a: { foo: number }): string[]␊
export function xxh64Alias(input: Buffer): BigInt␊ 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 mapOption(val?: number | undefined | null): number | undefined | null␊
export function returnNull(): null␊ export function returnNull(): null␊
export function returnUndefined(): void␊ export function returnUndefined(): void␊

View file

@ -14,6 +14,8 @@ import {
getNums, getNums,
getWords, getWords,
sumNums, sumNums,
getMapping,
sumMapping,
getCwd, getCwd,
Animal, Animal,
Kind, Kind,
@ -104,6 +106,11 @@ test('array', (t) => {
t.is(sumNums([1, 2, 3, 4, 5]), 15) 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) => { test('enum', (t) => {
t.deepEqual([Kind.Dog, Kind.Cat, Kind.Duck], [0, 1, 2]) t.deepEqual([Kind.Dog, Kind.Cat, Kind.Duck], [0, 1, 2])
t.is(enumToI32(CustomNumEnum.Eight), 8) 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 mutateExternal(external: ExternalObject<number>, newVal: number): void
export function tsRename(a: { foo: number }): string[] export function tsRename(a: { foo: number }): string[]
export function xxh64Alias(input: Buffer): BigInt 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 mapOption(val?: number | undefined | null): number | undefined | null
export function returnNull(): null export function returnNull(): null
export function returnUndefined(): void export function returnUndefined(): void

View file

@ -22,6 +22,7 @@ mod error;
mod external; mod external;
mod fn_ts_override; mod fn_ts_override;
mod js_mod; mod js_mod;
mod map;
mod nullable; mod nullable;
mod number; mod number;
mod object; 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()
}