Skip to content

Commit

Permalink
test: standardize unit test file names
Browse files Browse the repository at this point in the history
- change all unit test file names to snake case
  this helps in parsing file names to tokens and infer metadata
  like export initializers

- all other changes other than file names is due to linter errors

PR-URL: #1056
Reviewed-By: Michael Dawson <midawson@redhat.com
Reviewed-By: NickNaso <nicoladelgobbo@gmail.com>
  • Loading branch information
Deepak Rajamohan authored and mhdawson committed Sep 9, 2021
1 parent da2e754 commit cbac3aa
Show file tree
Hide file tree
Showing 20 changed files with 124 additions and 108 deletions.
92 changes: 54 additions & 38 deletions test/arraybuffer.cc → test/array_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Value CreateBuffer(const CallbackInfo& info) {
ArrayBuffer buffer = ArrayBuffer::New(info.Env(), testLength);

if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return Value();
}

Expand All @@ -41,12 +42,14 @@ Value CreateExternalBuffer(const CallbackInfo& info) {
ArrayBuffer buffer = ArrayBuffer::New(info.Env(), testData, testLength);

if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return Value();
}

if (buffer.Data() != testData) {
Error::New(info.Env(), "Incorrect buffer data.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect buffer data.")
.ThrowAsJavaScriptException();
return Value();
}

Expand All @@ -60,21 +63,20 @@ Value CreateExternalBufferWithFinalize(const CallbackInfo& info) {
uint8_t* data = new uint8_t[testLength];

ArrayBuffer buffer = ArrayBuffer::New(
info.Env(),
data,
testLength,
[](Env /*env*/, void* finalizeData) {
delete[] static_cast<uint8_t*>(finalizeData);
finalizeCount++;
});
info.Env(), data, testLength, [](Env /*env*/, void* finalizeData) {
delete[] static_cast<uint8_t*>(finalizeData);
finalizeCount++;
});

if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return Value();
}

if (buffer.Data() != data) {
Error::New(info.Env(), "Incorrect buffer data.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect buffer data.")
.ThrowAsJavaScriptException();
return Value();
}

Expand All @@ -89,22 +91,24 @@ Value CreateExternalBufferWithFinalizeHint(const CallbackInfo& info) {

char* hint = nullptr;
ArrayBuffer buffer = ArrayBuffer::New(
info.Env(),
data,
testLength,
[](Env /*env*/, void* finalizeData, char* /*finalizeHint*/) {
delete[] static_cast<uint8_t*>(finalizeData);
finalizeCount++;
},
hint);
info.Env(),
data,
testLength,
[](Env /*env*/, void* finalizeData, char* /*finalizeHint*/) {
delete[] static_cast<uint8_t*>(finalizeData);
finalizeCount++;
},
hint);

if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return Value();
}

if (buffer.Data() != data) {
Error::New(info.Env(), "Incorrect buffer data.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect buffer data.")
.ThrowAsJavaScriptException();
return Value();
}

Expand All @@ -114,31 +118,35 @@ Value CreateExternalBufferWithFinalizeHint(const CallbackInfo& info) {

void CheckBuffer(const CallbackInfo& info) {
if (!info[0].IsArrayBuffer()) {
Error::New(info.Env(), "A buffer was expected.").ThrowAsJavaScriptException();
Error::New(info.Env(), "A buffer was expected.")
.ThrowAsJavaScriptException();
return;
}

ArrayBuffer buffer = info[0].As<ArrayBuffer>();

if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return;
}

if (!VerifyData(static_cast<uint8_t*>(buffer.Data()), testLength)) {
Error::New(info.Env(), "Incorrect buffer data.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect buffer data.")
.ThrowAsJavaScriptException();
return;
}
}

Value GetFinalizeCount(const CallbackInfo& info) {
return Number::New(info.Env(), finalizeCount);
return Number::New(info.Env(), finalizeCount);
}

