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

Adding iterator to binary tree structure breaks representation #76

Closed
jcubic opened this issue Sep 14, 2021 · 7 comments
Closed

Adding iterator to binary tree structure breaks representation #76

jcubic opened this issue Sep 14, 2021 · 7 comments

Comments

@jcubic
Copy link

jcubic commented Sep 14, 2021

This is related to avajs/ava#2811

Here is simple reproducation:

const concordance = require('concordance');


function Pair(car, cdr) {
  this.car = car;
  this.cdr = cdr;
}

function Nil() {}
var nil = new Nil();


var x = new Pair(
    new Pair(
        new Pair(
            10,
            new Pair(
                20,
                nil
            )
        ),
        nil
    ),
    nil
);


let data = concordance.describe(x);
console.log(concordance.format(data));



Pair.prototype[Symbol.iterator] = function() {
    var node = this;
    return {
        next: function() {
            var cur = node;
            node = cur.cdr;
            if (cur === nil) {
                return { value: undefined, done: true };
            } else {
                return { value: cur.car, done: false };
            }
        }
    };
};

data = concordance.describe(x);
console.log(concordance.format(data));

Here is the output:

@Object {
  ctor: 'Pair',
  describeAny: Function describeAny {},
  describeItem: Function describeItem {},
  describeMapEntry: Function describeMapEntry {},
  describeProperty: Function describeProperty {},
  isArray: false,
  isIterable: false,
  isList: false,
  iterableState: null,
  listState: null,
  pointer: 1,
  propertyState: null,
  stringTag: 'Object',
  value: Pair {
    car: Pair {
      car: Pair {
        car: 10,
        cdr: Pair {
          car: 20,
          cdr: Nil {},
        },
      },
      cdr: Nil {},
    },
    cdr: Nil {},
  },
}
@Object {
  ctor: 'Pair',
  describeAny: Function describeAny {},
  describeItem: Function describeItem {},
  describeMapEntry: Function describeMapEntry {},
  describeProperty: Function describeProperty {},
  isArray: false,
  isIterable: true,
  isList: false,
  iterableState: null,
  listState: null,
  pointer: 1,
  propertyState: null,
  stringTag: 'Object',
  value: Pair {
    car: Pair {
      car: Pair {
        car: 10,
        cdr: Pair {
          car: 20,
          cdr: Nil {},
          ---
          20,
        },
        ---
        10,
        20,
      },
      cdr: Nil {},
      ---
      Pair {
        car: 10,
        cdr: Pair {
          car: 20,
          cdr: Nil {},
          ---
          20,
        },
        ---
        10,
        20,
      },
    },
    cdr: Nil {},
    ---
    Pair {
      car: Pair {
        car: 10,
        cdr: Pair {
          car: 20,
          cdr: Nil {},
          ---
          20,
        },
        ---
        10,
        20,
      },
      cdr: Nil {},
      ---
      Pair {
        car: 10,
        cdr: Pair {
          car: 20,
          cdr: Nil {},
          ---
          20,
        },
        ---
        10,
        20,
      },
    },
  },
}

@novemberborn
Copy link
Member

There doesn't seem to be an error though — what am I looking for?

@jcubic
Copy link
Author

jcubic commented Sep 15, 2021

There are two different outputs, the second one has a lot of duplicated data in value, which is invalid tree structure. The output should be the same after adding the iterator.

@jcubic
Copy link
Author

jcubic commented Sep 15, 2021

The output for second print should be:

  value: Pair {
    car: Pair {
      car: Pair {
        car: 10,
        cdr: Pair {
          car: 20,
          cdr: Nil {},
        },
      },
      cdr: Nil {},
    },
    cdr: Nil {},
  },

I don't understand what those other data suppose to be, why they are duplicated?

@jcubic
Copy link
Author

jcubic commented Sep 15, 2021

Also what is --- in output?

@jcubic
Copy link
Author

jcubic commented Sep 15, 2021

Note that I only want to know if the output is correct. So I can finally be sure that my tests that use snapshots are working after adding iterator. Because the snapshot says the output is different and that tests are broken.

@novemberborn
Copy link
Member

Gotcha, I think I tried to answer this in the original AVA issue but it got lost in the confusion around snapshots failing and then passing. Sorry for the confusion!

Concordance / AVA does iterate over objects when serializing / t.snapshot() and comparing / t.deepEqual(). The --- separates the own-properties of the object from the iterated items.

Generally this is nice, in that custom iterables may have a couple fields and yield a bunch of items. In your case it's confusing because the iteration yields the same values as are available directly on the object (I think?).

avajs/ava#1861 may be of interest, it would let you extend AVA to recognize your objects and format / compare them as appropriate.

@jcubic
Copy link
Author

jcubic commented Sep 18, 2021

Thanks for the explanation, yes the iteration return the same values as are in objects properties. They are linked lists and iteration just returns an array of the items in that list.

Thanks again for taking the time to think about my issue.

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

2 participants