node/test/cctest/test_report.cc
Joyee Cheung 42d8143ce5
test: make IsolateData per-isolate in cctest
This ensures that we only create one IsolateData for each isolate
inthe cctest, since IsolateData are meant to be per-isolate.
We need to make the isolate and isolate_data static in the
test fixtures as a result, similar to how the event loops and
array buffer allocators are managed in the
NodeZeroIsolateTestFixture but it is fine because gtest ensures
that the Setup() and TearDown() of the fixtures are always run
in order and would never overlap in one process.

PR-URL: https://github.com/nodejs/node/pull/48450
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2023-06-25 05:41:19 +00:00

126 lines
3.7 KiB
C++

#include "node.h"
#include <string>
#include "gtest/gtest.h"
#include "node_test_fixture.h"
using node::Environment;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::SealHandleScope;
using v8::String;
using v8::Value;
bool report_callback_called = false;
class ReportTest : public EnvironmentTestFixture {
private:
void TearDown() override {
EnvironmentTestFixture::TearDown();
report_callback_called = false;
}
};
TEST_F(ReportTest, ReportWithNoIsolate) {
SealHandleScope handle_scope(isolate_);
std::ostringstream oss;
node::GetNodeReport(static_cast<Isolate*>(nullptr),
"FooMessage",
"BarTrigger",
Local<Value>(),
oss);
// Simple checks on the output string contains the message and trigger.
std::string actual = oss.str();
EXPECT_NE(actual.find("FooMessage"), std::string::npos);
EXPECT_NE(actual.find("BarTrigger"), std::string::npos);
}
TEST_F(ReportTest, ReportWithNoEnv) {
SealHandleScope handle_scope(isolate_);
std::ostringstream oss;
node::GetNodeReport(static_cast<Environment*>(nullptr),
"FooMessage",
"BarTrigger",
Local<Value>(),
oss);
// Simple checks on the output string contains the message and trigger.
std::string actual = oss.str();
EXPECT_NE(actual.find("FooMessage"), std::string::npos);
EXPECT_NE(actual.find("BarTrigger"), std::string::npos);
}
TEST_F(ReportTest, ReportWithIsolate) {
const HandleScope handle_scope(isolate_);
const Argv argv;
Env env{handle_scope, argv};
Local<Context> context = isolate_->GetCurrentContext();
Local<Function> fn =
Function::New(context, [](const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
std::ostringstream oss;
node::GetNodeReport(isolate, "FooMessage", "BarTrigger", args[0], oss);
// Simple checks on the output string contains the message and trigger.
std::string actual = oss.str();
EXPECT_NE(actual.find("FooMessage"), std::string::npos);
EXPECT_NE(actual.find("BarTrigger"), std::string::npos);
report_callback_called = true;
}).ToLocalChecked();
context->Global()
->Set(context, String::NewFromUtf8(isolate_, "foo").ToLocalChecked(), fn)
.FromJust();
node::LoadEnvironment(*env, "foo()").ToLocalChecked();
EXPECT_TRUE(report_callback_called);
}
TEST_F(ReportTest, ReportWithEnv) {
const HandleScope handle_scope(isolate_);
const Argv argv;
Env env{handle_scope, argv};
Local<Context> context = isolate_->GetCurrentContext();
Local<Function> fn =
Function::New(context, [](const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
std::ostringstream oss;
node::GetNodeReport(
node::GetCurrentEnvironment(isolate->GetCurrentContext()),
"FooMessage",
"BarTrigger",
args[0],
oss);
// Simple checks on the output string contains the message and trigger.
std::string actual = oss.str();
EXPECT_NE(actual.find("FooMessage"), std::string::npos);
EXPECT_NE(actual.find("BarTrigger"), std::string::npos);
report_callback_called = true;
}).ToLocalChecked();
context->Global()
->Set(context, String::NewFromUtf8(isolate_, "foo").ToLocalChecked(), fn)
.FromJust();
node::LoadEnvironment(*env, "foo()").ToLocalChecked();
EXPECT_TRUE(report_callback_called);
}