const test = require('tap').testconst fss = require('./')const clone = require('clone')const s = JSON.stringifytest('circular reference to root', function (assert) {const fixture = { name: 'Tywin Lannister' }fixture.circle = fixtureconst expected = s({ name: 'Tywin Lannister', circle: '[Circular]' })const actual = fss(fixture)assert.is(actual, expected)assert.end()})test('circular getter reference to root', function (assert) {const fixture = {name: 'Tywin Lannister',get circle () {return fixture}}const expected = s({ name: 'Tywin Lannister', circle: '[Circular]' })const actual = fss(fixture)assert.is(actual, expected)assert.end()})test('nested circular reference to root', function (assert) {const fixture = { name: 'Tywin Lannister' }fixture.id = { circle: fixture }const expected = s({ name: 'Tywin Lannister', id: { circle: '[Circular]' } })const actual = fss(fixture)assert.is(actual, expected)assert.end()})test('child circular reference', function (assert) {const fixture = { name: 'Tywin Lannister', child: { name: 'Tyrion Lannister' } }fixture.child.dinklage = fixture.childconst expected = s({name: 'Tywin Lannister',child: {name: 'Tyrion Lannister', dinklage: '[Circular]'}})const actual = fss(fixture)assert.is(actual, expected)assert.end()})test('nested child circular reference', function (assert) {const fixture = { name: 'Tywin Lannister', child: { name: 'Tyrion Lannister' } }fixture.child.actor = { dinklage: fixture.child }const expected = s({name: 'Tywin Lannister',child: {name: 'Tyrion Lannister', actor: { dinklage: '[Circular]' }}})const actual = fss(fixture)assert.is(actual, expected)assert.end()})test('circular objects in an array', function (assert) {const fixture = { name: 'Tywin Lannister' }fixture.hand = [fixture, fixture]const expected = s({name: 'Tywin Lannister', hand: ['[Circular]', '[Circular]']})const actual = fss(fixture)assert.is(actual, expected)assert.end()})test('nested circular references in an array', function (assert) {const fixture = {name: 'Tywin Lannister',offspring: [{ name: 'Tyrion Lannister' }, { name: 'Cersei Lannister' }]}fixture.offspring[0].dinklage = fixture.offspring[0]fixture.offspring[1].headey = fixture.offspring[1]const expected = s({name: 'Tywin Lannister',offspring: [{ name: 'Tyrion Lannister', dinklage: '[Circular]' },{ name: 'Cersei Lannister', headey: '[Circular]' }]})const actual = fss(fixture)assert.is(actual, expected)assert.end()})test('circular arrays', function (assert) {const fixture = []fixture.push(fixture, fixture)const expected = s(['[Circular]', '[Circular]'])const actual = fss(fixture)assert.is(actual, expected)assert.end()})test('nested circular arrays', function (assert) {const fixture = []fixture.push({ name: 'Jon Snow', bastards: fixture },{ name: 'Ramsay Bolton', bastards: fixture })const expected = s([{ name: 'Jon Snow', bastards: '[Circular]' },{ name: 'Ramsay Bolton', bastards: '[Circular]' }])const actual = fss(fixture)assert.is(actual, expected)assert.end()})test('repeated non-circular references in objects', function (assert) {const daenerys = { name: 'Daenerys Targaryen' }const fixture = {motherOfDragons: daenerys,queenOfMeereen: daenerys}const expected = s(fixture)const actual = fss(fixture)assert.is(actual, expected)assert.end()})test('repeated non-circular references in arrays', function (assert) {const daenerys = { name: 'Daenerys Targaryen' }const fixture = [daenerys, daenerys]const expected = s(fixture)const actual = fss(fixture)assert.is(actual, expected)assert.end()})test('double child circular reference', function (assert) {// create circular referenceconst child = { name: 'Tyrion Lannister' }child.dinklage = child// include it twice in the fixtureconst fixture = { name: 'Tywin Lannister', childA: child, childB: child }const cloned = clone(fixture)const expected = s({name: 'Tywin Lannister',childA: {name: 'Tyrion Lannister', dinklage: '[Circular]'},childB: {name: 'Tyrion Lannister', dinklage: '[Circular]'}})const actual = fss(fixture)assert.is(actual, expected)// check if the fixture has not been modifiedassert.deepEqual(fixture, cloned)assert.end()})test('child circular reference with toJSON', function (assert) {// Create a test object that has an overriden `toJSON` propertyTestObject.prototype.toJSON = function () { return { special: 'case' } }function TestObject (content) {}// Creating a simple circular object structureconst parentObject = {}parentObject.childObject = new TestObject()parentObject.childObject.parentObject = parentObject// Creating a simple circular object structureconst otherParentObject = new TestObject()otherParentObject.otherChildObject = {}otherParentObject.otherChildObject.otherParentObject = otherParentObject// Making sure our original tests workassert.deepEqual(parentObject.childObject.parentObject, parentObject)assert.deepEqual(otherParentObject.otherChildObject.otherParentObject, otherParentObject)// Should both be idempotentassert.equal(fss(parentObject), '{"childObject":{"special":"case"}}')assert.equal(fss(otherParentObject), '{"special":"case"}')// Therefore the following assertion should be `true`assert.deepEqual(parentObject.childObject.parentObject, parentObject)assert.deepEqual(otherParentObject.otherChildObject.otherParentObject, otherParentObject)assert.end()})test('null object', function (assert) {const expected = s(null)const actual = fss(null)assert.is(actual, expected)assert.end()})test('null property', function (assert) {const expected = s({ f: null })const actual = fss({ f: null })assert.is(actual, expected)assert.end()})test('nested child circular reference in toJSON', function (assert) {const circle = { some: 'data' }circle.circle = circleconst a = {b: {toJSON: function () {a.b = 2return '[Redacted]'}},baz: {circle,toJSON: function () {a.baz = circlereturn '[Redacted]'}}}const o = {a,bar: a}const expected = s({a: {b: '[Redacted]',baz: '[Redacted]'},bar: {b: 2,baz: {some: 'data',circle: '[Circular]'}}})const actual = fss(o)assert.is(actual, expected)assert.end()})test('circular getters are restored when stringified', function (assert) {const fixture = {name: 'Tywin Lannister',get circle () {return fixture}}fss(fixture)assert.is(fixture.circle, fixture)assert.end()})test('non-configurable circular getters use a replacer instead of markers', function (assert) {const fixture = { name: 'Tywin Lannister' }Object.defineProperty(fixture, 'circle', {configurable: false,get: function () { return fixture },enumerable: true})fss(fixture)assert.is(fixture.circle, fixture)assert.end()})test('getter child circular reference are replaced instead of marked', function (assert) {const fixture = {name: 'Tywin Lannister',child: {name: 'Tyrion Lannister',get dinklage () { return fixture.child }},get self () { return fixture }}const expected = s({name: 'Tywin Lannister',child: {name: 'Tyrion Lannister', dinklage: '[Circular]'},self: '[Circular]'})const actual = fss(fixture)assert.is(actual, expected)assert.end()})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。