Skip to content

Commit

Permalink
test: add out of range date test case
Browse files Browse the repository at this point in the history
  • Loading branch information
amilajack committed Jan 1, 2021
1 parent e49bb58 commit 1e5ae76
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
5 changes: 5 additions & 0 deletions crates/neon-runtime/src/napi/bindings/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ generate!(extern "C" {

fn create_double(env: Env, value: f64, result: *mut Value) -> Status;

fn create_date(env: Env, value: f64, result: *mut Value) -> Status;

fn get_date_value(env: Env, value: Value, result: *mut f64) -> Status;

fn create_object(env: Env, result: *mut Value) -> Status;

fn get_value_bool(env: Env, value: Value, result: *mut bool) -> Status;
Expand Down Expand Up @@ -57,6 +61,7 @@ generate!(extern "C" {
fn is_buffer(env: Env, value: Value, result: *mut bool) -> Status;
fn is_error(env: Env, value: Value, result: *mut bool) -> Status;
fn is_array(env: Env, value: Value, result: *mut bool) -> Status;
fn is_date(env: Env, value: Value, result: *mut bool) -> Status;

fn get_value_string_utf8(
env: Env,
Expand Down
12 changes: 6 additions & 6 deletions crates/neon-runtime/src/napi/date.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::mem::MaybeUninit;
use nodejs_sys as napi;
use raw::{Env, Local};
use crate::napi::bindings as napi;
use crate::raw::{Env, Local};

pub unsafe fn new_date(env: Env, value: f64) -> Local {
let mut local = MaybeUninit::zeroed();
let status = napi::napi_create_date(env, value, local.as_mut_ptr());
assert_eq!(status, napi::napi_status::napi_ok);
let status = napi::create_date(env, value, local.as_mut_ptr());
assert_eq!(status, napi::Status::Ok);
local.assume_init()
}

pub unsafe fn value(env: Env, p: Local) -> f64 {
let mut value = 0.0;
let status = napi::napi_get_date_value(env, p, &mut value as *mut _);
assert_eq!(status, napi::napi_status::napi_ok);
let status = napi::get_date_value(env, p, &mut value as *mut _);
assert_eq!(status, napi::Status::Ok);
return value;
}
2 changes: 1 addition & 1 deletion crates/neon-runtime/src/napi/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ pub unsafe extern "C" fn is_arraybuffer(env: Env, val: Local) -> bool {

pub unsafe extern "C" fn is_date(env: Env, val: Local) -> bool {
let mut result = false;
assert_eq!(napi::napi_is_date(env, val, &mut result as *mut _), napi::napi_status::napi_ok);
assert_eq!(napi::is_date(env, val, &mut result as *mut _), napi::Status::Ok);
result
}
5 changes: 5 additions & 0 deletions test/napi/lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ describe('JsDate', function() {
assert.isNaN(date);
});

it('should fail when creating out of bounds dates', function () {
const date = addon.create_out_of_bounds_date();
assert.isNaN(date);
});

it('should get date value', function () {
const dateValue = addon.get_date_value();
assert.equal(dateValue, 31415);
Expand Down
8 changes: 8 additions & 0 deletions test/napi/src/js/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ pub fn create_and_get_invalid_date(mut cx: FunctionContext) -> JsResult<JsNumber
Ok(cx.number(date))
}

pub fn create_out_of_bounds_date(mut cx: FunctionContext) -> JsResult<JsNumber> {
let time = 9_007_199_254_740_991.0;
let date = cx.date(time).value(&mut cx);
assert!(!cx.date(time).is_valid(&mut cx));
assert!(cx.date(time).value(&mut cx).is_nan());
Ok(cx.number(date))
}

pub fn get_date_value(mut cx: FunctionContext) -> JsResult<JsNumber> {
let date = cx.date(31415);
let value = date.value(&mut cx);
Expand Down
1 change: 1 addition & 0 deletions test/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("check_date_is_valid", check_date_is_valid)?;
cx.export_function("create_date_from_value", create_date_from_value)?;
cx.export_function("create_and_get_invalid_date", create_and_get_invalid_date)?;
cx.export_function("create_out_of_bounds_date", create_out_of_bounds_date)?;

cx.export_function("is_array", is_array)?;
cx.export_function("is_array_buffer", is_array_buffer)?;
Expand Down

0 comments on commit 1e5ae76

Please sign in to comment.