Merge pull request #946 from mischnic/test-hashmap
Add tests for hashmap <-> object
This commit is contained in:
commit
163ddb69ef
6 changed files with 26 additions and 0 deletions
|
@ -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␊
|
||||||
|
|
Binary file not shown.
|
@ -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)
|
||||||
|
|
2
examples/napi/index.d.ts
vendored
2
examples/napi/index.d.ts
vendored
|
@ -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
|
||||||
|
|
|
@ -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
14
examples/napi/src/map.rs
Normal 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()
|
||||||
|
}
|
Loading…
Reference in a new issue