-
Notifications
You must be signed in to change notification settings - Fork 465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ObjectReference test case #212
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6937cd9
Add ObjectReference test case
3920572
naming changes
6bba141
Added ref tests and more unref tests
6e6ba3c
added javascript string tests
df90260
added comments to explain parameters and test cases
3f04b75
Added an assert.doesNotThrow to separate assert statements
cefbdd3
Added suppressdestruct in order to prevent segfault
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* ObjectReference cant be used to create references to Values that | ||
are not Objects by creating a blank Object and setting Values to | ||
it. Subclasses of Objects can only be set using an ObjectReference | ||
by first casting it as an Object. */ | ||
|
||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
ObjectReference weak; | ||
ObjectReference persistent; | ||
ObjectReference reference; | ||
|
||
ObjectReference casted_weak; | ||
ObjectReference casted_persistent; | ||
ObjectReference casted_reference; | ||
|
||
void SetObjects(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
HandleScope scope(env); | ||
|
||
dummy_weak = Weak(Object::New(env)); | ||
dummy_persistent = Persistent(Object::New(env)); | ||
dummy_reference = Reference<Object>::New(Object::New(env), 2); | ||
|
||
if (info[0].IsString()) { | ||
if (info[2].As<String>() == String::New(env, "javascript")) { | ||
dummy_weak.Set(info[0].As<String>(), info[1]); | ||
dummy_persistent.Set(info[0].As<String>(), info[1]); | ||
unrefDummy(); | ||
dummy_reference.Set(info[0].As<String>(), info[1]); | ||
} else { | ||
dummy_weak.Set(info[0].As<String>().Utf8Value(), info[1]); | ||
dummy_persistent.Set(info[0].As<String>().Utf8Value(), info[1]); | ||
dummy_reference.Set(info[0].As<String>().Utf8Value(), info[1]); | ||
} | ||
} else if (info[0].IsNumber()) { | ||
dummy_weak.Set(info[0].As<Number>(), info[1]); | ||
dummy_persistent.Set(info[0].As<Number>(), info[1]); | ||
dummy_reference.Set(info[0].As<Number>(), info[1]); | ||
} | ||
} | ||
|
||
void SetCastedObjects(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
HandleScope scope(env); | ||
|
||
Array ex = Array::New(env); | ||
ex.Set(Number::New(env, 0), String::New(env, "hello")); | ||
ex.Set(Number::New(env, 1), String::New(env, "world")); | ||
ex.Set(Number::New(env, 2), String::New(env, "!")); | ||
|
||
casted_weak = Weak(ex.As<Object>()); | ||
casted_persistent = Persistent(ex.As<Object>()); | ||
casted_reference = Reference<Object>::New(ex.As<Object>(), 2); | ||
} | ||
|
||
|
||
Value GetFromValue(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
|
||
if (info[0].As<String>() == String::New(env, "weak")) { | ||
if (dummy_weak.IsEmpty()) { | ||
return String::New(env, "No Referenced Value"); | ||
} else { | ||
return dummy_weak.Value(); | ||
} | ||
} else if (info[0].As<String>() == String::New(env, "persistent")) { | ||
return dummy_persistent.Value(); | ||
} else { | ||
return dummy_reference.Value(); | ||
} | ||
} | ||
|
||
Value GetFromGetter(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
|
||
if (info[0].As<String>() == String::New(env, "weak")) { | ||
if (dummy_weak.IsEmpty()) { | ||
return String::New(env, "No Referenced Value"); | ||
} else { | ||
if (info[1].IsString()) { | ||
return dummy_weak.Get(info[1].As<String>().Utf8Value()); | ||
} else if (info[1].IsNumber()) { | ||
return dummy_weak.Get(info[1].As<Number>().Uint32Value()); | ||
} | ||
} | ||
} else if (info[0].As<String>() == String::New(env, "persistent")) { | ||
if (info[1].IsString()) { | ||
return dummy_persistent.Get(info[1].As<String>().Utf8Value()); | ||
} else if (info[1].IsNumber()) { | ||
return dummy_persistent.Get(info[1].As<Number>().Uint32Value()); | ||
} | ||
} else { | ||
if (info[0].IsString()) { | ||
return dummy_reference.Get(info[0].As<String>().Utf8Value()); | ||
} else if (info[0].IsNumber()) { | ||
return dummy_reference.Get(info[0].As<Number>().Uint32Value()); | ||
} | ||
} | ||
|
||
return String::New(env, "Error: Reached end of getter"); | ||
} | ||
|
||
Value GetCastedFromValue(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
|
||
if (info[0].As<String>() == String::New(env, "weak")) { | ||
if (casted_weak.IsEmpty()) { | ||
return String::New(env, "No Referenced Value"); | ||
} else { | ||
return casted_weak.Value(); | ||
} | ||
} else if (info[0].As<String>() == String::New(env, "persistent")) { | ||
return casted_persistent.Value(); | ||
} else { | ||
return casted_reference.Value(); | ||
} | ||
} | ||
|
||
Value GetCastedFromGetter(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
|
||
if (info[0].As<String>() == String::New(env, "weak")) { | ||
if (casted_weak.IsEmpty()) { | ||
return String::New(env, "No Referenced Value"); | ||
} else { | ||
return casted_weak.Get(info[1].As<Number>()); | ||
} | ||
} else if (info[0].As<String>() == String::New(env, "persistent")) { | ||
return casted_persistent.Get(info[1].As<Number>()); | ||
} else { | ||
return casted_reference.Get(info[1].As<Number>()); | ||
} | ||
} | ||
|
||
void UnrefObjects(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
|
||
if (info[0].As<String>() == String::New(env, "dummy persistent")) { | ||
dummy_persistent.Unref(); | ||
} else if (info[0].As<String>() == String::New(env, "dummy references")) { | ||
dummy_reference.Unref(); | ||
} else if (info[0].As<String>() == String::New(env, "casted persistent")) { | ||
casted_persistent.Unref(); | ||
} else if (info[0].As<String>() == String::New(env, "casted reference")) { | ||
casted_reference.Unref(); | ||
} | ||
} | ||
|
||
Object InitObjectReference(Env env) { | ||
Object exports = Object::New(env); | ||
|
||
exports["setCastedObjects"] = Function::New(env, SetCastedObjects); | ||
exports["setObjects"] = Function::New(env, SetObjects); | ||
exports["getCastedFromValue"] = Function::New(env, GetCastedFromValue); | ||
exports["getFromGetter"] = Function::New(env, GetFromGetter); | ||
exports["getCastedFromGetter"] = Function::New(env, GetCastedFromGetter); | ||
exports["getFromValue"] = Function::New(env, GetFromValue); | ||
exports["unrefObjects"] = Function::New(env, UnrefObjects); | ||
|
||
return exports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
const testUtil = require('./testUtil'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
function test(binding) { | ||
function testCastedEqual(testToCompare) { | ||
var compare_test = ["hello", "world", "!"]; | ||
if (testToCompare instanceof Array) { | ||
assert.deepEqual(compare_test, testToCompare); | ||
} else if (testToCompare instanceof String) { | ||
assert.deepEqual("No Referenced Value", testToCompare); | ||
} else { | ||
assert.fail(); | ||
} | ||
} | ||
|
||
testUtil.runGCTests([ | ||
'Weak Casted Array', | ||
() => { | ||
binding.objectreference.setCastedObjects(); | ||
var test = binding.objectreference.getCastedFromValue("weak"); | ||
var test2 = new Array(); | ||
test2[0] = binding.objectreference.getCastedFromGetter("weak", 0); | ||
test2[1] = binding.objectreference.getCastedFromGetter("weak", 1); | ||
test2[2] = binding.objectreference.getCastedFromGetter("weak", 2); | ||
|
||
testCastedEqual(test); | ||
testCastedEqual(test2); | ||
}, | ||
|
||
'Persistent Casted Array', | ||
() => { | ||
binding.objectreference.setCastedObjects(); | ||
const test = binding.objectreference.getCastedFromValue("persistent"); | ||
const test2 = new Array(); | ||
test2[0] = binding.objectreference.getCastedFromGetter("persistent", 0); | ||
test2[1] = binding.objectreference.getCastedFromGetter("persistent", 1); | ||
test2[2] = binding.objectreference.getCastedFromGetter("persistent", 2); | ||
|
||
assert.ok(test instanceof Array); | ||
assert.ok(test2 instanceof Array); | ||
testCastedEqual(test); | ||
testCastedEqual(test2); | ||
}, | ||
|
||
'References Casted Array', | ||
() => { | ||
binding.objectreference.setCastedObjects(); | ||
const test = binding.objectreference.getCastedFromValue(); | ||
const test2 = new Array(); | ||
test2[0] = binding.objectreference.getCastedFromGetter("reference", 0); | ||
test2[1] = binding.objectreference.getCastedFromGetter("reference", 1); | ||
test2[2] = binding.objectreference.getCastedFromGetter("reference", 2); | ||
|
||
assert.ok(test instanceof Array); | ||
assert.ok(test2 instanceof Array); | ||
testCastedEqual(test); | ||
testCastedEqual(test2); | ||
}, | ||
|
||
'Weak Dummy', | ||
() => { | ||
binding.objectreference.setDummyObjects("hello", "world"); | ||
const test = binding.objectreference.getDummyFromValue("weak"); | ||
const test2 = binding.objectreference.getDummyFromGetter("weak", "hello"); | ||
|
||
assert.deepEqual({ hello: "world"}, test); | ||
assert.equal("world", test2); | ||
assert.equal(test["hello"], test2); | ||
}, | ||
() => { | ||
binding.objectreference.setDummyObjects(1, "hello world"); | ||
const test = binding.objectreference.getDummyFromValue("weak"); | ||
const test2 = binding.objectreference.getDummyFromGetter("weak", 1); | ||
|
||
assert.deepEqual({ 1: "hello world"}, test); | ||
assert.equal("hello world", test2); | ||
assert.equal(test[1], test2); | ||
}, | ||
() => { | ||
binding.objectreference.setDummyObjects(0, "hello"); | ||
binding.objectreference.setDummyObjects(1, "world"); | ||
const test = binding.objectreference.getDummyFromValue("weak"); | ||
const test2 = binding.objectreference.getDummyFromGetter("weak", 0); | ||
const test3 = binding.objectreference.getDummyFromGetter("weak", 1); | ||
|
||
assert.deepEqual({ 1: "world"}, test); | ||
assert.equal(undefined, test2); | ||
assert.equal("world", test3); | ||
}, | ||
|
||
'Persistent Dummy', | ||
() => { | ||
binding.objectreference.setDummyObjects("hello", "world"); | ||
const test = binding.objectreference.getDummyFromValue("persistent"); | ||
const test2 = binding.objectreference.getDummyFromGetter("persistent", "hello"); | ||
|
||
assert.deepEqual({ hello: "world"}, test); | ||
assert.equal("world", test2); | ||
assert.equal(test["hello"], test2); | ||
}, | ||
() => { | ||
binding.objectreference.setDummyObjects(1, "hello world"); | ||
const test = binding.objectreference.getDummyFromValue("persistent"); | ||
const test2 = binding.objectreference.getDummyFromGetter("persistent", 1); | ||
|
||
assert.deepEqual({ 1: "hello world"}, test); | ||
assert.equal("hello world", test2); | ||
assert.equal(test[1], test2); | ||
}, | ||
() => { | ||
binding.objectreference.setDummyObjects(0, "hello"); | ||
binding.objectreference.setDummyObjects(1, "world"); | ||
const test = binding.objectreference.getDummyFromValue("persistent"); | ||
const test2 = binding.objectreference.getDummyFromGetter("persistent", 0); | ||
const test3 = binding.objectreference.getDummyFromGetter("persistent", 1); | ||
|
||
assert.deepEqual({ 1: "world"}, test); | ||
assert.equal(undefined, test2); | ||
assert.equal("world", test3); | ||
}, | ||
() => { | ||
binding.objectreference.setDummyObjects("hello", "world"); | ||
assert.doesNotThrow( | ||
() => { | ||
binding.objectreference.unrefObjects("dummy persistent"); | ||
}, | ||
Error | ||
); | ||
assert.throws( | ||
() => { | ||
binding.objectreference.unrefObjects("dummy persistent"); | ||
}, | ||
Error | ||
); | ||
}, | ||
|
||
'References Dummy', | ||
() => { | ||
binding.objectreference.setDummyObjects("hello", "world"); | ||
const test = binding.objectreference.getDummyFromValue(); | ||
const test2 = binding.objectreference.getDummyFromGetter("hello"); | ||
|
||
assert.deepEqual({ hello: "world"}, test); | ||
assert.equal("world", test2); | ||
assert.equal(test["hello"], test2); | ||
}, | ||
() => { | ||
binding.objectreference.setDummyObjects(1, "hello world"); | ||
const test = binding.objectreference.getDummyFromValue(); | ||
const test2 = binding.objectreference.getDummyFromGetter(1); | ||
|
||
assert.deepEqual({ 1: "hello world"}, test); | ||
assert.equal("hello world", test2); | ||
assert.equal(test[1], test2); | ||
}, | ||
() => { | ||
binding.objectreference.setDummyObjects(0, "hello"); | ||
binding.objectreference.setDummyObjects(1, "world"); | ||
const test = binding.objectreference.getDummyFromValue(); | ||
const test2 = binding.objectreference.getDummyFromGetter(0); | ||
const test3 = binding.objectreference.getDummyFromGetter(1); | ||
|
||
assert.deepEqual({ 1: "world"}, test); | ||
assert.equal(undefined, test2); | ||
assert.equal("world", test3); | ||
}, | ||
() => { | ||
binding.objectreference.setDummyObjects("hello", "world"); | ||
assert.doesNotThrow( | ||
() => { | ||
binding.objectreference.unrefObjects("dummy reference"); | ||
}, | ||
Error | ||
); | ||
assert.doesNotThrow( | ||
() => { | ||
binding.objectreference.unrefObjects("dummy reference"); | ||
}, | ||
Error | ||
); | ||
assert.throws( | ||
() => { | ||
binding.objectreference.unrefObjects("dummy reference"); | ||
}, | ||
Error | ||
); | ||
} | ||
]) | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be can as opposed to cant