• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "node.h"
2 
3 #include <string>
4 #include "gtest/gtest.h"
5 #include "node_test_fixture.h"
6 
7 using node::Environment;
8 using v8::Context;
9 using v8::Function;
10 using v8::FunctionCallbackInfo;
11 using v8::HandleScope;
12 using v8::Isolate;
13 using v8::Local;
14 using v8::SealHandleScope;
15 using v8::String;
16 using v8::Value;
17 
18 bool report_callback_called = false;
19 
20 class ReportTest : public EnvironmentTestFixture {
21  private:
TearDown()22   void TearDown() override {
23     EnvironmentTestFixture::TearDown();
24     report_callback_called = false;
25   }
26 };
27 
TEST_F(ReportTest,ReportWithNoIsolate)28 TEST_F(ReportTest, ReportWithNoIsolate) {
29   SealHandleScope handle_scope(isolate_);
30 
31   std::ostringstream oss;
32   node::GetNodeReport(static_cast<Isolate*>(nullptr),
33                       "FooMessage",
34                       "BarTrigger",
35                       Local<Value>(),
36                       oss);
37 
38   // Simple checks on the output string contains the message and trigger.
39   std::string actual = oss.str();
40   EXPECT_NE(actual.find("FooMessage"), std::string::npos);
41   EXPECT_NE(actual.find("BarTrigger"), std::string::npos);
42 }
43 
TEST_F(ReportTest,ReportWithNoEnv)44 TEST_F(ReportTest, ReportWithNoEnv) {
45   SealHandleScope handle_scope(isolate_);
46 
47   std::ostringstream oss;
48   node::GetNodeReport(static_cast<Environment*>(nullptr),
49                       "FooMessage",
50                       "BarTrigger",
51                       Local<Value>(),
52                       oss);
53 
54   // Simple checks on the output string contains the message and trigger.
55   std::string actual = oss.str();
56   EXPECT_NE(actual.find("FooMessage"), std::string::npos);
57   EXPECT_NE(actual.find("BarTrigger"), std::string::npos);
58 }
59 
TEST_F(ReportTest,ReportWithIsolate)60 TEST_F(ReportTest, ReportWithIsolate) {
61   const HandleScope handle_scope(isolate_);
62   const Argv argv;
63   Env env{handle_scope, argv};
64 
65   Local<Context> context = isolate_->GetCurrentContext();
66   Local<Function> fn =
67       Function::New(context, [](const FunctionCallbackInfo<Value>& args) {
68         Isolate* isolate = args.GetIsolate();
69         HandleScope scope(isolate);
70 
71         std::ostringstream oss;
72         node::GetNodeReport(isolate, "FooMessage", "BarTrigger", args[0], oss);
73 
74         // Simple checks on the output string contains the message and trigger.
75         std::string actual = oss.str();
76         EXPECT_NE(actual.find("FooMessage"), std::string::npos);
77         EXPECT_NE(actual.find("BarTrigger"), std::string::npos);
78 
79         report_callback_called = true;
80       }).ToLocalChecked();
81 
82   context->Global()
83       ->Set(context, String::NewFromUtf8(isolate_, "foo").ToLocalChecked(), fn)
84       .FromJust();
85 
86   node::LoadEnvironment(*env, "foo()").ToLocalChecked();
87 
88   EXPECT_TRUE(report_callback_called);
89 }
90 
TEST_F(ReportTest,ReportWithEnv)91 TEST_F(ReportTest, ReportWithEnv) {
92   const HandleScope handle_scope(isolate_);
93   const Argv argv;
94   Env env{handle_scope, argv};
95 
96   Local<Context> context = isolate_->GetCurrentContext();
97   Local<Function> fn =
98       Function::New(context, [](const FunctionCallbackInfo<Value>& args) {
99         Isolate* isolate = args.GetIsolate();
100         HandleScope scope(isolate);
101 
102         std::ostringstream oss;
103         node::GetNodeReport(
104             node::GetCurrentEnvironment(isolate->GetCurrentContext()),
105             "FooMessage",
106             "BarTrigger",
107             args[0],
108             oss);
109 
110         // Simple checks on the output string contains the message and trigger.
111         std::string actual = oss.str();
112         EXPECT_NE(actual.find("FooMessage"), std::string::npos);
113         EXPECT_NE(actual.find("BarTrigger"), std::string::npos);
114 
115         report_callback_called = true;
116       }).ToLocalChecked();
117 
118   context->Global()
119       ->Set(context, String::NewFromUtf8(isolate_, "foo").ToLocalChecked(), fn)
120       .FromJust();
121 
122   node::LoadEnvironment(*env, "foo()").ToLocalChecked();
123 
124   EXPECT_TRUE(report_callback_called);
125 }
126