Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoopa committed Jun 15, 2016
1 parent e58252a commit 267ce79
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ LINT_SOURCES = \
test/cpp/morenews.cpp \
test/cpp/converters.cpp \
test/cpp/isolatedata.cpp \
test/cpp/global.cpp \
test/cpp/makecallback.cpp \
test/cpp/morenews.cpp \
test/cpp/multifile1.cpp \
Expand Down
4 changes: 4 additions & 0 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,8 @@
"target_name" : "typedarrays"
, "sources" : [ "cpp/typedarrays.cpp" ]
}
, {
"target_name" : "global"
, "sources" : [ "cpp/global.cpp" ]
}
]}
75 changes: 75 additions & 0 deletions test/cpp/global.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2016 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/

#include <nan.h>

using namespace Nan; // NOLINT(build/namespaces)

template<typename T> Global<T> passer(v8::Local<T> handle) {
return Global<T>(handle).Pass();
}

NAN_METHOD(PassGlobal) {
info.GetReturnValue().Set(passer(New(42)));
}

NAN_METHOD(EmptyGlobal) {
Global<v8::String> g(New("value").ToLocalChecked());
bool b1 = !g.IsEmpty();
g.Empty(); // this will leak, never do it
info.GetReturnValue().Set(b1 && g.IsEmpty());
}

NAN_METHOD(MoveConstructGlobal) {
Global<v8::String> g(Global<v8::String>(New("value").ToLocalChecked()));
info.GetReturnValue().Set(!g.IsEmpty());
}

NAN_METHOD(CopyConstructGlobal) {
Persistent<v8::String> p(New("value").ToLocalChecked());
bool b1 = !p.IsEmpty();
Global<v8::String> g(p);
bool b2 = !p.IsEmpty();
p.Reset();
info.GetReturnValue().Set(b1 && b2 && !g.IsEmpty());
}

NAN_METHOD(MoveAssignGlobal) {
Global<v8::String> g = passer(New("value").ToLocalChecked());
info.GetReturnValue().Set(!g.IsEmpty());
}

NAN_MODULE_INIT(Init) {
Set(target
, New<v8::String>("passGlobal").ToLocalChecked()
, GetFunction(New<v8::FunctionTemplate>(PassGlobal))
.ToLocalChecked()
);
Set(target
, New<v8::String>("emptyGlobal").ToLocalChecked()
, GetFunction(New<v8::FunctionTemplate>(EmptyGlobal))
.ToLocalChecked()
);
Set(target
, New<v8::String>("moveConstructGlobal").ToLocalChecked()
, GetFunction(New<v8::FunctionTemplate>(MoveConstructGlobal))
.ToLocalChecked()
);
Set(target
, New<v8::String>("copyConstructGlobal").ToLocalChecked()
, GetFunction(New<v8::FunctionTemplate>(CopyConstructGlobal))
.ToLocalChecked()
);
Set(target
, New<v8::String>("moveAssignGlobal").ToLocalChecked()
, GetFunction(New<v8::FunctionTemplate>(MoveAssignGlobal))
.ToLocalChecked()
);
}

NODE_MODULE(global, Init)
59 changes: 59 additions & 0 deletions test/cpp/persistent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using namespace Nan; // NOLINT(build/namespaces)

static Persistent<v8::String> persistentTest1;
static Persistent<v8::String> persistentTest2;

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

NAN_METHOD(EmptyPersistent) {
persistentTest2.Reset(New("value").ToLocalChecked());
bool b1 = !persistentTest2.IsEmpty();
persistentTest2.Empty(); // this will leak, never do it
info.GetReturnValue().Set(b1 && persistentTest2.IsEmpty());
}

NAN_METHOD(EmptyGlobal) {
Global<v8::String> g(New("value").ToLocalChecked());
bool b1 = !g.IsEmpty();
g.Empty(); // this will leak, never do it
info.GetReturnValue().Set(b1 && g.IsEmpty());
}

NAN_METHOD(MoveConstructGlobal) {
Global<v8::String> g(Global<v8::String>(New("value").ToLocalChecked()));
info.GetReturnValue().Set(!g.IsEmpty());
}

NAN_METHOD(CopyConstructGlobal) {
Persistent<v8::String> p(New("value").ToLocalChecked());
bool b1 = !p.IsEmpty();
Global<v8::String> g(p);
bool b2 = !p.IsEmpty();
p.Reset();
info.GetReturnValue().Set(b1 && b2 && !g.IsEmpty());
}

NAN_METHOD(MoveAssignGlobal) {
Global<v8::String> g = passer(New("value").ToLocalChecked());
info.GetReturnValue().Set(!g.IsEmpty());
}

