nested.js 931 B

1234567891011121314151617181920212223242526272829303132333435
  1. var test = require('tape');
  2. var stringify = require('../');
  3. test('nested', function (t) {
  4. t.plan(1);
  5. var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
  6. t.equal(stringify(obj), '{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}');
  7. });
  8. test('cyclic (default)', function (t) {
  9. t.plan(1);
  10. var one = { a: 1 };
  11. var two = { a: 2, one: one };
  12. one.two = two;
  13. try {
  14. stringify(one);
  15. } catch (ex) {
  16. t.equal(ex.toString(), 'TypeError: Converting circular structure to JSON');
  17. }
  18. });
  19. test('cyclic (specifically allowed)', function (t) {
  20. t.plan(1);
  21. var one = { a: 1 };
  22. var two = { a: 2, one: one };
  23. one.two = two;
  24. t.equal(stringify(one, {cycles:true}), '{"a":1,"two":{"a":2,"one":"__cycle__"}}');
  25. });
  26. test('repeated non-cyclic value', function(t) {
  27. t.plan(1);
  28. var one = { x: 1 };
  29. var two = { a: one, b: one };
  30. t.equal(stringify(two), '{"a":{"x":1},"b":{"x":1}}');
  31. });