Skip to content

Commit 3ba2162

Browse files
committed
Add more tests
1 parent e58252a commit 3ba2162

File tree

8 files changed

+207
-4
lines changed

8 files changed

+207
-4
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ LINT_SOURCES = \
4747
test/cpp/morenews.cpp \
4848
test/cpp/converters.cpp \
4949
test/cpp/isolatedata.cpp \
50+
test/cpp/global.cpp \
5051
test/cpp/makecallback.cpp \
5152
test/cpp/morenews.cpp \
5253
test/cpp/multifile1.cpp \

test/binding.gyp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,8 @@
148148
"target_name" : "typedarrays"
149149
, "sources" : [ "cpp/typedarrays.cpp" ]
150150
}
151+
, {
152+
"target_name" : "global"
153+
, "sources" : [ "cpp/global.cpp" ]
154+
}
151155
]}

test/cpp/global.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*********************************************************************
2+
* NAN - Native Abstractions for Node.js
3+
*
4+
* Copyright (c) 2016 NAN contributors
5+
*
6+
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7+
********************************************************************/
8+
9+
#include <nan.h>
10+
11+
using namespace Nan; // NOLINT(build/namespaces)
12+
13+
template<typename T> Global<T> passer(v8::Local<T> handle) {
14+
return Global<T>(handle).Pass();
15+
}
16+
17+
NAN_METHOD(PassGlobal) {
18+
info.GetReturnValue().Set(passer(New(42)));
19+
}
20+
21+
NAN_METHOD(EmptyGlobal) {
22+
Global<v8::String> g(New("value").ToLocalChecked());
23+
bool b1 = !g.IsEmpty();
24+
g.Empty(); // this will leak, never do it
25+
info.GetReturnValue().Set(b1 && g.IsEmpty());
26+
}
27+
28+
NAN_METHOD(MoveConstructGlobal) {
29+
Global<v8::String> g(Global<v8::String>(New("value").ToLocalChecked()));
30+
info.GetReturnValue().Set(!g.IsEmpty());
31+
}
32+
33+
NAN_METHOD(CopyConstructGlobal) {
34+
Persistent<v8::String> p(New("value").ToLocalChecked());
35+
bool b1 = !p.IsEmpty();
36+
Global<v8::String> g(p);
37+
bool b2 = !p.IsEmpty();
38+
p.Reset();
39+
info.GetReturnValue().Set(b1 && b2 && !g.IsEmpty());
40+
}
41+
42+
NAN_METHOD(MoveAssignGlobal) {
43+
Global<v8::String> g = passer(New("value").ToLocalChecked());
44+
info.GetReturnValue().Set(!g.IsEmpty());
45+
}
46+
47+
NAN_MODULE_INIT(Init) {
48+
Set(target
49+
, New<v8::String>("passGlobal").ToLocalChecked()
50+
, GetFunction(New<v8::FunctionTemplate>(PassGlobal))
51+
.ToLocalChecked()
52+
);
53+
Set(target
54+
, New<v8::String>("emptyGlobal").ToLocalChecked()
55+
, GetFunction(New<v8::FunctionTemplate>(EmptyGlobal))
56+
.ToLocalChecked()
57+
);
58+
Set(target
59+
, New<v8::String>("moveConstructGlobal").ToLocalChecked()
60+
, GetFunction(New<v8::FunctionTemplate>(MoveConstructGlobal))
61+
.ToLocalChecked()
62+
);
63+
Set(target
64+
, New<v8::String>("copyConstructGlobal").ToLocalChecked()
65+
, GetFunction(New<v8::FunctionTemplate>(CopyConstructGlobal))
66+
.ToLocalChecked()
67+
);
68+
Set(target
69+
, New<v8::String>("moveAssignGlobal").ToLocalChecked()
70+
, GetFunction(New<v8::FunctionTemplate>(MoveAssignGlobal))
71+
.ToLocalChecked()
72+
);
73+
}
74+
75+
NODE_MODULE(global, Init)

test/cpp/persistent.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using namespace Nan; // NOLINT(build/namespaces)
1313

1414
static Persistent<v8::String> persistentTest1;
15+
static Persistent<v8::String> persistentTest2;
1516

