Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Corrected hole-related error reporting.

  • Loading branch information...
commit 7996d4a3cb5199c80170d039d51757eedb8c9b9f 1 parent f8e73e2
Dan Bornstein authored
Showing with 48 additions and 2 deletions.
  1. +13 −1 lib/serial.js
  2. +1 −1  package.json
  3. +34 −0 test/test.js
View
14 lib/serial.js
@@ -113,6 +113,7 @@ function findBackRefsAndHoles(root, holeFilter) {
visitValue(root);
var holes = oid.createMap();
+ allowHoles = false;
holeSet.forEach(function (obj) {
var replacement = holeFilter(obj);
holes.set(obj, replacement);
@@ -163,6 +164,8 @@ function findBackRefsAndHoles(root, holeFilter) {
}
function visitFunction(x) {
+ holeCheck();
+
var count = allRefs.get(x, 0) + 1;
allRefs.set(x, count);
@@ -198,6 +201,7 @@ function findBackRefsAndHoles(root, holeFilter) {
} else if (Buffer.isBuffer(x)) {
// Nothing more to do here.
} else {
+ holeCheck();
addHole(x);
}
}
@@ -224,8 +228,16 @@ function findBackRefsAndHoles(root, holeFilter) {
}
function addHole(x) {
+ holeSet.add(x);
+ }
+
+ /**
+ * Check to see if holes are currently being allowed. If not,
+ * complain appropriateley.
+ */
+ function holeCheck() {
if (allowHoles) {
- holeSet.add(x);
+ return;
} else if (type.isUndefined(holeFilter)) {
throw new Error("Hole-ful graph, but no hole filter.");
} else {
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "bidar",
- "version": "0.1.6",
+ "version": "0.1.7",
"keywords":
["object", "serialization", "data", "graph"],
"description":
View
34 test/test.js
@@ -315,6 +315,39 @@ function testInnerHole() {
}
}
+function test_fail_hole() {
+ var obj = { a: hole };
+
+ function hole() {
+ return "holy!";
+ }
+
+ function f1() {
+ bidar.serialize(obj);
+ }
+ assert.throws(f1, /Hole-ful graph, but no hole filter/);
+
+ function f2() {
+ bidar.serialize(obj, filter);
+
+ function filter(x) {
+ // An arbitrary-but-different hole.
+ return filter;
+ }
+ }
+ assert.throws(f2, /Holes not allowed in holes/);
+
+ function f3() {
+ bidar.serialize(obj, filter);
+
+ function filter(x) {
+ // The same hole.
+ return x;
+ }
+ }
+ assert.throws(f3, /Holes not allowed in holes/);
+}
+
function testLargeString() {
var string1 = "abcdefg1234567890---THIS IS YOUR CAPTAIN SPEAKING---" +
"\0\u2000\u2010\u2011\u2022\ufffe---ARE YOU RECEIVING ME?---";
@@ -388,6 +421,7 @@ testCircularArray();
testCircularObject();
testSimpleHole();
testInnerHole();
+test_fail_hole();
testLargeString();
test_parsePartial();
Please sign in to comment.
Something went wrong with that request. Please try again.