GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../test/cctest/test_environment.cc Lines: 70 70 100.0 %
Date: 2019-01-07 12:15:22 Branches: 26 50 52.0 %

Line Branch Exec Source
1
#include "node_internals.h"
2
#include "libplatform/libplatform.h"
3
4
#include <string>
5
#include "gtest/gtest.h"
6
#include "node_test_fixture.h"
7
8
using node::AtExit;
9
using node::RunAtExit;
10
11
static bool called_cb_1 = false;
12
static bool called_cb_2 = false;
13
static void at_exit_callback1(void* arg);
14
static void at_exit_callback2(void* arg);
15
1
static std::string cb_1_arg;  // NOLINT(runtime/string)
16
17
10
class EnvironmentTest : public EnvironmentTestFixture {
18
 private:
19
5
  virtual void TearDown() {
20
5
    NodeTestFixture::TearDown();
21
5
    called_cb_1 = false;
22
5
    called_cb_2 = false;
23
5
  }
24
};
25
26
5
TEST_F(EnvironmentTest, AtExitWithEnvironment) {
27
1
  const v8::HandleScope handle_scope(isolate_);
28
2
  const Argv argv;
29
2
  Env env {handle_scope, argv};
30
31
1
  AtExit(*env, at_exit_callback1);
32
1
  RunAtExit(*env);
33
2
  EXPECT_TRUE(called_cb_1);
34
1
}
35
36
5
TEST_F(EnvironmentTest, AtExitWithoutEnvironment) {
37
1
  const v8::HandleScope handle_scope(isolate_);
38
2
  const Argv argv;
39
2
  Env env {handle_scope, argv};
40
41
1
  AtExit(at_exit_callback1);  // No Environment is passed to AtExit.
42
1
  RunAtExit(*env);
43
2
  EXPECT_TRUE(called_cb_1);
44
1
}
45
46
5
TEST_F(EnvironmentTest, AtExitWithArgument) {
47
1
  const v8::HandleScope handle_scope(isolate_);
48
2
  const Argv argv;
49
2
  Env env {handle_scope, argv};
50
51
2
  std::string arg{"some args"};
52
1
  AtExit(*env, at_exit_callback1, static_cast<void*>(&arg));
53
1
  RunAtExit(*env);
54
2
  EXPECT_EQ(arg, cb_1_arg);
55
1
}
56
57
5
TEST_F(EnvironmentTest, MultipleEnvironmentsPerIsolate) {
58
1
  const v8::HandleScope handle_scope(isolate_);
59
2
  const Argv argv;
60
2
  Env env1 {handle_scope, argv};
61
2
  Env env2 {handle_scope, argv};
62
63
1
  AtExit(*env1, at_exit_callback1);
64
1
  AtExit(*env2, at_exit_callback2);
65
1
  RunAtExit(*env1);
66
1
  EXPECT_TRUE(called_cb_1);
67
1
  EXPECT_FALSE(called_cb_2);
68
69
1
  RunAtExit(*env2);
70
2
  EXPECT_TRUE(called_cb_2);
71
1
}
72
73
5
TEST_F(EnvironmentTest, NonNodeJSContext) {
74
1
  const v8::HandleScope handle_scope(isolate_);
75
2
  const Argv argv;
76
2
  Env test_env {handle_scope, argv};
77
78
2
  EXPECT_EQ(node::Environment::GetCurrent(v8::Local<v8::Context>()), nullptr);
79
80
1
  node::Environment* env = *test_env;
81
1
  EXPECT_EQ(node::Environment::GetCurrent(isolate_), env);
82
1
  EXPECT_EQ(node::Environment::GetCurrent(env->context()), env);
83
1
  EXPECT_EQ(node::GetCurrentEnvironment(env->context()), env);
84
85
2
  v8::Local<v8::Context> context = v8::Context::New(isolate_);
86
1
  EXPECT_EQ(node::Environment::GetCurrent(context), nullptr);
87
1
  EXPECT_EQ(node::GetCurrentEnvironment(context), nullptr);
88
1
  EXPECT_EQ(node::Environment::GetCurrent(isolate_), env);
89
90
  v8::Context::Scope context_scope(context);
91
1
  EXPECT_EQ(node::Environment::GetCurrent(context), nullptr);
92
1
  EXPECT_EQ(node::GetCurrentEnvironment(context), nullptr);
93
2
  EXPECT_EQ(node::Environment::GetCurrent(isolate_), nullptr);
94
1
}
95
96
4
static void at_exit_callback1(void* arg) {
97
4
  called_cb_1 = true;
98
4
  if (arg) {
99
1
    cb_1_arg = *static_cast<std::string*>(arg);
100
  }
101
4
}
102
103
1
static void at_exit_callback2(void* arg) {
104
1
  called_cb_2 = true;
105

4
}