From d70fdcef7028d81f026d683ff80a996eddc17bfc Mon Sep 17 00:00:00 2001 From: NickNaso Date: Mon, 24 Sep 2018 23:25:17 +0200 Subject: [PATCH 1/3] Some fix on `Napi::Boolean` documentation --- doc/boolean.md | 53 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/doc/boolean.md b/doc/boolean.md index 2886b1d0c..95c42fa61 100644 --- a/doc/boolean.md +++ b/doc/boolean.md @@ -1,29 +1,64 @@ # Boolean -# Methods +`Napi::Boolean` class is a representation of the JavaScript `Boolean` object. The +`Napi::Boolean` class inherits its behavior from the `Napi::Value` class +(for more info see: [`Napi::Value`](value.md)). + +## Methods ### Constructor +Creates a new empty instance of an `Napi::Boolean` object. + ```cpp -Napi::Boolean::New(Napi::Env env, bool value); +Napi::Boolean::Boolean(); ``` - - `[in] env`: The `napi_env` Environment - - `[in] value`: The Javascript boolean value + +Returns a new _empty_ `Napi::Boolean` object. + +### Contructor + +Creates a new instance of the `Napi::Boolean` object. ```cpp -Napi::Boolean::Boolean(); +Napi::Boolean(napi_env env, napi_value value); ``` -returns a new empty Javascript Boolean value type. -### operator bool -Converts a `Napi::Boolean` value to a boolean primitive. +- `[in] env`: The `napi_env` environment in which to construct the `Napi::Boolean` object. +- `[in] value`: The `napi_value` which is a handle for a JavaScript `Boolean`. + +Returns a non-empty `Napi::Boolean` object. + +### New + +Initializes a new instance of the `Napi::Boolean` object. + ```cpp -Napi::Boolean::operator bool() const; +Napi::Boolean Napi::Boolean::New(napi_env env, bool value); ``` +- `[in] env`: The `napi_env` environment in which to construct the `Napi::Boolean` object. +- `[in] value`: The primitive boolean value (`true` or `false`). + +Returns a new instance of the `Napi::Boolean` object. ### Value + Converts a `Napi::Boolean` value to a boolean primitive. ```cpp bool Napi::Boolean::Value() const; ``` + +Returns the boolean primitive type of the corresponding `Napi::Boolean` object. + +## Operators + +### operator bool + +Converts a `Napi::Boolean` value to a boolean primitive. + +```cpp +Napi::Boolean::operator bool() const; +``` + +Returns the boolean primitive type of the corresponding `Napi::Boolean` object. From 3f7371543f153c6f3451eeaf320f38324e53e451 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Tue, 25 Sep 2018 00:18:28 +0200 Subject: [PATCH 2/3] Added two use case test --- test/basic_types/boolean.cc | 12 ++++++++++++ test/basic_types/boolean.js | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/test/basic_types/boolean.cc b/test/basic_types/boolean.cc index c874845eb..99d58d3d9 100644 --- a/test/basic_types/boolean.cc +++ b/test/basic_types/boolean.cc @@ -6,10 +6,22 @@ Value CreateBoolean(const CallbackInfo& info) { return Boolean::New(info.Env(), info[0].As().Value()); } +Value CreateEmptyBoolean(const CallbackInfo& info) { + Boolean* boolean = new Boolean(); + return Boolean::New(info.Env(), boolean->IsEmpty()); +} + +Value CreateBooleanFromExistingValue(const CallbackInfo& info) { + Boolean* boolean = new Boolean(info.Env(), info[0].As()); + return Boolean::New(info.Env(), boolean); +} + Object InitBasicTypesBoolean(Env env) { Object exports = Object::New(env); exports["createBoolean"] = Function::New(env, CreateBoolean); + exports["createEmptyBoolean"] = Function::New(env, CreateEmptyBoolean); + exports["createBooleanFromExistingValue"] = Function::New(env, CreateBooleanFromExistingValue); return exports; } diff --git a/test/basic_types/boolean.js b/test/basic_types/boolean.js index 3a9c88da8..169a3f038 100644 --- a/test/basic_types/boolean.js +++ b/test/basic_types/boolean.js @@ -11,4 +11,14 @@ function test(binding) { const bool2 = binding.basic_types_boolean.createBoolean(false); assert.strictEqual(bool2, false); + + const emptyBoolean = binding.basic_types_boolean.createEmptyBoolean(); + assert.strictEqual(emptyBoolean, true); + + const bool3 = binding.basic_types_boolean.createBoolean(true); + assert.strictEqual(bool3, true); + + const bool4 = binding.basic_types_boolean.createBoolean(false); + assert.strictEqual(bool4, false); + } From 9e2d33691a662ffb70cdc12774d7d0e6d8608162 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Tue, 25 Sep 2018 00:46:20 +0200 Subject: [PATCH 3/3] Fixed error on test --- test/basic_types/boolean.cc | 7 ++++++- test/basic_types/boolean.js | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/test/basic_types/boolean.cc b/test/basic_types/boolean.cc index 99d58d3d9..900438f62 100644 --- a/test/basic_types/boolean.cc +++ b/test/basic_types/boolean.cc @@ -13,6 +13,11 @@ Value CreateEmptyBoolean(const CallbackInfo& info) { Value CreateBooleanFromExistingValue(const CallbackInfo& info) { Boolean* boolean = new Boolean(info.Env(), info[0].As()); + return Boolean::New(info.Env(), boolean->Value()); +} + +Value CreateBooleanFromPrimitive(const CallbackInfo& info) { + bool boolean = info[0].As(); return Boolean::New(info.Env(), boolean); } @@ -22,6 +27,6 @@ Object InitBasicTypesBoolean(Env env) { exports["createBoolean"] = Function::New(env, CreateBoolean); exports["createEmptyBoolean"] = Function::New(env, CreateEmptyBoolean); exports["createBooleanFromExistingValue"] = Function::New(env, CreateBooleanFromExistingValue); - + exports["createBooleanFromPrimitive"] = Function::New(env, CreateBooleanFromPrimitive); return exports; } diff --git a/test/basic_types/boolean.js b/test/basic_types/boolean.js index 169a3f038..1c27664f1 100644 --- a/test/basic_types/boolean.js +++ b/test/basic_types/boolean.js @@ -15,10 +15,16 @@ function test(binding) { const emptyBoolean = binding.basic_types_boolean.createEmptyBoolean(); assert.strictEqual(emptyBoolean, true); - const bool3 = binding.basic_types_boolean.createBoolean(true); + const bool3 = binding.basic_types_boolean.createBooleanFromExistingValue(true); assert.strictEqual(bool3, true); - const bool4 = binding.basic_types_boolean.createBoolean(false); + const bool4 = binding.basic_types_boolean.createBooleanFromExistingValue(false); assert.strictEqual(bool4, false); + const bool5 = binding.basic_types_boolean.createBooleanFromPrimitive(true); + assert.strictEqual(bool5, true); + + const bool6 = binding.basic_types_boolean.createBooleanFromPrimitive(false); + assert.strictEqual(bool6, false); + }