-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sinon matchers for PeerIds and Multiaddrs to test module (#…
…2373) * feat: add sinon matchers for PeerIds and Multiaddrs to test module To allow matching PeerId/Multiaddrs by value (not just by reference) add reusible matchers to the compliance test module. * chore: add test
- Loading branch information
1 parent
74fb567
commit 2e464c0
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Sinon from 'sinon' | ||
import type { PeerId } from '@libp2p/interface' | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
|
||
export function matchPeerId (peerId: PeerId): Sinon.SinonMatcher { | ||
return Sinon.match(p => p.toString() === peerId.toString()) | ||
} | ||
|
||
export function matchMultiaddr (ma: Multiaddr): Sinon.SinonMatcher { | ||
return Sinon.match(m => m.toString() === ma.toString()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { peerIdFromString } from '@libp2p/peer-id' | ||
import { createEd25519PeerId } from '@libp2p/peer-id-factory' | ||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { expect } from 'aegir/chai' | ||
import Sinon from 'sinon' | ||
import { matchMultiaddr, matchPeerId } from '../src/matchers.js' | ||
|
||
describe('peer id matcher', () => { | ||
it('should match the same object', async () => { | ||
const peerId = await createEd25519PeerId() | ||
|
||
const stub = Sinon.stub() | ||
stub(peerId) | ||
|
||
expect(stub.calledWith(matchPeerId(peerId))).to.be.true() | ||
}) | ||
|
||
it('should match the same value', async () => { | ||
const peerId = await createEd25519PeerId() | ||
const peerId2 = peerIdFromString(peerId.toString()) | ||
|
||
const stub = Sinon.stub() | ||
stub(peerId) | ||
|
||
// this does not match because peerId2 does not contain the private key so | ||
// the values are not deeply equal | ||
expect(stub.calledWith(peerId2)).to.be.false() | ||
expect(stub.calledWith(matchPeerId(peerId2))).to.be.true() | ||
}) | ||
}) | ||
|
||
describe('multiaddr matcher', () => { | ||
it('should match the same object', async () => { | ||
const ma = multiaddr('/ip4/127.0.0.1/tcp/4001') | ||
|
||
const stub = Sinon.stub() | ||
stub(ma) | ||
|
||
expect(stub.calledWith(matchMultiaddr(ma))).to.be.true() | ||
}) | ||
|
||
it('should match the same value', async () => { | ||
const ma = multiaddr('/ip4/127.0.0.1/tcp/4001') | ||
const ma2 = multiaddr('/ip4/127.0.0.1/tcp/4001') | ||
|
||
const stub = Sinon.stub() | ||
stub(ma) | ||
|
||
// this would match because no properties are changed after creation since | ||
// https://github.com/multiformats/js-multiaddr/pull/330 | ||
// expect(stub.calledWith(ma2)).to.be.false() | ||
expect(stub.calledWith(matchMultiaddr(ma2))).to.be.true() | ||
}) | ||
}) |