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

Comparing object with null prototype and objects derived from Object #729

Closed
rohitarondekar opened this issue Dec 18, 2014 · 4 comments · Fixed by #731
Closed

Comparing object with null prototype and objects derived from Object #729

rohitarondekar opened this issue Dec 18, 2014 · 4 comments · Fixed by #731

Comments

@rohitarondekar
Copy link
Contributor

Should the following test pass? Currently it fails but IMHO I think it should pass. The reason is both the objects have the same keys. However objA is lacking a hasOwnProperty method which is why I think the test fails. However shouldn't that throw an exception?

How about two objects with null prototypes? The second test should pass right? It's complaining that the object doesn't have hasOwnProperty method which I expected for the first test also. I'm guessing using Object.hasOwnProperty.call(obj, property) should do the trick to make the second test work and pass. However should the first test pass or fail? I'm new to Javascript so I wasn't sure but I'd be happy to work on a PR if required. 😄

describe("Prototypeless Object", function() {
    it("should equal regular obj with same keys", function () {
        var objA = Object.create(null);
        var objB = {};

        objA['name'] = 'Rohit';
        objB['name'] = 'Rohit';
        expect(objA).toEqual(objB);
    });

    it("should equal obj with null prototype", function () {
        var objA = Object.create(null);
        var objB = Object.create(null);

        objA.name = 'Rohit';
        objB.name = 'Rohit';

        expect(objA).toEqual(objB);
    });
});

The test fails with the following failure message:

Failures:
1) Prototypeless Object should equal obj with same keys
  Message:
    Expected { name: 'Rohit' } to equal { name: 'Rohit' }.
  Stack:
    Error: Expected { name: 'Rohit' } to equal { name: 'Rohit' }.
        at Object.<anonymous> (/home/rohit/Code/prototypeless-obj/spec/obj.spec.js:9:22)

2) Prototypeless Object should equal obj with null prototype
  Message:
    TypeError: Object object has no method 'hasOwnProperty'
  Stack:
    TypeError: Object object has no method 'hasOwnProperty'
        at Object.<anonymous> (/home/rohit/Code/prototypeless-obj/spec/obj.spec.js:19:22)

EDIT 1: Added the output of a test run.
EDIT 2: Added one more test case for both objects with null prototype

@slackersoft
Copy link
Member

The toEqual matcher checks whether the constructors match for two given objects as well. There is a bug (#598) in the currently released version that should be fixed on master so that the error message indicates that the constructor function is different.

Closing this.

@rohitarondekar
Copy link
Contributor Author

Thank you for clarifying the first test. However I still have doubts about the second test. If their constructor is null then shouldn't only the two objects properties be considered for equality? I added the following test to Jasmine specs and it fails on master with the same error.

    it("does something with null prototype objects", function() {
      var objA = Object.create(null),
            objB = Object.create(null);

        objA.name = 'Test';
        objB.name = 'Test';

        expect(j$.matchersUtil.equals(objA, objB)).toBe(true);
    });

It fails with the following error message:

Failures:
1) matchersUtil equals does something with null prototype objects
  Message:
    TypeError: Object object has no method 'hasOwnProperty'
  Stack:
    TypeError: Object object has no method 'hasOwnProperty'
        at has (/home/rohit/Projects/opensource/jasmine/src/core/matchers/matchersUtil.js:200:18)
        at eq (/home/rohit/Projects/opensource/jasmine/src/core/matchers/matchersUtil.js:178:13)
        at Object.equals (/home/rohit/Projects/opensource/jasmine/src/core/matchers/matchersUtil.js:8:14)
        at Object.<anonymous> (/home/rohit/Projects/opensource/jasmine/spec/core/matchers/matchersUtilSpec.js:261:32)

Even if the test should not pass shouldn't it actually run by using Object.hasOwnProperty? instead of throwing an error? Apologies if I'm asking something basic about JS.

@slackersoft
Copy link
Member

Hmm, yeah, that part sounds like a bug. We'll have to take a look.

@slackersoft slackersoft reopened this Dec 19, 2014
@slackersoft
Copy link
Member

If you have an idea on how to fix this, I'd be happy to review a pull request.

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 a pull request may close this issue.

2 participants