mirror of
https://github.com/denoland/deno
synced 2024-08-25 02:57:22 +00:00
chore: add tests for N-API symbol (#19205)
This commit is contained in:
parent
3e03865d89
commit
6255cf4642
|
@ -23,6 +23,7 @@ pub mod primitives;
|
|||
pub mod promise;
|
||||
pub mod properties;
|
||||
pub mod strings;
|
||||
pub mod symbol;
|
||||
pub mod tsfn;
|
||||
pub mod typedarray;
|
||||
|
||||
|
@ -160,6 +161,7 @@ unsafe extern "C" fn napi_register_module_v1(
|
|||
tsfn::init(env, exports);
|
||||
mem::init(env, exports);
|
||||
bigint::init(env, exports);
|
||||
symbol::init(env, exports);
|
||||
|
||||
init_cleanup_hook(env, exports);
|
||||
|
||||
|
|
39
test_napi/src/symbol.rs
Normal file
39
test_napi/src/symbol.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use crate::assert_napi_ok;
|
||||
use crate::napi_get_callback_info;
|
||||
use crate::napi_new_property;
|
||||
use napi_sys::ValueType::napi_string;
|
||||
use napi_sys::*;
|
||||
|
||||
extern "C" fn symbol_new(
|
||||
env: napi_env,
|
||||
info: napi_callback_info,
|
||||
) -> napi_value {
|
||||
let (args, argc, _) = napi_get_callback_info!(env, info, 1);
|
||||
|
||||
let mut description: napi_value = std::ptr::null_mut();
|
||||
|
||||
if argc >= 1 {
|
||||
let mut ty = -1;
|
||||
assert_napi_ok!(napi_typeof(env, args[0], &mut ty));
|
||||
assert_eq!(ty, napi_string);
|
||||
description = args[0];
|
||||
}
|
||||
|
||||
let mut symbol: napi_value = std::ptr::null_mut();
|
||||
assert_napi_ok!(napi_create_symbol(env, description, &mut symbol));
|
||||
|
||||
symbol
|
||||
}
|
||||
|
||||
pub fn init(env: napi_env, exports: napi_value) {
|
||||
let properties = &[napi_new_property!(env, "symbolNew", symbol_new)];
|
||||
|
||||
assert_napi_ok!(napi_define_properties(
|
||||
env,
|
||||
exports,
|
||||
properties.len(),
|
||||
properties.as_ptr()
|
||||
));
|
||||
}
|
49
test_napi/symbol_test.js
Normal file
49
test_napi/symbol_test.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
import { assert, assertEquals, loadTestLibrary } from "./common.js";
|
||||
|
||||
const testSymbol = loadTestLibrary();
|
||||
|
||||
Deno.test("napi symbol1", () => {
|
||||
const sym = testSymbol.symbolNew("test");
|
||||
assertEquals(sym.toString(), "Symbol(test)");
|
||||
|
||||
const myObj = {};
|
||||
const fooSym = testSymbol.symbolNew("foo");
|
||||
const otherSym = testSymbol.symbolNew("bar");
|
||||
myObj.foo = "bar";
|
||||
myObj[fooSym] = "baz";
|
||||
myObj[otherSym] = "bing";
|
||||
assertEquals(myObj.foo, "bar");
|
||||
assertEquals(myObj[fooSym], "baz");
|
||||
assertEquals(myObj[otherSym], "bing");
|
||||
});
|
||||
|
||||
Deno.test("napi symbol2", () => {
|
||||
const sym = testSymbol.symbolNew("test");
|
||||
assertEquals(sym.toString(), "Symbol(test)");
|
||||
|
||||
const myObj = {};
|
||||
const fooSym = testSymbol.symbolNew("foo");
|
||||
myObj.foo = "bar";
|
||||
myObj[fooSym] = "baz";
|
||||
|
||||
assertEquals(Object.keys(myObj), ["foo"]);
|
||||
assertEquals(Object.getOwnPropertyNames(myObj), ["foo"]);
|
||||
assertEquals(Object.getOwnPropertySymbols(myObj), [fooSym]);
|
||||
});
|
||||
|
||||
Deno.test("napi symbol3", () => {
|
||||
assert(testSymbol.symbolNew() !== testSymbol.symbolNew());
|
||||
assert(testSymbol.symbolNew("foo") !== testSymbol.symbolNew("foo"));
|
||||
assert(testSymbol.symbolNew("foo") !== testSymbol.symbolNew("bar"));
|
||||
|
||||
const foo1 = testSymbol.symbolNew("foo");
|
||||
const foo2 = testSymbol.symbolNew("foo");
|
||||
const object = {
|
||||
[foo1]: 1,
|
||||
[foo2]: 2,
|
||||
};
|
||||
assertEquals(object[foo1], 1);
|
||||
assertEquals(object[foo2], 2);
|
||||
});
|
Loading…
Reference in a new issue