|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { expect } = require('chai') |
| 4 | +const stringify = require('../lib/stringify') |
| 5 | + |
| 6 | +describe('stringify', () => { |
| 7 | + it('should stringify a number', () => { |
| 8 | + expect(stringify(42)).to.equal('42') |
| 9 | + }) |
| 10 | + |
| 11 | + it('should stringify a string', () => { |
| 12 | + expect(stringify('foo')).to.equal('"foo"') |
| 13 | + }) |
| 14 | + |
| 15 | + it('should stringify a boolean', () => { |
| 16 | + expect(stringify(true)).to.equal('true') |
| 17 | + }) |
| 18 | + |
| 19 | + it('should stringify null', () => { |
| 20 | + expect(stringify(null)).to.equal('null') |
| 21 | + }) |
| 22 | + |
| 23 | + it('should stringify an object', () => { |
| 24 | + expect(stringify({ foo: 'bar' })).to.equal('{"foo":"bar"}') |
| 25 | + }) |
| 26 | + |
| 27 | + it('should stringify an array', () => { |
| 28 | + expect(stringify([1, 2, 3])).to.equal('[1,2,3]') |
| 29 | + }) |
| 30 | + |
| 31 | + it('should handle a circular reference in an object', () => { |
| 32 | + const obj = { foo: 'bar' } |
| 33 | + obj.self = obj |
| 34 | + expect(stringify(obj)).to.equal('{"foo":"bar","self":"[Circular ~]"}') |
| 35 | + }) |
| 36 | + |
| 37 | + it('should handle a circular reference in an array', () => { |
| 38 | + const arr = [1, 2, 3] |
| 39 | + arr.push(arr) |
| 40 | + expect(stringify(arr)).to.equal('[1,2,3,"[Circular ~]"]') |
| 41 | + }) |
| 42 | + |
| 43 | + it('should handle a nested circular reference', () => { |
| 44 | + const obj = { name: 'parent' } |
| 45 | + obj.child = { name: 'child', parent: obj } |
| 46 | + expect(stringify(obj)).to.equal( |
| 47 | + '{"name":"parent","child":{"name":"child","parent":"[Circular ~]"}}', |
| 48 | + ) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should handle a deeply nested circular reference with path notation', () => { |
| 52 | + const obj = { level1: { level2: { level3: {} } } } |
| 53 | + obj.level1.level2.level3.self = obj.level1 |
| 54 | + expect(stringify(obj)).to.equal( |
| 55 | + '{"level1":{"level2":{"level3":{"self":"[Circular ~.level1]"}}}}', |
| 56 | + ) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should handle multiple references to the same object', () => { |
| 60 | + const shared = { value: 'shared' } |
| 61 | + const obj = { a: shared, b: shared } |
| 62 | + expect(stringify(obj)).to.equal( |
| 63 | + '{"a":{"value":"shared"},"b":{"value":"shared"}}', |
| 64 | + ) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should handle an object with a toJSON method that returns a circular reference', () => { |
| 68 | + const obj = { |
| 69 | + name: 'circular', |
| 70 | + toJSON() { |
| 71 | + return this |
| 72 | + }, |
| 73 | + } |
| 74 | + expect(stringify(obj)).to.equal('{"name":"circular"}') |
| 75 | + }) |
| 76 | + |
| 77 | + it('should handle an object with circular references in different properties', () => { |
| 78 | + const obj = { a: {}, b: {} } |
| 79 | + obj.a.self = obj.a |
| 80 | + obj.b.self = obj.b |
| 81 | + expect(stringify(obj)).to.equal( |
| 82 | + '{"a":{"self":"[Circular ~.a]"},"b":{"self":"[Circular ~.b]"}}', |
| 83 | + ) |
| 84 | + }) |
| 85 | + |
| 86 | + it('should handle a circular reference inside a nested array', () => { |
| 87 | + const arr = [1, [2, 3]] |
| 88 | + arr[1].push(arr) |
| 89 | + expect(stringify(arr)).to.equal('[1,[2,3,"[Circular ~]"]]') |
| 90 | + }) |
| 91 | +}) |
0 commit comments