NAN_MODULE_INIT(Init) {
Set(target
, New<v8::String>("save1").ToLocalChecked()
Expand Down Expand Up @@ -88,6 +122,31 @@ NAN_MODULE_INIT(Init) {
, GetFunction(New<v8::FunctionTemplate>(PassGlobal))
.ToLocalChecked()
);
Set(target
, New<v8::String>("emptyPersistent").ToLocalChecked()
, GetFunction(New<v8::FunctionTemplate>(EmptyPersistent))
.ToLocalChecked()
);
Set(target
, New<v8::String>("emptyGlobal").ToLocalChecked()
, GetFunction(New<v8::FunctionTemplate>(EmptyGlobal))
.ToLocalChecked()
);
Set(target
, New<v8::String>("moveConstructGlobal").ToLocalChecked()
, GetFunction(New<v8::FunctionTemplate>(MoveConstructGlobal))
.ToLocalChecked()
);
Set(target
, New<v8::String>("copyConstructGlobal").ToLocalChecked()
, GetFunction(New<v8::FunctionTemplate>(CopyConstructGlobal))
.ToLocalChecked()
);
Set(target
, New<v8::String>("moveAssignGlobal").ToLocalChecked()
, GetFunction(New<v8::FunctionTemplate>(MoveAssignGlobal))
.ToLocalChecked()
);
}

NODE_MODULE(persistent, Init)
20 changes: 20 additions & 0 deletions test/cpp/weak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using namespace Nan; // NOLINT(build/namespaces)

static Persistent<v8::Function> cb;
static Global<v8::Function> gcb;

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

v8::Local<v8::String> gwrap(v8::Local<v8::Function> func) {
EscapableHandleScope scope;
v8::Local<v8::String> lstring = New<v8::String>("result").ToLocalChecked();
int *parameter = new int(42);
gcb.Reset(func);
gcb.SetWeak(parameter, weakCallback, WeakCallbackType::kParameter);
assert(gcb.IsWeak());
return scope.Escape(lstring);
}

NAN_METHOD(Hustle) {
cb.Reset(info[1].As<v8::Function>());
info.GetReturnValue().Set(wrap(info[0].As<v8::Function>()));
}

NAN_METHOD(HustleGlobal) {
cb.Reset(info[1].As<v8::Function>());
info.GetReturnValue().Set(gwrap(info[0].As<v8::Function>()));
}

NAN_MODULE_INIT(Init) {
Set(target
, New<v8::String>("hustle").ToLocalChecked()
, New<v8::FunctionTemplate>(Hustle)->GetFunction()
);
Set(target
, New<v8::String>("hustleGlobal").ToLocalChecked()
, New<v8::FunctionTemplate>(HustleGlobal)->GetFunction()
);
}

NODE_MODULE(weak, Init)
28 changes: 28 additions & 0 deletions test/js/global-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2016 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/

const test = require('tap').test
, testRoot = require('path').resolve(__dirname, '..')
, bindings = require('bindings')({ module_root: testRoot, bindings: 'global' });

test('global', function (t) {
t.plan(10);

var global_ = bindings;
t.type(global_.passGlobal, 'function');
t.type(global_.emptyGlobal, 'function');
t.type(global_.moveConstructGlobal, 'function');
t.type(global_.copyConstructGlobal, 'function');
t.type(global_.moveAssignGlobal, 'function');

t.equal(global_.passGlobal(), 42, 'pass global');
t.ok(global_.emptyGlobal());
t.ok(global_.moveConstructGlobal());
t.ok(global_.copyConstructGlobal());
t.ok(global_.moveAssignGlobal());
});
6 changes: 3 additions & 3 deletions test/js/persistent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ test('persistent', function (t) {
t.type(persistent.toPersistentAndBackAgain, 'function');
t.type(persistent.persistentToPersistent, 'function');
t.type(persistent.copyablePersistent, 'function');
t.type(persistent.passGlobal, 'function');
t.type(persistent.emptyPersistent, 'function');

t.ok(persistent.emptyPersistent());

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

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

t.equal(persistent.passGlobal(), 42, 'pass global');

setTimeout(function () {
t.equal(persistent.get1(), 'a string to save');
persistent.dispose1();
Expand Down
18 changes: 17 additions & 1 deletion test/js/weak-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ const test = require('tap').test
, bindings = require('bindings')({ module_root: testRoot, bindings: 'weak' });

test('weak', function (t) {
t.plan(3);
t.plan(6);

var weak = bindings, count = 0;
t.type(weak.hustle, 'function');
t.type(weak.hustleGlobal, 'function');

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

t.equal(count, 1);

count = 0;

weak.hustleGlobal(function () {}, function (val) {
t.equal(val, 42);
count++;
});

// run weak callback, should dispose
gc();

// do not run weak callback
gc();

t.equal(count, 1);
});

0 comments on commit 267ce79

Please sign in to comment.