Value CreateBufferWithConstructor(const CallbackInfo& info) {
ArrayBuffer buffer = ArrayBuffer::New(info.Env(), testLength);
if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return Value();
}
InitData(static_cast<uint8_t*>(buffer.Data()), testLength);
Expand All @@ -153,7 +161,8 @@ Value CheckEmptyBuffer(const CallbackInfo& info) {

void CheckDetachUpdatesData(const CallbackInfo& info) {
if (!info[0].IsArrayBuffer()) {
Error::New(info.Env(), "A buffer was expected.").ThrowAsJavaScriptException();
Error::New(info.Env(), "A buffer was expected.")
.ThrowAsJavaScriptException();
return;
}

Expand All @@ -165,15 +174,17 @@ void CheckDetachUpdatesData(const CallbackInfo& info) {

#if NAPI_VERSION >= 7
if (buffer.IsDetached()) {
Error::New(info.Env(), "Buffer should not be detached.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Buffer should not be detached.")
.ThrowAsJavaScriptException();
return;
}
#endif

if (info.Length() == 2) {
// Detach externally (in JavaScript).
if (!info[1].IsFunction()) {
Error::New(info.Env(), "A function was expected.").ThrowAsJavaScriptException();
Error::New(info.Env(), "A function was expected.")
.ThrowAsJavaScriptException();
return;
}

Expand All @@ -190,38 +201,43 @@ void CheckDetachUpdatesData(const CallbackInfo& info) {

#if NAPI_VERSION >= 7
if (!buffer.IsDetached()) {
Error::New(info.Env(), "Buffer should be detached.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Buffer should be detached.")
.ThrowAsJavaScriptException();
return;
}
#endif

if (buffer.Data() != nullptr) {
Error::New(info.Env(), "Incorrect data pointer.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect data pointer.")
.ThrowAsJavaScriptException();
return;
}

if (buffer.ByteLength() != 0) {
Error::New(info.Env(), "Incorrect buffer length.").ThrowAsJavaScriptException();
Error::New(info.Env(), "Incorrect buffer length.")
.ThrowAsJavaScriptException();
return;
}
}

} // end anonymous namespace
} // end anonymous namespace

Object InitArrayBuffer(Env env) {
Object exports = Object::New(env);

exports["createBuffer"] = Function::New(env, CreateBuffer);
exports["createExternalBuffer"] = Function::New(env, CreateExternalBuffer);
exports["createExternalBufferWithFinalize"] =
Function::New(env, CreateExternalBufferWithFinalize);
Function::New(env, CreateExternalBufferWithFinalize);
exports["createExternalBufferWithFinalizeHint"] =
Function::New(env, CreateExternalBufferWithFinalizeHint);
Function::New(env, CreateExternalBufferWithFinalizeHint);
exports["checkBuffer"] = Function::New(env, CheckBuffer);
exports["getFinalizeCount"] = Function::New(env, GetFinalizeCount);
exports["createBufferWithConstructor"] = Function::New(env, CreateBufferWithConstructor);
exports["createBufferWithConstructor"] =
Function::New(env, CreateBufferWithConstructor);
exports["checkEmptyBuffer"] = Function::New(env, CheckEmptyBuffer);
exports["checkDetachUpdatesData"] = Function::New(env, CheckDetachUpdatesData);
exports["checkDetachUpdatesData"] =
Function::New(env, CheckDetachUpdatesData);

return exports;
}
File renamed without changes.
6 changes: 3 additions & 3 deletions test/asynccontext.cc → test/async_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ static void MakeCallback(const CallbackInfo& info) {
Function callback = info[0].As<Function>();
Object resource = info[1].As<Object>();
AsyncContext context(info.Env(), "async_context_test", resource);
callback.MakeCallback(Object::New(info.Env()),
std::initializer_list<napi_value>{}, context);
callback.MakeCallback(
Object::New(info.Env()), std::initializer_list<napi_value>{}, context);
}

} // end anonymous namespace
} // end anonymous namespace

