source: imaps-frontend/node_modules/json-stable-stringify-without-jsonify/test/nested.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.1 KB
Line 
1var test = require('tape');
2var stringify = require('../');
3
4test('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
10test('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
22test('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
30test('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
37test('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});
Note: See TracBrowser for help on using the repository browser.