1617
NAN_METHOD(Save1) {
1718
persistentTest1.Reset(info[0].As<v8::String>());
@@ -55,6 +56,39 @@ NAN_METHOD(PassGlobal) {
5556
info.GetReturnValue().Set(passer(New(42)));
5657
}
5758

59+
NAN_METHOD(EmptyPersistent) {
60+
persistentTest2.Reset(New("value").ToLocalChecked());
61+
bool b1 = !persistentTest2.IsEmpty();
62+
persistentTest2.Empty(); // this will leak, never do it
63+
info.GetReturnValue().Set(b1 && persistentTest2.IsEmpty());
64+
}
65+
66+
NAN_METHOD(EmptyGlobal) {
67+
Global<v8::String> g(New("value").ToLocalChecked());
68+
bool b1 = !g.IsEmpty();
69+
g.Empty(); // this will leak, never do it
70+
info.GetReturnValue().Set(b1 && g.IsEmpty());
71+
}
72+
73+
NAN_METHOD(MoveConstructGlobal) {
74+
Global<v8::String> g(Global<v8::String>(New("value").ToLocalChecked()));
75+
info.GetReturnValue().Set(!g.IsEmpty());
76+
}
77+
78+
NAN_METHOD(CopyConstructGlobal) {
79+
Persistent<v8::String> p(New("value").ToLocalChecked());
80+
bool b1 = !p.IsEmpty();
81+
Global<v8::String> g(p);
82+
bool b2 = !p.IsEmpty();
83+
p.Reset();
84+
info.GetReturnValue().Set(b1 && b2 && !g.IsEmpty());
85+
}
86+
87+
NAN_METHOD(MoveAssignGlobal) {
88+
Global<v8::String> g = passer(New("value").ToLocalChecked());
89+
info.GetReturnValue().Set(!g.IsEmpty());
90+
}
91+
5892
NAN_MODULE_INIT(Init) {
5993
Set(target
6094
, New<v8::String>("save1").ToLocalChecked()
@@ -88,6 +122,31 @@ NAN_MODULE_INIT(Init) {
88122
, GetFunction(New<v8::FunctionTemplate>(PassGlobal))
89123
.ToLocalChecked()
90124
);
125+
Set(target
126+
, New<v8::String>("emptyPersistent").ToLocalChecked()
127+
, GetFunction(New<v8::FunctionTemplate>(EmptyPersistent))
128+
.ToLocalChecked()
129+
);
130+
Set(target
131+
, New<v8::String>("emptyGlobal").ToLocalChecked()
132+
, GetFunction(New<v8::FunctionTemplate>(EmptyGlobal))
133+
.ToLocalChecked()
134+
);
135+
Set(target
136+
, New<v8::String>("moveConstructGlobal").ToLocalChecked()
137+
, GetFunction(New<v8::FunctionTemplate>(MoveConstructGlobal))
138+
.ToLocalChecked()
139+
);
140+
Set(target
141+
, New<v8::String>("copyConstructGlobal").ToLocalChecked()
142+
, GetFunction(New<v8::FunctionTemplate>(CopyConstructGlobal))
143+
.ToLocalChecked()
144+
);
145+
Set(target
146+
, New<v8::String>("moveAssignGlobal").ToLocalChecked()
147+
, GetFunction(New<v8::FunctionTemplate>(MoveAssignGlobal))
148+
.ToLocalChecked()
149+
);
91150
}
92151

93152
NODE_MODULE(persistent, Init)

test/cpp/weak.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using namespace Nan; // NOLINT(build/namespaces)
1212

1313
static Persistent<v8::Function> cb;
14+
static Global<v8::Function> gcb;
1415

1516
void weakCallback(
1617
const WeakCallbackInfo<int> &data) { // NOLINT(runtime/references)
@@ -30,16 +31,35 @@ v8::Local<v8::String> wrap(v8::Local<v8::Function> func) {
3031
return scope.Escape(lstring);
3132
}
3233

34+
v8::Local<v8::String> gwrap(v8::Local<v8::Function> func) {
35+
EscapableHandleScope scope;
36+
v8::Local<v8::String> lstring = New<v8::String>("result").ToLocalChecked();
37+
int *parameter = new int(42);
38+
gcb.Reset(func);
39+
gcb.SetWeak(parameter, weakCallback, WeakCallbackType::kParameter);
40+
assert(gcb.IsWeak());
41+
return scope.Escape(lstring);
42+
}
43+
3344
NAN_METHOD(Hustle) {
3445
cb.Reset(info[1].As<v8::Function>());
3546
info.GetReturnValue().Set(wrap(info[0].As<v8::Function>()));
3647
}
3748

49+
NAN_METHOD(HustleGlobal) {
50+
cb.Reset(info[1].As<v8::Function>());
51+
info.GetReturnValue().Set(gwrap(info[0].As<v8::Function>()));
52+
}
53+
3854
NAN_MODULE_INIT(Init) {
3955
Set(target
4056
, New<v8::String>("hustle").ToLocalChecked()
4157
, New<v8::FunctionTemplate>(Hustle)->GetFunction()
4258
);
59+
Set(target
60+
, New<v8::String>("hustleGlobal").ToLocalChecked()
61+
, New<v8::FunctionTemplate>(HustleGlobal)->GetFunction()
62+
);
4363
}
4464

4565
NODE_MODULE(weak, Init)

test/js/global-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*********************************************************************
2+
* NAN - Native Abstractions for Node.js
3+
*
4+
* Copyright (c) 2016 NAN contributors
5+
*
6+
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7+
********************************************************************/
8+
9+
const test = require('tap').test
10+
, testRoot = require('path').resolve(__dirname, '..')
11+
, bindings = require('bindings')({ module_root: testRoot, bindings: 'global' });
12+
13+
test('global', function (t) {
14+
t.plan(10);
15+
16+
var global_ = bindings;
17+
t.type(global_.passGlobal, 'function');
18+
t.type(global_.emptyGlobal, 'function');
19+
t.type(global_.moveConstructGlobal, 'function');
20+
t.type(global_.copyConstructGlobal, 'function');
21+
t.type(global_.moveAssignGlobal, 'function');
22+
23+
t.equal(global_.passGlobal(), 42, 'pass global');
24+
t.ok(global_.emptyGlobal());
25+
t.ok(global_.moveConstructGlobal());
26+
t.ok(global_.copyConstructGlobal());
27+
t.ok(global_.moveAssignGlobal());
28+
});

test/js/persistent-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ test('persistent', function (t) {
2020
t.type(persistent.toPersistentAndBackAgain, 'function');
2121
t.type(persistent.persistentToPersistent, 'function');
2222
t.type(persistent.copyablePersistent, 'function');
23-
t.type(persistent.passGlobal, 'function');
23+
t.type(persistent.emptyPersistent, 'function');
24+
25+
t.ok(persistent.emptyPersistent());
2426

2527
t.deepEqual(persistent.toPersistentAndBackAgain({ x: 42 }), { x: 42 });
2628

@@ -30,8 +32,6 @@ test('persistent', function (t) {
3032
t.equal(persistent.get1(), 'a string to save');
3133
t.equal(persistent.copyablePersistent(), 'a string to save');
3234

33-
t.equal(persistent.passGlobal(), 42, 'pass global');
34-
3535
setTimeout(function () {
3636
t.equal(persistent.get1(), 'a string to save');
3737
persistent.dispose1();

test/js/weak-test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ const test = require('tap').test
1111
, bindings = require('bindings')({ module_root: testRoot, bindings: 'weak' });
1212

1313
test('weak', function (t) {
14-
t.plan(3);
14+
t.plan(6);
1515

1616
var weak = bindings, count = 0;
1717
t.type(weak.hustle, 'function');
18+
t.type(weak.hustleGlobal, 'function');
1819

1920
weak.hustle(function () {}, function (val) {
2021
t.equal(val, 42);
@@ -27,5 +28,20 @@ test('weak', function (t) {
2728
// do not run weak callback
2829
gc();
2930

31+
t.equal(count, 1);
32+
33+
count = 0;
34+
35+
weak.hustleGlobal(function () {}, function (val) {
36+
t.equal(val, 42);
37+
count++;
38+
});
39+
40+
// run weak callback, should dispose
41+
gc();
42+
43+
// do not run weak callback
44+
gc();
45+
3046
t.equal(count, 1);
3147
});

0 commit comments

Comments
 (0)