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