test(napi): add class specs

This commit is contained in:
LongYinan 2020-08-02 23:31:22 +08:00
parent 88c2479a16
commit 5a0b9919ea
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
5 changed files with 59 additions and 2 deletions

View file

@ -203,11 +203,11 @@ rules:
overrides: overrides:
- files: - files:
- ./**/*.js - ./test_module/**/*.js
plugins: plugins:
- '@typescript-eslint' - '@typescript-eslint'
parserOptions: parserOptions:
project: ./tsconfig.json project: ./test_module/tsconfig.json
rules: rules:
'@typescript-eslint/prefer-nullish-coalescing': 0 '@typescript-eslint/prefer-nullish-coalescing': 0
'@typescript-eslint/prefer-optional-chain': 0 '@typescript-eslint/prefer-optional-chain': 0

View 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
View 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()
}

View file

@ -15,6 +15,7 @@ mod napi5;
mod tokio_rt; mod tokio_rt;
mod buffer; mod buffer;
mod class;
mod either; mod either;
mod external; mod external;
mod function; 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("testCallFunctionWithThis", call_function_with_this)?;
module.create_named_method("eitherNumberString", either_number_string)?; module.create_named_method("eitherNumberString", either_number_string)?;
module.create_named_method("dynamicArgumentLength", dynamic_argument_length)?; module.create_named_method("dynamicArgumentLength", dynamic_argument_length)?;
module.create_named_method("createTestClass", class::create_test_class)?;
#[cfg(napi4)] #[cfg(napi4)]
module.create_named_method("testExecuteTokioReadfile", test_execute_tokio_readfile)?; module.create_named_method("testExecuteTokioReadfile", test_execute_tokio_readfile)?;
#[cfg(napi4)] #[cfg(napi4)]

View file

@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"allowJs": true
},
"include": ["."]
}