Skip to content

Commit

Permalink
run specific test cases with filter conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepak Rajamohan committed Jan 28, 2022
1 parent cb22841 commit 6603f37
Show file tree
Hide file tree
Showing 30 changed files with 1,204 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: CLANG_FORMAT_START=refs/remotes/origin/main npm run lint
- run: FORMAT_START=refs/remotes/origin/main npm run lint
6 changes: 6 additions & 0 deletions doc/addon.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ to either attach methods, accessors, and/or values to the `exports` object or to
create its own `exports` object and attach methods, accessors, and/or values to
it.

**Note:** `Napi::Addon<T>` uses `Napi::Env::SetInstanceData()` internally. This
means that the add-on should only use `Napi::Env::GetInstanceData` explicitly to
retrieve the instance of the `Napi::Addon<T>` class. Variables whose scope would
otherwise be global should be stored as instance variables in the
`Napi::Addon<T>` class.

Functions created with `Napi::Function::New()`, accessors created with
`PropertyDescriptor::Accessor()`, and values can also be attached. If their
implementation requires the `ExampleAddon` instance, it can be retrieved from
Expand Down
98 changes: 98 additions & 0 deletions doc/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,34 @@ Napi::Value Napi::Object::operator[] (uint32_t index) const;

Returns an indexed property or array element as a [`Napi::Value`](value.md).

### begin()

```cpp
Napi::Object::iterator Napi::Object::begin() const;
```

Returns a constant iterator to the beginning of the object.

```cpp
Napi::Object::iterator Napi::Object::begin();
```

Returns a non constant iterator to the beginning of the object.

### end()

```cpp
Napi::Object::iterator Napi::Object::end() const;
```

Returns a constant iterator to the end of the object.

```cpp
Napi::Object::iterator Napi::Object::end();
```

Returns a non constant iterator to the end of the object.

## Iterator

Iterators expose an `std::pair<...>`, where the `first` property is a
Expand All @@ -300,6 +328,41 @@ exceptions are enabled (by defining `NAPI_CPP_EXCEPTIONS` during the build).

In constant iterators, the iterated values are immutable.

#### operator++()

```cpp
inline Napi::Object::const_iterator& Napi::Object::const_iterator::operator++();
```

Moves the iterator one step forward.

#### operator==

```cpp
inline bool Napi::Object::const_iterator::operator==(const Napi::Object::const_iterator& other) const;
```
- `[in] other`: Another iterator to compare the current iterator to.

Returns whether both iterators are at the same index.

#### operator!=

```cpp
inline bool Napi::Object::const_iterator::operator!=(const Napi::Object::const_iterator& other) const;
```
- `[in] other`: Another iterator to compare the current iterator to.

Returns whether both iterators are at different indices.

#### operator*()

```cpp
inline const std::pair<Napi::Value, Napi::Object::PropertyLValue<Napi::Value>> Napi::Object::const_iterator::operator*() const;
```

Returns the currently iterated key and value.