Object InitAsyncContext(Env env) {
Object exports = Object::New(env);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ struct ProgressData {
};

class TestWorker : public AsyncProgressQueueWorker<ProgressData> {
public:
public:
static Napi::Value CreateWork(const CallbackInfo& info) {
int32_t times = info[0].As<Number>().Int32Value();
Function cb = info[1].As<Function>();
Function progress = info[2].As<Function>();

TestWorker* worker = new TestWorker(cb,
progress,
"TestResource",
Object::New(info.Env()),
times);
TestWorker* worker = new TestWorker(
cb, progress, "TestResource", Object::New(info.Env()), times);

return Napi::External<TestWorker>::New(info.Env(), worker);
}
Expand All @@ -37,7 +34,7 @@ class TestWorker : public AsyncProgressQueueWorker<ProgressData> {
worker->Queue();
}

protected:
protected:
void Execute(const ExecutionProgress& progress) override {
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
Expand All @@ -56,26 +53,25 @@ class TestWorker : public AsyncProgressQueueWorker<ProgressData> {
Napi::Env env = Env();
if (!_js_progress_cb.IsEmpty()) {
Number progress = Number::New(env, data->progress);
_js_progress_cb.Call(Receiver().Value(), { progress });
_js_progress_cb.Call(Receiver().Value(), {progress});
}
}

private:
private:
TestWorker(Function cb,
Function progress,
const char* resource_name,
const Object& resource,
int32_t times)
: AsyncProgressQueueWorker(cb, resource_name, resource),
_times(times) {
: AsyncProgressQueueWorker(cb, resource_name, resource), _times(times) {
_js_progress_cb.Reset(progress, 1);
}

int32_t _times;
FunctionReference _js_progress_cb;
};

} // namespace
} // namespace

Object InitAsyncProgressQueueWorker(Env env) {
Object exports = Object::New(env);
Expand Down
File renamed without changes.
18 changes: 11 additions & 7 deletions test/asyncprogressworker.cc → test/async_progress_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ struct ProgressData {
};

class TestWorker : public AsyncProgressWorker<ProgressData> {
public:
public:
static void DoWork(const CallbackInfo& info) {
int32_t times = info[0].As<Number>().Int32Value();
Function cb = info[1].As<Function>();
Function progress = info[2].As<Function>();

TestWorker* worker = new TestWorker(cb, progress, "TestResource", Object::New(info.Env()));
TestWorker* worker =
new TestWorker(cb, progress, "TestResource", Object::New(info.Env()));
worker->_times = times;
worker->Queue();
}

protected:
protected:
void Execute(const ExecutionProgress& progress) override {
if (_times < 0) {
SetError("test error");
Expand All @@ -45,13 +46,16 @@ class TestWorker : public AsyncProgressWorker<ProgressData> {
Napi::Env env = Env();
if (!_progress.IsEmpty()) {
Number progress = Number::New(env, data->progress);
_progress.MakeCallback(Receiver().Value(), { progress });
_progress.MakeCallback(Receiver().Value(), {progress});
}
_cv.notify_one();
}

private:
TestWorker(Function cb, Function progress, const char* resource_name, const Object& resource)
private:
TestWorker(Function cb,
Function progress,
const char* resource_name,
const Object& resource)
: AsyncProgressWorker(cb, resource_name, resource) {
_progress.Reset(progress, 1);
}
Expand Down Expand Up @@ -118,7 +122,7 @@ class MalignWorker : public AsyncProgressWorker<ProgressData> {
std::mutex _cvm;
FunctionReference _progress;
};
}
} // namespace

Object InitAsyncProgressWorker(Env env) {
Object exports = Object::New(env);
Expand Down
File renamed without changes.
Loading

0 comments on commit cbac3aa

Please sign in to comment.