Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Addressing review comments.

  • Loading branch information...
commit b5ca39c4c4dbe8fdcad5944251fc31dd8c972599 1 parent 77c8b29
@Gozala authored
View
69 packages/api-utils/tests/sandbox/all.js
@@ -1,6 +1,6 @@
const { Cc, Ci, Cu } = require("chrome");
-function Sandboxed(principal) {
+function Sandbox(principal) {
let sandbox = Cu.Sandbox(principal || "http://www.mozilla.com/");
return Cu.evalInSandbox("({" +
" create: function create(prototype, descriptor) {" +
@@ -28,30 +28,31 @@ function Sandboxed(principal) {
};
exports.Assert = require("./asserts").create(Object);
-exports["test inheritence"] = function (assert) {
+
+exports["test inheritance"] = function (assert) {
function Type() {}
let prototype = Type.prototype;
- let sandbox = Sandboxed();
+ let sandbox = Sandbox();
let properties = { a: { value: "a" }, b: { get: function() "b" } };
let f1 = Object.create(Type.prototype, properties);
- let f2 = Object.create(Type.prototype, properties);
+ let f2 = sandbox.create(Type.prototype, properties);
- assert.equal(Type.prototype, prototype, "prototype did not changed");
- assert.equal(f1.constructor, Type, "consturctor is a Type function");
- assert.equal(f2.constructor, Type, "sandbox: consturctor is a Type function");
+ assert.equal(Type.prototype, prototype,
+ "prototype did not change (bug 608959)");
+ assert.equal(f1.constructor, Type, "constructor is a Type function");
+ assert.equal(f2.constructor, f1.constructor,
+ "sandbox: constructor is a Type function");
assert.equal(Object.getPrototypeOf(f1), Type.prototype,
"prototype is `Type.prototype`");
- assert.equal(sandbox.getPrototypeOf(f1), Type.prototype,
- "`getPrototypeOf` in sandbox returs `Type.prototype`");
- assert.equal(Object.getPrototypeOf(f2), Type.prototype,
- "sandbox: prototype is `Type.prototype`");
- assert.equal(sandbox.getPrototypeOf(f2), Type.prototype,
- "getPrototypeOf in sandbox returns `Type.prototype`");
+ assert.equal(Object.getPrototypeOf(f1), sandbox.getPrototypeOf(f1),
+ "getPrototypeOf is consistant for local objects");
+ assert.equal(Object.getPrototypeOf(f2), sandboxe.getPrototypeOf(f2),
+ "getPrototypeOf is consistant for sandboxed objects");
@mykmelez
mykmelez added a note

Nit: consistant -> consistent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
};
exports["test writable / non-writable properties"] = function (assert) {
- let sandbox = Sandboxed();
+ let sandbox = Sandbox();
let prototype = Object.create(Object.prototype, {
a: { value: "a", writable: false },
b: { value: "b", writable: true },
@@ -75,13 +76,13 @@ exports["test writable / non-writable properties"] = function (assert) {
assert.equal(Object.getOwnPropertyDescriptor(f1, "a"),
Object.getOwnPropertyDescriptor(f2, "a"),
- "proprety `a` descriptors are undefined");
+ "property `a` descriptors are undefined");
assert.equal(Object.getOwnPropertyDescriptor(f1, "b"),
Object.getOwnPropertyDescriptor(f2, "b"),
- "proprety `c` descriptors are undefined");
+ "property `b` descriptors are undefined");
assert.equal(Object.getOwnPropertyDescriptor(f1, "c"),
Object.getOwnPropertyDescriptor(f2, "c"),
- "proprety `c` descriptors are undefined");
+ "property `c` descriptors are undefined");
assert.equalDescriptors(Object.getOwnPropertyDescriptor(f1, "d"),
@@ -97,9 +98,9 @@ exports["test writable / non-writable properties"] = function (assert) {
assert.nonWritable(f1, "a", "property `a` is non-writable");
assert.nonWritable(f2, "a", "sandbox: property `a` is non-writable");
assert.nonWritable(f1, "c", "property `c` is non-writable");
- assert.nonWritable(f2, "c", "sandbox: property `d` is non-writable");
- assert.nonWritable(f1, "d", "property `i` is non-writable");
- assert.nonWritable(f2, "d", "sandbox: property `a` is non-writable");
+ assert.nonWritable(f2, "c", "sandbox: property `c` is non-writable");
+ assert.nonWritable(f1, "d", "property `d` is non-writable");
+ assert.nonWritable(f2, "d", "sandbox: property `d` is non-writable");
assert.nonWritable(f1, "f", "property `f` is non-writable");
assert.nonWritable(f2, "f", "sandbox: property `f` is non-writable");
@@ -107,11 +108,11 @@ exports["test writable / non-writable properties"] = function (assert) {
assert.equal(f1.b, f2.b, "property `b` values are the same after set");
assert.equal(f1.e, f2.e, "property `e` values are the same after set");
- assert.equal(f1.b, f2.e, "all writable propeperties changed");
+ assert.equal(f1.b, f2.e, "all writable properties changed");
};
exports["test configurable / non-configurable properties"] = function (assert) {
- let sandbox = Sandboxed();
+ let sandbox = Sandbox();
let properties = {
a: { value: "a", configurable: false },
b: { value: "b", configurable: true },
@@ -158,7 +159,7 @@ exports["test configurable / non-configurable properties"] = function (assert) {
assert.nonConfigurable(f1, "c",
"property `c` defaults to non-configurable");
assert.nonConfigurable(f2, "c",
- "sandbox: property `d` defaults to non-configurable");
+ "sandbox: property `c` defaults to non-configurable");
assert.nonConfigurable(f1, "d", "property `d` is non-configurable");
assert.nonConfigurable(f2, "d",
"sandbox: property `d` is non-configurable");
@@ -172,8 +173,8 @@ exports["test configurable / non-configurable properties"] = function (assert) {
Object.defineProperty(f2, "e", override);
assert.equal(f1.b, f2.b, "property `b` values are the same after redefine");
- assert.equal(f1.e, f2.e, "property `e` values are the same after redifine");
- assert.equal(f1.b, f2.e, "all propeperties redefined to same");
+ assert.equal(f1.e, f2.e, "property `e` values are the same after redefine");
+ assert.equal(f1.b, f2.e, "all properties redefined to same");
delete f1.b;
delete f2.b;
@@ -188,7 +189,7 @@ exports["test configurable / non-configurable properties"] = function (assert) {
exports["test enumerable / non-enumerable properties"] = function (assert) {
- let sandbox = Sandboxed();
+ let sandbox = Sandbox();
let properties = {
a: { value: "a", enumerable: false },
b: { value: "b", enumerable: true },
@@ -233,7 +234,7 @@ exports["test enumerable / non-enumerable properties"] = function (assert) {
assert.nonEnumerable(f1, "c",
"property `c` defaults to non-enumerable");
assert.nonEnumerable(f2, "c",
- "sandbox: property `d` defaults to non-enumerable");
+ "sandbox: property `c` defaults to non-enumerable");
assert.nonEnumerable(f1, "d", "property `d` is non-enumerable");
assert.nonEnumerable(f2, "d",
"sandbox: property `d` is non-enumerable");
@@ -242,8 +243,8 @@ exports["test enumerable / non-enumerable properties"] = function (assert) {
"sandbox: property `f` defaults to non-enumerable");
};
-exports["property names / keys"] = function (assert) {
- let sandbox = Sandboxed();
+exports["test property names / keys"] = function (assert) {
+ let sandbox = Sandbox();
let properties = {
a: { value: "a", enumerable: false },
b: { value: "b", enumerable: true },
@@ -264,14 +265,14 @@ exports["property names / keys"] = function (assert) {
assert.equal(sandbox.keys(f2).length, 6,
"`getOwnPropertyNames` from sandbox reports 6 own properties");
- assert.equal(Object.keys(f1).length, 4, "4 own enumerable properties");
+ assert.equal(Object.keys(f1).length, 4,
+ "local keys returns 4 properties on local object");
assert.equal(Object.keys(f2).length, 4,
- "sandbox: 6 own enumerable properties");
+ "local keys returns 4 properties on sandboxed object");
assert.equal(sandbox.keys(f1).length, 4,
- "`keys` from sandbox reports 4 own properties");
+ "sandboxed keys returns 4 properties on local object");
assert.equal(sandbox.keys(f2).length, 4,
- "`keys` from sandbox reports 4 own properties");
-
+ "sandboxed keys returns 4 properties on sandboxed object");
};
if (module == require.main)
View
311 packages/api-utils/tests/sandbox/asserts.js
@@ -1,45 +1,183 @@
// We intentionally don't use strict mode in this module as we need to test
// behavior of ES5 methods in both strict & non-strict modes.
-const AssertBase = require("test/assert").Assert;
+const BaseAssert = require("test/assert").Assert;
-// Saving build-in `Object` in case we will need to access it form the `create`
+// Save built-in `Object` in case we need to access it from the `create`
// exported function.
const _Object = Object;
+function isEnumerable(descriptor) {
+ return descriptor.enumerable || descriptor.enumerable === undefined;
+}
+function isConfigurable(descriptor) {
+ return descriptor.configurable || descriptor.configurable === undefined;
+}
+function isWritable(descriptor) {
+ return descriptor.writable || descriptor.writable === undefined;
+}
+
/**
- * Creates `Assert` function that can be used as commonjs custom Assertor.
+ * Creates `Assert` function that can be used as CommonJS custom Assertor.
* Optionally `Object` can be passed in order to provide assertion methods
- * bounded to the sandbox from where `Object` is originated.
+ * bound to the sandbox from where `Object` originates.
*/
exports.create = function create(Object) {
- // Using `Object` from the current sandbox if `Object` was not provided.
+ // Use `Object` from the current sandbox if `Object` was not provided.
Object = Object || _Object;
- let DAssert = {
+ function canPut(target, name) {
+ "use strict";
+
+ let descriptor = Object.getOwnPropertyDescriptor(target, name);
+ return !descriptor || isWritable(descriptor);
+ }
+
+ function doesPutFails(target, name) {
@mykmelez
mykmelez added a note

Nit: doesPutFails -> doesPutFail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ let result = true;
+ let value = target[name];
+
+ target[name] = "<changed>" + value;
+
+ if (target[name] !== value) {
+ // revert value back.
+ target[name] = value;
+ result = false;
+ }
+
+ return result;
+ }
+
+ function doesPutThrowsInStrictMode(target, name) {
@mykmelez
mykmelez added a note

Nit: doesPutThrowsInStrictMode -> doesPutThrowInStrictMode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ "use strict";
+
+ let value = target[name];
+ let result = false;
+
+ try {
+ target[name] = "<changed>" + value;
+ // Revert value back if exception was not thrown.
+ target[name] = value;
+ }
+ catch (e) {
+ result = true;
+ }
+
+ return result;
+ }
+
+ function canDelete(target, name) {
+ "use strict";
+
+ let descriptor = Object.getOwnPropertyDescriptor(target, name);
+ return !descriptor || isConfigurable(descriptor);
+ }
+
+ function doesDeleteFails(target, name) {
+ let descriptor = Object.getOwnPropertyDescriptor(target, name);
+ let result = false;
+
+ // `delete` should return `false` for non-configurable properties and
+ // property still must be defined.
+ result = !(delete target[name]) &&
+ !!Object.getOwnPropertyDescriptor(target, name);
+
+ // Define original property back.
+ Object.defineProperty(target, name, descriptor);
+
+ return result;
+ }
+
+ function doesDeleteThrowsInStrictMode(target, name) {
+ "use strict";
+
+ let descriptor = Object.getOwnPropertyDescriptor(target, name);
+ let result = false;
+
+ try {
+ delete target[name];
+ // Revert value back in case exception was not thrown.
+ Object.defineProperty(target, name, descriptor);
+ }
+ catch (e) {
+ result = true;
+ }
+
+ return result;
+
+ }
+
+ function doesDefineThrows(target, name) {
+ "use strict";
+
+ let descriptor = Object.getOwnPropertyDescriptor(target, name);
+ let result = false;
+
+ try {
+ Object.defineProperty(target, name, {
+ value: "<changed>" + target[name]
+ });
+ // Revert property back if exception was not thrown.
+ Object.defineProperty(target, name, descriptor);
+ }
+ catch (e) {
+ result = true;
+ }
+
+ return result;
+ }
+
+ function canEnumerate(target, name) {
+ "use strict";
+
+ let descriptor = Object.getOwnPropertyDescriptor(target, name);
+ return !descriptor || isEnumerable(descriptor);
+ }
+
+ function isEnumerated(target, name) {
+ "use strict";
+
+ return ~Object.keys(target).indexOf(name);
+ }
+
+ function equalDescriptors(expected, actual) {
+ return actual == expected ||
+ (actual.get === expected.get &&
+ actual.set === expected.set &&
+ actual.value === expected.value &&
+ isEnumerable(actual) === isEnumerable(expected) &&
+ isConfigurable(actual) === isConfigurable(expected) &&
+ isWritable(actual) === isWritable(expected));
+ }
+
+ const assertDescriptor = {
+
/**
- * Asserts if given objects property descriptors are equivalent, contain
+ * Asserts if given object's property descriptors are equivalent, contain
* same attributes and have same getter / setter / value.
*/
equalDescriptors: {
value: function (expected, actual, message) {
if (equalDescriptors(expected, actual)) {
this.pass(message);
- } else {
+ }
+ else {
this.fail({
- expected: expectedDesc,
- actual: actualDesc,
+ expected: expected,
+ actual: actual,
operator: "equalDescriptors",
message: message
});
}
- }
+ },
+ enumerable: true
},
+
/**
* Tests if `object`'s `name` is ES5 non-writable property:
* 1. Property descriptor has `writable` attribute with value `false`.
* 2. [[Put]] throws a `TypeError` in strict mode.
- * 3. [[Put]] does not succeeds.
+ * 3. [[Put]] does not succeed.
*/
nonWritable: {
value: function (object, name, message) {
@@ -49,30 +187,32 @@ exports.create = function create(Object) {
message: message + " (`writable` attribute is not `false`)"
});
}
- else if (!isPutFails(object, name)) {
+ else if (!doesPutFails(object, name)) {
this.fail({
operator: "nonWritable",
- message: message + " (proprety value can be changed)"
+ message: message + " (property value can be changed)"
});
}
- else if (!isPutThrowsInStrictMode(object, name)) {
+ else if (!doesPutThrowsInStrictMode(object, name)) {
this.fail({
operator: "nonWritable",
message: message +
- " (property change does not throws in strict mode)"
+ " (property change does not throw in strict mode)"
});
- } else {
+ }
+ else {
this.pass(message);
}
},
enumerable: true
},
+
/**
* Tests if `object`'s `name` is ES5 non-configurable property:
* 1. Property descriptor has `configurable` attribute with value `false`.
* 2. [[Delete]] throws a `TypeError` in strict mode.
- * 3. [[Delete]] does not succeeds.
- * 4. Defining same named property throws `TypeError`.
+ * 3. [[Delete]] does not succeed.
+ * 4. Defining same-named property throws `TypeError`.
*/
nonConfigurable: {
value: function nonConfigurable(object, name, message) {
@@ -82,19 +222,19 @@ exports.create = function create(Object) {
message: message + " (`configurable` attribute is not `false`)"
});
}
- else if (!isDeleteFails(object, name)) {
+ else if (!doesDeleteFails(object, name)) {
this.fail({
operator: "nonConfigurable",
message: message + " (`delete` succeeded or returned `true`)"
});
}
- else if (!isDeleteThrowsInStrictMode(object, name, message)) {
+ else if (!doesDeleteThrowsInStrictMode(object, name, message)) {
this.fail({
operator: "nonConfigurable",
- message: message + " (`delete` does not throws in strict mode)"
+ message: message + " (`delete` does not throw in strict mode)"
});
}
- else if (!isDefineThrows(object, name)) {
+ else if (!doesDefineThrows(object, name)) {
this.fail({
operator: "nonConfigurable",
message: message + " (redefining property did not throw)"
@@ -106,6 +246,7 @@ exports.create = function create(Object) {
},
enumerable: true
},
+
/**
* Tests if `object`'s `name` is ES5 non-enumerable property:
* 1. Property descriptor has `enumerable` attribute with value `false`.
@@ -122,7 +263,7 @@ exports.create = function create(Object) {
else if (isEnumerated(object, name)) {
this.fail({
operator: "nonEnumerable",
- message: message + " (property `" + name + "` was enumarated)"
+ message: message + " (property `" + name + "` was enumerated)"
});
}
else {
@@ -133,127 +274,7 @@ exports.create = function create(Object) {
}
};
- function canPut(target, name) {
- "use strict";
-
- var descriptor = Object.getOwnPropertyDescriptor(target, name);
- return !descriptor ||
- ("value" in descriptor && descriptor.writable !== false);
- }
-
- function isPutFails(target, name) {
- var result = true;
- var value = target[name];
-
- target[name] = "<changed>" + value;
-
- if (target[name] !== value) {
- // reverting value back.
- target[name] = value;
- result = false;
- }
-
- return result;
- }
-
- function isPutThrowsInStrictMode(target, name) {
- "use strict";
-
- var value = target[name];
- var result = false;
-
- try {
- target[name] = "<changed>" + value;
- // Reverting value back in case exception was not thrown.
- target[name] = value;
- } catch (e) {
- result = true;
- }
-
- return result;
- }
-
- function canDelete(target, name) {
- "use strict";
-
- var descriptor = Object.getOwnPropertyDescriptor(target, name);
- return !descriptor || descriptor.configurable !== false;
- }
-
- function isDeleteFails(target, name) {
- var descriptor = Object.getOwnPropertyDescriptor(target, name);
- var result = false;
-
- // `delete` should return `false` for non-configurable properties and
- // property still must be defined.
- result = !(delete target[name]) &&
- !!Object.getOwnPropertyDescriptor(target, name);
-
- // Defining original property back.
- Object.defineProperty(target, name, descriptor)
-
- return result
- }
-
- function isDeleteThrowsInStrictMode(target, name) {
- "use strict";
-
- var descriptor = Object.getOwnPropertyDescriptor(target, name);
- var result = false;
-
- try {
- delete target[name];
- // Reverting value back in case exception was not thrown.
- Object.defineProperty(target, name, descriptor);
- } catch (e) {
- result = true;
- }
-
- return result;
-
- }
-
- function isDefineThrows(target, name) {
- "use strict";
-
- var descriptor = Object.getOwnPropertyDescriptor(target, name);
- var result = false;
-
- try {
- Object.defineProperty(target, name, { value: "<changed>" + target[name] });
- // Reverting property back in case if no exception was thrown.
- Object.defineProperty(target, name, descriptor);
- } catch (e) {
- result = true;
- }
-
- return result;
- }
-
- function canEnumerate(target, name) {
- "use strict";
-
- var descriptor = Object.getOwnPropertyDescriptor(target, name);
- return descriptor && descriptor.enumerable !== false;
- }
-
- function isEnumerated(target, name) {
- "use strict";
-
- return Object.keys(target).indexOf(name) >= 0;
- }
-
- function equalDescriptors(expected, actual) {
- return actual == expected ||
- (actual.get === expected.get &&
- actual.set === expected.set &&
- actual.value === expected.value &&
- (true !== actual.enumerable) === (true !== expected.enumerable) &&
- (true !== actual.configurable) === (true !== expected.configurable) &&
- (true !== actual.writable) === (true !== expected.writable));
- }
-
return function Assert() {
- return Object.create(AssertBase.apply(null, arguments), DAssert);
+ return Object.create(BaseAssert.apply(null, arguments), assertDescriptor);
};
};
@mykmelez

Nit: doesPutFails -> doesPutFail

@mykmelez

Nit: consistant -> consistent

@mykmelez

Nit: doesPutThrowsInStrictMode -> doesPutThrowInStrictMode

Please sign in to comment.
Something went wrong with that request. Please try again.