Skip to content

Commit

Permalink
unit tests for RefCell extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
dherman committed Feb 2, 2025
1 parent 0986c4d commit e735e08
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/neon/src/types_impl/extract/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<T> Container for RefCell<T> {
}
}

impl<'cx, T: Container + 'static> TryFromJs<'cx> for &'cx RefCell<T> {
impl<'cx, T: 'static> TryFromJs<'cx> for &'cx RefCell<T> {
type Error = RustTypeExpected<RefCell<T>>;

fn try_from_js(
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/napi/lib/container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const addon = require("..");
const { expect } = require("chai");
const assert = require("chai").assert;

describe("container", function () {
it("can produce and consume a RefCell", function () {
const cell = addon.createStringRefCell("my sekret mesij");
const s = addon.readStringRefCell(cell);
assert.strictEqual("my sekret mesij", s);
});

it("concatenates a RefCell<String> with a String", function () {
const cell = addon.createStringRefCell("hello");
const s = addon.stringRefCellConcat(cell, " world");
assert.strictEqual("hello world", s);
});
});
16 changes: 16 additions & 0 deletions test/napi/src/js/container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::cell::RefCell;

#[neon::export]
fn create_string_ref_cell(s: String) -> RefCell<String> {
RefCell::new(s)
}

#[neon::export]
fn read_string_ref_cell(s: &RefCell<String>) -> String {
s.borrow().clone()
}

#[neon::export]
fn string_ref_cell_concat(lhs: &RefCell<String>, rhs: String) -> String {
lhs.borrow().clone() + &rhs
}
1 change: 1 addition & 0 deletions test/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod js {
pub mod bigint;
pub mod boxed;
pub mod coercions;
pub mod container;
pub mod date;
pub mod errors;
pub mod export;
Expand Down

0 comments on commit e735e08

Please sign in to comment.