nested.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. });
  32. test('acyclic but with reused obj-property pointers', function (t) {
  33. t.plan(1);
  34. var x = { a: 1 }
  35. var y = { b: x, c: x }
  36. t.equal(stringify(y), '{"b":{"a":1},"c":{"a":1}}');
  37. });