Skip to content

Commit a4eaf7b

Browse files
patrickwoodheadrvagg
patrickwoodhead
authored andcommitted
no sinon and rename to traversal
1 parent 97bd8c7 commit a4eaf7b

File tree

3 files changed

+54
-45
lines changed

3 files changed

+54
-45
lines changed

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"./block": {
4949
"import": "./src/block.js"
5050
},
51-
"./traverse": {
52-
"import": "./src/traverse.js"
51+
"./traversal": {
52+
"import": "./src/traversal.js"
5353
},
5454
"./bases/identity": {
5555
"import": "./src/bases/identity.js"
@@ -110,7 +110,6 @@
110110
"ipjs": "^5.1.2",
111111
"mocha": "^9.1.1",
112112
"polendina": "^1.1.0",
113-
"sinon": "^12.0.1",
114113
"standard": "^16.0.3",
115114
"typescript": "~4.4.2"
116115
},

src/traverse.js renamed to src/traversal.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import { base58btc } from 'multiformats/bases/base58'
1414
* @param {Object} options
1515
* @param {CID} options.cid
1616
* @param {(cid: CID) => Promise<Block<T>>} options.load
17-
* @param {Set<string>} options.seen
17+
* @param {Set<string>?} options.seen
1818
*/
19-
const walk = async ({ cid, load, seen = new Set() }) => {
19+
const walk = async ({ cid, load, seen }) => {
20+
seen = seen || new Set()
2021
const b58Cid = cid.toString(base58btc)
2122
if (seen.has(b58Cid)) {
2223
return
@@ -25,9 +26,8 @@ const walk = async ({ cid, load, seen = new Set() }) => {
2526
const block = await load(cid)
2627
seen.add(b58Cid)
2728

28-
for (const link of block.links()) {
29-
const linkCid = link[1]
30-
await walk({ cid: linkCid, load, seen })
29+
for (const [_,cid] of block.links()) {
30+
await walk({ cid, load, seen })
3131
}
3232
}
3333

test/test-traverse.js renamed to test/test-traversal.js

+47-37
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/* globals describe, it */
2-
import sinon from 'sinon'
32
import * as codec from 'multiformats/codecs/json'
43
import * as dagPB from '@ipld/dag-pb'
54
import { sha256 as hasher } from 'multiformats/hashes/sha2'
65
import * as main from 'multiformats/block'
7-
import { walk } from 'multiformats/traverse'
6+
import { walk } from 'multiformats/traversal'
7+
import { deepStrictEqual as same } from 'assert'
88

99
const test = it
1010
const { createLink, createNode } = dagPB
1111

12-
describe('traverse', () => {
12+
describe('traversal', () => {
1313
describe('walk', async () => {
1414
// Forming the following DAG for testing
1515
// A
@@ -61,18 +61,25 @@ describe('traverse', () => {
6161
throw new Error('unmatched CID')
6262
}
6363

64+
const loadWrapper = (load, arr = []) => (cid) => {
65+
arr.push(cid.toString())
66+
return load(cid)
67+
}
68+
6469
test('block with no links', async () => {
6570
// Test Case 1
6671
// Input DAG
6772
// D
6873
//
6974
// Expect load to be called with D
70-
71-
const spy = sinon.spy(load)
72-
73-
await walk({ cid: cidD, load: spy })
74-
sinon.assert.callCount(spy, 1)
75-
sinon.assert.calledOnceWithExactly(spy, cidD)
75+
const expectedCallArray = [cidD.toString()]
76+
const callArray = []
77+
78+
await walk({ cid: cidD, load: loadWrapper(load, callArray) })
79+
80+
expectedCallArray.forEach((value, index) => {
81+
same(value, callArray[index])
82+
})
7683
})
7784
test('block with links', async () => {
7885
// Test Case 2
@@ -82,15 +89,14 @@ describe('traverse', () => {
8289
// D E
8390
//
8491
// Expect load to be called with C, then D, then E
85-
86-
const spy = sinon.spy(load)
87-
88-
await walk({ cid: cidC, load: spy })
89-
90-
sinon.assert.callCount(spy, 3)
91-
sinon.assert.calledWith(spy.firstCall, cidC)
92-
sinon.assert.calledWith(spy.secondCall, cidD)
93-
sinon.assert.calledWith(spy.thirdCall, cidE)
92+
const expectedCallArray = [cidC.toString(), cidD.toString(), cidE.toString()]
93+
const callArray = []
94+
95+
await walk({ cid: cidC, load: loadWrapper(load, callArray) })
96+
97+
expectedCallArray.forEach((value, index) => {
98+
same(value, callArray[index])
99+
})
94100
})
95101
test('block with matching links', async () => {
96102
// Test Case 3
@@ -100,14 +106,14 @@ describe('traverse', () => {
100106
// D D
101107
//
102108
// Expect load to be called with B, then D
103-
104-
const spy = sinon.spy(load)
105-
106-
await walk({ cid: cidB, load: spy })
107-
108-
sinon.assert.callCount(spy, 2)
109-
sinon.assert.calledWith(spy.firstCall, cidB)
110-
sinon.assert.calledWith(spy.secondCall, cidD)
109+
const expectedCallArray = [cidB.toString(), cidD.toString()]
110+
const callArray = []
111+
112+
await walk({ cid: cidB, load: loadWrapper(load, callArray) })
113+
114+
expectedCallArray.forEach((value, index) => {
115+
same(value, callArray[index])
116+
})
111117
})
112118
test('depth first with duplicated block', async () => {
113119
// Test Case 4
@@ -117,18 +123,22 @@ describe('traverse', () => {
117123
// B C
118124
// / \ / \
119125
// D D D E
126+
//
120127
// Expect load to be called with A, then B, then D, then C, then E
121-
122-
const spy = sinon.spy(load)
123-
124-
await walk({ cid: cidA, load: spy })
125-
126-
sinon.assert.callCount(spy, 5)
127-
sinon.assert.calledWith(spy.firstCall, cidA)
128-
sinon.assert.calledWith(spy.secondCall, cidB)
129-
sinon.assert.calledWith(spy.thirdCall, cidD)
130-
sinon.assert.calledWith(spy.getCall(-2), cidC)
131-
sinon.assert.calledWith(spy.lastCall, cidE)
128+
const expectedCallArray = [
129+
cidA.toString(),
130+
cidB.toString(),
131+
cidD.toString(),
132+
cidC.toString(),
133+
cidE.toString(),
134+
]
135+
const callArray = []
136+
137+
await walk({ cid: cidA, load: loadWrapper(load, callArray) })
138+
139+
expectedCallArray.forEach((value, index) => {
140+
same(value, callArray[index])
141+
})
132142
})
133143
})
134144
})

0 commit comments

Comments
 (0)