#### Example
```cpp
Value Sum(const CallbackInfo& info) {
Object object = info[0].As<Object>();
Expand All @@ -317,6 +380,41 @@ Value Sum(const CallbackInfo& info) {
In non constant iterators, the iterated values are mutable.
#### operator++()
```cpp
inline Napi::Object::iterator& Napi::Object::iterator::operator++();
```

Moves the iterator one step forward.

#### operator==

```cpp
inline bool Napi::Object::iterator::operator==(const Napi::Object::iterator& other) const;
```
- `[in] other`: Another iterator to compare the current iterator to.

Returns whether both iterators are at the same index.

#### operator!=

```cpp
inline bool Napi::Object::iterator::operator!=(const Napi::Object::iterator& other) const;
```
- `[in] other`: Another iterator to compare the current iterator to.

Returns whether both iterators are at different indices.

#### operator*()

```cpp
inline std::pair<Napi::Value, Napi::Object::PropertyLValue<Napi::Value>> Napi::Object::iterator::operator*();
```

Returns the currently iterated key and value.

#### Example
```cpp
void Increment(const CallbackInfo& info) {
Env env = info.Env();
Expand Down
6 changes: 3 additions & 3 deletions doc/object_wrap.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class Example : public Napi::ObjectWrap<Example> {
Napi::Object Example::Init(Napi::Env env, Napi::Object exports) {
// This method is used to hook the accessor and method callbacks
Napi::Function func = DefineClass(env, "Example", {
InstanceMethod<&Example::GetValue>("GetValue"),
InstanceMethod<&Example::SetValue>("SetValue"),
StaticMethod<&Example::CreateNewItem>("CreateNewItem"),
InstanceMethod<&Example::GetValue>("GetValue", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&Example::SetValue>("SetValue", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
StaticMethod<&Example::CreateNewItem>("CreateNewItem", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
});

Napi::FunctionReference* constructor = new Napi::FunctionReference();
Expand Down
46 changes: 29 additions & 17 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2520,12 +2520,23 @@ inline Error Error::New(napi_env env) {
napi_status status;
napi_value error = nullptr;
bool is_exception_pending;
const napi_extended_error_info* info;
napi_extended_error_info last_error_info_copy;

// We must retrieve the last error info before doing anything else, because
// doing anything else will replace the last error info.
status = napi_get_last_error_info(env, &info);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_last_error_info");
{
// We must retrieve the last error info before doing anything else because
// doing anything else will replace the last error info.
const napi_extended_error_info* last_error_info;
status = napi_get_last_error_info(env, &last_error_info);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_last_error_info");

// All fields of the `napi_extended_error_info` structure gets reset in
// subsequent Node-API function calls on the same `env`. This includes a
// call to `napi_is_exception_pending()`. So here it is necessary to make a
// copy of the information as the `error_code` field is used later on.
memcpy(&last_error_info_copy,
last_error_info,
sizeof(napi_extended_error_info));
}

status = napi_is_exception_pending(env, &is_exception_pending);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_is_exception_pending");
Expand All @@ -2536,8 +2547,9 @@ inline Error Error::New(napi_env env) {
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_and_clear_last_exception");
}
else {
const char* error_message = info->error_message != nullptr ?
info->error_message : "Error in native callback";
const char* error_message = last_error_info_copy.error_message != nullptr
? last_error_info_copy.error_message
: "Error in native callback";

napi_value message;
status = napi_create_string_utf8(
Expand All @@ -2547,16 +2559,16 @@ inline Error Error::New(napi_env env) {
&message);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_string_utf8");

switch (info->error_code) {
case napi_object_expected:
case napi_string_expected:
case napi_boolean_expected:
case napi_number_expected:
status = napi_create_type_error(env, nullptr, message, &error);
break;
default:
status = napi_create_error(env, nullptr, message, &error);
break;
switch (last_error_info_copy.error_code) {
case napi_object_expected:
case napi_string_expected:
case napi_boolean_expected:
case napi_number_expected:
status = napi_create_type_error(env, nullptr, message, &error);
break;
default:
status = napi_create_error(env, nullptr, message, &error);
break;
}
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_error");
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,9 @@
"dev:incremental": "node test",
"doc": "doxygen doc/Doxyfile",
"lint": "eslint $(git diff --name-only refs/remotes/origin/main '**/*.js' | xargs) && node tools/clang-format",
"lint:fix": "node tools/clang-format --fix && eslint --fix $(git diff --cached --name-only '**/*.js' | xargs && git diff --name-only '**/*.js' | xargs)"
"lint:fix": "node tools/clang-format --fix && eslint --fix $(git diff --name-only refs/remotes/origin/main '**/*.js' | xargs && git diff --name-only refs/remotes/origin/main '**/*.js' | xargs)",
"preunit": "filter=\"$npm_config_filter\" node-gyp rebuild -C unit-test",
"unit": "filter=\"$npm_config_filter\" node unit-test/test"
},
"pre-commit": "lint",
"version": "4.2.0",
Expand Down
9 changes: 9 additions & 0 deletions test/bigint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ Value IsLossless(const CallbackInfo& info) {
return Boolean::New(env, lossless);
}

Value IsBigInt(const CallbackInfo& info) {
Env env = info.Env();

BigInt big = info[0].As<BigInt>();

return Boolean::New(env, big.IsBigInt());
}

Value TestInt64(const CallbackInfo& info) {
bool lossless;
int64_t input = info[0].As<BigInt>().Int64Value(&lossless);
Expand Down Expand Up @@ -71,6 +79,7 @@ Value TestTooBigBigInt(const CallbackInfo& info) {
Object InitBigInt(Env env) {
Object exports = Object::New(env);
exports["IsLossless"] = Function::New(env, IsLossless);
exports["IsBigInt"] = Function::New(env, IsBigInt);
exports["TestInt64"] = Function::New(env, TestInt64);
exports["TestUint64"] = Function::New(env, TestUint64);
exports["TestWords"] = Function::New(env, TestWords);
Expand Down
11 changes: 7 additions & 4 deletions test/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ const assert = require('assert');

module.exports = require('./common').runTest(test);

function test(binding) {
function test (binding) {
const {
TestInt64,
TestUint64,
TestWords,
IsLossless,
TestTooBigBigInt,
IsBigInt,
TestTooBigBigInt
} = binding.bigint;

[
Expand All @@ -24,7 +25,7 @@ function test(binding) {
986583n,
-976675n,
98765432213456789876546896323445679887645323232436587988766545658n,
-4350987086545760976737453646576078997096876957864353245245769809n,
-4350987086545760976737453646576078997096876957864353245245769809n
].forEach((num) => {
if (num > -(2n ** 63n) && num < 2n ** 63n) {
assert.strictEqual(TestInt64(num), num);
Expand All @@ -40,11 +41,13 @@ function test(binding) {
assert.strictEqual(IsLossless(num, false), false);
}

assert.strictEqual(IsBigInt(num), true);

assert.strictEqual(num, TestWords(num));
});

assert.throws(TestTooBigBigInt, {
name: /^(RangeError|Error)$/,
message: /^(Maximum BigInt size exceeded|Invalid argument)$/,
message: /^(Maximum BigInt size exceeded|Invalid argument)$/
});
}
Loading

0 comments on commit 6603f37

Please sign in to comment.