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

Test fails with undefined property #105

Closed
aloscha opened this issue May 30, 2016 · 10 comments
Closed

Test fails with undefined property #105

aloscha opened this issue May 30, 2016 · 10 comments

Comments

@aloscha
Copy link
Collaborator

aloscha commented May 30, 2016

Two tests fails with undefined as a default property.
First test:
it('undefined property is set to something', function() {
var obj = {foo: undefined};

      var observer = jsonpatch.observe(obj);
      obj.foo = "something";

      var patches = jsonpatch.generate(observer);
      expect(patches).toEqual([{op: 'replace', path: '/foo', value: "something"}]);
    });

IMHO there need to be 'add' instead of 'replace'

Second test:

  it("should generate remove when `undefined` property get removed", function(){
    var obj = {foo: undefined};

    var observer = jsonpatch.observe(obj);
    delete obj.foo;

    var patches = jsonpatch.generate(observer);
    expect(patches).toEqual([{ op: 'remove', path: '/foo' }]);
  });

Here JSON-Patch create empty array as a result.

Also In that task needs to refactor readme:

README says: "We treat undefined as any other falsy value, and
implementation & tests works: "We tread undefined as no property specified"

@tomalec, @warpech what guys do you think about it?

@tomalec
Copy link
Collaborator

tomalec commented May 30, 2016

There are many issues related to undefined

Some time ago I have tried to add support for undefined as any other falsy value, but it seems it no longer works this way.

If i's to hard to fix JSON-Patch to work that way once again, I would re-describe applicable test cases, and add note on README what is the current behavior - make README in sync with spec.

@warpech
Copy link
Collaborator

warpech commented May 31, 2016

Also, in NodeJS test case 13 tests fail:

screen shot 2016-05-31 at 10 22 32

@aloscha
Copy link
Collaborator Author

aloscha commented May 31, 2016

@warpech that happens before #104 ? (I mean it's happens because changes in that task occurs)?

warpech added a commit that referenced this issue May 31, 2016
This commit only changes the test to the proposed solution to treating undefineds.
I think that `jsonpatch.generate` should treat undefined exactly the same way it is treated in `json.stringify` and `jsonpatch.compare`.

Every test should go the following path:

    var a = /*some object*/;
    observe(a);
    a.change = /*new value*/;
    var patches1 = generate(a);

    var b = (same object);
    var c = clone(b);
    b.change = /*new value*/
    var patches2 = jsonpatch.compare(b, c);

    expect(patches1).toReallyEqual(/*expected patch*/);
    expect(patches1).toReallyEqual(patches2);

toReallyEqual is a wrapper around Underscore isEqual, which knows the difference between undefined, null and unset values.
@warpech
Copy link
Collaborator

warpech commented May 31, 2016

@warpech that happens before #104 ? (I mean it's happens because changes in that task occurs)?

Yes

@aloscha
Copy link
Collaborator Author

aloscha commented May 31, 2016

@warpech I fixed that, by start using jsdom. Now node test returns only 2 errors (the same like in SpecRunnerDuplex.html)

@warpech
Copy link
Collaborator

warpech commented May 31, 2016

Thanks @aloscha

@aloscha
Copy link
Collaborator Author

aloscha commented May 31, 2016

If i's to hard to fix JSON-Patch to work that way once again, I would re-describe applicable test cases, and add note on README what is the current behavior - make README in sync with spec.

Ok, waiting for that

@warpech
Copy link
Collaborator

warpech commented May 31, 2016

Back to the original subject - first of all, we should stop using Jasmine's method toEqual, because it thinks the following statement is correct:

expect({a: undefined}).toEqual({});

Secondly, I propose to use the following approach. jsonpatch.generate should treat undefined exactly the same way it is treated in JSON.stringify and jsonpatch.compare.

Every test should go the following path (pseudocode):

var a = /*some object*/;
observe(a);
a.change = /*new value*/;
var patches1 = generate(a);

var b = /*same object as "a"*/;
var c = clone(b);
b.change = /*same value as "a.change"*/
var patches2 = compare(b, c);

expect(patches1).toReallyEqual(/*expected patch*/);
expect(patches1).toReallyEqual(patches2);

The following commit implements that: ffaa563

@tomalec WDYT?

In that commit, I don't use Jasmine's toEqual, but Underscore's isEqual (wrapped in a matcher called toReallyEqual).

warpech added a commit that referenced this issue Jun 3, 2016
this is a fix for issues #105, #90, #60, #32 and a part of #76 that describes a problem with undefined
all tests pass after this commit
@warpech
Copy link
Collaborator

warpech commented Jun 3, 2016

I created a pull request. This is a breaking change for undefined.

@tomalec, @miyconst, @aloscha could you please review the PR? There is a chance that I have overlooked some problem, so please be very critical :)

@warpech
Copy link
Collaborator

warpech commented Jun 16, 2016

Changes are now merged to master.

@warpech warpech closed this as completed Jun 16, 2016
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

No branches or pull requests

3 participants