test(napi): add class specs
This commit is contained in:
parent
88c2479a16
commit
5a0b9919ea
5 changed files with 59 additions and 2 deletions
|
@ -203,11 +203,11 @@ rules:
|
|||
|
||||
overrides:
|
||||
- files:
|
||||
- ./**/*.js
|
||||
- ./test_module/**/*.js
|
||||
plugins:
|
||||
- '@typescript-eslint'
|
||||
parserOptions:
|
||||
project: ./tsconfig.json
|
||||
project: ./test_module/tsconfig.json
|
||||
rules:
|
||||
'@typescript-eslint/prefer-nullish-coalescing': 0
|
||||
'@typescript-eslint/prefer-optional-chain': 0
|
||||
|
|
13
test_module/__test__/class.spec.js
Normal file
13
test_module/__test__/class.spec.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
const test = require('ava')
|
||||
|
||||
const bindings = require('../index.node')
|
||||
|
||||
test('should create class', (t) => {
|
||||
const TestClass = bindings.createTestClass()
|
||||
const fixture = 20
|
||||
const testClass = new TestClass(fixture)
|
||||
t.is(testClass.count, fixture)
|
||||
const add = 101
|
||||
testClass.addCount(add)
|
||||
t.is(testClass.count, fixture + add)
|
||||
})
|
34
test_module/src/class.rs
Normal file
34
test_module/src/class.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
use std::convert::TryInto;
|
||||
|
||||
use napi::{CallContext, JsFunction, JsNumber, JsObject, JsUndefined, Property, Result};
|
||||
|
||||
#[js_function(1)]
|
||||
pub fn create_test_class(ctx: CallContext) -> Result<JsFunction> {
|
||||
let add_count_method = Property::new("addCount").with_method(add_count);
|
||||
let properties = vec![add_count_method];
|
||||
ctx
|
||||
.env
|
||||
.define_class("TestClass", test_class_constructor, properties)
|
||||
}
|
||||
|
||||
#[js_function(1)]
|
||||
pub fn test_class_constructor(mut ctx: CallContext<JsObject>) -> Result<JsUndefined> {
|
||||
let count = ctx.get::<JsNumber>(0)?;
|
||||
ctx
|
||||
.this
|
||||
.set_named_property("count", ctx.env.create_int32(count.try_into()?)?)?;
|
||||
ctx.env.get_undefined()
|
||||
}
|
||||
|
||||
#[js_function(1)]
|
||||
pub fn add_count(mut ctx: CallContext<JsObject>) -> Result<JsUndefined> {
|
||||
let add: i32 = ctx.get::<JsNumber>(0)?.try_into()?;
|
||||
let count: i32 = ctx
|
||||
.this
|
||||
.get_named_property::<JsNumber>("count")?
|
||||
.try_into()?;
|
||||
ctx
|
||||
.this
|
||||
.set_named_property("count", ctx.env.create_int32(count + add)?)?;
|
||||
ctx.env.get_undefined()
|
||||
}
|
|
@ -15,6 +15,7 @@ mod napi5;
|
|||
mod tokio_rt;
|
||||
|
||||
mod buffer;
|
||||
mod class;
|
||||
mod either;
|
||||
mod external;
|
||||
mod function;
|
||||
|
@ -56,6 +57,7 @@ fn init(module: &mut Module) -> Result<()> {
|
|||
module.create_named_method("testCallFunctionWithThis", call_function_with_this)?;
|
||||
module.create_named_method("eitherNumberString", either_number_string)?;
|
||||
module.create_named_method("dynamicArgumentLength", dynamic_argument_length)?;
|
||||
module.create_named_method("createTestClass", class::create_test_class)?;
|
||||
#[cfg(napi4)]
|
||||
module.create_named_method("testExecuteTokioReadfile", test_execute_tokio_readfile)?;
|
||||
#[cfg(napi4)]
|
||||
|
|
8
test_module/tsconfig.json
Normal file
8
test_module/tsconfig.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"allowJs": true
|
||||
},
|
||||
"include": ["."]
|
||||
}
|
Loading…
Reference in a new issue