Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some updates on Boolean documentation and added 5 new test case #354

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions doc/boolean.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 18 additions & 1 deletion test/basic_types/boolean.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ Value CreateBoolean(const CallbackInfo& info) {
return Boolean::New(info.Env(), info[0].As<Boolean>().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<Boolean>());
return Boolean::New(info.Env(), boolean->Value());
}

Value CreateBooleanFromPrimitive(const CallbackInfo& info) {
bool boolean = info[0].As<Boolean>();
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);
exports["createBooleanFromPrimitive"] = Function::New(env, CreateBooleanFromPrimitive);
return exports;
}
16 changes: 16 additions & 0 deletions test/basic_types/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,20 @@ 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.createBooleanFromExistingValue(true);
assert.strictEqual(bool3, true);

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);

}