Skip to content
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 Object.getOwnPropertyDescriptors tests #484

Merged
merged 8 commits into from Feb 19, 2016

Conversation

ljharb
Copy link
Member

@ljharb ljharb commented Feb 1, 2016

Tests for the stage 3 proposal for Object.getOwnPropertyDescriptors.

author: Jordan Harband
---*/

var F = function G() {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The G identifier is not relevant for this test

@jugglinmike
Copy link
Contributor

Looking good, @ljharb! I left some in-line notes for minor alterations. One patch-wide suggestion concerns the es7id tag. Given that this is not targeting ES7 (and that we're moving to a version-independent identification scheme), these tests should probably specify esid: pendingid: pending instead.

@jugglinmike
Copy link
Contributor

Oh, and one more test! For

[...]
3. Let descriptors be ! ObjectCreate( %ObjectPrototype%).
[...]
5. Return descriptors. 

We're want something like,

assert.sameValue(
  Object.getPrototypeOf(Object.getOwnPropertyDescriptors({})),
  Object.prototype
);

@ljharb
Copy link
Member Author

ljharb commented Feb 1, 2016

Thanks, I'll make the requested changes

@ljharb
Copy link
Member Author

ljharb commented Feb 1, 2016

Updated!

@caitp
Copy link
Contributor

caitp commented Feb 1, 2016

here's one you could try:

var i = 0;
var descriptors = [
  { enumerable: false, value: "A1", writable: true, configurable: true },
  { enumerable: true, value: "A2", writable: true, configurable: true }
];
var log = "";
var proxy = new Proxy({}, {
  ownKeys() {
    log += "ownKeys|";
    return ["DUPLICATE", "DUPLICATE", "DUPLICATE"];
  },
  getOwnPropertyDescriptor(t, name) {
    log += "getOwnPropertyDescriptor:" + name + "|";
    return descriptors[i++];
  }
});

var result = Object.getOwnPropertyDescriptors(proxy);
assert.sameValue(result.hasOwnProperty("DUPLICATE"), true);
assert.sameValue(result.DUPLICATE, undefined);
assert.sameValue(log, "ownKeys|getOwnPropertyDescriptor:DUPLICATE|getOwnPropertyDescriptor:DUPLICATE|getOwnPropertyDescriptor:DUPLICATE|");

This exposes some currently broken code in v8, and from IRC discussions, v8 will probably have to fix its bug :d

ljharb added a commit to ljharb/test262 that referenced this pull request Feb 2, 2016
@ljharb
Copy link
Member Author

ljharb commented Feb 2, 2016

@jugglinmike just noticed your edit - IDs updated to "id".


assert.sameValue(
Object.keys(result).length,
Object.getOwnPropertyNames(f).length,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb, what do you think about using the constant 2 here instead of Object.getOwnPropertyNames? That way, we won't depend on the correctness of the function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, np

@ljharb ljharb force-pushed the object_get_own_property_descriptors branch from 73f5f40 to 90f8b4e Compare February 4, 2016 22:03
ljharb added a commit to ljharb/test262 that referenced this pull request Feb 4, 2016
@ljharb ljharb force-pushed the object_get_own_property_descriptors branch from 90f8b4e to 30faaeb Compare February 6, 2016 07:58
ljharb added a commit to ljharb/test262 that referenced this pull request Feb 6, 2016
@ljharb ljharb force-pushed the object_get_own_property_descriptors branch from 30faaeb to a07be18 Compare February 6, 2016 07:59
ljharb added a commit to ljharb/test262 that referenced this pull request Feb 6, 2016
@ljharb ljharb force-pushed the object_get_own_property_descriptors branch from a07be18 to 53d109c Compare February 9, 2016 17:55
ljharb added a commit to ljharb/test262 that referenced this pull request Feb 9, 2016
@ljharb ljharb force-pushed the object_get_own_property_descriptors branch from 53d109c to b264c5c Compare February 13, 2016 23:57
ljharb added a commit to ljharb/test262 that referenced this pull request Feb 13, 2016
@ljharb
Copy link
Member Author

ljharb commented Feb 17, 2016

@goyakin @jugglinmike friendly ping! :-D

@jugglinmike
Copy link
Contributor

Since we use the list of traps in multiple files, I think we should create a helper to enumerate them. This would be similar to testWithTypedArrayConstructors defined in harness/testTypedArray.js. We can use it like:

testWithTraps(assertTrapThrows);

I disagree. These tests are themselves for the a helper function. Introducing a helper to test a helper seems a little too abstract (and would necessitate tests for the helper for the tests for the helper :P).

construct: function () {},
});

function assertTrapSucceeds(trap) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not enough to check that the trap didn't throw because we also want to be
sure that the specified function was invoked. If you want to enforce a loose
contract, you could assert this with a counter variable for each provided trap.
If you don't mind stricter tests (or just want to avoid all those counters),
you could

-var traps = allowProxyTraps({
-var input = {
   getPrototypeOf: function () {},
   setPrototypeOf: function () {},

 ...
   apply: function () {},
   construct: function () {},
-});
+};
+var traps = allowProxyTraps(input);

...and then

-function assertTrapSucceeds(trap) {
+function assertCustom(trap) {
+  if (input[trap] !== traps[trap]) {
+    throw new Test262Error('Expected custom trap');
+  }
+}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair point - although I'd say it is enough here since there's a parallel test that asserts that it does throw when an override isn't defined. I'll update with this change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

ljharb added a commit to ljharb/test262 that referenced this pull request Feb 17, 2016
@goyakin
Copy link
Member

goyakin commented Feb 17, 2016

I disagree. These tests are themselves for the a helper function. Introducing a helper to test a helper seems a little too abstract (and would necessitate tests for the helper for the tests for the helper :P).

I can see how testing a helper for a helper might be weird, but we don't have to have the additional tests. We enumerate the traps manually in two places and need to manually verify that all of them are covered in both anyway.

Moreover, when a new trap is added, we won't have to look for all the files where all traps are used if we have the helper.

We currently need all traps only in two files, so this isn't much of a problem though.

@jugglinmike
Copy link
Contributor

Looks good to me!

@ljharb ljharb force-pushed the object_get_own_property_descriptors branch from 5a0dc67 to f162c8d Compare February 18, 2016 22:12
ljharb added a commit to ljharb/test262 that referenced this pull request Feb 18, 2016
ljharb added a commit to ljharb/test262 that referenced this pull request Feb 18, 2016
ownKeys: overrides.ownKeys || throwTest262Error('[[OwnPropertyKeys]] trap called'),
apply: overrides.apply || throwTest262Error('[[Call]] trap called'),
construct: overrides.construct || throwTest262Error('[[Construct]] trap called')
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to return traps from the function 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops again :-) thanks, fixed

@ljharb ljharb force-pushed the object_get_own_property_descriptors branch from f162c8d to a6fad62 Compare February 19, 2016 19:27
goyakin pushed a commit that referenced this pull request Feb 19, 2016
Add Object.getOwnPropertyDescriptors tests
@goyakin goyakin merged commit 7d345fc into tc39:master Feb 19, 2016
@ljharb ljharb deleted the object_get_own_property_descriptors branch February 19, 2016 23:59
@ljharb
Copy link
Member Author

ljharb commented Feb 20, 2016

Thank you! Onward towards stage 4!


var result = Object.getOwnPropertyDescriptors(obj);

assert.sameValue(Object.keys(result).length, 3, 'obj has 3 descriptors');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#518 fixes this issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright, leave a note on my PR I guess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants